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

export class NotificationPage {
  readonly page: Page;
  readonly table;
  readonly addButton;
  readonly saveButton;
  readonly cancelButton;
  readonly dialog;
  readonly titleInput;
  readonly contentInput;
  readonly noticeTypeSelect;
  readonly statusSelect;

  constructor(page: Page) {
    this.page = page;
    this.table = page.locator('.el-table');
    this.addButton = page.getByRole('button', { name: '新增公告' });
    this.saveButton = page.getByRole('button', { name: '确定' });
    this.cancelButton = page.getByRole('button', { name: '取消' });
    this.dialog = page.locator('.el-dialog');
    this.titleInput = page.locator('.el-dialog').getByRole('textbox', { name: '公告标题' });
    this.contentInput = page.locator('.el-dialog').getByRole('textbox', { name: '公告内容' });
    this.noticeTypeSelect = page.locator('.el-dialog').getByRole('combobox', { name: '公告类型' });
    this.statusSelect = page.locator('.el-dialog').getByRole('combobox', { name: '状态' });
  }

  async goto() {
    try {
      console.log('导航到通知管理页面...');
      await this.page.goto('/notice');

      await this.page.waitForLoadState('domcontentloaded');
      await this.page.waitForTimeout(1000);
      await this.table.waitFor({ state: 'visible', timeout: 15000 });
      await expect(this.page).toHaveURL(/.*notice/, { timeout: 15000 });

      console.log('通知管理页面加载完成');
    } catch (error) {
      if (!this.page.isClosed()) {
        await this.page.screenshot({ path: `test-results/notification-error-${Date.now()}.png` });
      }
      console.error('导航到通知管理页面失败:', error);
      throw new Error(`导航到通知管理页面失败: ${error instanceof Error ? error.message : String(error)}`);
    }
  }

  async addNotification(title: string, content: string) {
    await this.addButton.click();
    await this.page.waitForTimeout(500);

    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();
    const editBtn = row.getByRole('button', { name: '编辑' });
    await editBtn.click();
    await this.page.waitForTimeout(500);

    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();
    const deleteBtn = row.getByRole('button', { name: '删除' });
    await deleteBtn.click();
    await this.page.waitForTimeout(500);

    const confirmBtn = this.page.locator('.el-message-box').getByRole('button', { name: '确定' });
    await confirmBtn.click();
    await this.page.waitForLoadState('networkidle');
  }

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

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