# -*- coding:utf-8 -*-
# 作者：齐振鋆
# 日期：2023/2/14
# 描述: 手术请领配货单
import logging
import allure

from random import randint
from component.GuKeYiTiHua.SurgeryFollow import first_menu
from component.GuKeYiTiHua import iframe, menu, list_page, button, order_detail_main, order_detail_table, \
    modal

log = logging.getLogger(__name__)


class SurgeryDistribute(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 surgery_distribute_menu_open(self):
        self.iframe.out_frame()
        self.menu.open_menu(first_menu, self.third_menu)

    @allure.step('查询：{}'.format(third_menu))
    def surgery_distribute_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_distribute(self, order_code):
        test_data_info = self.menu.dd.get_value('test_data_info')
        log.info('手术请领单配货')
        self.list_page.open_order(order_code=order_code)

        self.order_detail_main.choose_tap('主体信息')
        self.order_detail_main.some_click_item('业务员名称 ')
        salesman_code = test_data_info['salesman']
        self.modal.search_and_choose_items(title='选择员工信息', text=salesman_code)

        self.order_detail_main.choose_tap('仓库信息')
        lend_warehouse_code = test_data_info['warehouse_lend']
        self.order_detail_main.some_click_item('借出仓库名称 ')
        self.modal.search_and_choose_items(title='选择借出仓库信息', text=lend_warehouse_code)
        borrow_warehouse_code = test_data_info['warehouse_borrow']
        self.order_detail_main.some_click_item('借入仓库名称 ')
        self.modal.search_and_choose_items(title='选择借入仓库信息', text=borrow_warehouse_code)

        goods = test_data_info['goods']
        for good in goods:
            not_distribute_number = int(self.order_detail_table.get_cell_text('未分配数量', good))
            distribute_number = randint(self.menu.dd.get_value('minNum'), not_distribute_number)
            self.order_detail_table.input_text('分配数量', good, distribute_number)
        self.button.submit()
        self.list_page.edit_iframe_is_show()
