import { test, expect } from '@playwright/test';
import { LoginPage } from './pages/LoginPage';
import { DashboardPage } from './pages/DashboardPage';
import { UserManagementPage } from './pages/UserManagementPage';

test.describe('简单登录测试', () => {
  let loginPage: LoginPage;
  let dashboardPage: DashboardPage;
  let userManagementPage: UserManagementPage;

  test.beforeEach(async ({ page }) => {
    loginPage = new LoginPage(page);
    dashboardPage = new DashboardPage(page);
    userManagementPage = new UserManagementPage(page);
    
    // 清理localStorage
    await page.goto('/');
    await page.evaluate(() => localStorage.clear());
    
    // 重新登录
    await loginPage.goto();
    await loginPage.login('e2e_test_user', 'admin123');
  });

  test('登录后导航到用户管理页面', async ({ page }) => {
    await dashboardPage.navigateToUserManagement();
    await expect(page).toHaveURL(/.*users/);
    
    // 验证表格存在
    await expect(userManagementPage.table).toBeVisible();
    
    // 验证"新增用户"按钮存在
    await expect(userManagementPage.createUserButton).toBeVisible();
  });

  test('点击新增用户按钮', async ({ page }) => {
    await dashboardPage.navigateToUserManagement();
    
    // 点击新增用户按钮
    await userManagementPage.clickCreateUser();
    
    // 验证对话框出现
    const dialog = page.locator('.el-dialog');
    await expect(dialog).toBeVisible();
    
    // 验证对话框标题
    const dialogTitle = dialog.locator('.el-dialog__title');
    await expect(dialogTitle).toContainText('新增用户');
  });
});
