'''
@Project ：gukeyitihua
@File    ：1.py
@IDE     ：PyCharm
@Author  ：何凯
@Date    ：2023/3/12 10:35
@desc   :调拨出库单
'''

import logging
from random import randint

import allure
from time import sleep

from SeleniumLibrary.errors import ElementNotFound

from component.GuKeYiTiHua import iframe, menu, list_page, button, order_detail_main, order_detail_table, \
    modal
from component.GuKeYiTiHua.Transfer import first_menu

from unit.public.YamlUtil import UtilYaml

log = logging.getLogger(__name__)


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

    @allure.step('查询：{}'.format(third_menu))
    def transfer_out_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 transfer_out_code_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()
        else:
            self.transfer_out_search(order_code=order_code)
            self.list_page.open_order('调拨出库订单号')

        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(root_str="//div[contains(@class, 'is-message-box')]", button_name='确定')
        except Exception:
            pass

        self.order_detail_main.some_click_item('业务员 ')
        customer_code = test_data_info['counterman']
        self.modal.search_and_choose_items(title='选择业务员(关联部门)(调拨)', text=customer_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)

        self.order_detail_main.choose_tap('主体信息')
        self.button.save()
        log.info('获取调拨出库单号')
        transfer_out_code = ''
        while transfer_out_code.startswith('ST') is False:
            sleep(1)
            transfer_out_code = self.order_detail_main.get_element_text('调拨出库订单号 ')
        self.menu.dd.set_value('transfer_out_code', transfer_out_code)
        UtilYaml(self.menu.dd.get_value('extract_path')).write_yaml({'transfer_out_code': transfer_out_code})

        log.info('添加商品')
        self.button.click_button(button_name='商品信息')
        goods = test_data_info['goods']
        self.modal.search_and_choose_items(placeholder='模糊检索:商品编码,商品全称,规格型号,厂家物料编码,生产厂家编码,行类型,产品描述,是否免备案',
                                           text=goods)
        sleep(5 * len(goods))
        self.order_detail_table.get_cell_text('商品名称', goods[0])
        for good in goods:
            self.order_detail_table.input_text('调拨数量', good, randint(2, 5))
        self.button.submit()
        self.list_page.edit_iframe_is_show()

    @allure.step('办理：{}'.format(third_menu))
    def transfer_out_code_transact(self, order_code):
        log.info('调拨出库单办理/审批')
        self.transfer_out_search(order_code=order_code, count=1)
        self.list_page.open_order('调拨出库订单号')
        self.button.transact()
        self.list_page.edit_iframe_is_show()