#  pytest -s test_purchase_management/test_new_purchase_op.py::TestNewPurchase::test_new_purchase --alluredir ./report

# “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 PIL import Image
from common import verifycode


@allure.feature("采购测试")
class TestNewPurchase:
    def housework(self):
        #案例执行前打开页面，登录
        pass

    def login(self):
        # 开始
        # 获取驱动，谷歌浏览器
        driver = webdriver.Chrome("./chromedriver.exe")
        # 10秒加载
        driver.implicitly_wait(10)
        # time_wait失效时间
        wait = 5
        # 该驱动器的失效时间控制
        time_wait = WebDriverWait(driver, wait)
        # 打开多采平台，买家
        driver.get(url="http://10.17.65.216:8088/portal/")
        print("打开骨科主页面成功")
        # 页面最大化设置
        driver.maximize_window()
        # 输入账号
        account = driver.find_element(By.ID, 'input1')
        account.send_keys("qinguanglei")
        print("账号输入完毕")
        # 输入密码
        password = driver.find_element(By.ID, 'input2')
        password.send_keys("Gyxc1234")
        print("输入密码成功")
        print('开始处理验证码')
        # 首先在页面展开完后，进行全页面截图,截图目录用shot_dir来存储，这个自己定
        # 页面全面展开后，再截图，否则会出现截图出问题,这里sleep一段时间
        # 指定全页面截图保存的目录
        page_shot_dir = "d:/rf/image/org.png"
        # 截图
        driver.save_screenshot(page_shot_dir)
        # 转换图片为Image对象
        page_shot_obj = Image.open(page_shot_dir)
        # 获取验证码元素
        verify_code_element = driver.find_element(By.ID, 'canvas')
        print("验证码元素：", verify_code_element)
        # 获取验证码元素图片的位置
        location = verify_code_element.location
        print("验证码元素图片位置：", location)
        # 获取验证码元素图片的大小
        size = verify_code_element.size
        print("验证码元素图片大小：", size)
        # 获取图片的四个坐标点
        left = location['x']
        bottom = location['y']
        right = left + size['width']
        top = bottom + size['height']
        print("图片的四个位置。", "左:", left, "右:", right, "上:", top, "下:", bottom)
        # 开始按照位置截图
        crop_image_obj = page_shot_obj.crop((left, bottom, right, top))
        # 将切割好的图片保存，保存的目录为crop_dir，这个自己定
        crop_dir = "d:/rf/image/curr.png"
        crop_image_obj.save(crop_dir)
        # 识别验证码中的字符
        verify_code_text = verifycode.gen_text_from_picture(crop_dir)
        print("当前验证码的文字为：", verify_code_text)
        # 开始填充验证码
        # 获取验证码输入元素
        verify_input = driver.find_element(By.ID, 'photoCode')
        verify_input.send_keys(verify_code_text)
        print("验证码填充完毕")
        # 提交
        submit_button = driver.find_element(By.ID, 'loginBtn')
        submit_button.click()
        print("提交完毕")

    @pytest.mark.yaohuodan
    def test_new_purchase(self):
        # 睡眠时间
        sleep_time = 5
        # 入库公司
        enter_company = '国药集团四川省医疗器械有限公司'
        # 出库公司
        exit_company = '柯惠医疗器材国际贸易(上海)有限公司黄无忌'
        # 商品名字
        product_name = 'PFC SIGMA膝关节系统'
        # 商品编码
        product_code = '10181391'
        # 订购数量
        purchase_sum = 4
        # 仓库编码内容
        ware_code_content = 122309
        # 仓库编码内容
        ware_name = '自管库-总库'
        # 开始
        # 获取驱动，谷歌浏览器
        driver = webdriver.Chrome("./chromedriver.exe")
        # 10秒加载
        driver.implicitly_wait(10)
        # time_wait失效时间
        wait = 5
        # 该驱动器的失效时间控制
        time_wait = WebDriverWait(driver, wait)
        # 打开多采平台，买家
        driver.get(url="http://10.17.65.216:8088/portal/")
        print("打开骨科主页面成功")
        # 页面最大化设置
        driver.maximize_window()
        # 输入账号
        account = driver.find_element(By.ID, 'input1')
        account.send_keys("qinguanglei")
        print("账号输入完毕")
        # 输入密码
        password = driver.find_element(By.ID, 'input2')
        password.send_keys("Gyxc1234")
        print("输入密码成功")
        print('开始处理验证码')
        # 首先在页面展开完后，进行全页面截图,截图目录用shot_dir来存储，这个自己定
        # 页面全面展开后，再截图，否则会出现截图出问题,这里sleep一段时间
        # 指定全页面截图保存的目录
        page_shot_dir = "d:/rf/image/org.png"
        # 截图
        driver.save_screenshot(page_shot_dir)
        # 转换图片为Image对象
        page_shot_obj = Image.open(page_shot_dir)
        # 获取验证码元素
        verify_code_element = driver.find_element(By.ID, 'canvas')
        print("验证码元素：", verify_code_element)
        # 获取验证码元素图片的位置
        location = verify_code_element.location
        print("验证码元素图片位置：", location)
        # 获取验证码元素图片的大小
        size = verify_code_element.size
        print("验证码元素图片大小：", size)
        # 获取图片的四个坐标点
        left = location['x']
        bottom = location['y']
        right = left + size['width']
        top = bottom + size['height']
        print("图片的四个位置。", "左:", left, "右:", right, "上:", top, "下:", bottom)
        # 开始按照位置截图
        crop_image_obj = page_shot_obj.crop((left, bottom, right, top))
        # 将切割好的图片保存，保存的目录为crop_dir，这个自己定
        crop_dir = "d:/rf/image/curr.png"
        crop_image_obj.save(crop_dir)
        # 识别验证码中的字符
        verify_code_text = verifycode.gen_text_from_picture(crop_dir)
        print("当前验证码的文字为：", verify_code_text)
        # 开始填充验证码
        # 获取验证码输入元素
        verify_input = driver.find_element(By.ID, 'photoCode')
        verify_input.send_keys(verify_code_text)
        print("验证码填充完毕")
        # 提交
        submit_button = driver.find_element(By.ID, 'loginBtn')
        submit_button.click()
        print("提交完毕")

        # 登录完成，开始下单
        print("登录完成，开始下单")
        # 定位采购管理元素
        purchase_management_button = time_wait.until(
            ec.visibility_of_element_located((By.XPATH, '/html/body/div[1]/div[4]/div/div/div[4]')))
        # 这里有浮动隐藏按钮展示，需要等待一下
        # time.sleep(sleep_time)
        # 鼠标移动到采购管理按钮
        ActionChains(driver).move_to_element(purchase_management_button).perform()
        # 定位到隐藏的采购按钮
        purchase_button = driver.find_element(By.XPATH, '/html/body/div[1]/div[9]/div[1]')
        # 这里有浮动隐藏按钮展示，需要等待一下
        time.sleep(sleep_time)
        # 鼠标移动到采购按钮
        ActionChains(driver).move_to_element(purchase_button).perform()
        # 定位到采购申请op
        purchase_op_button = driver.find_element(By.XPATH, '/html/body/div[1]/div[9]/div[1]/div/div[2]')
        # 鼠标移动到要货申请特一级，点击
        ActionChains(driver).move_to_element(purchase_op_button).perform()
        # 点击要货申请特一级
        purchase_op_button.click()
        # 获取新建按钮
        # 新建按钮在iframe下，先定位到iframe
        iframe = driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div/iframe')
        print('1111111')
        # 转到这个iframe下
        driver.switch_to.frame(iframe)
        print('11222211111')
        # 在这个iframe下定位新建按钮,点击新建
        new_create_button = driver.find_element(By.XPATH,
                                                '/html/body/div[2]/div/div/div[2]/div/div[1]/div[2]/div/div[2]/div/div[1]/div[1]/div/div[1]/div/div/div[1]/div/div/button')
        print('11234534511111')
        new_create_button.click()
        # 开始填写要货申请单
        # 要货申请单在另外一个frame中，为上个frame的子frame，切换到这个子框架
        # 从框架切换到子框架，若使用xpath，一定要用绝对路径
        sub_iframe = driver.find_element(By.XPATH, '/html/body/div[2]/div/div/div[2]/div/div[2]/div/div/div/iframe')
        driver.switch_to.frame(sub_iframe)
        print('1122211111111111')
        # 定位主体信息中的公司名称
        body_company_name = driver.find_element(By.XPATH,
                                                '/html/body/div[2]/div/div[2]/div/div[2]/div[2]/div/div[1]/form/div[1]/div/div/div/div[2]/div[1]/div/div/div/div[3]/div/div[2]/div/div/div/div/div/div/div/span/span/i')
        print('111111155555555cle')
        body_company_name.click()

        # 获取对话框中的搜索输入条件
        search_input = driver.find_element(By.XPATH,
                                           '/html/body/div[6]/div/div/div/div[1]/div[1]/div[1]/div/div/form/div/div/div/div/input')
        search_input.send_keys(enter_company)
        # 输入搜索条件后，点击搜索
        search_action = driver.find_element(By.XPATH,
                                            '/html/body/div[6]/div/div/div/div[1]/div[1]/div[1]/div/div/form/div/div/div/div/span/span/i')
        search_action.click()

        # 表内容刷新一下
        time.sleep(sleep_time)
        # 查询后表信息处理
        # 先获取该表
        table_element = driver.find_element(By.XPATH,
                                            '/html/body/div[6]/div/div/div/div[1]/div[2]/div[1]/div/div/div/div[1]/div[3]/div/div[1]/div/table')
        # 在获取该表的行信息
        company_name_rows = table_element.find_elements(By.TAG_NAME, "tr")
        for i, curr_company_name_row in enumerate(company_name_rows):
            if i == 0:
                curr_company_name_row.click()
                # 点击确定
                sure_button = driver.find_element(By.XPATH, '/html/body/div[6]/div/div/div/div[2]/span/button[1]')
                sure_button.click()
        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_management_element = time_wait.until(ec.visibility_of_element_located((By.XPATH,
                                                                                    '/html/body/div[2]/div/div[2]/div/div[2]/div[2]/div/div[1]/form/div[1]/div/div/div/div[1]/div/div/div/div[3]')))
        ware_management_element.click()
        # 仓库“122306代管库”入库
        # 点击仓库请求按钮
        ware_request_button = time_wait.until(ec.visibility_of_element_located((By.XPATH,
                                                                                '/html/body/div[2]/div/div[2]/div/div[2]/div[2]/div/div[1]/form/div[1]/div/div/div/div[2]/div[2]/div/div/div[1]/div[1]/div/div[2]/div/div/div/div/div/div/div/span/span/i')))
        ware_request_button.click()
        # 输入仓库编码
        ware_code_input = time_wait.until(ec.visibility_of_element_located(
            (By.XPATH, '/html/body/div[8]/div/div/div/div[1]/div[1]/div[1]/div/div/form/div/div/div/div/input')))
        ware_code_input.send_keys(ware_code_content)
        # 搜索返回仓库编码挑选出的内容
        ware_response_button = time_wait.until(ec.visibility_of_element_located(
            (By.XPATH, '/html/body/div[8]/div/div/div/div[1]/div[1]/div[1]/div/div/form/div/div/div/div/span/span/i')))
        ware_response_button.click()
        # 操作选择出来的仓库编码
        # 确定仓库信息的表
        ware_table = time_wait.until(ec.visibility_of_element_located((By.XPATH,
                                                                       '/html/body/div[8]/div/div/div/div[1]/div[2]/div[1]/div/div/div/div[1]/div[3]/div/div[1]/div/table')))
        # 获取该表下的行数据集
        time.sleep(sleep_time)
        ware_table_rows = ware_table.find_elements(By.TAG_NAME, 'tr')
        # 判断表的大小，若为0，表示所选数据为空，结束
        if len(ware_table_rows) == 0:
            print("仓库编码", ware_code_content, '不存在')
        else:
            print("仓库编码", ware_code_content, '挑选出来的记录数为：', len(ware_table_rows))
            # 选择仓库编码行数据
            for index, current_row in enumerate(ware_table_rows):
                print('当前仓库编码行', index)
                # 获取当前仓库的名称
                curr_ware_columns = current_row.find_elements(By.TAG_NAME, 'td')
                # 当前行的列数
                column_count = len(curr_ware_columns)
                print('当前仓库信息的列数：', column_count)
                # 展示当前仓库信息
                for i, curr_column in enumerate(curr_ware_columns):
                    print('第', i, '列的值为', curr_column.text)
                # 当前仓库名称
                curr_ware_name = curr_ware_columns[1].text
                print('当前仓库名称：', curr_ware_name)
                if curr_ware_name == ware_name:
                    # 表示找到了需要的仓库名称，选中该仓库
                    current_row.click()
                    # 然后确定：
                    ware_sure_button = time_wait.until(ec.visibility_of_element_located(
                        (By.XPATH, '/html/body/div[8]/div/div/div/div[2]/span/button[1]')))
                    ware_sure_button.click()
                    break
        # 获取供应商
        # 触发请求供应商按钮
        supplier_request_button = time_wait.until(ec.visibility_of_element_located((By.XPATH,
                                                                                    '/html/body/div[2]/div/div[2]/div/div[2]/div[2]/div/div[1]/form/div[1]/div/div/div/div[2]/div[2]/div/div/div[1]/div[2]/div/div[2]/div/div/div/div/div/div/div/span/span/i')))
        supplier_request_button.click()
        # 弹出框，填入需要查询的供应商名
        supplier_input = time_wait.until(ec.visibility_of_element_located((By.XPATH,
                                                                           '/html/body/div[10]/div/div/div/div[1]/div[1]/div[1]/div/div/form/div/div/div/div/input')))
        supplier_input.send_keys(exit_company)
        # 开始查询
        supplier_inquiry_button = time_wait.until(ec.visibility_of_element_located(
            (By.XPATH, '/html/body/div[10]/div/div/div/div[1]/div[1]/div[1]/div/div/form/div/div/div/div/span/span/i')))
        supplier_inquiry_button.click()
        # 获取查询结果，先获取结果表
        time.sleep(sleep_time)
        supplier_table = time_wait.until(ec.visibility_of_element_located((By.XPATH,
                                                                           '/html/body/div[10]/div/div/div/div[1]/div[2]/div[1]/div/div/div/div[1]/div[3]/div/div[1]/div/table')))

        supplier_rows = supplier_table.find_elements(By.TAG_NAME, 'tr')
        print("根据条件", exit_company, '查询出的供应商名数量为', len(supplier_rows))
        # 遍历供应商
        for i, curr_supplier_row in enumerate(supplier_rows):
            print('当前是第', i, '个供应商')
            # 获取当前供应商的列信息
            curr_supplier_row_columns = curr_supplier_row.find_elements(By.TAG_NAME, 'td')
            print('当前供应商一共有', len(curr_supplier_row_columns), '列')
            # 打印列信息
            for j, curr_column in enumerate(curr_supplier_row_columns):
                print('第', j, '列为', curr_column.text)
            # 若供应商名称满足，则挑选
            if curr_supplier_row_columns[1].text == exit_company:
                # 选中该供应商
                curr_supplier_row.click()
                # 点击确认
                supplier_sure_button = time_wait.until(ec.visibility_of_element_located(
                    (By.XPATH, '/html/body/div[10]/div/div/div/div[2]/span/button[1]')))
                supplier_sure_button.click()
                break
        # 客户协议
        # 客户协议请求按钮
        protocol_request_button = time_wait.until(ec.visibility_of_element_located((By.XPATH,
                                                                                    '/html/body/div[2]/div/div[2]/div/div[2]/div[2]/div/div[1]/form/div[1]/div/div/div/div[2]/div[2]/div/div/div[1]/div[3]/div/div[2]/div/div/div/div/div/div/div/span/span/i')))
        protocol_request_button.click()
        time.sleep(sleep_time)
        # 获取协议表
        protocol_table = time_wait.until(ec.visibility_of_element_located(
            (By.XPATH,
             '/html/body/div[12]/div/div/div/div[1]/div[2]/div[1]/div/div/div/div[1]/div[3]/div/div[1]/div/table')))
        # 获取这个表中的行
        protocol_table_rows = protocol_table.find_elements(By.TAG_NAME, 'tr')
        print('当前展示协议记录条数：', len(protocol_table_rows))
        # 遍历行
        for i, protocol_curr_row in enumerate(protocol_table_rows):
            print('当前是第', i, '条记录')
            # 展开当前协议信息
            # 获取当前协议的列
            protocol_curr_row_columns = protocol_curr_row.find_elements(By.TAG_NAME, 'td')
            print('第', i, '条协议共有', len(protocol_curr_row_columns), '列')
            for j, protocol_curr_column in enumerate(protocol_curr_row_columns):
                print('第', j, '列的值为', protocol_curr_column.text)
            if i == 0:  # 简化，选择第一行
                protocol_curr_row.click()
                protocol_sure_button = time_wait.until(ec.visibility_of_element_located(
                    (By.XPATH, '/html/body/div[12]/div/div/div/div[2]/span/button[1]')))
                protocol_sure_button.click()
                break
        #
        # 填写业务员
        # 点击业务员按钮
        businessman_element = driver.find_element(By.XPATH,
                                                  '/html/body/div[2]/div/div[2]/div/div[2]/div[2]/div/div[1]/form/div[1]/div/div/div/div[2]/div[2]/div/div/div[2]/div[1]/div/div[2]/div/div/div/div/div/div/div/span/span/i')
        businessman_element.click()

        # 此时会弹框，等下
        time.sleep(sleep_time)
        # 获取表信息
        businessman_table = driver.find_element(By.XPATH,
                                                '/html/body/div[14]/div/div/div/div[1]/div[2]/div[1]/div/div/div/div[1]/div[3]/div/div[1]/div/table')
        # 表的行数与列数
        businessman_row_count = 0
        businessman_column_count = 0
        # 获取表信息里面的当前行
        businessman_rows = businessman_table.find_elements(By.TAG_NAME, 'tr')
        # 获取行数量
        businessman_row_count = len(businessman_rows)
        # 获取列数量
        if businessman_row_count > 0:
            businessman_columns = businessman_rows[0].find_elements(By.TAG_NAME, 'td')
            businessman_column_count = len(businessman_columns)
        print("当前业务人员，行数：", businessman_row_count, "列数：", businessman_column_count)
        # 循环遍历行，找出部门类型为is的行
        for index, current_row in enumerate(businessman_rows):
            # 定位当前的部门类型
            department_type = current_row.find_elements(By.TAG_NAME, 'td')[5]
            # 当前的部门类型
            department_type_text = department_type.text
            print("当前部门类型为：", department_type_text)
            print("当前行：", index)
            if department_type_text == 'IS':
                # 选中当前出现is的行
                current_row.click()
                # 选中后，确定
                sure_businessman = time_wait.until(
                    ec.visibility_of_element_located(
                        (By.XPATH, '/html/body/div[14]/div/div/div/div[2]/span/button[1]')))
                sure_businessman.click()
                break

        # 商品录入
        # 点击商品录入按钮
        product_type_button = time_wait.until(ec.visibility_of_element_located((By.XPATH,
                                                                                '/html/body/div[2]/div/div[2]/div/div[2]/div[2]/div/div[1]/form/div[2]/div/div/div[2]/div/div/div[1]/div/div[1]/div/div[1]/div/div/span/div/button[1]')))
        product_type_button.click()

        # 输入商品信息
        # 获取输入商品信息位置
        product_input = time_wait.until(ec.visibility_of_element_located(
            (By.XPATH, '/html/body/div[38]/div/div/div/div[1]/div[1]/div[1]/div/div/form/div/div/div/div/input')))
        product_input.send_keys(product_name)
        # 点击按钮开始查询
        product_inquire_button = time_wait.until(ec.visibility_of_element_located(
            (By.XPATH, '/html/body/div[38]/div/div/div/div[1]/div[1]/div[1]/div/div/form/div/div/div/div/span/span/i')))
        product_inquire_button.click()
        # 开始处理查出的商品信息
        time.sleep(sleep_time)
        # 获取查出的商品表信息
        product_table = time_wait.until(ec.visibility_of_element_located((By.XPATH,
                                                                          '/html/body/div[38]/div/div/div/div[1]/div[2]/div[1]/div/div/div/div[1]/div[3]/div/div[1]/div/table')))
        # 获取商品表里当前展示的行
        product_table_rows = product_table.find_elements(By.TAG_NAME, 'tr')
        print('当前根据条件：', product_name, ',搜出的商品记录有', len(product_table_rows), '条')
        # 处理具体的记录
        for i, product_curr_row in enumerate(product_table_rows):
            # 获取当前行的列信息
            product_columns = product_curr_row.find_elements(By.TAG_NAME, 'td')
            print('当前是第', i, '条记录，本记录的共有', len(product_columns), '列')
            # 查看具体的列信息
            for j, product_curr_column in enumerate(product_columns):
                print('第', j, '列为', product_curr_column.text)
            # 商品编码匹配就选择
            if product_columns[2].text == product_code:
                # 选中当前的商品信息
                product_curr_row.click()
                # 确认商品
                product_sure_button = time_wait.until(ec.visibility_of_element_located(
                    (By.XPATH, '/html/body/div[38]/div/div/div/div[2]/span/button[1]')))
                product_sure_button.click()
                break
        # 编辑商品详情信息
        # 获取详情表
        time.sleep(sleep_time)
        detail_table = time_wait.until(ec.visibility_of_element_located((By.XPATH,
                                                                         '/html/body/div[2]/div/div[2]/div/div[2]/div[2]/div/div[1]/form/div[2]/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table')))
        # 获取表的行
        detail_table_rows = detail_table.find_elements(By.TAG_NAME, 'tr')
        print('商品详情一共有', len(detail_table_rows), '条记录')
        for i, detail_curr_row in enumerate(detail_table_rows):
            print('当前是第', i, '条记录')
            # 获取当前行的列信息
            detail_curr_columns = detail_curr_row.find_elements(By.TAG_NAME, 'td')
            for j, detail_curr_column in enumerate(detail_curr_columns):
                print('第', i, ',行第', j, '列的内容为', detail_curr_column.text)
                # 填入订购数量
                if j == 12:
                    # 第四列表示订购数量
                    detail_curr_column.click()
                    # 从该列元素中获取input元素，专门用来输入数据
                    input_element = detail_curr_column.find_element(By.TAG_NAME, 'input')
                    # 输入订购数量
                    input_element.send_keys(purchase_sum)
                    time.sleep(sleep_time)
        # 保存
        time.sleep(sleep_time)
        save_button = time_wait.until(
            ec.visibility_of_element_located((By.XPATH, '/html/body/div[2]/div/div[1]/div[2]/div[2]/div[2]/button[5]')))
        save_button.click()

        # 切换到主体信息
        body_module = time_wait.until(ec.visibility_of_element_located((By.XPATH,
                                                                        '/html/body/div[2]/div/div[2]/div/div[2]/div[2]/div/div[1]/form/div[1]/div/div/div/div[1]/div/div/div/div[2]')))

        body_module.click()

        # 获取采购单号
        time.sleep(sleep_time)
        apply_no = time_wait.until(ec.visibility_of_element_located((By.XPATH,
                                                                     '/html/body/div[2]/div/div[2]/div/div[2]/div[2]/div/div[1]/form/div[1]/div/div/div/div[2]/div[1]/div/div/div/div[1]/div/div[2]/span')))
        apply_no_text = apply_no.text

        print('最终的申请单号为', apply_no_text)
        time.sleep(sleep_time)
