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

test.describe('通知管理工作流', () => {
  let noticePage: NotificationPage;
  const timestamp = Date.now();
  const noticeTitle = `测试通知_${timestamp}`;
  const noticeContent = `这是测试通知内容_${timestamp}`;

  test.beforeEach(async ({ page }) => {
    noticePage = new NotificationPage(page);
  });

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

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

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

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

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

    await test.step('填写通知表单', async () => {
      await noticePage.titleInput.fill(noticeTitle);
      await noticePage.contentInput.fill(noticeContent);
    });

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

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

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

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

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

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

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

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

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

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