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

test.describe('调试角色分配', () => {
  test('调试角色分配功能', async ({ page }) => {
    await test.step('导航到用户管理', async () => {
      await page.goto('/users');
      await page.waitForLoadState('networkidle');
      await expect(page.locator('.el-table')).toBeVisible({ timeout: 10000 });
    });

    await test.step('查找测试用户', async () => {
      await page.waitForTimeout(1000);
      
      const userRow = page.locator('.el-table__row').first();
      await expect(userRow).toBeVisible({ timeout: 10000 });
      await page.screenshot({ path: 'test-results/debug-role-1-user-found.png' });
    });

    await test.step('打开分配角色对话框', async () => {
      const userRow = page.locator('.el-table__row').first();
      await userRow.locator('button:has-text("分配角色")').click();
      await page.waitForSelector('.el-dialog:has-text("分配角色")', { state: 'visible', timeout: 5000 });
      await page.screenshot({ path: 'test-results/debug-role-2-dialog-open.png' });
    });

    await test.step('检查转移组件状态', async () => {
      const transfer = page.locator('.el-transfer');
      
      const leftPanel = transfer.locator('.el-transfer-panel').first();
      const rightPanel = transfer.locator('.el-transfer-panel').last();
      
      const leftItems = await leftPanel.locator('.el-checkbox').all();
      const rightItems = await rightPanel.locator('.el-checkbox').all();
      
      console.log(`左侧面板有 ${leftItems.length} 个选项`);
      console.log(`右侧面板有 ${rightItems.length} 个选项`);
      
      for (let i = 0; i < leftItems.length; i++) {
        const text = await leftItems[i].textContent();
        const isChecked = await leftItems[i].locator('input').isChecked();
        console.log(`左侧选项 ${i}: ${text}, 是否选中: ${isChecked}`);
      }
      
      for (let i = 0; i < rightItems.length; i++) {
        const text = await rightItems[i].textContent();
        const isChecked = await rightItems[i].locator('input').isChecked();
        console.log(`右侧选项 ${i}: ${text}, 是否选中: ${isChecked}`);
      }
      
      await page.screenshot({ path: 'test-results/debug-role-3-transfer-state.png' });
    });

    await test.step('选择并转移角色', async () => {
      const transfer = page.locator('.el-transfer');
      
      const superAdminCheckbox = transfer.locator('.el-checkbox:has-text("超级管理员")');
      const isChecked = await superAdminCheckbox.locator('input').isChecked();
      
      console.log(`超级管理员复选框是否已选中: ${isChecked}`);
      
      if (!isChecked) {
        await superAdminCheckbox.click();
        await page.waitForTimeout(500);
        console.log('点击了超级管理员复选框');
      }
      
      await page.screenshot({ path: 'test-results/debug-role-4-checkbox-clicked.png' });
      
      const moveToRightButton = transfer.locator('.el-transfer__buttons button').nth(1);
      const isEnabled = await moveToRightButton.isEnabled();
      console.log(`转移按钮是否可用: ${isEnabled}`);
      
      if (isEnabled) {
        await moveToRightButton.click();
        await page.waitForTimeout(500);
        console.log('点击了转移按钮');
      }
      
      await page.screenshot({ path: 'test-results/debug-role-5-role-transferred.png' });
      
      const rightPanel = transfer.locator('.el-transfer-panel').last();
      const rightItems = await rightPanel.locator('.el-checkbox').all();
      console.log(`转移后右侧面板有 ${rightItems.length} 个选项`);
    });

    await test.step('点击确定按钮', async () => {
      const confirmButton = page.locator('.el-dialog:has-text("分配角色") button:has-text("确定")');
      await confirmButton.click();
      console.log('点击了确定按钮');
      
      await page.waitForTimeout(2000);
      await page.screenshot({ path: 'test-results/debug-role-6-after-confirm.png' });
      
      const dialog = page.locator('.el-dialog:has-text("分配角色")');
      const isVisible = await dialog.isVisible();
      console.log(`对话框是否可见: ${isVisible}`);
      
      if (isVisible) {
        const errorMessage = page.locator('.el-message--error');
        const hasError = await errorMessage.isVisible().catch(() => false);
        if (hasError) {
          const errorText = await errorMessage.textContent();
          console.log(`错误消息: ${errorText}`);
        }
        
        const formItems = await dialog.locator('.el-form-item').all();
        console.log(`表单项数量: ${formItems.length}`);
        
        for (let i = 0; i < formItems.length; i++) {
          const hasError = await formItems[i].locator('.el-form-item__error').isVisible().catch(() => false);
          if (hasError) {
            const label = await formItems[i].locator('.el-form-item__label').textContent();
            const errorText = await formItems[i].locator('.el-form-item__error').textContent();
            console.log(`表单项 "${label}" 有错误: ${errorText}`);
          }
        }
      }
    });
  });
});
