#  pytest -s test_purchase_management/test_new_purchase_op.py::TestNewPurchase::test_new_purchase --alluredir ./report --clean-alluredir
# pytest -s test_purchase_management/test_new_purchase_op.py::TestNewPurchase::test_new_purchase --count=2

# “00122国药集团四川省医疗器械有限公司”向“1018063 成都安进科技有限公司”;采购“10022230 玻璃火罐222";
# 商品从仓库“103303北京公司-顺义库”出库到仓库“122466 四川骨科分仓”入库。

import pytest
import allure
import time
from selenium import webdriver
from selenium.common import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
from common.read_config import ReadConfig
from common.verifycode import VerifyCode
from common.logger import log
from common import constant
from test_purchase_management.page_purchase_op import PagePurchaseOP


@allure.feature("采购测试")
class TestNewPurchase:
    '''每执行一个案例都进行登录与退出'''

    @pytest.fixture(scope='function', autouse=True)
    def house_work(self, browser):
        # 驱动器从fixture传来
        self.driver = browser
        # 从配置文件中获取url
        self.url = ReadConfig().get('HOST', 'URL')
        # 引入本页面的操作元素
        self.page = PagePurchaseOP(self.driver)
        # 打开页面
        self.page.open_page(self.url)

        # 登录
        self.login_process()

    def login_process(self):
        account_element = self.page.get_account()
        # 密码
        password_element = self.page.get_password()
        # 验证码
        verify_input_element = self.page.get_verify_code()
        # 输入账号密码，账号密码从配置文件中来
        self.page.input_text(account_element, ReadConfig().get('ACCOUNT-PURCHASE', 'ACCOUNT'))
        self.page.input_text(password_element, ReadConfig().get('ACCOUNT-PURCHASE', 'PASSWORD'))
        # 登录时，最多尝试5次，5次失败，该案例失败
        i = 5
        while i > 0:
            # 获取验证码
            # 先获取验证码图片元素
            verify_code_element = self.page.get_verify_image()
            # 获取验证码中的文字信息
            verify_code_text = VerifyCode(verify_code_element, self.driver).indentify_verify_image_guke()
            # 填充验证码文字
            self.page.input_text(verify_input_element, verify_code_text)
            # 获取登录按钮
            login_button_element = self.page.get_login_button()
            # 点击登录
            self.page.single_click(login_button_element)
            # 判断登录是否成功，登录成功会切换到另外一个页面，通过页面title来判断是否切换成功
            if self.page.get_window_title() != constant.LOGIN_SUCCESS:
                # 表示不登录成功，此时将1减去1
                i -= 1
            else:
                # 表示登录成功，跳出循环
                log.info('登录成功，开始执行案例')
                break
        if i <= 0:
            log.info('本案例执行时登录失败')
            assert False

    @pytest.mark.start
    def test_new_purchase(self):
        # 睡眠时间
        sleep_time = 2
        # 入库公司
        enter_company = '00103'
        # 供应商
        supplier_content = '1003207'
        # 协议合同内容
        protocol_content = ''
        # 商品名字
        product_name = '测试商品HCL-0721'
        # 商品编码
        product_code = '10155572'
        # 商品信息
        merchandise_content = '10005528'
        # 订购数量
        purchase_sum = 10
        # 仓库模糊搜索内容
        ware_content = '103356'
        # 业务员内容
        businessman_content = ""
        # 仓库编码内容
        ware_name = '自管库-总库'
        # 开始
        # 登录完成，开始下单
        # 定位采购管理元素
        purchase_management_button = self.page.get_purchase_management()
        # 鼠标移动到采购管理按钮
        self.page.move_to_element(purchase_management_button)
        # 定位到隐藏的采购按钮
        purchase_button = self.page.get_purchase()
        # 鼠标移动到采购按钮
        self.page.move_to_element(purchase_button)
        # 定位到采购申请op
        purchase_op_button = self.page.get_purchase_op()
        # 鼠标移动到采购申请op
        self.page.move_to_element(purchase_op_button)
        # 点击采购申请op
        purchase_op_button.click()

        # 后续所有操作在采购iframe下先进入到采购iframe
        iframe_purchase = self.page.get_iframe_purchase()
        # 转到这个iframe下
        self.page.switch_to_iframe(iframe_purchase)

        # 点击新建
        new_create_button = self.page.get_new()
        self.page.single_click(new_create_button)

        # 后续操作在一对话框形式的iframe里，切换到这个子iframe
        sub_iframe_dialog = self.page.get_sub_iframe_dialog()
        self.page.switch_to_iframe(sub_iframe_dialog)

        # 获取查询公司请求元素
        company_request = self.page.get_company_request()
        # 点击请求进入查询
        self.page.single_click(company_request)
        # 获取对话框中的搜索输入条件
        company_input = self.page.get_company_input()
        # 输入公司编码
        self.page.input_text(company_input, enter_company)
        # 公司信息输入后，点击搜索
        company_search = self.page.get_company_search()
        self.page.single_click(company_search)
        # 查询后表信息处理，数据详情记录在公司表里，公司表在公司dialog中，先去这个dialog
        # 获取公司dialog
        company_dialog = self.page.get_company_dialog()
        # 从dialog中获取表信息
        company_table = self.page.get_table_from_dialog(company_dialog)
        # company_table = self.page.get_company_table()
        # 在获取该表的行信息
        company_table_rows = self.page.get_all_rows_from_tale(company_table)
        # 遍历该表所有的行
        for i, curr_row in enumerate(company_table_rows):
            # 获取当前行的列
            columns = self.page.get_all_columns_from_row(curr_row)
            # 遍历列内容,这里只是先打印出日志，便于查看问题
            #for j, curr_column in enumerate(columns):
            #    log.info('查询出的公司信息表，的第 %s 行，第 %s 列的内容为 %s' % (i, j, self.page.get_text(curr_column)))
            # 挑选出一列，公司编码与输入编码一样的公司信息，因为前端是模糊查询，有可能是公司编码，有可能是公司名称，匹配第一条即可
            if (enter_company in self.page.get_text(columns[2])) or (enter_company in self.page.get_text(columns[1])):
                '''选中当前的行'''
                self.page.single_click(curr_row)
                '''点击确认'''
                company_table_ensure = self.page.get_company_table_ensure()
                self.page.single_click(company_table_ensure)
                '''选中后退出，不再查询'''
                break

        # try:
        #    # 处理清空提示信息
        #    clear_mind_element = driver.find_element(By.XPATH, '/html/body/div[41]/div/div/div[3]/button[2]')
        #    clear_mind_element.click()
        # except NoSuchElementException:
        #    pass

        # 切换到仓库信息
        ware_info = self.page.get_ware_info()
        self.page.single_click(ware_info)
        # 点击仓库请求按钮
        ware_request = self.page.get_ware_request()
        self.page.single_click(ware_request)
        # 输入仓库数据
        ware_input = self.page.get_ware_input()
        self.page.input_text(ware_input, ware_content)
        # 搜索仓库信息
        ware_search = self.page.get_ware_search()
        self.page.single_click(ware_search)
        # 仓库dialog
        ware_dialog = self.page.get_ware_dialog()
        # 确定仓库信息的表
        ware_table = self.page.get_ware_table()
        # 获取该表下的行数据集
        ware_table_rows = self.page.get_all_rows_from_tale(ware_table)
        log.info("仓库表的行：%s" % ware_table_rows)
        # 遍历仓库表行
        for i, current_row in enumerate(ware_table_rows):
            # 获取当前行的列
            columns = self.page.get_all_columns_from_row(current_row)
            # 遍历列内容,这里只是先打印出日志，便于查看问题
            #for j, curr_column in enumerate(columns):
            #    log.info('查询出仓库表，的第 %s 行，第 %s 列的内容为 %s' % (i, j, self.page.get_text(curr_column)))
            # 挑选出一列，因为前端是模糊查询，有可能是仓库编码，有可能是仓库名称，匹配第一条即可
            if (ware_content in self.page.get_text(columns[1])) or (ware_content in self.page.get_text(columns[2])):
                '''选中当前行'''
                self.page.single_click(current_row)
                '''点击确认'''
                # ware_table_ensure = self.page.get_ware_table_ensure()
                ware_table_ensure = self.page.get_ensure_from_dialog(ware_dialog)
                self.page.single_click(ware_table_ensure)
                '''选中后退出，不再查询'''
                break

        # 获取供应商
        # 触发请求供应商按钮
        supplier_request = self.page.get_supplier_request()
        self.page.single_click(supplier_request)
        # 输入供应商信息
        supplier_input = self.page.get_supplier_input()
        self.page.input_text(supplier_input, supplier_content)
        # 开始查询,点击搜索按钮查询
        supplier_search = self.page.get_supplier_search()
        self.page.single_click(supplier_search)
        # 获取供应商dialog
        supplier_dialog = self.page.get_supplier_dialog()
        # 获取查询结果，先获取结果表
        supplier_table = self.page.get_table_from_dialog(supplier_dialog)
        # 获取供应商表中的所有行
        supplier_table_rows = self.page.get_all_rows_from_tale(supplier_table)
        # 遍历供应商
        for i, current_row in enumerate(supplier_table_rows):
            # 获取当前供应商的列信息
            columns = self.page.get_all_columns_from_row(current_row)
            # 遍历列内容,这里只是先打印出日志，便于查看问题
            #for j, curr_column in enumerate(columns):
            #    log.info('查询出供应商表，的第 %s 行，第 %s 列的内容为 %s' % (i, j, self.page.get_text(curr_column)))
            # 挑选出一列，因为前端是模糊查询，有可能是供应商编码，有可能是供应商名称，匹配第一条即可
            if (supplier_content in self.page.get_text(columns[1])) or (
                    supplier_content in self.page.get_text(columns[2])):
                '''选中当前行'''
                self.page.single_click(current_row)
                '''点击确认'''
                supplier_table_ensure = self.page.get_ensure_from_dialog(supplier_dialog)
                self.page.single_click(supplier_table_ensure)
                '''选中后退出，不再查询'''
                break
        # 客户协议
        # 客户协议请求按钮
        protocol_request = self.page.get_protocol_request()
        self.page.single_click(protocol_request)
        # 输入协议内容
        protocol_input = self.page.get_protocol_input()
        self.page.input_text(protocol_input, protocol_content)
        # 开始搜索协议
        protocol_search = self.page.get_protocol_search()
        self.page.single_click(protocol_search)
        # 获取协议dialog
        protocol_dialog = self.page.get_protocol_dialog()
        # 获取协议表
        protocol_table = self.page.get_table_from_dialog(protocol_dialog)
        # 获取这个表中的行
        protocol_table_rows = self.page.get_all_rows_from_tale(protocol_table)
        # 遍历行
        for i, curr_row in enumerate(protocol_table_rows):
            # 获取当前协议的所有列
            columns = self.page.get_all_columns_from_row(curr_row)
            #for j, curr_column in enumerate(columns):
                # 遍历列内容,这里只是先打印出日志，便于查看问题
            #    log.info('查询出合同协议表，的第 %s 行，第 %s 列的内容为 %s' % (i, j, self.page.get_text(curr_column)))
            # 挑选一列，模糊搜索，匹配合同/协议号,合同号,项目编码,项目名称,业态编码,对方编码,产品线编码
            if ((protocol_content in self.page.get_text(columns[1])) or
                    (protocol_content in self.page.get_text(columns[2])) or
                    (protocol_content in self.page.get_text(columns[3])) or
                    (protocol_content in self.page.get_text(columns[4])) or
                    (protocol_content in self.page.get_text(columns[5])) or
                    (protocol_content in self.page.get_text(columns[6])) or
                    (protocol_content in self.page.get_text(columns[8]))):
                # 选中当前行
                self.page.single_click(curr_row)
                # 获取协议确定按钮,点击确定
                protocol_ensure = self.page.get_ensure_from_dialog(protocol_dialog)
                self.page.single_click(protocol_ensure)
                break

        # 填写业务员
        # 获取业务员请求按钮，点击请求业务员信息
        businessman_request = self.page.get_businessman_request()
        self.page.single_click(businessman_request)
        # 输入业务员信息
        businessman_input = self.page.get_businessman_input()
        self.page.input_text(businessman_input, businessman_content)
        # 点击搜索业务员信息
        businessman_search = self.page.get_businessman_search()
        self.page.single_click(businessman_search)
        # 获取业务员dialog
        businessman_dialog = self.page.get_businessman_dialog()
        # 获取表信息
        businessman_table = self.page.get_table_from_dialog(businessman_dialog)
        # 获取表信息里面的当前行
        businessman_rows = self.page.get_all_rows_from_tale(businessman_table)
        # 遍历业务员
        for i, current_row in enumerate(businessman_rows):
            # 获取当前供应商的列信息
            columns = self.page.get_all_columns_from_row(current_row)
            # 遍历列内容,这里只是先打印出日志，便于查看问题
            #for j, curr_column in enumerate(columns):
            #    log.info('查询出业务员表，的第 %s 行，第 %s 列的内容为 %s' % (i, j, self.page.get_text(curr_column)))
            # 挑选出一列，模糊查询，匹配业务员编码,业务员名称,部门编码,部门名称,保证业务类型是‘IS’
            if ((businessman_content in self.page.get_text(columns[1])) or
                (businessman_content in self.page.get_text(columns[2])) or
                (businessman_content in self.page.get_text(columns[3])) or
                (businessman_content in self.page.get_text(columns[4]))) and ('IS' == self.page.get_text(columns[5])):
                '''选中当前行'''
                self.page.single_click(current_row)
                '''点击确认'''
                ensure = self.page.get_ensure_from_dialog(businessman_dialog)
                self.page.single_click(ensure)
                '''选中后退出，不再查询'''
                break
        # 商品录入
        # 点击商品录入按钮
        merchandise = self.page.get_merchandise()
        self.page.single_click(merchandise)
        # 获取商品dialog
        merchandise_dialog = self.page.get_merchandise_dialog()
        # 输入商品信息
        #merchandise_input = self.page.get_input_from_dialog(merchandise_dialog)
        merchandise_input = self.page.get_merchandise_input()
        self.page.input_text(merchandise_input, merchandise_content)
        # 点击搜索
        #merchandise_search = self.page.get_search_from_dialog(merchandise_dialog)
        merchandise_search = self.page.get_merchandise_search()
        self.page.single_click(merchandise_search)
        # 获取搜索出来的表信息
        merchandise_table = self.page.get_table_from_dialog(merchandise_dialog)
        # 获取表的全部行
        merchandise_table_rows = self.page.get_all_rows_from_tale(merchandise_table)
        # 遍历行
        for i, current_row in enumerate(merchandise_table_rows):
            # 获取当前供应商的列信息
            columns = self.page.get_all_columns_from_row(current_row)
            # 遍历列内容,这里只是先打印出日志，便于查看问题
            #for j, curr_column in enumerate(columns):
            #    log.info('查询出商品表，的第 %s 行，第 %s 列的内容为 %s' % (i, j, self.page.get_text(curr_column)))
            # 挑选出一列，模糊查询，匹配商品编码,商品名称,规格型号,厂家物料编码
            if ((merchandise_content in self.page.get_text(columns[2])) or
                    (businessman_content in self.page.get_text(columns[3])) or
                    (businessman_content in self.page.get_text(columns[4])) or
                    (businessman_content in self.page.get_text(columns[6]))):
                '''选中当前行'''
                self.page.single_click(current_row)
                '''点击确认'''
                ensure = self.page.get_ensure_from_dialog(merchandise_dialog)
                self.page.single_click(ensure)
                '''选中后退出，不再查询'''
                break

        # 保存
        save_button = self.page.get_save()
        self.page.single_click(save_button)
        # 获取采购单号
        order_no = self.page.get_order_no()
        log.info("订单号：%s" % self.page.get_text(order_no))


