from test_login.page_login import PageLogin
from selenium.webdriver.remote.webdriver import By


class PageBuy(PageLogin):
    # 采购管理元素
    _buy_management_locator = (By.XPATH, '//div[@class="menuNavItem" and @sysname="采购管理"]')
    # 采购业务元素
    _buy_locator = (By.XPATH, '//div[@class="sec-menu" and @navname="采购业务"]')
    # 采退业务元素
    _return_locator = (By.XPATH, '//div[@class="sec-menu" and @navname="采退业务"]')
    # 普通采购订单元素
    _buy_op_locator = (By.XPATH, '//div[@class="third-menu" and @navname="普通采购订单"]')
    # 采购iframe
    _iframe_buy_locator = (
        By.XPATH, '//iframe[@class="metro-main-frame" and contains(@src,"purchases")]')
    # 新建按钮
    _new_locator = (By.XPATH, '//span[text() = "新建"]')
    # 对话框iframe，填写订单信息需要
    _sub_iframe_dialog_locator = (By.XPATH, '//iframe[contains(@name,"dialog-iframe")]')
    # 请求查询公司信息按钮元素
    _company_request_locator = (By.XPATH, '//input[starts-with(@id,"el-id-") and contains(@id,"-106")]/../span/span/i')
    # 公司信息输入元素
    _company_input_locator = (By.XPATH, '//input[@placeholder = "模糊检索:公司名称,公司编码"]')
    # 公司信息搜索元素
    _company_search_locator = (By.XPATH, '//input[@placeholder = "模糊检索:公司名称,公司编码"]/../span/span/i')
    # 公司表dialog body
    _company_dialog_locator = (
        By.XPATH, '//div[starts-with(@id,"el-id-") and contains(@id,"-21") and @class="el-dialog__body"]')
    # 公司表-----这个只能找绝对路径了，开发太狠了
    # _company_table_locator = (
    #    By.XPATH, '/html/body/div[6]/div/div/div/div[1]/div[2]/div[1]/div/div/div/div[1]/div[3]/div/div[1]/div/table')
    _company_table_locator = (By.CLASS_NAME, "el-table__body")
    # 公司确定按钮
    _company_table_ensure_locator = (By.XPATH, '//span[@class="ensureBtn"]/button/span[text() = "确定"]')
    # 仓库信息
    _ware_info_locator = (By.ID, 'tab-a58f571e-7488-4c22-bec0-67428972871d')
    # 主体信息
    _main_info_locator = (By.ID, 'tab-7667cf55-c391-4f2c-a5ba-c0f6d27b6d3a')
    # 请求查询仓库元素
    _ware_request_locator = (By.XPATH, '//input[starts-with(@id,"el-id-") and contains(@id,"-107")]/../span/span/i')
    # 仓库输入元素
    _ware_input_locator = (By.XPATH, '//input[@placeholder = "模糊检索:仓库名称,仓库编码"]')
    # 仓库搜索元素
    _ware_search_locator = (By.XPATH, '//input[@placeholder = "模糊检索:仓库名称,仓库编码"]/../span/span/i')
    # 仓库表-----这个只能找绝对路径了，开发太狠了
    # _ware_table_locator = (
    #    By.XPATH, '/html/body/div[8]/div/div/div/div[1]/div[2]/div[1]/div/div/div/div[1]/div[3]/div/div[1]/div/table')
    # 仓库表,通过dialog定位
    # _ware_table_locator = (By.XPATH, '//div[@role = "dialog" and @aria-label = "选择仓库"]/*/table[@class = "el-table__body"]')
    _ware_dialog_locator = (
        By.XPATH, '//div[starts-with(@id,"el-id-") and contains(@id,"-26") and @class="el-dialog__body"]')
    _ware_table_locator = 'By.XPATH, "//table[@class = "el-table__body""'
    # 仓库表确认元素---这中确定看着可以统一
    _ware_table_ensure_locator = (By.XPATH, '//span[@class="ensureBtn"]/button/span[text() = "确定"]')
    # 查询供应商请求元素
    _supplier_request_locator = (By.XPATH, '//input[starts-with(@id,"el-id-") and contains(@id,"-119")]/../span/span/i')
    # 供应商输入信息
    _supplier_input_locator = (By.XPATH, '//input[@placeholder = "模糊检索:供应商名称,供应商编码"]')
    # 供应商搜索元素
    _supplier_search_locator = (By.XPATH, '//input[@placeholder = "模糊检索:供应商名称,供应商编码"]/../span/span/i')
    # 供应商表元素-----这个只能找绝对路径了，开发太狠了
    _supplier_table_locator = (
        By.XPATH, '/html/body/div[10]/div/div/div/div[1]/div[2]/div[1]/div/div/div/div[1]/div[3]/div/div[1]/div/table')
    # 供应商dialog
    _supplier_dialog_locator = (
        By.XPATH, '//div[starts-with(@id,"el-id-") and contains(@id,"-20") and @class="el-dialog__body"]')
    # 供应商表确定元素---这中确定看着可以统一
    _supplier_table_ensure_locator = (By.XPATH, './span[@class="ensureBtn"]/button/span[text() = "确定"]')

    _dialog_ensure_locator = './*/span[@class="ensureBtn"]/button/span[text() = "确定"]'

    # 协议请求元素
    _protocol_request_locator = (By.XPATH, '//input[starts-with(@id,"el-id-") and contains(@id,"-109")]/../span/span/i')
    # 协议输入元素
    _protocol_input_locator = (By.XPATH,
                               '//input[@placeholder = "模糊检索:合同/协议号,合同号,项目编码,项目名称,业态编码,对方编码,产品线编码"]')
    # 协议搜索元素
    _protocol_search_locator = (By.XPATH,
                                '//input[@placeholder = "模糊检索:合同/协议号,合同号,项目编码,项目名称,业态编码,对方编码,产品线编码"]/../span/span/i')
    # 协议dialog元素
    _protocol_dialog_locator = (
        By.XPATH, '//div[starts-with(@id,"el-id-") and contains(@id,"-36") and @class="el-dialog__body"]')

    # 业务员请求元素
    _businessman_request_locator = (
        By.XPATH, '//input[starts-with(@id,"el-id-") and contains(@id,"-110")]/../span/span/i')
    # 业务员输入元素
    _businessman_input_locator = (By.XPATH, '//input[@placeholder="模糊检索:业务员编码,业务员名称,部门编码,部门名称"]')
    # 业务员搜索元素
    _businessman_search_locator = (
        By.XPATH, '//input[@placeholder="模糊检索:业务员编码,业务员名称,部门编码,部门名称"]/../span/span/i')
    # 业务员dialog元素
    _businessman_dialog_locator = (
        By.XPATH, '//div[starts-with(@id,"el-id-") and contains(@id,"-41") and @class="el-dialog__body"]')

    # 商品录入按钮
    _merchandise_locator = (By.XPATH, '//span[text()="商品录入"]')
    # 商品dialog
    _merchandise_dialog_locator = (
        By.XPATH, '//div[starts-with(@id,"el-id-") and contains(@id,"-124") and @class="el-dialog__body"]')
    # 商品输入元素
    _merchandise_input_locator = (By.XPATH, '//input[@placeholder="模糊检索:商品编码,商品名称,规格型号,厂家物料编码"]')
    # 商品搜索元素
    _merchandise_search_locator = (
        By.XPATH, '//input[@placeholder="模糊检索:商品编码,商品名称,规格型号,厂家物料编码"]/../span/span/i')

    # dialog中的输入元素
    _dialog_input_locator = './*/input[@class="el-input__inner" and @placeholder="模糊检索:商品编码,商品名称,规格型号,厂家物料编码"]'
    # dialog中的搜索元素
    _dialog_search_locator = './*/span[@class="el-input__prefix-inner"]/i'
    """表格中输入元素"""
    _input_in_table_locator = 'input'

    # 保存按钮
    # _save_locator = (By.XPATH, '//*[@id="process-toolbar"]/button[4]/span/text()')
    _save_locator = (By.XPATH, '//*[@id="process-toolbar"]/*/span[text()="保存"]')

    # 订单号
    _order_no_locator = (By.XPATH, '//*[@id="0b83dfe6-67bc-46bf-b271-5e768a59e415"]/div/div[2]/span')
    # 商品明细表
    _detail_table = (
    By.XPATH, '//*[@id="a55ef0dd-0262-4caa-b701-684a33007ac5"]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table')
    # _detail_table = (
    #    By.XPATH, '//*[@id="a55ef0dd-0262-4caa-b701-684a33007ac5"]/*/table[@class="vxe-table--body"]')

    '''获取采购管理元素'''

    def get_buy_management(self):
        return self.get_element_wait_presence(self._buy_management_locator)

    '''获取采购元素'''

    def get_buy(self):
        return self.get_element_wait_presence(self._buy_locator)

    '''获取采退元素'''

    def get_return(self):
        return self.get_element_wait_presence(self._return_locator)

    '''获取采购订单op元素'''

    def get_buy_op(self):
        return self.get_element_wait_presence(self._buy_op_locator)

    '''获取采购iframe元素'''

    def get_iframe_buy(self):
        return self.get_element_wait_presence(self._iframe_buy_locator)

    '''获取新建按钮元素'''

    def get_new(self):
        return self.get_element_wait_presence(self._new_locator)

    '''获取对话框iframe元素'''

    def get_sub_iframe_dialog(self):
        return self.get_element_wait_presence(self._sub_iframe_dialog_locator)

    '''获取查询公司请求按钮'''

    def get_company_request(self):
        return self.get_element_wait_presence(self._company_request_locator)

    '''获取公司输入信息元素'''

    def get_company_input(self):
        return self.get_element_wait_presence(self._company_input_locator)

    '''获取公司搜索按钮元素'''

    def get_company_search(self):
        return self.get_element_wait_presence(self._company_search_locator)

    '''获取公司表元素'''

    def get_company_table(self):
        # return self.get_element_wait_presence(self._company_table_locator)
        # 首先获取公司dialog
        company_dialog = self.get_company_dialog()
        '''再从公司dialog中获取公司表'''
        return self.get_table_from_dialog(company_dialog)

    '''获取公司表确定按钮'''

    def get_company_table_ensure(self):
        return self.get_element_wait_presence(self._company_table_ensure_locator)

    '''获取仓库信息'''

    def get_ware_info(self):
        return self.get_element_wait_presence(self._ware_info_locator)

    '''获取主体信息'''

    def get_main_info(self):
        return self.get_element_wait_presence(self._main_info_locator)

    '''获取请求查询仓库元素'''

    def get_ware_request(self):
        return self.get_element_wait_presence(self._ware_request_locator)

    '''获取仓库输入元素'''

    def get_ware_input(self):
        return self.get_element_wait_presence(self._ware_input_locator)

    '''获取仓库搜索元素'''

    def get_ware_search(self):
        return self.get_element_wait_presence(self._ware_search_locator)

    '''获取仓库dialog'''

    def get_ware_dialog(self):
        return self.get_element_wait_presence(self._ware_dialog_locator)

    '''获取仓库表元素'''

    def get_ware_table(self):
        ware_dialog = self.get_ware_dialog()
        return self.get_table_from_dialog(ware_dialog)

    '''获取仓库表确定按钮'''

    def get_ware_table_ensure(self):
        ware_dialog = self.get_ware_dialog()
        return ware_dialog.find_element(By.XPATH, '//span[@class="ensureBtn"]/button/span[text() = "确定"]')

        # return self.get_child(ware_dialog, self._ware_table_ensure_locator)

    '''获取供应商dialog元素'''

    def get_supplier_dialog(self):
        return self.get_element_wait_presence(self._supplier_dialog_locator)

    '''从dialog中获取输入元素'''

    def get_input_from_dialog(self, dialog_element):
        result = dialog_element.find_element(By.XPATH, self._dialog_input_locator)
        self.wait_seconds(self.time_seconds)
        return result

    '''从dialog中获取搜索元素'''

    def get_search_from_dialog(self, dialog_element):
        search = dialog_element.find_element(By.XPATH, self._dialog_search_locator)
        self.wait_seconds(self.time_seconds)
        return search

    '''从dialog中获取表信息'''

    def get_table_from_dialog(self, dialog_element):
        tables = dialog_element.find_elements(By.TAG_NAME, "table")
        self.wait_seconds(self.time_seconds)
        return tables[1]

    '''从dialog中获取确定按钮'''

    def get_ensure_from_dialog(self, dialog_element):
        ensure = dialog_element.find_element(By.XPATH, self._dialog_ensure_locator)
        return ensure

    """从表格中的一个栏位中获取输入元素"""

    def get_input_from_table_field(self, field):
        element = field.find_element(By.XPATH, self._input_in_table_locator)
        return element

    '''获取供应商请求元素'''

    def get_supplier_request(self):
        return self.get_element_wait_presence(self._supplier_request_locator)

    '''获取供应商输入元素'''

    def get_supplier_input(self):
        return self.get_element_wait_presence(self._supplier_input_locator)

    '''获取供应商搜索元素'''

    def get_supplier_search(self):
        return self.get_element_wait_presence(self._supplier_search_locator)

    '''获取供应商表元素'''

    def get_supplier_table(self):
        return self.get_element_wait_presence(self._supplier_table_locator)

    '''获取供应商表确定元素'''

    def get_supplier_table_ensure(self):
        return self.get_element_wait_presence(self._supplier_table_ensure_locator)

    '''获取公司dialog'''

    def get_company_dialog(self):
        return self.get_element_wait_presence(self._company_dialog_locator)

    '''获取协议请求元素'''

    def get_protocol_request(self):
        return self.get_element_wait_presence(self._protocol_request_locator)

    '''获取协议输入元素'''

    def get_protocol_input(self):
        return self.get_element_wait_presence(self._protocol_input_locator)

    '''获取协议搜索元素'''

    def get_protocol_search(self):
        return self.get_element_wait_presence(self._protocol_search_locator)

    '''获取协议dialog'''

    def get_protocol_dialog(self):
        return self.get_element_wait_presence(self._protocol_dialog_locator)

    '''获取业务员请求元素'''

    def get_businessman_request(self):
        return self.get_element_wait_presence(self._businessman_request_locator)

    '''获取业务员输入元素'''

    def get_businessman_input(self):
        return self.get_element_wait_presence(self._businessman_input_locator)

    '''获取业务员搜索元素'''

    def get_businessman_search(self):
        return self.get_element_wait_presence(self._businessman_search_locator)

    '''获取业务员dialog'''

    def get_businessman_dialog(self):
        return self.get_element_wait_presence(self._businessman_dialog_locator)

    '''商品录入按钮'''

    def get_merchandise(self):
        return self.get_element_wait_presence(self._merchandise_locator)

    '''商品dialog按钮'''

    def get_merchandise_dialog(self):
        return self.get_element_wait_presence(self._merchandise_dialog_locator)

    '''商品输入元素'''

    def get_merchandise_input(self):
        return self.get_element_wait_presence(self._merchandise_input_locator)

    '''商品搜索元素'''

    def get_merchandise_search(self):
        return self.get_element_wait_presence(self._merchandise_search_locator)

    '''保存按钮元素'''

    def get_save(self):
        return self.get_element_wait_presence(self._save_locator)

    '''订单号'''

    def get_order_no(self):
        return self.get_element_wait_presence(self._order_no_locator)

    '''商品明细表'''

    def get_detail_table(self):
        return self.get_element_wait_presence(self._detail_table)
