#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@File    :   home_page.py
@Time    :   2024/11/06 22:51:00
@Author  :   dulip3ng 
@Version :   1.0
@Desc    :   None
'''
from pages.base_page import BasePage
from pages.locator.home_page_locator import HomePageLocator
from playwright.sync_api import Page, expect


class HomePage(BasePage):

    def change_org(self, org_num:str):
        """
        切换组织
        :param org_num:组织代码
        :return: None
        """
        self.page.locator(HomePageLocator.ORGANIZATION_LOC).click()
        self.page.locator(HomePageLocator.ORG_ITEM_ARGS_LOC % org_num).click()

    def search_bill(self, bill_name:str, order:int=1):
        """
        查询并打开单据
        :param bill_name:单据名称
        :param order: 重名单据的情况下打开第几个
        :return: None
        """
        self.page.locator(HomePageLocator.SEARCH_IMG_LOC).click()
        self.page.locator(HomePageLocator.SEARCH_LOC).fill(bill_name)
        self.page.locator(HomePageLocator.BILL_LIST_ARGS_LOC % (bill_name, order)).click()

    def close_bill_by_name(self, bill_name:str):
        """
        点击单据页签的"x"图标关闭单据，模糊匹配
        :TODO: 加入关闭单据提示
        :param bill_name:单据名称
        :return: None
        """
        self.page.locator(HomePageLocator.BILL_CLOSE_ARGS_LOC % bill_name).click()

    def close_all_opening_bills(self):
        """
        关闭所有已打开单据页签
        :TODO: 加入关闭单据提示
        :return: None
        """
        self.page.locator(HomePageLocator.ALL_OPEN_FORM_BTN_LOC).click()
        self.page.locator(HomePageLocator.CLOSE_ALL_BILLS_ITEM_LOC).click()

    def is_bill_opening(self, bill_name:str):
        """
        判断单据是否是打开状态, 默认等待5秒
        :param bill_name:单据名称
        :return:打开True，未打开False
        """
        try:
            loctor = self.page.locator(HomePageLocator.IS_BILL_OPENING_ARGS_LOC % bill_name)
            expect(loctor).to_be_attached()
            return True
        except AssertionError:
            return False

    def change_bill_tab(self, bill_name:str):
        """
        在已打开单据间切换
        :param bill_name:单据名称
        :return: None
        """
        self.page.locator(HomePageLocator.BILL_TAB_ARGS_LOC % bill_name).click()

