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

export class NotificationPage {
  readonly page: Page;
  readonly table;
  readonly addButton;
  readonly editButton;
  readonly deleteButton;
  readonly saveButton;
  readonly cancelButton;
  readonly searchInput;
  readonly searchButton;
  readonly titleInput;
  readonly contentInput;
  readonly typeSelect;
  readonly statusSelect;

  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.titleInput = page.getByPlaceholder('请输入通知标题');
    this.contentInput = page.getByPlaceholder('请输入通知内容');
    this.typeSelect = page.locator('.el-select');
    this.statusSelect = page.locator('.el-select');
  }

  async goto() {
    await this.page.goto('/system/notice');
    await this.page.waitForLoadState('networkidle');
  }

  async addNotification(title: string, content: string, type: string = '1', status: string = '0') {
    await this.addButton.click();
    
    await this.titleInput.fill(title);
    await this.contentInput.fill(content);
    
    await this.saveButton.click();
    await this.page.waitForLoadState('networkidle');
  }

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

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

  async searchNotification(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;
  }
}