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

test.describe('系统配置工作流', () => {
  let configPage: SystemConfigPage;
  const timestamp = Date.now();
  const configKey = `test_config_${timestamp}`;
  const configName = `测试配置_${timestamp}`;
  const configValue = `测试值_${timestamp}`;

  test.beforeEach(async ({ page }) => {
    configPage = new SystemConfigPage(page);
  });

  test('查看系统配置列表', async ({ page }) => {
    await test.step('导航到系统配置页面', async () => {
      await configPage.goto();
    });

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

    await test.step('验证数据加载', async () => {
      const rowCount = await configPage.getTableRowCount();
      console.log(`系统配置列表包含 ${rowCount} 条记录`);
      expect(rowCount).toBeGreaterThanOrEqual(0);
    });
  });

  test('新增系统配置', async ({ page }) => {
    await test.step('导航到系统配置页面', async () => {
      await configPage.goto();
    });

    await test.step('点击新增配置按钮', async () => {
      await configPage.addButton.click();
      await configPage.dialog.waitFor({ state: 'visible', timeout: 5000 });
    });

    await test.step('填写配置表单', async () => {
      await configPage.configNameInput.fill(configName);
      await configPage.configKeyInput.fill(configKey);
      await configPage.configValueInput.fill(configValue);
    });

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

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

  test('编辑系统配置', async ({ page }) => {
    await test.step('导航到系统配置页面', async () => {
      await configPage.goto();
    });

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

    await test.step('点击编辑按钮', async () => {
      const rows = await configPage.getTableRowCount();
      if (rows > 0) {
        const firstRow = configPage.table.locator('tr').first();
        const editBtn = firstRow.getByRole('button', { name: '编辑' });
        if (await editBtn.isVisible({ timeout: 3000 }).catch(() => false)) {
          await editBtn.click();
          await configPage.dialog.waitFor({ state: 'visible', timeout: 5000 });
          
          await test.step('修改配置值', async () => {
            const newValue = `更新值_${timestamp}`;
            await configPage.configValueInput.clear();
            await configPage.configValueInput.fill(newValue);
          });

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

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

  test('删除系统配置', async ({ page }) => {
    await test.step('导航到系统配置页面', async () => {
      await configPage.goto();
    });

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

    await test.step('点击删除按钮', async () => {
      const rows = await configPage.getTableRowCount();
      if (rows > 0) {
        const firstRow = configPage.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('当前没有配置记录，跳过删除测试');
      }
    });
  });
});
