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

test.describe('UAT阶段四：菜单管理功能验证', () => {
  test.beforeEach(async ({ page }) => {
    await page.goto('/');
    await page.waitForLoadState('networkidle');
    
    const usernameInput = page.locator('input[type="text"]').first();
    const passwordInput = page.locator('input[type="password"]').first();
    const loginButton = page.locator('button:has-text("登录")');
    
    await usernameInput.fill('admin');
    await passwordInput.fill('admin123');
    await loginButton.click();
    
    await page.waitForURL('**/dashboard', { timeout: 30000 });
    await page.waitForLoadState('networkidle');
  });

  test('UAT-MENU-001: 菜单树形结构展示', async ({ page }) => {
    const systemMenu = page.locator('.el-sub-menu__title:has-text("系统管理")');
    await systemMenu.click();
    await page.waitForTimeout(1000);
    
    await page.click('text=菜单管理');
    await page.waitForURL('**/menus', { timeout: 30000 });
    await page.waitForLoadState('networkidle');
    
    await expect(page.locator('.el-table')).toBeVisible({ timeout: 10000 });
    
    await page.waitForTimeout(1000);
    
    const tableBody = page.locator('.el-table__body-wrapper');
    await expect(tableBody).toBeVisible();
    
    const emptyText = page.locator('text=暂无数据');
    const hasEmptyText = await emptyText.isVisible().catch(() => false);
    
    if (!hasEmptyText) {
      const treeNodes = page.locator('.el-table__row');
      const count = await treeNodes.count();
      expect(count).toBeGreaterThanOrEqual(0);
    }
  });

  test('UAT-MENU-002: 新增菜单表单验证', async ({ page }) => {
    const systemMenu = page.locator('.el-sub-menu__title:has-text("系统管理")');
    await systemMenu.click();
    await page.waitForTimeout(1000);
    
    await page.click('text=菜单管理');
    await page.waitForURL('**/menus', { timeout: 30000 });
    await page.waitForLoadState('networkidle');
    
    const addButton = page.locator('button:has-text("新增")').first();
    if (await addButton.isVisible()) {
      await addButton.click();
      await page.waitForTimeout(500);
      
      await expect(page.locator('.el-dialog')).toBeVisible();
      
      const menuNameInput = page.locator('.el-dialog input[placeholder*="菜单名称"]').first();
      if (await menuNameInput.isVisible()) {
        await menuNameInput.fill('测试菜单');
        
        const permsInput = page.locator('.el-dialog input[placeholder*="路由地址"]').first();
        if (await permsInput.isVisible()) {
          await permsInput.fill('/test-menu');
          
          const componentInput = page.locator('.el-dialog input[placeholder*="组件路径"]').first();
          if (await componentInput.isVisible()) {
            await componentInput.fill('views/test/TestMenu.vue');
            
            const confirmButton = page.locator('.el-dialog button:has-text("确定")');
            if (await confirmButton.isVisible()) {
              await confirmButton.click();
              await page.waitForTimeout(1000);
            }
          }
        }
      }
    }
  });

  test('UAT-MENU-003: 菜单类型选择', async ({ page }) => {
    const systemMenu = page.locator('.el-sub-menu__title:has-text("系统管理")');
    await systemMenu.click();
    await page.waitForTimeout(1000);
    
    await page.click('text=菜单管理');
    await page.waitForURL('**/menus', { timeout: 30000 });
    await page.waitForLoadState('networkidle');
    
    const addButton = page.locator('button:has-text("新增")').first();
    if (await addButton.isVisible()) {
      await addButton.click();
      await page.waitForTimeout(500);
      
      const menuTypeSelect = page.locator('.el-dialog .el-select').first();
      if (await menuTypeSelect.isVisible()) {
        await menuTypeSelect.click();
        await page.waitForTimeout(300);
        
        const options = page.locator('.el-select-dropdown__item');
        const count = await options.count();
        expect(count).toBeGreaterThan(0);
      }
    }
  });
});
