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

test.describe('字典管理完整工作流', () => {
  test.describe.configure({ mode: 'serial' });

  const timestamp = Date.now();
  const dictName = `测试字典_${timestamp}`;
  const dictType = `test_dict_${timestamp}`;

  test('创建字典', async ({ page }) => {
    await test.step('导航到字典管理', async () => {
      await page.goto('/dashboard');
      await page.waitForLoadState('domcontentloaded');
      await page.waitForTimeout(1000);
      await page.locator('text=系统管理').waitFor({ state: 'visible', timeout: 5000 });
      await page.locator('text=系统管理').click();
      await page.waitForTimeout(500);
      await page.locator('text=字典管理').waitFor({ state: 'visible', timeout: 5000 });
      await page.locator('text=字典管理').click();
      await page.waitForLoadState('domcontentloaded');
      await expect(page).toHaveURL(/.*dict/, { timeout: 15000 });
    });

    await test.step('点击新增字典按钮', async () => {
      await page.locator('button:has-text("新增字典")').click();
      await page.waitForSelector('.el-dialog', { state: 'visible', timeout: 5000 });
    });

    await test.step('填写字典信息', async () => {
      const dialog = page.locator('.el-dialog');
      await dialog.locator('input').first().fill(dictName);
      await dialog.locator('input').nth(1).fill(dictType);
    });

    await test.step('提交表单', async () => {
      await page.locator('.el-dialog button:has-text("确定")').click();
      await page.waitForSelector('.el-dialog', { state: 'hidden', timeout: 10000 });
      await expect(page.locator('.el-message--success')).toBeVisible({ timeout: 5000 });
    });

    await test.step('验证字典已创建', async () => {
      await page.waitForTimeout(2000);
      const dictRow = page.locator(`tr:has-text("${dictName}")`);
      await expect(dictRow).toBeVisible({ timeout: 15000 });
    });
  });

  test('编辑字典', async ({ page }) => {
    const updatedName = `更新字典_${timestamp}`;

    await test.step('导航到字典管理', async () => {
      await page.goto('/dashboard');
      await page.waitForLoadState('networkidle');
      await page.locator('text=系统管理').click();
      await page.waitForTimeout(500);
      await page.locator('text=字典管理').waitFor({ state: 'visible', timeout: 5000 });
      await page.locator('text=字典管理').click();
      await page.waitForLoadState('networkidle');
      await expect(page).toHaveURL(/.*dict/, { timeout: 10000 });
    });

    await test.step('搜索并编辑字典', async () => {
      const dictRow = page.locator(`tr:has-text("${dictName}")`);
      await dictRow.locator('button:has-text("编辑")').click();
      await page.waitForSelector('.el-dialog', { state: 'visible', timeout: 5000 });
    });

    await test.step('修改字典信息', async () => {
      const dialog = page.locator('.el-dialog');
      await dialog.locator('input').first().fill(updatedName);
    });

    await test.step('提交更新', async () => {
      await page.locator('.el-dialog button:has-text("确定")').click();
      await page.waitForSelector('.el-dialog', { state: 'hidden', timeout: 10000 });
      await expect(page.locator('.el-message--success')).toBeVisible({ timeout: 5000 });
    });

    await test.step('验证字典已更新', async () => {
      await page.waitForTimeout(2000);
      const dictRow = page.locator(`tr:has-text("${updatedName}")`);
      await expect(dictRow).toBeVisible({ timeout: 15000 });
    });
  });

  test('删除字典', async ({ page }) => {
    await test.step('导航到字典管理', async () => {
      await page.goto('/dashboard');
      await page.waitForLoadState('networkidle');
      await page.locator('text=系统管理').click();
      await page.waitForTimeout(500);
      await page.locator('text=字典管理').click();
      await page.waitForLoadState('networkidle');
    });

    await test.step('搜索并删除字典', async () => {
      const dictRow = page.locator(`tr:has-text("更新字典_${timestamp}")`);
      await dictRow.locator('button:has-text("删除")').click();
      await page.waitForSelector('.el-message-box', { state: 'visible', timeout: 5000 });
    });

    await test.step('确认删除', async () => {
      await page.locator('.el-message-box button:has-text("确定")').click();
      await page.waitForSelector('.el-message-box', { state: 'hidden', timeout: 10000 });
      await expect(page.locator('.el-message--success')).toBeVisible({ timeout: 5000 });
    });

    await test.step('验证字典已删除', async () => {
      await page.waitForTimeout(1000);
      const dictRow = page.locator(`tr:has-text("更新字典_${timestamp}")`);
      await expect(dictRow).not.toBeVisible({ timeout: 5000 });
    });
  });

  test('字典管理功能验证', async ({ page }) => {
    await test.step('验证字典管理页面访问权限', async () => {
      await page.goto('/dict');
      await page.waitForLoadState('networkidle');
      
      await expect(page.locator('.el-card')).toBeVisible({ timeout: 5000 });
      await expect(page.locator('button:has-text("新增字典")')).toBeVisible();
    });
  });
});
