from selenium.webdriver.support.select import Select

from testcase.test_scene1.scene1_mainflow import *
from testcase.test_scene4.scene4_mainflow import Instore1

SIT_URL = 'http://scdev.cmic.com.cn:8088/beta/'#SIT环境地址
#通行证
usr1,pwd1 = 'bj_01','Gyxc1234'
usr2,pwd2 = 'bj_02','Gyxc1234'
usr3,pwd3 = 'bj_03','Gyxc1234'
usr4,pwd4 = 'bj_04','Gyxc1234'
usr5,pwd5 = 'bj_05','Gyxc1234'
lendin= '103503'#借入仓库名称
lendout= '103396'#借出仓库名称
salesman = '杨春刚'#业务员名称
custname = 1000486#客户名称
productcode = 10005528#商品编码
contractnumber = 'YLTEST77'#合同/协议号

ARRIVAL_TEMP = 24 #到货温度
ARRIVAL_DATE = '2023-10-01'#到货日期
ARRIVAL_TIME = '00:00:00'#到货时间
STASHIP_TEMP = 24 #启运温度
STASHIP_DATE = '2023-11-01' #启运日期
STASHIP_TIME = '00:00:00'#启运时间
TRANS_MACHINE = '冷冻车'#运输工具




errmsg11 = '流程十一：经销商要货申请_退货流程异常，请检查！'#流程异常提示信息


class DealerPurchaseRequisition():
    def data_add_requisition(self):
        ###打开经销商要货申请单###
        wait.until(ec.presence_of_element_located((By.XPATH, '//span[text()="经销商要货申请"]')))
        sleep(0.5)
        order_link = chrome.find_element_by_xpath('//span[text()="经销商要货申请"]')
        chrome.execute_script("arguments[0].click();", order_link)
        chrome.switch_to.frame(chrome.find_element_by_xpath('(//iframe[@class="metro-main-frame"])[2]'))
        sleep(0.5)
        # 点击新建按钮
        wait.until(ec.visibility_of_element_located((By.XPATH, '//span[text()="新建"]')))
        sleep(0.5)
        chrome.find_element_by_xpath('//span[text()="新建"]').click()
        sleep(4)
        chrome.switch_to.frame(chrome.find_element_by_xpath('//iframe[contains(@name,"awsui-dialog-iframe")]'))
        sleep(2)

        ###获取订单号###
        global orderno1
        wait.until(
            ec.presence_of_element_located((By.XPATH, '//div[text() = "要货申请单号 "]/following-sibling::div/span')))
        sleep(0.5)
        orderno1 = chrome.find_element_by_xpath('//div[text() = "要货申请单号 "]/following-sibling::div/span').text
        print('经销商要货申请单号为:', orderno1)

        # 填写客户名称
        chrome.find_element_by_xpath(
            '//div[text()="客户名称 "]/following-sibling::div/div/div/div/div/div/div/div/span/span/i').click()
        wait.until(ec.visibility_of_element_located((By.XPATH, '//input[@placeholder="模糊检索:客户编码,客户名称,收货地址,产品线名称,产品线编码"]')))
        sleep(1)
        chrome.find_element_by_xpath('//input[@placeholder="模糊检索:客户编码,客户名称,收货地址,产品线名称,产品线编码"]').send_keys(custname)
        sleep(1)
        chrome.find_element_by_xpath('//input[@placeholder="模糊检索:客户编码,客户名称,收货地址,产品线名称,产品线编码"]').send_keys(Keys.ENTER)
        sleep(1)
        btn_div = chrome.find_element_by_xpath(
            '//span/input[@type="radio" and @value="0abe60e70de4a83318e9ef5148d1c9c3a"]')
        chrome.execute_script("arguments[0].click();", btn_div)
        chrome.find_element_by_xpath('(//button[@class="el-button el-button--primary el-button--default"])[2]').click()
        sleep(1)

        # 详细信息页签
        chrome.find_element_by_xpath('//div[text()="详细信息"]').click()

        # 点击合同/协议号并输入信息，查询出结果
        wait.until(ec.visibility_of_element_located((By.XPATH,'//div[text()="合同/协议号 "]/following-sibling::div/div/div/div/div/div/div/div/span/span/i')))
        sleep(0.5)
        chrome.find_element_by_xpath(
            '//div[text()="合同/协议号 "]/following-sibling::div/div/div/div/div/div/div/div/span/span/i').click()
        sleep(1)
        chrome.find_element_by_xpath(
            '//input[@placeholder="模糊检索:合同协议号,合同号"]').send_keys(contractnumber)
        sleep(1)
        chrome.find_element_by_xpath(
            '//input[@placeholder="模糊检索:合同协议号,合同号"]').send_keys(Keys.ENTER)
        sleep(1)
        # chrome.find_element_by_xpath(
        #     '//div[@class="multiple-header string_col" and text()="38443"]/ancestor-or-self::td/preceding-sibling::td/div/label/span/span').click()
        # 勾选查询出的合同号并点击确定按钮
        btn_div = chrome.find_element_by_xpath(
            '//span/input[@type="radio" and @value="0407e6f21ad3dabbb9beb9425e1b6cd87"]')
        chrome.execute_script("arguments[0].click();", btn_div)
        sleep(1)
        chrome.find_element_by_xpath('(//button[@class="el-button el-button--primary el-button--default"])[2]').click()
        sleep(1)

        # 点击业务员名称并输入查询条件，查询出结果
        chrome.find_element_by_xpath(
            '//div[text()="业务员名称 "]/following-sibling::div/div/div/div/div/div/div/div/span/span/i').click()
        sleep(1)
        chrome.find_element_by_xpath(
            '//input[@placeholder="模糊检索:业务员名称,业务员编码,业务部门名称,部门编码,部门类型编码"]').send_keys(salesman)
        sleep(1)
        chrome.find_element_by_xpath(
            '//input[@placeholder="模糊检索:业务员名称,业务员编码,业务部门名称,部门编码,部门类型编码"]').send_keys(Keys.ENTER)
        sleep(1)
        # 勾选查询出的业务员并点击确定按钮
        btn_div = chrome.find_element_by_xpath(
            '//span/input[@type="radio" and @value="0f5e829fc0c62946bbcea9f7f403a814a"]')
        chrome.execute_script("arguments[0].click();", btn_div)
        sleep(1)
        chrome.find_element_by_xpath('(//span[@class="ensureBtn"]/button[1])[3]').click()
        sleep(1)

        ###子表信息录入###
        chrome.find_element_by_xpath('//div[text()="主体信息"]').click()
        sleep(1)
        # 点击添加商品并输入商品编码，查询出结果
        chrome.find_element_by_xpath('//span[text()="添加商品(外部)"]').click()
        sleep(1)
        chrome.find_element_by_xpath('//input[@placeholder="模糊检索:商品编码,商品名称,规格型号,销售计量单位,厂家物料编码,存储条件,存储条件说明"]').send_keys(productcode)
        sleep(0.5)
        chrome.find_element_by_xpath('//input[@placeholder="模糊检索:商品编码,商品名称,规格型号,销售计量单位,厂家物料编码,存储条件,存储条件说明"]').send_keys(Keys.ENTER)
        sleep(2)
        # 勾选查询出的商品并点击确定按钮
        chrome.find_element_by_xpath(
            '//th[@class="el-table_15_column_146 multiple-column el-table-column--selection is-leaf headerCellClassName el-table__cell"]/div/label/span/span').click()
        sleep(0.5)
        chrome.find_element_by_xpath('(//div[@class="el-dialog__body"]/div[2]/span/button[1]/span)[4]').click()
        sleep(3)

        ###填写数量价格###
        for i in range(1):
            wait.until(ec.visibility_of_element_located(
                (By.XPATH, '(//td[@colid="col_93" and @f="F_QUANTITY_ORD" and @i=%s])' % i)))
            # 定位数量框并输入数量
            sleep(0.5)
            element = chrome.find_element_by_xpath('(//td[@colid="col_93" and @f="F_QUANTITY_ORD" and @i=%s])' % i)
            chrome.execute_script("arguments[0].click();", element)
            sleep(1)
            chrome.find_element_by_class_name('awsui-input-number-right').send_keys('1')
            sleep(1)
            # 点击其它地方让数量框失焦，让数量真正填入数量框(平台设计如此，不用纠结为啥要此步)
            chrome.find_element_by_xpath('(//td[@colid="col_96" and @f="F_SALE_UNIT_TXT" and @i=%s])' % i).click()
            sleep(1)

    def return_jyhtorder(self):  # 返回采购单号
        return orderno1

class DivisionPurchase():
    def divisionpurchase(self):
        #打开分配经销商要货菜单
        wait.until(ec.presence_of_element_located((By.XPATH,'//span[text()="分配经销商要货"]')))
        sleep(0.5)
        order_link = chrome.find_element_by_xpath('//span[text()="分配经销商要货"]')
        chrome.execute_script("arguments[0].click();", order_link)
        sleep(2)
        chrome.switch_to.frame(chrome.find_element_by_xpath('(//iframe[@class="metro-main-frame"])[2]'))
        sleep(1)
        chrome.switch_to.frame(chrome.find_element_by_xpath('//iframe[contains(@name,"awsui-dialog-iframe")]'))
        sleep(0.5)
        #选择需分配经销商
        s1 = Select(chrome.find_element_by_id('purchasingCoOptSelect'))
        s1.select_by_index(0)
        sleep(1)
        #选择订单
        chrome.find_element_by_xpath('//span[text()="%s"]' % orderno1).click()
        sleep(2)
        #重置配货
        chrome.find_element_by_xpath('//button[text()="重置配货"]').click()
        sleep(1)
        #选择仓库
        s2 = Select(chrome.find_element_by_id('warehouseSelect'))
        s2.select_by_value('103356')
        sleep(0.5)
        #自动填充配货
        chrome.find_element_by_xpath('//button[text()="自动填充"]').click()
        sleep(0.5)
        wait.until_not(ec.presence_of_element_located((By.XPATH,'//div[@class="layui-layer layui-layer-loading"]')))
        sleep(0.5)
        chrome.find_element_by_xpath('//button[text()="提交"]').click()
        wait.until(ec.visibility_of_element_located((By.XPATH,'//a[text()="确定"]')))
        sleep(0.5)
        chrome.find_element_by_xpath('//a[text()="确定"]').click()
        wait.until(ec.visibility_of_element_located((By.XPATH,'//div[text()="提交成功"]')))
        print('分配经销商要货成功')

class SalesOrder(PurchaseOrder):

    def return_sotorderno(self):
        wait.until(ec.visibility_of_element_located((By.XPATH, '//span[text()="保存"]')))
        ###获取销售订单号###
        global orderno2
        orderno2 = chrome.find_element_by_xpath('//div[text() = "销售订单号 "]/following-sibling::div/span').text
        return orderno2
    def button_click1(self,type=2,el1='el-loading-spinner',el2='//p[text()="办理成功"]'):

        if type == 1:  # 保存
            # 点击保存按钮
            chrome.find_element_by_xpath('//span[text()="保存"]').click()
            sleep(1)
            # 元素el1不可见时认为保存成功
            locator = (By.CLASS_NAME, f'{el1}')
            wait.until_not(ec.presence_of_element_located(locator))
        elif type == 2:  # 提交(提交后界面无弹框)
            # 点击提交按钮
            chrome.find_element_by_xpath('//span[text()="提交"]').click()
            sleep(1)
            # 元素el2出现时认为提交成功
            locator = (By.XPATH, f'{el2}')
            wait.until(ec.presence_of_element_located(locator))
        elif type == 3:  # 提交(提交后界面还有弹框)
            # 点击提交按钮
            chrome.find_element_by_xpath('//span[text()="提交"]').click()
            sleep(0.5)
            wait.until(ec.presence_of_element_located((By.XPATH,'//button[@class="el-button el-button--primary el-button--mini awsui-button awsui-button--primary"]/span')))
            sleep(0.5)
            chrome.find_element_by_xpath('//button[@class="el-button el-button--primary el-button--mini awsui-button awsui-button--primary"]/span').click()
            # 元素el2出现时认为提交成功
            locator = (By.XPATH, f'{el2}')
            wait.until(ec.presence_of_element_located(locator))
        self.closetab()
        print('销售订单提交成功，订单号为:',orderno2)


    def button_click2(self,type=1,msg1='办理成功'):#审批/驳回订单：type=1为审批/其余为驳回订单
        wait.until(ec.visibility_of_element_located((By.XPATH, '//span[text()="审核通过"]')))
        sleep(1)
        if type == 1:#审核流程
            #点击审核通过
            chrome.find_element_by_xpath('//span[text()="审核通过"]').click()

            #点击确定按钮
            wait.until(ec.presence_of_element_located((By.XPATH,
                                                       '//button[@class="el-button el-button--primary el-button--mini awsui-button awsui-button--primary"]/span')))
            sleep(0.5)
            chrome.find_element_by_xpath(
                '//button[@class="el-button el-button--primary el-button--mini awsui-button awsui-button--primary"]/span').click()


            #填写审核通过信息
            wait.until(ec.presence_of_element_located((By.XPATH, '//textarea[@placeholder="意见留言"]')))
            sleep(0.5)
            chrome.find_element_by_xpath('//textarea[@placeholder="意见留言"]').send_keys('审核通过')
            sleep(0.5)
            #点击发送按钮
            chrome.find_element_by_xpath('//span[text()="发送"]').click()
            #判断审核成功
            locator = (By.XPATH, '//p[text()="%s"]'%msg1)
            wait.until(ec.presence_of_element_located(locator))
            print('订单审核成功')
        else:#驳回流程
            #点击驳回
            chrome.find_element_by_xpath('//span[text()="审核驳回"]').click()
            locator = (By.XPATH, '//textarea[@placeholder="意见留言"]')
            wait.until(ec.presence_of_element_located(locator))
            sleep(0.5)
            #填写驳回信息
            chrome.find_element_by_xpath('//textarea[@placeholder="意见留言"]').send_keys('审核驳回')
            sleep(0.5)
            #点击发送
            chrome.find_element_by_xpath('//span[text()="发送"]').click()
            #判断驳回成功
            locator = (By.XPATH, '//p[text()="发送成功"]')
            wait.until(ec.presence_of_element_located(locator))
            print('订单驳回成功')
        self.closetab()

class Outstore1(PurchaseOrder):  # 出库三步曲
    def outbound_review1(self,orderno,type=2):#出库复核
        # 调用query_click进入表单视图
        PurchaseOrder.query_click(self,'出库复核',orderno,'vxe-body--column col_19 col--left col--ellipsis dw-click-cell')
        sleep(2)
        wait.until(ec.visibility_of_element_located((By.XPATH, '(//td[@colid="col_105" and @f="F_8VDB5FTL" and @i=0])')))
        sleep(0.5)
        #填写商品信息
        for i in range(1):
            chrome.execute_script(
                "document.getElementsByClassName('vxe-table--footer-wrapper body--wrapper')[0].scrollLeft = 1000")
            sleep(1)
            wait.until(ec.visibility_of_element_located((By.XPATH,'(//td[@colid="col_117" and @f="F_RECHECK_NUM" and @i=%s])'%i)))
            chrome.find_element_by_xpath('(//td[@colid="col_117" and @f="F_RECHECK_NUM" and @i=%s])'%i).click()
            sleep(0.5)
            chrome.find_element_by_class_name('awsui-input-number-right').send_keys('1')
            sleep(1)
            chrome.find_element_by_xpath('(//td[@colid="col_119" and @f="F_DIF_NUM" and @i=%s])'%i).click()
            sleep(1)

            chrome.execute_script(
                "document.getElementsByClassName('vxe-table--body-wrapper body--wrapper')[0].scrollLeft = 3400")
            sleep(1)
            # 填写到货温度
            chrome.find_element_by_xpath('(//td[@colid="col_135" and @f="F_ARRIVAL_TEMP" and @i=%s])' % i).click()
            sleep(0.5)
            chrome.find_element_by_class_name('awsui-input-number-right').send_keys(ARRIVAL_TEMP)
            sleep(0.5)

            # 填写到货日期
            chrome.find_element_by_xpath('(//td[@colid="col_136" and @f="F_ARRIVAL_DATE" and @i=%s])' % i).click()
            sleep(0.5)
            chrome.find_element_by_class_name('el-input__inner').send_keys(ARRIVAL_DATE)
            sleep(0.5)

            # 填写到货时间
            chrome.find_element_by_xpath('(//td[@colid="col_137" and @f="F_ARRIVAL_TIME" and @i=%s])' % i).click()
            sleep(0.5)
            chrome.find_element_by_class_name('el-input__inner').send_keys(ARRIVAL_TIME)
            sleep(0.5)

            # 填写启运温度
            chrome.find_element_by_xpath('(//td[@colid="col_138" and @f="F_STASHIP_TEMP" and @i=%s])' % i).click()
            sleep(0.5)
            chrome.find_element_by_class_name('awsui-input-number-right').send_keys(STASHIP_TEMP)
            sleep(0.5)

            # 填写启运日期
            chrome.find_element_by_xpath('(//td[@colid="col_139" and @f="F_STASHIP_DATE" and @i=%s])' % i).click()
            sleep(0.5)
            chrome.find_element_by_class_name('el-input__inner').send_keys(STASHIP_DATE)
            sleep(0.5)

            # 填写启运时间
            chrome.find_element_by_xpath('(//td[@colid="col_140" and @f="F_STASHIP_TIME" and @i=%s])' % i).click()
            sleep(0.5)
            chrome.find_element_by_class_name('el-input__inner').send_keys(STASHIP_TIME)
            sleep(0.5)

        # 点击提交按钮
        chrome.find_element_by_xpath('//span[text()="提交"]').click()
        sleep(0.5)
        wait.until(ec.presence_of_element_located((By.XPATH,
                                                   '//button[@class="el-button el-button--primary el-button--mini awsui-button awsui-button--primary"]/span')))
        sleep(0.5)
        chrome.find_element_by_xpath(
            '//button[@class="el-button el-button--primary el-button--mini awsui-button awsui-button--primary"]/span').click()
        # 元素el2出现时认为提交成功
        locator = (By.XPATH, '//p[text()="流程结束"]')
        wait.until(ec.presence_of_element_located(locator))
        PurchaseOrder.closetab(self)
        print('出库复核成功')

###采购退货单页面操作###
class SalesReject(PurchaseOrder):
    def relate_order(self,orderno,type=2):#关联原单退货
        #打开销退单
        order_link = chrome.find_element_by_xpath('//span[text()="销售退货单"]')
        chrome.execute_script("arguments[0].click();", order_link)
        chrome.switch_to.frame(chrome.find_element_by_xpath('(//iframe[@class="metro-main-frame"])[2]'))
        sleep(2)
        #关联原单采退
        wait.until(ec.visibility_of_element_located((By.XPATH,'//span[text()="关联原单"]')))
        sleep(0.5)
        chrome.find_element_by_xpath('//span[text()="关联原单"]').click()
        sleep(3)
        chrome.switch_to.frame(chrome.find_element_by_xpath('//iframe[contains(@name,"awsui-dialog-iframe")]'))
        sleep(0.5)
        # 输入采购单号并查询
        chrome.find_element_by_xpath('//label[text()="销售订单号"]/parent::div/div/div/div/input').send_keys(orderno)
        sleep(1)
        chrome.find_element_by_xpath('//label[text()="销售订单号"]/parent::div/div/div/div/input').send_keys(Keys.ENTER)

        #选中订单点击采退
        wait.until(ec.visibility_of_element_located((By.XPATH,'//th[@class="vxe-header--column col_18 col--center col--checkbox col--ellipsis"]/div/span/span')))
        sleep(1)
        chrome.find_element_by_xpath('//th[@class="vxe-header--column col_18 col--center col--checkbox col--ellipsis"]/div/span/span').click()
        sleep(0.5)
        chrome.find_element_by_xpath('//span[text()="销退"]').click()
        sleep(0.5)
        # chrome.find_element_by_class_name("el-button el-button--primary el-button--mini").click()
        chrome.find_element_by_xpath('//button[@class="el-button el-button--primary el-button--mini"]').click()
        sleep(3)
        #获取采退单号
        # wait.until(ec.presence_of_element_located((By.XPATH, '//span[text()="提交"]')))
        # locator = (By.XPATH,'//iframe[contains(@name,"awsui-dialog-iframe")]')
        # wait.until(ec.frame_to_be_available_and_switch_to_it(locator))
        chrome.switch_to.frame(chrome.find_element_by_xpath('//iframe[contains(@name,"awsui-dialog-iframe")]'))
        wait.until(ec.visibility_of_element_located((By.XPATH, '//span[text()="提交"]')))

        global orderno3
        orderno3 = chrome.find_element_by_xpath('//div[text() = "销退订单号 "]/following-sibling::div/span').text
        #选择退货原因
        chrome.find_element_by_xpath('//label[text() = "退货原因 "]/following-sibling::div//input').click()
        sleep(0.5)
        chrome.find_element_by_xpath('//li[text()="质量召回"]').click()
        sleep(1)

        #保存/提交订单
        # 点击提交按钮
        chrome.find_element_by_xpath('//span[text()="提交"]').click()
        sleep(0.5)
        wait.until(ec.presence_of_element_located((By.XPATH,
                                                   '//button[@class="el-button el-button--primary el-button--mini awsui-button awsui-button--primary"]/span')))
        sleep(0.5)
        chrome.find_element_by_xpath(
            '//button[@class="el-button el-button--primary el-button--mini awsui-button awsui-button--primary"]/span').click()
        # 元素el2出现时认为提交成功
        locator = (By.XPATH, '//p[text()="办理成功"]')
        wait.until(ec.presence_of_element_located(locator))
        PurchaseOrder.closetab(self)
        print("关联原单创建销退单成功，采退单号为:",orderno3)

    def return_sftorderno(self):#返回采退单号
        return orderno3


if __name__ == '__main__':
    try:
        step1 = LoginSys()
        step2_1 = DealerPurchaseRequisition()
        step2_2 = PurchaseOrder()
        step3 = DivisionPurchase()
        step4 = SalesOrder()
        step5_1 = Outstore()
        step5_2 = Outstore1()
        step6 = SalesReject()
        step7_1 = Instore()
        step7_2 = Instore1()
        step1.loginsys(SIT_URL, usr2, pwd2)
        step2_1.data_add_requisition()
        jyhtorder = step2_1.return_jyhtorder()
        step2_2.button_click1()
        print('经销商要货申请提交成功')
        step2_2.switch_loggin(usr1,pwd1)
        step2_2.query_click('经销商要货申请',jyhtorder,'vxe-body--column col_20 col--center col--ellipsis dw-click-cell','要货申请单号')
        step2_2.button_click2()
        step2_2.switch_loggin(usr2,pwd2)
        step3.divisionpurchase()
        step2_2.closetab()
        step2_2.query_click('销售订单',jyhtorder,'vxe-body--column col_19 col--center col--ellipsis dw-click-cell','要货申请订单号')
        sotorderno = step4.return_sotorderno()
        step4.button_click1(type=3)#提交订单
        step2_2.switch_loggin(usr1,pwd1)
        step2_2.query_click('销售订单',jyhtorder,'vxe-body--column col_19 col--center col--ellipsis dw-click-cell','要货申请订单号')
        step2_2.button_click2(msg1='发送成功')#审核订单
        step2_2.switch_loggin(usr4,pwd4)
        step5_1.deliver_goods(sotorderno)#仓库发货
        step5_1.picking_goods(sotorderno)#仓库拣货
        step2_2.switch_loggin(usr5,pwd5)
        step5_2.outbound_review1(sotorderno)#出库复核
        step2_2.switch_loggin(usr2,pwd2)
        step6.relate_order(sotorderno)#关联原单采退并提交
        sftorderno = step6.return_sftorderno()
        step2_2.switch_loggin(usr1,pwd1)
        step2_2.query_click('销售退货单',sftorderno,'vxe-body--column col_19 col--ellipsis dw-click-cell','销退订单号')
        step4.button_click2(msg1='发送成功')#审批采退单
        step2_2.switch_loggin(usr4,pwd4)
        step7_1.take_over_goods(sftorderno)#仓库收货
        step2_2.switch_loggin(usr5,pwd5)
        step7_2.goods_check(sftorderno,type=3)    #仓库验收
        step2_2.switch_loggin(usr4,pwd4)
        step7_1.goods_grounding(sftorderno)#仓库上架
    except Exception as e:
        step6 =ExceptionHandling()
        traceback.print_exc()
        step6.handle(errmsg1,e)

    # finally:
    #     chrome.quit()




