# -*- coding:utf-8 -*-
# 作者：齐振鋆
# 日期：2023/2/14
# 描述: 骨科仓借出单
import allure
import logging
from random import randint
from time import sleep

from selenium.common.exceptions import TimeoutException

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

log = logging.getLogger(__name__)


class OrthopedicLend(object):
    third_menu = '骨科仓借出单'

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

    @allure.step('查询：{}'.format(third_menu))
    def orthopedic_lend_search(self, tap_name='待办', ot='JC', order_code=None, count=1):
        order_obj = {
            'JC': '借出单号',
            'QL': '请领单号'
        }
        self.list_page.order_search(self.third_menu, tap_name, order_obj[ot], order_code, count)

    @allure.step('新建：{}'.format(third_menu))
    def orthopedic_lend_create(self, order_code=None):
        test_data_info = self.menu.dd.get_value('test_data_info')
        log.info('骨科仓借出单创建')
        self.iframe.out_frame()
        self.iframe.to_main_iframe()
        if order_code is None:
            self.button.new()
        else:
            self.orthopedic_lend_search(order_code=order_code)
            self.list_page.open_order('借出单号')
        self.em.check_message()
        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)
        try:
            self.button.click_button("//*[contains(string(), '提示')]/ancestor::div[@class='el-message-box']", '确定')
        except TimeoutException:
            pass
        self.order_detail_main.drop_choose_item('手术类型 ', '翻修手术')
        self.order_detail_main.drop_choose_item('手术种类 ', '踝关节')
        self.order_detail_main.some_click_item('业务员名称 ')
        self.modal.search_and_choose_items(title='根据公司编码选择员工信息', text=test_data_info['salesman'])
        # 手术日期
        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.choose_tap('仓库信息')
        lend_warehouse_code = test_data_info['warehouse_lend']
        borrow_warehouse_code = test_data_info['warehouse_borrow']
        return_warehouse_code = test_data_info['warehouse_return']
        self.order_detail_main.some_click_item('借出仓库名称 ')
        self.modal.search_and_choose_items(title='选择借出仓库', text=lend_warehouse_code)
        self.order_detail_main.some_click_item('借入仓库名称 ')
        self.modal.search_and_choose_items(title='选择借入仓库', text=borrow_warehouse_code)
        if lend_warehouse_code != return_warehouse_code:
            self.order_detail_main.some_click_item('还回仓库名称 ')
            self.modal.search_and_choose_items(title='选择还回仓库', text=return_warehouse_code)
        self.order_detail_main.choose_tap('合同信息')
        self.order_detail_main.some_click_item('合同号 ')
        self.modal.search_and_choose_items(title='选择合同协议', text=test_data_info['agreement'])
        # 保存获得请领单号
        self.order_detail_main.choose_tap('主体信息')
        self.button.save()
        log.info('获取请领单号&存储')
        jc_code = ''
        while jc_code.startswith('JC') is False:
            sleep(1)
            jc_code = self.order_detail_main.get_element_text('借出单号 ')
        log.info('借出单号：【{}】'.format(jc_code))
        self.menu.dd.set_value('jc_code', jc_code)
        UtilYaml(self.menu.dd.get_value('extract_path')).write_yaml({'jc_code': jc_code})

        log.info('添加商品')
        self.button.click_button(button_name='商品信息')
        goods = test_data_info['goods']
        goods_code_list = []
        for good in goods:
            goods_code_list.append(good)
        self.modal.search_and_choose_items(placeholder='模糊检索:商品编码,商品名称,规格型号,厂家物料编码,存储条件', text=goods_code_list)
        sleep(3 * len(goods))
        self.order_detail_table.get_cell_text('商品名称', goods_code_list[0])
        min_num = self.menu.dd.get_value('minNum')
        for good in goods:
            good_number = randint(min_num, 5) if self.dbq.get_serial_flag(lend_warehouse_code,
                                                                          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()

    @allure.step('编辑：{}'.format(third_menu))
    def orthopedic_lend_edit_submit(self, follow='N'):
        log.info('骨科仓借出单编辑&提交')
        self.list_page.open_order('借出单号')
        self.order_detail_main.choose_tap('主体信息')
        log.info('获取借出单号&保存')
        jc_code = self.order_detail_main.get_element_text('借出单号 ')
        self.menu.dd.set_value('jc_code', jc_code) if follow == 'N' else self.menu.dd.set_value('new_jc_code', jc_code)
        log.info('借出单号：【{}】'.format(jc_code))
        UtilYaml(self.menu.dd.get_value('extract_path')).write_yaml({'jc_code': jc_code})
        self.order_detail_main.drop_choose_item('手术种类 ', '髋关节')
        self.order_detail_main.text_input_item('手术医生 ', '张三丰主任医师')
        self.order_detail_main.choose_tap('其他信息')
        self.order_detail_main.text_input_item('联系人 ', '高启强')
        self.order_detail_main.text_input_item('联系电话 ', '13333333333')
        self.button.submit()
        self.list_page.edit_iframe_is_show()

    @allure.step('办理：{}'.format(third_menu))
    def orthopedic_lend_transact(self, order_code):
        log.info('骨科仓借出单办理/审批')
        self.list_page.open_order(order_code=order_code)
        self.button.transact()
        self.em.check_message()
        self.list_page.edit_iframe_is_show()
