#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
模块名称：generate_asset_info_cases
模块描述：将资产信息 3 个用例改为 navigate 直达 Assetinformation 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)

ASSET_INFO_URL = "https://192.168.5.44/#/meetingV3?meetingV3=%2FmeetingV3%2F%23%2FAssetinformation"


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) 资产信息-页面访问验证
# 真实DOM：input[placeholder='请输入关键字查询']、input[placeholder='请选择状态'](readonly)、
#          按钮 添加条目/下载模板/导入表格/批量报修/批量报废/批量删除、.el-table
# 表格有 26 条数据，选择器 .el-table__row
UPDATES["case_eac47c7888dd425dbb18ebca95ffeb25"] = [
    make_step(1, "导航到资产信息页面", "navigate", {"url": ASSET_INFO_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_exists", "selector": ".el-table"}),
]

# 2) 资产信息-表格数据验证
UPDATES["case_f4f9239c31d34fc0909980eafc5f27df"] = [
    make_step(1, "导航到资产信息页面", "navigate", {"url": ASSET_INFO_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": "text=品牌"}),
    make_step(5, "验证型号列存在", "assert", {"type": "element_exists", "selector": "text=型号"}),
    make_step(6, "验证编辑操作按钮存在", "assert", {"type": "element_exists", "selector": "button:has-text('编辑')"}),
    make_step(7, "验证删除操作按钮存在", "assert", {"type": "element_exists", "selector": "button:has-text('删除')"}),
]

# 3) 资产信息-筛选器交互验证
# 状态下拉框：input[placeholder='请选择状态'] readonly
# 关键字查询：input[placeholder='请输入关键字查询']
UPDATES["case_bad683575a0f4dbb9b784de63f8624e2"] = [
    make_step(1, "导航到资产信息页面", "navigate", {"url": ASSET_INFO_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}),
]


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