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

sys.stdout.reconfigure(encoding='utf-8')
import os
os.chdir(Path(__file__).parent.parent)

MEETING_ROOM_LIST_URL = "https://192.168.5.44/#/meetingV3?meetingV3=%2FmeetingV3%2F%23%2FMeetingRoomList"


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,
    }


UPDATES = {}

# 1) 会议室列表页面访问验证 — 验证页面基本元素
UPDATES["case_05d43343a6b34910a86c080450225dfc"] = [
    make_step(1, "导航到会议室列表页面", "navigate", {"url": MEETING_ROOM_LIST_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": "input[placeholder='会议室名称']"}),
    make_step(5, "验证新建会议按钮存在", "assert", {"type": "element_exists", "selector": "button:has-text('新建会议')"}),
    make_step(6, "验证有会议室列表卡片", "assert", {"type": "element_count_min", "selector": ".room_item", "expected": "1"}),
]

# 2) 会议室列表-查看详情 — 验证页面元素和预览功能
UPDATES["case_98abcdfcd8c644e8a086dc4444254b00"] = [
    make_step(1, "导航到会议室列表页面", "navigate", {"url": MEETING_ROOM_LIST_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_count_min", "selector": ".room_item", "expected": "1"}),
]

# 3) 会议室列表-预览功能 — 点击预览查看弹窗
UPDATES["case_5e6a80d82ea646c5b00c663aafe85494"] = [
    make_step(1, "导航到会议室列表页面", "navigate", {"url": MEETING_ROOM_LIST_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-dialog:visible", "timeout": 5000}),
    make_step(6, "验证预览弹窗存在", "assert", {"type": "element_exists", "selector": ".el-dialog"}),
    make_step(7, "关闭预览弹窗", "click", {"selector": ".el-dialog__headerbtn"}),
]

# 4) 会议室列表-搜索功能 — 搜索会议室名称
UPDATES["case_099ce65993ea47b98aca16ca5adcb796"] = [
    make_step(1, "导航到会议室列表页面", "navigate", {"url": MEETING_ROOM_LIST_URL}),
    make_step(2, "等待页面加载", "wait", {"selector": "body", "timeout": 15000}),
    make_step(3, "验证搜索框存在", "assert", {"type": "element_exists", "selector": "input[placeholder='会议室名称']"}),
    make_step(4, "填写会议室名称", "fill", {"selector": "input[placeholder='会议室名称']", "value": "会议室"}),
    make_step(5, "点击确定搜索", "click", {"selector": "button:has-text('确定')"}),
    make_step(6, "等待搜索结果", "wait", {"selector": "body", "timeout": 5000}),
]

# 5) 会议室列表-筛选功能验证 — 条件筛选交互
UPDATES["case_98bdb3f6b5054f3db383bd8a6aeda2b2"] = [
    make_step(1, "导航到会议室列表页面", "navigate", {"url": MEETING_ROOM_LIST_URL}),
    make_step(2, "等待页面加载", "wait", {"selector": "body", "timeout": 15000}),
    make_step(3, "验证有筛选输入框", "assert", {"type": "element_count_min", "selector": "input.el-input__inner", "expected": "2"}),
    make_step(4, "填写会议室名称", "fill", {"selector": "input[placeholder='会议室名称']", "value": "会议室"}),
    make_step(5, "点击确定筛选", "click", {"selector": "button:has-text('确定')"}),
    make_step(6, "等待筛选结果", "wait", {"selector": "body", "timeout": 5000}),
]

# 6) 会议室列表-预约会议验证 — 点击新建会议跳转到新建会议页
UPDATES["case_6e00f1983f91441395f468cf967c49ba"] = [
    make_step(1, "导航到会议室列表页面", "navigate", {"url": MEETING_ROOM_LIST_URL}),
    make_step(2, "等待页面加载", "wait", {"selector": "body", "timeout": 15000}),
    make_step(3, "点击新建会议按钮", "click", {"selector": "button:has-text('新建会议')"}),
    make_step(4, "等待跳转到新建会议", "wait", {"selector": "input[placeholder='会议名称/会议议题']", "timeout": 10000}),
    make_step(5, "验证进入新建会议页面", "assert", {"type": "element_exists", "selector": "input[placeholder='会议名称/会议议题']"}),
]

# 7) 会议室列表-预览详情验证 — 点击预览查看弹窗详情
UPDATES["case_8e89f99b86ba441ca2030beac86ec93f"] = [
    make_step(1, "导航到会议室列表页面", "navigate", {"url": MEETING_ROOM_LIST_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-dialog:visible", "timeout": 5000}),
    make_step(6, "验证弹窗有会议室名称", "assert", {"type": "element_exists", "selector": ".el-dialog:visible .room_name, .el-dialog:visible .el-dialog__body"}),
    make_step(7, "关闭预览弹窗", "click", {"selector": ".el-dialog__headerbtn"}),
]


NOW = "2026-08-21 12: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()