from datetime import  timedelta
import sys
import os

# 获取当前脚本的绝对路径
current_dir = os.path.dirname(os.path.abspath(__file__))
# 构建预定系统的绝对路径
预定系统_path = os.path.abspath(os.path.join(current_dir, '..','..','..','..'))
# 添加路径
sys.path.append(预定系统_path)
# 导入模块
try:
    from 预定系统.Base.base import *
except ModuleNotFoundError as e:
    print(f"ModuleNotFoundError: {e}")
    print("尝试使用绝对路径导入")
    from 预定系统.Base.base import *

# 获取当前脚本所在的目录
current_dir = os.path.dirname(os.path.abspath(__file__))

# 构建CSV文件的绝对路径
csv_file_path = os.path.join(current_dir, '../../../测试数据/展厅巡检/会议预约.csv')
# 定义时间格式
time_format = "%H:%M"

class Exhibition_hall_inspection_000x:
    """
        执行指令：
            1.cd 预定系统
            2.hytest --report_title 会议预约测试报告 --report_url_prefix http://192.168.1.225 --test 展厅巡检_0**
    """
    tags = ['展厅巡检']
    ddt_cases = read_csv_data(csv_file_path)

    def teststeps(self):
        """
            执行测试步骤以会议室删除功能。
            本函数根据参数输入会议室名称进行搜索并删除会议室，并检查删除后的提示信息是否与预期相符。
            字段含义：
              name:用例名称
              conference_name:会议室名称
              message_name:会议名称
              message_type:会议类型
              book_type:预约类型
              book_start_time:会议开始时间
              book_end_time:会议结束时间
              message_notification:消息提醒时间
              check_text:检查提示文本
        """
        # 初始化通知文本为空字符串
        notify_text = ""
        # 从全局存储中获取webdriver实例
        wd = GSTORE['wd']
        name = self.name
        conference_name,message_name,book_type,issue_name,book_start_time,book_end_time,message_notification,check_text = self.para

        # 在会议室列表搜索会议室
        STEP(1, f"在会议室列表搜索：{conference_name}")
        # 展开筛选下拉框
        safe_click((By.XPATH, "//i[@class='el-collapse-item__arrow el-icon-arrow-right']"),wd)
        sleep(1)
        # 输入会议室名称，并点击【查询】按钮
        safe_send_keys((By.XPATH, "//input[@placeholder='请输入会议室名称']"),conference_name,wd)
        safe_click((By.XPATH, "//button[@type='button']//span[contains(text(),'查询')]"),wd)
        sleep(1)

        # 点击【会议预定】按钮，进入会议预定界面
        STEP(2, "点击【会议预定】按钮")
        safe_click((By.XPATH, "//span[@class='MeetingCityList_t_btn']"),wd)
        sleep(1)

        # 在会议预定第一步界面进行数据填入
        STEP(3,"会议第一步数据填写")
        INFO(f"输入会议名称：{message_name}，选择会议类型：视讯融合会议，选择预约类型：{book_type}，选择会议开始时间：{book_start_time}，选择会议结束时间：{book_end_time}")
        # 输入会议名称
        safe_send_keys((By.XPATH, "//input[@placeholder='请输入会议名称']"),message_name,wd)

        # 判断会议类型
        safe_click((By.XPATH, f"//div[@class='reserve_input']//span[@class='el-checkbox__label'][normalize-space()='SMC3.0视频会议']"),wd)
        safe_click((By.XPATH, f"//div[@class='reserve_input']//span[@class='el-checkbox__label'][normalize-space()='讯飞语音转录']"),wd)
        safe_click((By.XPATH, f"//div[@class='reserve_input']//span[@class='el-checkbox__label'][normalize-space()='腾讯会议']"),wd)
        sleep(2)

        # 选择普通会议
        safe_click((By.XPATH, f"//span[contains(text(),'{book_type}')]"), wd)
        sleep(1)

        # 调用函数添加议题
        INFO("添加议题文件")
        issue_send_and_upload(wd, 5, issue_name)
        sleep(2)

        # 选择签到时间
        INFO("选择提前30分钟开始签到")
        safe_click((By.XPATH, "(//span[@class='el-switch__core'])[2]"),wd)
        safe_click((By.XPATH, "(//span[@class='el-switch__core'])[3]"),wd)
        sleep(1)
        safe_send_keys((By.XPATH, "//input[@id='input_checkin']"), "30", wd)

        # 将字符串转换为时间对象1
        start_time = datetime.strptime(book_start_time, time_format)
        end_time = datetime.strptime(book_end_time, time_format)

        # 如果需要将时间差转换为分钟数
        time_diff_minutes = (end_time - start_time).total_seconds() / 60
        # INFO(f"时间差为：time_diff_minutes")

        # 判断如果开始时间与结束时间相等，则不重复点击
        if book_start_time == book_end_time:
            INFO(f"开始时间与结束时间相等，选择时间块：{book_start_time}")
            # 根据开始时间选择对应时间块，时间格式为：**:**
            safe_click((By.XPATH, f"(//div[normalize-space()='{book_start_time}'])[1]"), wd)
        elif book_start_time != book_end_time:
            INFO(f"开始时间与结束时间不相等，选择时间块：{book_start_time}，{book_end_time}")
            # 根据开始时间选择对应时间块，时间格式为：**:**
            safe_click((By.XPATH, f"(//div[normalize-space()='{book_start_time}'])[1]"), wd)
            # 根据结束时间选择对应时间块，时间格式为：**:**
            safe_click((By.XPATH, f"(//div[normalize-space()='{book_end_time}'])[1]"), wd)
        sleep(2)

        # 判断时间差是否大于最大预约时长1小时，即60分钟
        if time_diff_minutes > 60:
            INFO(f"时间差大于最大预约时长1小时，选择【超出预约时长】")
            notify_text = get_notify_text(wd, (By.XPATH, "//p[contains(@class,'el-message__content')]"), "Message_Manage", "Message_Book", f"{name}_超出预约时长")
            INFO(f"提示信息为：{notify_text}")
            sleep(2)

            # 不选时间点击【下一步】查看提示信息
            INFO("不选时间点击【下一步】查看提示信息")
            safe_click((By.XPATH, "//div[contains(text(),'下一步')]"),wd)
            sleep(1)
            notify_text = get_notify_text(wd, (By.XPATH, "//p[contains(@class,'el-message__content')]"), "Message_Manage", "Message_Book", f"{name}请选择会议时间提示信息")
            INFO(f"提示信息为：{notify_text}")
            sleep(1)

            # 重新选择时间，将字符串转换为时间对象
            time_obj = datetime.strptime(book_end_time, time_format)
            time_obj_minus_5_minutes = time_obj - timedelta(minutes=30)

            # 将时间对象转换回字符串
            book_end_time = time_obj_minus_5_minutes.strftime(time_format)
            INFO(f"重新选择结束时间为：{book_end_time}")
            # 根据开始时间选择对应时间块，时间格式为：**:**
            safe_click((By.XPATH, f"(//div[normalize-space()='{book_start_time}'])[1]"), wd)
            # 根据结束时间选择对应时间块，时间格式为：**:**
            safe_click((By.XPATH, f"(//div[normalize-space()='{book_end_time}'])[1]"), wd)
            sleep(2)

        # 在会议预定第二步界面进行数据填入
        STEP(4, "会议第二步数据填写")
        # 点击【下一步】进入会议预定第二步界面
        safe_click((By.XPATH, "//div[contains(text(),'下一步')]"),wd)
        sleep(1)

        # 根据参会人名称搜搜对应参会人
        ipn = ['赵嘉诚','CZJ','向彩娇','马晓丽','王兵','陈林','李立键','丁海洪','刘建胜','潘松林','卢培锽','黄史恭','罗一龙','彭甘宇']
        for i in range(0, 14):
            safe_send_keys((By.XPATH, "//input[@placeholder='账号/用户名']"), ipn[i], wd)
            send_keyboard((By.XPATH, "//input[@placeholder='账号/用户名']"), wd)
            sleep(1)
            # 选择内部参会人
            safe_click((By.XPATH, "//th[contains(@class,'is-leaf el-table__cell')]//span[contains(@class,'el-checkbox__inner')]"), wd)
            # 获取已选的参会人名称
            # notify_text = elment_get_text((By.XPATH, f"//span[normalize-space()='{ipn[i]}']"), wd)
        INFO(f"已选择参会人：{ipn}")
        SELENIUM_LOG_SCREEN(wd, "50%", "Exhibit_Inspect", "Meeting_Message", "选择内部参会人")

        # 点击【下一步】按钮
        safe_click((By.XPATH,"//div[contains(text(),'下一步')]"),wd)
        sleep(1)
        safe_click((By.XPATH,"//div[contains(text(),'下一步')]"),wd)

        # 进入会议预约第三步进行填写
        STEP(5, "会议第三步数据填写")

        # 选择消息提醒时间
        INFO(f"选择消息提醒时间：{message_notification}")
        # 判断消息提醒时间是否为全选
        if message_notification == "全选":
            # 点击【全选】按钮
            safe_click((By.XPATH,"//div[@class='Reminds']//span[@class='el-checkbox__label'][contains(text(),'全选')]"),wd)
            sleep(1)
        else:
            # 选择对应的消息提醒时间
            safe_click((By.XPATH, f"//span[contains(text(),'{message_notification}')]"), wd)
            sleep(1)

        # 点击【确定】按钮
        STEP(6, "点击【确定】按钮")
        safe_click((By.XPATH,"//div[contains(text(),'确定')]"),wd)

        # 点击最后【确定】按钮，预定成功后会跳转至已预定界面
        STEP(7, "点击【确定】按钮")
        safe_click((By.XPATH,"//div[contains(@aria-label,'会议信息')]//span[contains(text(),'确定')]"),wd)
        sleep(3)

        # 搜索会议名称
        safe_send_keys((By.XPATH,"//input[@placeholder='输入关键字搜索']"),message_name,wd)
        send_keyboard((By.XPATH,"//input[@placeholder='输入关键字搜索']"), wd)

        # 在已预定列表界面【提前开始】会议
        INFO("提前开始会议")
        safe_click((By.XPATH,"//span[contains(text(),'更多操作')]"),wd)
        safe_click((By.XPATH,"//li[contains(text(),'会议状态')]"),wd)
        sleep(2)
        safe_click((By.XPATH,"//div[@slot='footer']//span[contains(text(),'确定')]"),wd)
        sleep(10)

        STEP(8, "检查门口屏会议显示")
        # 进入后台-安卓信息界面截屏查看门口屏显示
        safe_click((By.XPATH,"(//img[contains(@title,'后台系统')])[1]"),wd)
        sleep(1)
        safe_click((By.XPATH,"(//span[contains(text(),'设备管理')])[1]"),wd)
        safe_click((By.XPATH,"(//li[contains(text(),'安卓信息')])[1]"),wd)
        INFO("进入安卓信息界面，查看门口屏设备列表，请查看设备列表状态显示是否正常")
        sleep(5)
        SELENIUM_LOG_SCREEN(wd, "50%", "Exhibit_Inspect", "Android", "门口屏设备列表")
        safe_click((By.XPATH,"(//span[contains(text(),'应用截屏')])[1]"),wd)
        sleep(30)
        INFO("查看门口屏截屏显示，请查看门口屏截屏功能是否正常")
        # 截屏查看门口屏的显示
        SELENIUM_LOG_SCREEN(wd, "50%", "Exhibit_Inspect", "Android", "门口屏截屏")
        # 返回上一级界面
        safe_click((By.XPATH,"//div[@aria-label='门口屏截屏']//i[@class='el-dialog__close el-icon el-icon-close']"), wd)

        STEP(9, "检查会控界面功能")
        # 返回前台已预定列表界面
        safe_click((By.XPATH,"//img[@title='返回预定首页']"),wd)
        sleep(1)
        safe_click((By.XPATH,"//span[contains(text(),'已预订会议')]"),wd)
        sleep(1)
        SELENIUM_LOG_SCREEN(wd, "50%", "Exhibit_Inspect", "MeetingControl", "查看会控按钮")

        # 判断是否存在会控按钮
        INFO("判断是否存在【进入会控】按钮")
        safe_click((By.XPATH,"//span[contains(text(),'进入会控')]"),wd)
        sleep(5)

        # 切换窗口2，转到会控界面
        wd.switch_to.window(wd.window_handles[1])

        # 会控操作
        STEP(10, "检查会控界面功能")
        SELENIUM_LOG_SCREEN(wd, "50%", "Exhibit_Inspect", "MeetingControl", "检查会控界面功能")
        INFO("请检查会控界面功能是否正常")

        # 添加SMC会场
        INFO("添加SMC会场：展厅指挥中心、展厅会议室")
        safe_click((By.XPATH,"//body/div[@id='app']/div[@id='Energy']/div[@class='main']/div[@class='mainList']/div[@class='main_l']/div[@class='mainFNC']/img[1]"),wd)
        safe_send_keys((By.XPATH,"//input[@placeholder='搜索与会方']"),"展厅指挥中心",wd)
        sleep(1)
        send_keyboard((By.XPATH,"//input[@placeholder='搜索与会方']"), wd)
        sleep(1)
        safe_click((By.XPATH,"//div[contains(@class,'el-table el-table--fit el-table--enable-row-hover el-table--enable-row-transition')]//div[contains(@class,'el-table__header-wrapper')]//span[contains(@class,'el-checkbox__inner')]"),wd)

        # 添加展厅指挥中心
        INFO("添加腾讯会场")
        safe_click((By.XPATH,"//div[@id='tab-TencentUser']"),wd)
        safe_click((By.XPATH,"//li[normalize-space()='6']"),wd)
        sleep(1)
        safe_click((By.XPATH,"//li[normalize-space()='8']"),wd)
        sleep(1)
        safe_click((By.XPATH,"//body[1]/div[1]/div[1]/div[3]/div[1]/div[2]/div[1]/div[1]/div[1]/div[2]/div[3]/div[1]/div[1]/div[1]/div[3]/table[1]/tbody[1]/tr[7]/td[1]/div[1]/label[1]/span[1]/span[1]"),wd)
        sleep(1)
        SELENIUM_LOG_SCREEN(wd, "50%", "Exhibit_Inspect", "MeetingControl", "添加会场功能")
        safe_click((By.XPATH,"//div[contains(text(),'确定')]"),wd)
        sleep(1)

        # 一键呼叫会场
        # safe_click((By.XPATH,"//body/div[@id='app']/div[@id='Energy']/div[contains(@class,'main')]/div[contains(@class,'mainList')]/div[contains(@class,'main_l')]/div[contains(@class,'mainFNC')]/img[1]"),wd)

        # 选择分屏功能
        INFO("选择分屏功能")
        sleep(10)
        safe_click((By.XPATH, "//li[4]//img[1]"), wd)
        SELENIUM_LOG_SCREEN(wd, "50%", "Exhibit_Inspect", "MeetingControl", "分屏功能")

        # 将左侧会场拖拽到右侧画面进行播放
        INFO("将左侧会场拖拽到右侧画面进行播放")
        single_click_and_drag((By.XPATH,"//span[contains(text(),'展厅会议室')]"), (By.XPATH,"(//div)[118]") , wd)
        wd.refresh()
        sleep(2)
        single_click_and_drag((By.XPATH,"//span[contains(text(),'展厅指挥中心')]"), (By.XPATH,"(//div)[119]") , wd)

        single_click_and_drag((By.CSS_SELECTOR,"body > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(3) > table:nth-child(1) > tbody:nth-child(2) > tr:nth-child(1) > td:nth-child(2) > div:nth-child(1) > span:nth-child(1)"),(By.XPATH,"(//div)[120]"), wd)
        sleep(4)
        SELENIUM_LOG_SCREEN(wd, "50%", "Exhibit_Inspect", "MeetingControl", "会场画面控制")

        INFO("使用基础会控功能：禁用摄像头、麦克风")
        safe_click((By.XPATH,"//span[contains(text(),'禁用摄像头')]"),wd)

        STEP(11, "检查会控界面功能")
        INFO("开启指挥中心麦克风")
        sleep(2)
        safe_click((By.XPATH,"(//img[@title='解除静音'])[3]"),wd)
        SELENIUM_LOG_SCREEN(wd, "50%", "Exhibit_Inspect", "MeetingControl", "会场画面控制")