# author:qinguanglei
# ddate:2023/11/20 10:15
import traceback
import allure
from common import constant
from common.logger import log
from common.read_yaml import get_filed_from_yaml
from common.verifycode import VerifyCode
from test_buy.page_buy import PageBuy

module = 'test_buy'


class StepsBuy(object):
    def __init__(self, driver):
        self.page_buy = PageBuy(driver)
        self.driver = driver

    @allure.step("打开浏览器")
    def open_browser(self):
        try:
            log.info('当前url： %s', get_filed_from_yaml(module, "data", "url"))
            self.page_buy.open_page(get_filed_from_yaml(module, "data", "url"))
        except Exception as e:
            log.error(f'打开浏览器问题：{e.__doc__}{traceback.format_exc()}')
            assert False

    @allure.step("登录")
    def login(self):
        try:
            # 获取账号输入元素
            account_element = self.page_buy.get_account()
            # 获取密码输入元素
            password_element = self.page_buy.get_password()
            # 获取账号与密码
            account = get_filed_from_yaml(module, "data", "account")
            password = get_filed_from_yaml(module, "data", "password")
            # 填充账号密码
            self.page_buy.input_text(account_element, account)
            self.page_buy.input_text(password_element, password)
            # 登录特殊处理一下，验证码最多尝试5次，有一次成功，则成功。若5次都失败，案例直接失败
            max_retry = 5
            i = 5
            while i > 0:
                # 先获取验证码图片元素
                verify_code_element = self.page_buy.get_verify_image()
                # 获取验证码输入元素
                verify_input_element = self.page_buy.get_verify_code()
                # 获取验证码中的文字信息
                verify_code_text = VerifyCode(verify_code_element, self.driver).indentify_verify_image()
                # 填充验证码
                self.page_buy.input_text(verify_input_element, verify_code_text)
                log.info(f'填充的验证码内容为{verify_code_text}。')
                # 获取登录按钮
                login_button_element = self.page_buy.get_login_button()
                # 点击登录
                self.page_buy.single_click(login_button_element)
                # 判断登录是否成功，登录成功会切换到另外一个页面，通过页面title来判断是否切换成功
                if self.page_buy.get_window_title() != constant.LOGIN_SUCCESS:
                    # 表示不登录成功，此时将1减去1
                    i -= 1
                    log.warn('执行登录失败一次，次数多了将直接导致案例失败。')
                else:
                    # 表示登录成功，跳出循环
                    log.info('登录成功，开始执行案例。')
                    break
            if i <= 0:
                log.error(f'验证码已经连续{max_retry}次识别失败，本案例执行时登录失败，请检查环境是否出现问题，特别是分辨率是否调整。')
                assert False
            log.info(f'登录成功。')
        except Exception as e:
            log.error(f'登录过程问题：{e.__doc__}{traceback.format_exc()}')
            assert False

    @allure.step("进入普通采购单界面")
    def enter_buy_op_page(self):
        try:
            # 定位采购管理元素
            buy_management_button = self.page_buy.get_buy_management()
            # 鼠标移动到采购管理按钮
            self.page_buy.move_to_element(buy_management_button)
            # 定位到隐藏的采购按钮
            buy_button = self.page_buy.get_buy()
            # 鼠标移动到采购按钮
            self.page_buy.move_to_element(buy_button)
            # 定位到采购申请op
            buy_op_button = self.page_buy.get_buy_op()
            # 鼠标移动到采购申请op
            self.page_buy.move_to_element(buy_op_button)
            # 点击采购申请op
            buy_op_button.click()
            log.info(f'成功进去普通采购单页面。')
        except Exception as e:
            log.error(f'进入普通采购单界面问题：{e.__doc__}{traceback.format_exc()}')
            assert False

    @allure.step("打开新建普通采购单界面")
    def enter_new_op_frame(self):
        try:
            # 先进入到采购iframe
            iframe_buy = self.page_buy.get_iframe_buy()
            # 转到这个iframe下
            self.page_buy.switch_to_iframe(iframe_buy)
            # 获取新建按钮
            new_create_button = self.page_buy.get_new()
            # 点击新建
            self.page_buy.single_click(new_create_button)

            # 后续操作在一对话框形式的iframe里，切换到这个子iframe
            sub_iframe_dialog = self.page_buy.get_sub_iframe_dialog()
            self.page_buy.switch_to_iframe(sub_iframe_dialog)
            log.info(f'成功打开新建普通采购单界面。')
        except Exception as e:
            log.error(f'进入新建普通采购单界面问题：{e.__doc__}{traceback.format_exc()}')
            assert False

    @allure.step("输入供应商")
    def input_supplier(self):
        try:
            # 先从yaml中获取供应商内容
            supplier_content = get_filed_from_yaml(module, "data", "supplier")
            # 触发请求供应商按钮
            supplier_request = self.page_buy.get_supplier_request()
            self.page_buy.single_click(supplier_request)
            # 输入供应商信息
            supplier_input = self.page_buy.get_supplier_input()
            self.page_buy.input_text(supplier_input, supplier_content)
            # 开始查询,点击搜索按钮查询
            supplier_search = self.page_buy.get_supplier_search()
            self.page_buy.single_click(supplier_search)
            # 获取供应商dialog
            supplier_dialog = self.page_buy.get_supplier_dialog()
            # 获取查询结果，先获取结果表
            supplier_table = self.page_buy.get_table_from_dialog(supplier_dialog)
            # 获取供应商表中的所有行
            supplier_table_rows = self.page_buy.get_all_rows_from_tale(supplier_table)
            # 遍历供应商
            for i, current_row in enumerate(supplier_table_rows):
                # 获取当前供应商的列信息
                columns = self.page_buy.get_all_columns_from_row(current_row)
                # 遍历列内容,这里只是先打印出日志，便于查看问题
                for j, curr_column in enumerate(columns):
                    log.info('查询出供应商表，的第 %s 行，第 %s 列的内容为 %s' % (i, j, self.page_buy.get_text(curr_column)))
                # 挑选出一列，因为前端是模糊查询，有可能是供应商编码，有可能是供应商名称，匹配第一条即可
                if (supplier_content in self.page_buy.get_text(columns[1])) or (
                        supplier_content in self.page_buy.get_text(columns[2])):
                    '''选中当前行'''
                    #self.page_buy.single_click(current_row)
                    #self.page_buy.single_click(columns[1])
                    current_row.click()
                    '''点击确认'''
                    supplier_table_ensure = self.page_buy.get_ensure_from_dialog(supplier_dialog)
                    self.page_buy.single_click(supplier_table_ensure)
                    '''选中后退出，不再查询'''
                    break
            log.info(f'供应商{supplier_content}输入成功。')
        except Exception as e:
            log.error(f'输入供应商问题：{e.__doc__}{traceback.format_exc()}')
            assert False















