import { test, expect } from '@playwright/test';
import { DictionaryManagementPage } from '../pages/DictionaryManagementPage';

test.describe('字典管理工作流', () => {
  let dictPage: DictionaryManagementPage;
  const timestamp = Date.now();
  const dictType = `test_dict_${timestamp}`;
  const dictName = `测试字典_${timestamp}`;

  test.beforeEach(async ({ page }) => {
    dictPage = new DictionaryManagementPage(page);
  });

  test('查看字典列表', async ({ page }) => {
    await test.step('导航到字典管理页面', async () => {
      await dictPage.goto();
    });

    await test.step('验证表格显示', async () => {
      await expect(dictPage.table).toBeVisible({ timeout: 10000 });
    });

    await test.step('验证数据加载', async () => {
      const rowCount = await dictPage.getDictCount();
      console.log(`字典列表包含 ${rowCount} 条记录`);
      expect(rowCount).toBeGreaterThanOrEqual(0);
    });
  });

  test('新增字典', async ({ page }) => {
    await test.step('导航到字典管理页面', async () => {
      await dictPage.goto();
    });

    await test.step('点击新增字典按钮', async () => {
      await dictPage.createDictButton.click();
      await dictPage.dialog.waitFor({ state: 'visible', timeout: 5000 });
    });

    await test.step('填写字典表单', async () => {
      await dictPage.dictNameInput.fill(dictName);
      await dictPage.dictTypeInput.fill(dictType);
    });

    await test.step('提交表单', async () => {
      await dictPage.saveButton.click();
      await page.waitForLoadState('networkidle');
    });

    await test.step('验证创建成功', async () => {
      await expect(dictPage.dialog).not.toBeVisible({ timeout: 5000 });
      console.log(`字典 ${dictName} 创建完成`);
    });
  });

  test('编辑字典', async ({ page }) => {
    await test.step('导航到字典管理页面', async () => {
      await dictPage.goto();
    });

    await test.step('等待数据加载', async () => {
      await expect(dictPage.table).toBeVisible({ timeout: 10000 });
    });

    await test.step('点击编辑按钮', async () => {
      const rows = await dictPage.getDictCount();
      if (rows > 0) {
        const firstRow = dictPage.table.locator('tr').first();
        const editBtn = firstRow.getByRole('button', { name: '编辑' });
        if (await editBtn.isVisible({ timeout: 3000 }).catch(() => false)) {
          await editBtn.click();
          await dictPage.dialog.waitFor({ state: 'visible', timeout: 5000 });
          
          await test.step('修改字典名称', async () => {
            const newName = `更新字典_${timestamp}`;
            await dictPage.dictNameInput.clear();
            await dictPage.dictNameInput.fill(newName);
          });

          await test.step('提交表单', async () => {
            await dictPage.saveButton.click();
            await page.waitForLoadState('networkidle');
          });

          await test.step('验证更新成功', async () => {
            await expect(dictPage.dialog).not.toBeVisible({ timeout: 5000 });
            console.log(`字典已更新`);
          });
        } else {
          console.log('未找到编辑按钮，跳过编辑测试');
        }
      } else {
        console.log('当前没有字典记录，跳过编辑测试');
      }
    });
  });

  test('删除字典', async ({ page }) => {
    await test.step('导航到字典管理页面', async () => {
      await dictPage.goto();
    });

    await test.step('等待数据加载', async () => {
      await expect(dictPage.table).toBeVisible({ timeout: 10000 });
    });

    await test.step('点击删除按钮', async () => {
      const rows = await dictPage.getDictCount();
      if (rows > 0) {
        const firstRow = dictPage.table.locator('tr').first();
        const deleteBtn = firstRow.getByRole('button', { name: '删除' });
        if (await deleteBtn.isVisible({ timeout: 3000 }).catch(() => false)) {
          await deleteBtn.click();
          const confirmBtn = page.locator('.el-message-box');
          await confirmBtn.waitFor({ state: 'visible', timeout: 3000 });
          
          await test.step('确认删除', async () => {
            const confirmBtn = page.locator('.el-message-box').getByRole('button', { name: '确定' });
            if (await confirmBtn.isVisible({ timeout: 2000 }).catch(() => false)) {
              await confirmBtn.click();
              await page.waitForLoadState('networkidle');
            }
          });

          await test.step('验证删除成功', async () => {
            const messageBox = page.locator('.el-message-box');
            await expect(messageBox).not.toBeVisible({ timeout: 5000 });
            console.log(`字典已删除`);
          });
        } else {
          console.log('未找到删除按钮，跳过删除测试');
        }
      } else {
        console.log('当前没有字典记录，跳过删除测试');
      }
    });
  });
});
