# -*- coding:utf-8 -*-
# 作者：齐振鋆
# 日期：2023/2/20
# 描述: 出库复核页面
import logging
import allure
import pytest

from time import sleep
from component.GuKeYiTiHua import iframe, menu, list_page, button, order_detail_main, modal, \
    order_detail_table, dbq
from component.GuKeYiTiHua.StorageManage import first_menu
from page.GuKeYiTiHua.Public.orderDetailPage import ScanTable

log = logging.getLogger(__name__)


class WarehouseCheck(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
        self.scan_table = ScanTable()

    @allure.step('打开菜单：{}'.format(third_menu))
    def open_menu_warehouse_check(self):
        self.iframe.out_frame()
        self.menu.open_menu(first_menu, self.third_menu)

    @allure.step('查询：{}'.format(third_menu))
    def check_order_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 check_confirm(self, order_code):
        """
        param check_data: [[商品编码，复核数量], ...]
        """
        log.info('复核确认')
        self.check_order_search(order_code=order_code)
        self.list_page.open_order(order_code=order_code)
        warehouse = self.order_detail_main.get_element_text('仓库编码 ')
        goods_batch_info = []
        goods_list = self.order_detail_table.get_details(['商品编码', '拣货数量', '批次/序号'])
        for good in goods_list:
            good_code = good[0]
            pick_number = int(good[1])
            check_number = pick_number
            batch_no = good[2]
            serial = []
            good_serial_flag = self.dbq.get_serial_flag(warehouse, good_code)
            if good_serial_flag != 'Y':
                self.order_detail_table.input_text('复核数量', good_code, check_number)
            else:
                # 序列号商品扫码
                self.button.click_button(button_name='复核扫码', end_str='')
                self.iframe.to_bar_iframe(name='出库复核扫码')
                # 通过数据库查询可用序列号
                serial_info = self.dbq.get_enable_serial(warehouse, good_code, batch_no, check_number)
                log.info('序列号info:\n{}'.format(serial_info))
                # 商品序列号不足测试结束
                pytest.fail('商品【{}】可用序列号不足'.format(good_code)) if len(serial_info) < check_number else 0
                for serial_item in serial_info:
                    serial.append(serial_item[0])
                for i in range(check_number):
                    self.order_detail_main.text_input_item('序列号 ', serial[i], enter=True)
                    while self.scan_table.check_enter_result(serial[i]) is False:
                        sleep(1)
                self.button.click_button(button_name='提交')
                while self.menu.wait_element("//span[text()='验收扫码']/ancestor::div[@class='el-overlay']",
                                             wait_time=3) is True:
                    sleep(1)
            goods_batch_info.append({
                'good': good_code,
                'sf': good_serial_flag,
                'batch': batch_no,
                'check': check_number,
                'serial': serial
            })
            self.iframe.out_frame()
            self.iframe.to_main_iframe()
            self.iframe.to_edit_iframe()
            if pick_number > check_number:
                self.order_detail_table.input_text('差异数量', good_code, pick_number - check_number)
                self.order_detail_table.input_text('差异原因', good_code, '复核差异')
        log.info('商品序列号信息：{}'.format(goods_batch_info))
        self.menu.dd.set_value('goods_batch_info', goods_batch_info)
        self.button.click_button(button_name='复核确认')
        self.list_page.edit_iframe_is_show()
