"""登录页面 — Page Object。

Usage:
    from tests.web.admin.pages.login_page import LoginPage

    login_page = LoginPage(page)
    login_page.login(phone="super_admin", password="951260684NiAn..")
"""

from playwright.sync_api import Page, expect

from tests.web.admin.pages.base_page import BasePage


class LoginPage(BasePage):
    """运八管理端登录页。"""

    def __init__(self, page: Page):
        super().__init__(page)
        # ── 定位器声明 ──
        self.username_input = page.get_by_placeholder("请输入用户名")
        self.password_input = page.get_by_placeholder("请输入密码")
        self.login_btn = page.get_by_role("button", name="登录")
        self.error_tip = page.locator(".ant-form-item-explain-error")

    def login(self, phone: str, password: str, expect_success: bool = True) -> None:
        """执行登录流程。

        Args:
            phone: 用户名/手机号
            password: 密码
            expect_success: True=预期成功跳转，False=预期留在登录页并有错误提示
        """
        self.fill_field(self.username_input, phone)
        self.fill_field(self.password_input, password)
        self.click_when_ready(self.login_btn)

        if expect_success:
            # 等待登录成功后页面跳转（hash 路由模式）
            self.page.wait_for_timeout(3000)
            assert "/#/login" not in self.page.url and "/login" not in self.page.url, \
                "登录失败，仍在登录页"
        else:
            # 预期登录失败，停留在当前页
            expect(self.page).to_have_url("**/login**", timeout=5_000)

    def get_error_message(self) -> str:
        """获取登录失败时的错误提示文本。"""
        self.wait_visible(self.error_tip)
        return self.error_tip.inner_text()

    def is_on_login_page(self) -> bool:
        """检查是否仍在登录页（兼容 hash 路由）。"""
        return "/login" in self.page.url or "/#/login" in self.page.url
