# -*- coding:utf-8 -*-
# 作者：齐振鋆
# 日期：2023/2/14
# 描述:
import logging
from time import sleep

import pytest

from page.GuKeYiTiHua.Public.errorMsg import ErrorMessage
from page.GuKeYiTiHua.Public.table import Table
from unit.public.UI.BasePage import BasePage

log = logging.getLogger(__name__)


class OrderDetailMain(BasePage):
    em = ErrorMessage()

    def choose_tap(self, tap_name):
        """
        主单信息tap页
        parameter: tap_name tap名称
        """
        log.info('切换详情tap页到：{}'.format(tap_name))
        self.click("//*[text()='{}']".format(tap_name))

    def text_input_item(self, name, text, enter=False, get=False):
        """输入框元素"""
        log.info('【{}】输入：{}'.format(name, text))
        text_input = "//*[string()='{}']/following-sibling::div//input".format(name)
        if get is True:
            return text_input
        else:
            self.type(text_input, text)
            self.enter(text_input) if enter is True else 0
            self.em.check_message()

    def text_area_item(self, name, text):
        """输入域元素"""
        log.info('【{}】输入：{}'.format(name, text))
        area = "//*[string()='{}']/following-sibling::div//textarea".format(name)
        self.type(area, text)

    def some_click_item(self, name, end_str="i[@class='awsui-iconfont']", get=False):
        log.info('点击：【{}】相关图标'.format(name))
        element = "//*[text()='{}']/following-sibling::div//{}".format(name, end_str)
        return element if get is True else self.click(element)

    def drop_choose_item(self, name, item):
        """下拉元素"""
        log.info('【{}】下拉选择：{}'.format(name, item))
        self.click("//*[string()='{}']/following-sibling::div//input".format(name))
        drop_item = "//div[@data-popper-placement='bottom-start' and @aria-hidden='false']//*[text()='{}']".format(item)
        self.click(drop_item)

    def date_input_item(self, name, date_str=None):
        """日期元素"""
        date_input = "//*[string()='{}']/following-sibling::div//input".format(name)
        date_str = self.tu.get_date_str(1) if date_str is None else date_str
        log.info('【{}】输入：{}'.format(name, date_str))
        self.type(date_input, date_str)

    def get_element_text(self, name, get=False):
        """获取元素内文本"""
        log.info('获取：【{}】'.format(name))
        element = "//div[text()='{}']/following-sibling::div/span".format(name)
        if get is True:
            return element
        else:
            text = self.get_ele_text(element)
            log.info('{}：【{}】'.format(name, text))
            return text

    def choose_transactor(self, transactor='管理员'):
        log.info('选择办理人&提交')
        self.click("//div[text()='办理人']/following-sibling::div//i[contains(@class, 'awsui-iconfont-add-ico')]")
        self.double_click("//span[text()='{} ']".format(transactor))


class OrderDetailTable(BasePage):
    table = Table()

    def position_cell(self, column_name, row_tag):
        """获取表单明细单元格Xpath"""
        colid = self.table.get_col_id(column_name)
        # 定位表格
        th_xpath = "//span[text()='{}']/ancestor::th".format(column_name)
        table_div = "{}/ancestor::div[@class='form-row']".format(th_xpath)
        # 定位行
        tr_xpath = "{}//span[text()='{}']/ancestor::tr[contains(@class,'vxe-body--row')]".format(table_div, row_tag)
        # 定位列
        return "{}/td[@colid='{}']/div".format(tr_xpath, colid)

    def get_cell_text(self, column_name, row_tag):
        """获取表单单元格内容"""
        log.info('获取{}的{}'.format(row_tag, column_name))
        # 获取列id
        cell = self.position_cell(column_name, row_tag)
        self.scroll_element(cell)
        return self.get_ele_text(cell)

    def input_text(self, column_name, row_tag, text):
        """表单单元格输入框输入内容"""
        log.info('【{}】的【{}】输入：【{}】'.format(row_tag, column_name, text))
        # 定位单元格
        td_xpath = self.position_cell(column_name, row_tag)
        self.scroll_element(td_xpath)
        # 点击单元格以加载输入框
        self.click(td_xpath)
        # 定位输入框
        input_xpath = "{}//input".format(td_xpath)
        # 输入内容
        self.type(input_xpath, text)
        # 移出光标
        th_xpath = "//span[text()='{}']/ancestor::th".format(column_name)
        self.click(th_xpath)
        sleep(0.5)

    def choose_item(self, column_name, row_tag, item):
        """表单单元格下拉选择"""
        log.info('【{}】的【{}】选择：【{}】'.format(row_tag, column_name, item))
        # 定位单元格
        td_xpath = self.position_cell(column_name, row_tag)
        self.scroll_element(td_xpath)
        # 点击单元格以加载输入框
        self.click(td_xpath)
        self.click("//div[@data-popper-placement='bottom-start' and @aria-hidden='false']//*[text()='{}']".format(item))
        # 移出光标
        th_xpath = "//span[text()='{}']/ancestor::th".format(column_name)
        self.click(th_xpath)
        sleep(0.5)

        # 移出光标
        th_xpath = "//span[text()='{}']/ancestor::th".format(column_name)
        self.click(th_xpath)
        sleep(0.5)

    def get_details(self, columns):
        """获取表单明细详情"""
        columns = [columns] if type(columns) == str else columns if type(columns) == list else pytest.fail(
            "获取单据明细详情参数类型错误")
        detail_list = []
        col_list = []
        for column in columns:
            colid = self.table.get_col_id(column)
            col_list.append(colid)
        lines = len(self.get_elements("//tr[@class='vxe-body--row']"))
        log.info('当前单据有：{}行商品明细'.format(lines))
        for index in range(lines):
            item = []
            for col_id in col_list:
                ele = "(//td[@colid='{}' and contains(@class, 'col--left')])[{}]//*[contains(@class, 'label')]".format(
                    col_id, index + 1)
                log.info('{}:{}'.format('元素：', ele))
                txt = self.get_ele_text(ele)
                log.info('{}:{}'.format('文本：', txt))
                item.append(txt)
            detail_list.append(item)
        return detail_list

    def scroll(self):
        # 浏览器页面右滑
        self.js(
            "document.getElementsByClassName('vxe-table--body-wrapper body--wrapper')[0].scrollLeft = 2000")


class ScanTable(BasePage):
    """扫码明细"""

    def check_enter_result(self, text):
        """检查扫码结果"""
        td = "//*[text()='扫码明细']/following-sibling::div//*[text()='{}']".format(text)
        self.wait_element(td, wait_time=3)
