
from selenium import webdriver
from selenium.webdriver import ChromeOptions

from public.tools import wait


def settle_accounts_point(chrome,product_id,old_type="中心库入库结算",new_type="",flag=1):
    if (flag==1):
        wait()
        chrome.find_element_by_xpath('//span[text()="%s"]'%old_type).click()
        wait()
        chrome.find_element_by_xpath('//span[text()="批量导入"]').click()
        wait(2)
        chrome.find_element_by_xpath('//input[@id="basic_goodsName" and @placeholder="编号/名称/简拼"]').send_keys(product_id)
        wait()
        chrome.find_element_by_xpath('(//span[text()="查 询"])[2]').click()
        wait()
        # 勾选产品前面的复选框
        try:
            chrome.find_element_by_xpath('//td[@title="%s"]/preceding-sibling::td/label/span/input' % product_id).click()
            wait()
            chrome.find_element_by_xpath('(//span[text()="确 定"])[1]').click()
        except:
            print('没查询出产品')
            chrome.find_element_by_xpath('(//span[text()="取 消"])[1]').click()
        wait()
    elif (flag==2):
        wait()
        chrome.find_element_by_xpath('//span[text()="%s"]' % old_type).click()
        wait()
        chrome.find_element_by_id('form_item_goodsName').send_keys(product_id)
        wait()
        chrome.find_element_by_xpath('(//span[text()="查 询"])').click()
        try:
            chrome.find_element_by_xpath('//input[@type="checkbox"]').click()
        except:
            return
        wait()
        chrome.find_element_by_xpath('//span[contains(text(),"批量转")]').click()
        wait()
        # chrome.find_element_by_xpath('//label[text()="调整后规则"]/parent::div/following-sibling::div').click()
        chrome.find_element_by_xpath('(//span[@class="ant-select-selection-item"])[2]').click()
        wait()
        chrome.find_element_by_xpath('//div[text()="中心库出库结算"]').click()
        wait()
        chrome.find_element_by_xpath('// span[text() = "确 定"]').click()


def nGuiBrowserObject():
    opt = ChromeOptions()  # 创建 Chrome 参数对象
    opt.headless = False  # 把 Chrome 设置成无界面模式，windows/Linux 皆可
    # opt.headless = True  # 无界面
    opt.add_argument('--start-maximized')
    opt.add_argument('--window-size=1920x887')
    chrome = webdriver.Chrome(options=opt,executable_path="c:/python34/chromedriver.exe")  # 创建无界面对象
    return chrome
#打开网页
def openBrowser(url):
    # chrome = webdriver.Chrome()       #因为firefox是在此处定义的
    chrome=nGuiBrowserObject()
    chrome.implicitly_wait(10)  #隐式等待 10秒
    chrome.get(url)
    return chrome                      #所以函数必须返回firefox，后面的脚本才能使用
#dbshop前台登录
def logIn(chrome,username,password):
    chrome.find_element_by_xpath('//input[@placeholder="请输入用户名"]').clear()
    chrome.find_element_by_xpath('//input[@placeholder="请输入用户名"]').send_keys(username)
    # chrome.find_element_by_xpath('//input[@placeholder="请输入密码"]').send_keys(password)
    chrome.find_element_by_xpath('//span[text()="登 录"]').click()
#dbshop前台修改密码
def changePassword(firefox,password,newPassword):
    firefox.find_element_by_link_text("用户中心").click()
    firefox.find_element_by_link_text("账户信息").click()
    firefox.find_element_by_link_text("密码修改").click()
    firefox.find_element_by_id("old_user_password").send_keys(password)
    firefox.find_element_by_id("user_password").send_keys(newPassword)
    firefox.find_element_by_id("user_password_con").send_keys(newPassword)
    firefox.find_element_by_xpath('//button[@type="submit"]').click()
#dbshop前台退出
def logOut(firefox):
    firefox.find_element_by_link_text("退出").click()
#dbshop后台登录
def logInAdmin(firefox):
    firefox.find_element_by_id("user_name").send_keys("admin")
    firefox.find_element_by_id("user_passwd").send_keys("123456")
    firefox.find_element_by_xpath('//button[@class="btn"]').click()
#dbshop进入后台二级菜单
def enterMenu3(chrome,menu1,menu2,menu3):
    wait()
    chrome.find_element_by_xpath(menu1).click()
    wait()
    chrome.find_element_by_xpath(menu2).click()
    wait()
    chrome.find_element_by_xpath(menu3).click()
    wait()
# #dbshop进入后台三级菜单
# def enterMenu3(firefox,menu1,menu2,menu3):
#     firefox.find_element_by_link_text(menu1).click()
#     from selenium.webdriver.common.action_chains import ActionChains  # 导入ActionChains类
#     mouse = ActionChains(firefox)  # 以浏览器为参数，实例化一个对象mouse，用于模拟鼠标操作浏览器
#     element = firefox.find_element_by_link_text(menu2)  # 先定位到要操作的对象element
#     mouse.move_to_element(element).perform()  # 鼠标移动到元素element上，perform()是立即的意思
#     firefox.find_element_by_link_text(menu3).click()
#dbshop项目页面切换
def switch_page(a,firefox):
    firefox.switch_to.window(firefox.window_handles[a-1])
