# -*- coding:utf-8 -*-
# 作者：齐振鋆
# 日期：2023/2/14
# 描述: 手术请领单
import logging
from random import randint
from time import sleep

import allure

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

log = logging.getLogger(__name__)


class SurgeryRequire(object):
    third_menu = '手术请领单'

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

    @allure.step('查询：{}'.format(third_menu))
    def surgery_require_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 surgery_require_create(self, order_code=None):
        test_data_info = self.menu.dd.get_value('test_data_info')
        log.info('新建手术请领单')
        self.iframe.to_main_iframe()
        if order_code is None:
            self.button.new()
            log.info('获取请领单号&存储')
            ql_code = ''
            while ql_code.startswith('QL') is False:
                sleep(1)
                ql_code = self.order_detail_main.get_element_text('请领单号 ')
            log.info('请领单号：【{}】'.format(ql_code))
        else:
            self.surgery_require_search(order_code=order_code)
            self.list_page.open_order(order_code=order_code)
            ql_code = order_code

        self.menu.dd.set_value('ql_code', ql_code)
        UtilYaml(self.menu.dd.get_value('extract_path')).write_yaml({'ql_code': ql_code})

        # self.order_detail_main.choose_tap('主体信息') 已作废
        # 公司
        # self.order_detail_main.some_click_item('公司名称 ')
        # company_code = test_data_info['company']
        # self.modal.search_and_choose_items(placeholder='模糊检索:公司名称,公司编码', text=company_code)

        self.order_detail_main.some_click_item('客户名称 ')
        customer_code = test_data_info['customer']
        self.modal.search_and_choose_items(title='选择客户', text=customer_code)
        self.order_detail_main.some_click_item('配销客户名称 ')
        self.modal.search_and_choose_items(title='选择配销客户', text=customer_code)

        # 手术类型
        self.order_detail_main.drop_choose_item('手术类型 ', '翻修髋')
        # 手术日期
        date_str = self.menu.tu.get_date_str(1)
        self.order_detail_main.date_input_item('手术日期 ', date_str)
        # 手术医生
        self.order_detail_main.text_input_item('手术医生 ', '齐振鋆')
        # 手术种类
        self.order_detail_main.drop_choose_item('手术种类 ', '髋关节')

        # self.order_detail_main.choose_tap('客户信息') 已作废

        # self.order_detail_main.choose_tap('其他信息') 已作废
        # self.order_detail_main.some_click_item(name='是否启用开单校验 ', end_str="div[@class='awsui-component']")
        # 保存获得请领单号
        # self.order_detail_main.choose_tap('主体信息') 已作废
        # self.button.save()

        log.info('添加商品')
        self.button.click_button(button_name='添加商品(内部)')
        goods = test_data_info['goods']
        self.modal.search_and_choose_items(placeholder='模糊检索:商品编码,商品名称,规格型号,厂家物料编码,存储条件',
                                           text=goods)
        sleep(3 * len(goods))
        self.order_detail_table.get_cell_text('商品名称', goods[0])
        min_num = self.menu.dd.get_value('minNum')
        for good in goods:
            good_number = randint(min_num, 8) if self.dbq.get_serial_flag(test_data_info['warehouse_lend'],
                                                                          good) != 'Y' else randint(min_num, 2)
            self.order_detail_table.input_text('数量', good, good_number)
        self.button.submit()
        self.list_page.edit_iframe_is_show()
