#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
模块名称：update_template_cases
模块描述：将会议模板 / 参会人模板的 9 个用例改为 navigate 直达 URL 模式
作者：czj
创建日期：2026-08-21
最后修改日期：2026-08-21
"""
import json
import sqlite3
import sys
from pathlib import Path

sys.stdout.reconfigure(encoding='utf-8')
os.chdir if False else None  # placeholder
import os
os.chdir(Path(__file__).parent.parent)

# ── 直达 URL ──
MEETING_TEMPLATE_URL = "https://192.168.5.44/#/meetingV3?meetingV3=%2FmeetingV3%2F%23%2FMeetingtemplate"
PARTICIPANT_TEMPLATE_URL = "https://192.168.5.44/#/meetingV2?meetingV2=%2FmeetingV2%2F%23%2FMeeting%2FParticipantTemplate"


def make_step(order, name, action, params, expected=""):
    return {
        "order": order,
        "name": name,
        "action": action,
        "params": params,
        "expected": expected,
        "selectors": None,
        "page_key": None,
        "element_key": None,
        "force": None,
        "wait_after": None,
    }


# ── 6 个会议模板用例 ──
UPDATES = {}

# 1) 会议模板-页面访问验证
UPDATES["case_52962d8b58754471952e9d02c5eec576"] = [
    make_step(1, "导航到会议模板页面", "navigate", {"url": MEETING_TEMPLATE_URL}),
    make_step(2, "等待页面加载", "wait", {"selector": "body", "timeout": 15000}),
    make_step(3, "验证搜索框存在", "assert", {"type": "element_exists", "selector": "input[placeholder*='请输入关键字']"}),
    make_step(4, "验证新增模板按钮存在", "assert", {"type": "element_exists", "selector": "button:has-text('新增模板')"}),
    make_step(5, "验证批量删除按钮存在", "assert", {"type": "element_exists", "selector": "button:has-text('批量删除')"}),
    make_step(6, "验证表格存在", "assert", {"type": "element_exists", "selector": ".el-table"}),
]

# 2) 会议模板-页面验证
UPDATES["case_50036f7ef33043fbbf5c7cfa70631ba6"] = [
    make_step(1, "导航到会议模板页面", "navigate", {"url": MEETING_TEMPLATE_URL}),
    make_step(2, "等待页面加载", "wait", {"selector": "body", "timeout": 15000}),
    make_step(3, "验证表格存在", "assert", {"type": "element_exists", "selector": ".el-table"}),
    make_step(4, "验证模板名称列头", "assert", {"type": "element_exists", "selector": ".el-table__header:has-text('模板名称')"}),
    make_step(5, "验证会议类型列头", "assert", {"type": "element_exists", "selector": ".el-table__header:has-text('会议类型')"}),
    make_step(6, "验证会议室列头", "assert", {"type": "element_exists", "selector": ".el-table__header:has-text('会议室')"}),
    make_step(7, "验证创建时间列头", "assert", {"type": "element_exists", "selector": ".el-table__header:has-text('创建时间')"}),
    make_step(8, "验证操作列头", "assert", {"type": "element_exists", "selector": ".el-table__header:has-text('操作')"}),
]

# 3) 会议模板-新增模板验证
UPDATES["case_af27f62f3a4c483e9953083f4f178517"] = [
    make_step(1, "导航到会议模板页面", "navigate", {"url": MEETING_TEMPLATE_URL}),
    make_step(2, "等待页面加载", "wait", {"selector": "body", "timeout": 15000}),
    make_step(3, "点击新增模板按钮", "click", {"selector": "button:has-text('新增模板')"}),
    make_step(4, "等待新增弹窗", "wait", {"selector": ".el-dialog:visible", "timeout": 5000}),
    make_step(5, "验证弹窗标题", "assert", {"type": "contains", "selector": ".el-dialog__title", "expected": "新增模板"}),
    make_step(6, "验证有模板名称输入框", "assert", {"type": "element_exists", "selector": ".el-dialog input"}),
    make_step(7, "关闭弹窗", "click", {"selector": ".el-dialog__headerbtn"}),
]

# 4) 会议模板-编辑模板验证
UPDATES["case_62fdcd83d9f1469b93216c6d03a81f32"] = [
    make_step(1, "导航到会议模板页面", "navigate", {"url": MEETING_TEMPLATE_URL}),
    make_step(2, "等待页面加载", "wait", {"selector": "body", "timeout": 15000}),
    make_step(3, "验证有模板数据", "assert", {"type": "element_count_min", "selector": ".el-table__row", "expected": "1"}),
    make_step(4, "点击编辑按钮", "click", {"selectors": [".el-table__row:first-child button:has-text('编辑')", ".el-table__row:first-child .el-button--text"]}),
    make_step(5, "等待编辑弹窗", "wait", {"selector": ".el-dialog:visible", "timeout": 5000}),
    make_step(6, "验证弹窗标题", "assert", {"type": "contains", "selector": ".el-dialog__title", "expected": "编辑模板"}),
    make_step(7, "关闭弹窗", "click", {"selector": ".el-dialog__headerbtn"}),
]

# 5) 会议模板-删除模板验证
UPDATES["case_605f3f8956374f06ad811e1cf5b384d7"] = [
    make_step(1, "导航到会议模板页面", "navigate", {"url": MEETING_TEMPLATE_URL}),
    make_step(2, "等待页面加载", "wait", {"selector": "body", "timeout": 15000}),
    make_step(3, "验证有模板数据", "assert", {"type": "element_count_min", "selector": ".el-table__row", "expected": "1"}),
    make_step(4, "点击删除按钮", "click", {"selectors": [".el-table__row:first-child button:has-text('删除')", ".el-table__row:first-child .el-button--danger"]}),
    make_step(5, "等待确认弹窗", "wait", {"selector": ".el-message-box:visible", "timeout": 5000}),
    make_step(6, "点击取消", "click", {"selector": ".el-message-box button:has-text('取 消')"}),
]

# 6) 会议模板-批量删除验证
UPDATES["case_f4a50c184a734b319409b6fa4f4b626c"] = [
    make_step(1, "导航到会议模板页面", "navigate", {"url": MEETING_TEMPLATE_URL}),
    make_step(2, "等待页面加载", "wait", {"selector": "body", "timeout": 15000}),
    make_step(3, "验证批量删除按钮存在", "assert", {"type": "element_exists", "selector": "button:has-text('批量删除')"}),
    make_step(4, "点击批量删除按钮", "click", {"selector": "button:has-text('批量删除')"}),
    make_step(5, "等待提示弹窗", "wait", {"selector": ".el-message-box:visible, .el-message:visible", "timeout": 5000}),
    make_step(6, "点击取消或关闭", "click", {"selectors": [".el-message-box button:has-text('取 消')", ".el-message-box__headerbtn"]}),
]

# ── 3 个参会人模板用例 ──

# 7) 参会人模板-页面访问验证
UPDATES["case_e6810d5d6499453b812aac5715ec7f5e"] = [
    make_step(1, "导航到参会人模板页面", "navigate", {"url": PARTICIPANT_TEMPLATE_URL}),
    make_step(2, "等待页面加载", "wait", {"selector": "body", "timeout": 15000}),
    make_step(3, "验证搜索框存在", "assert", {"type": "element_exists", "selector": "input[placeholder*='输入关键字']"}),
    make_step(4, "验证新增参会人模板按钮存在", "assert", {"type": "element_exists", "selector": "button:has-text('新增参会人模板')"}),
    make_step(5, "验证表格存在", "assert", {"type": "element_exists", "selector": ".el-table"}),
    make_step(6, "验证模板名称列头", "assert", {"type": "element_exists", "selector": ".el-table__header:has-text('模版名称')"}),
    make_step(7, "验证日期列头", "assert", {"type": "element_exists", "selector": ".el-table__header:has-text('日期')"}),
    make_step(8, "验证备注列头", "assert", {"type": "element_exists", "selector": ".el-table__header:has-text('备注')"}),
    make_step(9, "验证操作列头", "assert", {"type": "element_exists", "selector": ".el-table__header:has-text('操作')"}),
]

# 8) 参会人模板-表格数据验证
UPDATES["case_acad37b095b7404f94db3ad5cce953b9"] = [
    make_step(1, "导航到参会人模板页面", "navigate", {"url": PARTICIPANT_TEMPLATE_URL}),
    make_step(2, "等待页面加载", "wait", {"selector": "body", "timeout": 15000}),
    make_step(3, "验证表格有数据行", "assert", {"type": "element_count_min", "selector": ".el-table__row", "expected": "1"}),
    make_step(4, "验证编辑模板按钮存在", "assert", {"type": "element_exists", "selector": "button:has-text('编辑模板')"}),
    make_step(5, "验证再次预定按钮存在", "assert", {"type": "element_exists", "selector": "button:has-text('再次预定')"}),
    make_step(6, "验证详细信息按钮存在", "assert", {"type": "element_exists", "selector": "button:has-text('详细信息')"}),
    make_step(7, "验证删除模版按钮存在", "assert", {"type": "element_exists", "selector": "button:has-text('删除模版')"}),
]

# 9) 参会人模板-操作按钮验证
UPDATES["case_8570499b54fa43eaa0717614e4377a5b"] = [
    make_step(1, "导航到参会人模板页面", "navigate", {"url": PARTICIPANT_TEMPLATE_URL}),
    make_step(2, "等待页面加载", "wait", {"selector": "body", "timeout": 15000}),
    make_step(3, "验证表格有数据行", "assert", {"type": "element_count_min", "selector": ".el-table__row", "expected": "1"}),
    make_step(4, "点击编辑模板", "click", {"selector": "button:has-text('编辑模板')"}),
    make_step(5, "等待弹窗", "wait", {"selector": ".el-dialog:visible", "timeout": 5000}),
    make_step(6, "验证弹窗出现", "assert", {"type": "element_exists", "selector": ".el-dialog"}),
    make_step(7, "关闭弹窗", "click", {"selector": ".el-dialog__headerbtn"}),
    make_step(8, "等待弹窗关闭", "wait", {"selector": "body", "timeout": 3000}),
    make_step(9, "点击详细信息", "click", {"selector": "button:has-text('详细信息')"}),
    make_step(10, "等待详情弹窗", "wait", {"selector": ".el-dialog:visible", "timeout": 5000}),
    make_step(11, "关闭详情弹窗", "click", {"selector": ".el-dialog__headerbtn"}),
]


NOW = "2026-08-21 10:00:00"


def main():
    conn = sqlite3.connect("data/test_platform.db")
    conn.row_factory = sqlite3.Row
    cur = conn.cursor()

    for cid, steps in UPDATES.items():
        steps_json = json.dumps(steps, ensure_ascii=False)
        cur.execute(
            "UPDATE test_cases SET steps=?, updated_at=?, version=version+1 WHERE id=?",
            (steps_json, NOW, cid),
        )
        cur.execute("SELECT name FROM test_cases WHERE id=?", (cid,))
        r = cur.fetchone()
        print(f"  ✅ {cid} | {r['name']} | steps={len(steps)}")

    conn.commit()
    conn.close()
    print(f"\n✅ 共更新 {len(UPDATES)} 个用例")


if __name__ == "__main__":
    main()
