# -*- coding:utf-8 -*-
# 作者：齐振鋆
# 日期：2023/3/20
# 描述: 选配类转销售

import allure
import logging
from time import sleep

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

log = logging.getLogger(__name__)


class OptionalResale(object):
    third_menu = '选配类转销售单'

    def __init__(self):
        self.dbq = dbq
        self.iframe = iframe
        self.menu = menu
        self.button = button
        self.list_page = list_page
        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_optional_resale(self):
        self.iframe.out_frame()
        self.menu.open_menu(first_menu, self.third_menu)

    @allure.step('查询：{}'.format(third_menu))
    def optional_resale_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 optional_resale_create(self, order_code):
        """潜在问题，JC单中序列号数量过多时，会导致需要翻页，点不到的情况"""
        test_data_info = self.menu.dd.get_value('test_data_info')
        self.iframe.to_main_iframe()
        if order_code is None:
            self.button.new()
        else:
            self.optional_resale_search(order_code=order_code)
            self.list_page.open_order(order_code=order_code)
        self.order_detail_main.choose_tap('主体信息')
        self.order_detail_main.some_click_item('公司名称 ')
        self.modal.search_and_choose_items(text=test_data_info['company'])
        self.order_detail_main.some_click_item('业务员名称 ')
        self.modal.search_and_choose_items(text=test_data_info['salesman'])
        self.order_detail_main.some_click_item('出库仓库 ')
        self.modal.search_and_choose_items(text=test_data_info['warehouse_borrow'])
        self.order_detail_main.date_input_item('手术日期 ', self.menu.tu.get_date_str())
        self.order_detail_main.choose_tap('客户及合同信息')
        self.order_detail_main.some_click_item('合同号 ')
        self.modal.search_and_choose_items(text=test_data_info['agreement'])
        self.order_detail_main.choose_tap('主体信息')
        self.button.save()
        while self.order_detail_main.get_element_text('销售订单号 ').startswith('XS') is False:
            sleep(1)
        xs_code = self.order_detail_main.get_element_text('销售订单号 ')
        UtilYaml(self.menu.dd.get_value('extract_path')).write_yaml({'xs_code': xs_code})
        log.info('销售订单号：【{}】'.format(xs_code))
        self.menu.dd.set_value('xs_code', xs_code)
        self.button.click_button(button_name='借出单明细')
        jc_code = self.menu.dd.get_value('jc_code')
        self.modal.search_and_choose_items(placeholder='模糊检索:借出单号,商品名称,规格型号,批次号,序列号,原始订单类型',
                                           text=jc_code, sing_choose=False)
        click_item = []
        back_info = self.menu.dd.get_value('back_info')
        for item in back_info:
            if item['sf'] != 'Y' and item['check'] - item['back'] > 0:
                click_item += [item['good']]
            if item['sf'] == 'Y' and item['check'] - item['back'] > 0:
                click_item += item['serial']
        for item in click_item:
            self.modal.click_cell(text=item)
        self.button.click_button(
            "//div[@tabindex='-1']/ancestor::div[contains(@class, 'el-overlay') and not(ancestor-or-self::*[contains(@style,'display: none;')])]",
            '确定', "/parent::button[@style='']")
        self.button.submit()
        self.list_page.edit_iframe_is_show()

    @allure.step('办理：{}'.format(third_menu))
    def optional_resale_transact(self, order_code):
        self.optional_resale_search(order_code=order_code)
        self.list_page.open_order(order_code=order_code)
        self.button.submit()
