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

export class SystemConfigPage {
  readonly page: Page;
  readonly table;
  readonly addButton;
  readonly editButton;
  readonly deleteButton;
  readonly saveButton;
  readonly cancelButton;
  readonly searchInput;
  readonly searchButton;
  readonly configNameInput;
  readonly configKeyInput;
  readonly configValueInput;
  readonly configTypeSelect;

  constructor(page: Page) {
    this.page = page;
    this.table = page.locator('.el-table');
    this.addButton = page.getByRole('button', { name: '新增配置' });
    this.editButton = page.getByRole('button', { name: '编辑' });
    this.deleteButton = page.getByRole('button', { name: '删除' });
    this.saveButton = page.getByRole('button', { name: '确定' });
    this.cancelButton = page.getByRole('button', { name: '取消' });
    this.searchInput = page.getByPlaceholder('搜索配置名称');
    this.searchButton = page.getByRole('button', { name: '搜索' });
    this.configNameInput = page.getByPlaceholder('请输入配置名称');
    this.configKeyInput = page.getByPlaceholder('请输入配置键名');
    this.configValueInput = page.getByPlaceholder('请输入配置键值');
    this.configTypeSelect = page.locator('.el-select');
  }

  async goto() {
    await this.page.goto('/sys/config');
    await this.page.waitForLoadState('networkidle');
  }

  async addConfig(configName: string, configKey: string, configValue: string, configType: string = 'Y') {
    await this.addButton.click();
    
    await this.configNameInput.fill(configName);
    await this.configKeyInput.fill(configKey);
    await this.configValueInput.fill(configValue);
    
    await this.saveButton.click();
    await this.page.waitForLoadState('networkidle');
  }

  async editConfig(configKey: string, newValue: string) {
    const row = this.table.locator('tr').filter({ hasText: configKey }).first();
    await row.locator('.el-button--primary').click();
    
    await this.configValueInput.clear();
    await this.configValueInput.fill(newValue);
    
    await this.saveButton.click();
    await this.page.waitForLoadState('networkidle');
  }

  async deleteConfig(configKey: string) {
    const row = this.table.locator('tr').filter({ hasText: configKey }).first();
    await row.locator('.el-button--danger').click();
    
    await this.saveButton.click();
    await this.page.waitForLoadState('networkidle');
  }

  async searchConfig(keyword: string) {
    await this.searchInput.fill(keyword);
    await this.searchButton.click();
    await this.page.waitForLoadState('networkidle');
  }

  async clearSearch() {
    await this.searchInput.clear();
    await this.searchButton.click();
    await this.page.waitForLoadState('networkidle');
  }

  async verifyTableContains(text: string) {
    await expect(this.table).toContainText(text);
  }

  async verifyTableNotContains(text: string) {
    await expect(this.table).not.toContainText(text);
  }

  async getTableRowCount() {
    const rows = await this.table.locator('.el-table__row').count();
    return rows;
  }
}