# -*- coding:utf-8 -*-
# 作者：齐振鋆
# 日期：2023/3/21
# 描述: 手术报台

import logging
from time import sleep

import allure

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

log = logging.getLogger(__name__)


class SurgeryReporting(object):
    third_menu = '手术报台'

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

    @allure.step('查询：{}'.format(third_menu))
    def surgery_reporting_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_reporting_create(self, order_code=None):
        log.info('手术报台创建')
        test_data_info = self.menu.dd.get_value('test_data_info')
        self.iframe.out_frame()
        self.iframe.to_main_iframe()
        if order_code is None:
            self.button.new()
        else:
            self.surgery_reporting_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)
        self.button.click_button("//*[contains(string(), '提示')]/ancestor::div[@class='el-message-box']", '确定')
        self.order_detail_main.drop_choose_item('手术类型 ', '翻修手术')
        self.order_detail_main.drop_choose_item('手术种类 ', '中型手术')
        self.order_detail_main.date_input_item('手术日期 ', self.menu.tu.get_date_str())
        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('病人年龄 ', 18)
        self.order_detail_main.text_input_item('床位号 ', 66)
        self.order_detail_main.text_input_item('住院号 ', 99)

        self.order_detail_main.choose_tap('主体信息')
        self.button.save()
        log.info('获取报台单号&存储')
        bt_code = ''
        while bt_code.startswith('BT') is False:
            sleep(1)
            bt_code = self.order_detail_main.get_element_text('手术报台单号 ')
        log.info('手术报台单号：【{}】'.format(bt_code))
        self.menu.dd.set_value('bt_code', bt_code)
        UtilYaml(self.menu.dd.get_value('extract_path')).write_yaml({'bt_code': bt_code})

        log.info('添加商品')
        self.button.click_button(button_name='添加')
        self.modal.search_and_choose_items(placeholder='销售单单号', text=self.menu.dd.get_value('xs_code'), all_choose=True)
        self.button.submit()
        self.list_page.edit_iframe_is_show()
