# -*- coding:utf-8 -*-
# 作者：齐振鋆
# 日期：2023/2/27
# 描述:
import logging
import allure

from time import sleep

from selenium.common.exceptions import TimeoutException

from component.GuKeYiTiHua import iframe, menu, list_page, button, order_detail_main, order_detail_table, \
    modal
from component.GuKeYiTiHua.purchaseManage import first_menu
from unit.public.YamlUtil import UtilYaml

log = logging.getLogger(__name__)


class PurchaseOP(object):
    third_menu = '普通采购订单'

    def __init__(self):
        self.iframe = iframe
        self.menu = menu
        self.list_page = list_page
        self.button = button
        self.order_detail_main = order_detail_main
        self.order_detail_table = order_detail_table
        self.modal = modal

    @allure.step('打开菜单：{}'.format(third_menu))
    def open_menu_purchase_op(self):
        self.iframe.out_frame()
        self.menu.open_menu(first_menu, self.third_menu)

    @allure.step('查询：{}'.format(third_menu))
    def purchase_op_search(self, tap_name='待办', order_code=None, count=1):
        self.list_page.order_search(self.third_menu, tap_name, '订单号', order_code, count)

    @allure.step('新建：{}'.format(third_menu))
    def purchase_op_create(self, order_code=None):
        log.info('新建采购订单(OP)')
        self.iframe.out_frame()
        self.iframe.to_main_iframe()
        if order_code is None:
            self.button.new()
        else:
            self.purchase_op_search(order_code=order_code)
            self.list_page.open_order('订单号')
        self.order_detail_main.choose_tap('主体信息')
        # self.order_detail_main.some_click_item('公司名称 ')
        # company_code = self.menu.dd.get_value('test_data_info')['company']
        # self.modal.search_and_choose_items(placeholder='模糊检索:公司名称,公司编码', text=company_code)
        # try:
        #     self.button.click_button(root_str="//div[contains(@class, 'is-message-box')]", button_name='确定')
        # except TimeoutException:
        #     pass
        # self.order_detail_main.choose_tap('仓库信息')
        self.order_detail_main.some_click_item('仓库名称 ')
        warehouse_code = self.menu.dd.get_value('test_data_info')['warehouse']
        self.modal.search_and_choose_items(title='选择仓库', text=warehouse_code)
        # ------
        self.order_detail_main.choose_tap('合同信息')
        self.order_detail_main.some_click_item('采购合同/协议号 ')
        agreement_code = self.menu.dd.get_value('test_data_info')['agreement']
        self.modal.search_and_choose_items(title='选择合同', text=agreement_code)
        self.order_detail_main.some_click_item('供应商名称 ')
        supplier_code = self.menu.dd.get_value('test_data_info')['supplier']
        self.modal.search_and_choose_items(title='选择供应商', text=supplier_code)
        # ------
        self.order_detail_main.some_click_item('业务员名称 ')
        salesman_code = self.menu.dd.get_value('test_data_info')['salesman']
        self.modal.search_and_choose_items(title='选择业务员及部门', text=salesman_code)

        # ------
        log.info('获取采购单号&存储')
        self.order_detail_main.choose_tap('主体信息')
        self.button.save()
        purchase_op_code = ''
        while purchase_op_code.startswith('OP') is False:
            sleep(1)
            purchase_op_code = self.order_detail_main.get_element_text('订单号 ')
        self.menu.dd.set_value('purchase_op_code', purchase_op_code)
        UtilYaml(self.menu.dd.get_value('extract_path')).write_yaml({'purchase_op_code': purchase_op_code})
        self.order_detail_main.some_click_item(name='是否启用开单校验(测试用) ', end_str="div[@class='awsui-component']")
        log.info('添加商品')
        self.button.click_button(button_name='商品录入')
        goods_obj = self.menu.dd.get_value('test_data_info')['goods']
        goods_code_list = []
        for good in goods_obj:
            goods_code_list.append(good[0])
        self.modal.search_and_choose_items(placeholder='模糊检索:商品编码,商品名称,规格型号,厂家物料编码', text=goods_code_list)
        sleep(5 * len(goods_code_list))
        self.order_detail_table.get_cell_text('商品名称', goods_code_list[0])
        for good in goods_obj:
            good_code = good[0]
            good_number = good[1]
            self.order_detail_table.input_text('订购数量', good_code, good_number)
        self.button.submit()
        self.list_page.edit_iframe_is_show()

    @allure.step('办理：{}'.format(third_menu))
    def purchase_op_transact(self, order_code):
        log.info('采购订单办理/审批')
        self.purchase_op_search(order_code=order_code, count=1)
        self.list_page.open_order('订单号')
        self.button.transact()
        self.list_page.edit_iframe_is_show()
