#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
模块名称：create_func_center_cases.py
模块描述：补充功能中心/会议运维/资产管理/会务管理/维护工单模块的UI自动化用例

用例方向：
1. 功能中心抽屉：打开/关闭、搜索、置顶/取消置顶
2. 会议运维：告警列表/视讯设备页面验证
3. 资产管理：资产信息页面验证
4. 会务管理：会务统筹页面验证
5. 维护工单：工单列表页面验证

作者：czj
创建日期：2026-07-22
最后修改日期：2026-07-22
"""
import asyncio
import httpx
import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

BASE_URL = "http://localhost:8001"
TARGET_URL = "https://192.168.5.44"

# 模块ID
FUNC_CENTER_MODULE_ID = "module_1c2cd6d565054bf6ac632aad77685b99"
MEETING_OPS_MODULE_ID = "module_c9496f382be1430c958936d5cf473f62"
ASSET_MODULE_ID = "module_251aebd2e6dd4d09acda0eb179c35d23"
CONFERENCE_MODULE_ID = "module_69cba085ed3c4b8288a363de7b97aa63"
WORK_ORDER_MODULE_ID = "module_0b5e956ade4c4b188ec1a6826f71229c"

DEFAULT_CONFIG = {
    "timeout": 120000,
    "screenshot": True,
    "auto_login": True,
    "step_retry_count": 2,
}


def build_all_cases():
    """构建所有新模块用例"""
    cases = []

    # ==================== 功能中心抽屉用例 ====================

    # 用例1: 功能中心-打开抽屉验证
    cases.append({
        "name": "功能中心-打开抽屉验证",
        "description": "验证点击功能中心图标后抽屉正常打开，确认抽屉内包含常用功能和全部功能分区",
        "module_id": FUNC_CENTER_MODULE_ID,
        "priority": "high",
        "order": 100,
        "config": DEFAULT_CONFIG,
        "steps": [
            {"order": 1, "name": "等待首页加载", "action": "wait", "params": {"selector": ".block", "timeout": 15000}},
            {"order": 2, "name": "点击功能中心图标", "action": "click", "params": {"selector": ".home_nav_left"}},
            {"order": 3, "name": "等待抽屉打开", "action": "wait", "params": {"selector": ".el-drawer", "timeout": 10000}},
            {"order": 4, "name": "验证抽屉存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-drawer"}},
            {"order": 5, "name": "验证常用功能分区", "action": "assert", "params": {"type": "element_exists", "selector": ".item_title:has-text('常用功能')"}},
            {"order": 6, "name": "验证全部功能分区", "action": "assert", "params": {"type": "element_exists", "selector": ".item_title:has-text('全部功能')"}},
        ],
    })

    # 用例2: 功能中心-搜索功能验证
    cases.append({
        "name": "功能中心-搜索功能验证",
        "description": "验证功能中心抽屉内搜索框可输入文字并筛选功能列表",
        "module_id": FUNC_CENTER_MODULE_ID,
        "priority": "high",
        "order": 101,
        "config": DEFAULT_CONFIG,
        "steps": [
            {"order": 1, "name": "等待首页加载", "action": "wait", "params": {"selector": ".block", "timeout": 15000}},
            {"order": 2, "name": "点击功能中心图标", "action": "click", "params": {"selector": ".home_nav_left"}},
            {"order": 3, "name": "等待抽屉打开", "action": "wait", "params": {"selector": ".el-drawer", "timeout": 10000}},
            {"order": 4, "name": "点击搜索框", "action": "click", "params": {"selector": ".el-drawer input[placeholder='搜索功能']"}},
            {"order": 5, "name": "输入搜索关键词", "action": "fill", "params": {"selector": ".el-drawer input[placeholder='搜索功能']", "value": "告警"}},
            {"order": 6, "name": "等待搜索结果", "action": "wait", "params": {"selector": "body", "timeout": 3000}},
            {"order": 7, "name": "验证搜索结果存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-drawer"}},
        ],
    })

    # 用例3: 功能中心-关闭抽屉验证
    cases.append({
        "name": "功能中心-关闭抽屉验证",
        "description": "验证功能中心抽屉可通过关闭按钮正常关闭",
        "module_id": FUNC_CENTER_MODULE_ID,
        "priority": "medium",
        "order": 102,
        "config": DEFAULT_CONFIG,
        "steps": [
            {"order": 1, "name": "等待首页加载", "action": "wait", "params": {"selector": ".block", "timeout": 15000}},
            {"order": 2, "name": "点击功能中心图标", "action": "click", "params": {"selector": ".home_nav_left"}},
            {"order": 3, "name": "等待抽屉打开", "action": "wait", "params": {"selector": ".el-drawer", "timeout": 10000}},
            {"order": 4, "name": "再次点击关闭图标", "action": "click", "params": {"selector": ".home_nav_left.active_menu"}},
            {"order": 5, "name": "等待抽屉关闭", "action": "wait", "params": {"selector": "body", "timeout": 3000}},
            {"order": 6, "name": "验证首页菜单卡片可见", "action": "assert", "params": {"type": "element_exists", "selector": ".block"}},
        ],
    })

    # ==================== 会议运维用例 ====================

    # 用例4: 告警列表-页面访问验证
    cases.append({
        "name": "告警列表-页面访问验证",
        "description": "验证通过功能中心访问告警列表页面，确认表格和筛选器正常加载",
        "module_id": MEETING_OPS_MODULE_ID,
        "priority": "high",
        "order": 100,
        "config": DEFAULT_CONFIG,
        "steps": [
            {"order": 1, "name": "等待首页加载", "action": "wait", "params": {"selector": ".block", "timeout": 15000}},
            {"order": 2, "name": "点击功能中心图标", "action": "click", "params": {"selector": ".home_nav_left"}},
            {"order": 3, "name": "等待抽屉打开", "action": "wait", "params": {"selector": ".el-drawer", "timeout": 10000}},
            {"order": 4, "name": "点击告警列表", "action": "click", "params": {"selector": ".el-drawer >> text='告警列表'"}},
            {"order": 5, "name": "等待页面加载", "action": "wait", "params": {"selector": ".el-table", "timeout": 20000}},
            {"order": 6, "name": "等待页面渲染", "action": "wait", "params": {"selector": "body", "timeout": 3000}},
            {"order": 7, "name": "验证表格存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-table"}},
            {"order": 8, "name": "验证筛选器存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-select"}},
            {"order": 9, "name": "验证分页组件", "action": "assert", "params": {"type": "element_exists", "selector": ".el-pagination"}},
        ],
    })

    # 用例5: 视讯设备-页面访问验证
    cases.append({
        "name": "视讯设备-页面访问验证",
        "description": "验证通过功能中心访问视讯设备页面，确认表格和树形结构正常加载",
        "module_id": MEETING_OPS_MODULE_ID,
        "priority": "high",
        "order": 101,
        "config": DEFAULT_CONFIG,
        "steps": [
            {"order": 1, "name": "等待首页加载", "action": "wait", "params": {"selector": ".block", "timeout": 15000}},
            {"order": 2, "name": "点击功能中心图标", "action": "click", "params": {"selector": ".home_nav_left"}},
            {"order": 3, "name": "等待抽屉打开", "action": "wait", "params": {"selector": ".el-drawer", "timeout": 10000}},
            {"order": 4, "name": "点击视讯设备", "action": "click", "params": {"selector": ".el-drawer >> text='视讯设备'"}},
            {"order": 5, "name": "等待页面加载", "action": "wait", "params": {"selector": ".el-table", "timeout": 20000}},
            {"order": 6, "name": "等待页面渲染", "action": "wait", "params": {"selector": "body", "timeout": 3000}},
            {"order": 7, "name": "验证表格存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-table"}},
            {"order": 8, "name": "验证树形结构存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-tree"}},
        ],
    })

    # ==================== 资产管理用例 ====================

    # 用例6: 资产信息-页面访问验证
    cases.append({
        "name": "资产信息-页面访问验证",
        "description": "验证通过功能中心访问资产信息页面，确认表格和筛选器正常加载",
        "module_id": ASSET_MODULE_ID,
        "priority": "high",
        "order": 100,
        "config": DEFAULT_CONFIG,
        "steps": [
            {"order": 1, "name": "等待首页加载", "action": "wait", "params": {"selector": ".block", "timeout": 15000}},
            {"order": 2, "name": "点击功能中心图标", "action": "click", "params": {"selector": ".home_nav_left"}},
            {"order": 3, "name": "等待抽屉打开", "action": "wait", "params": {"selector": ".el-drawer", "timeout": 10000}},
            {"order": 4, "name": "点击资产信息", "action": "click", "params": {"selector": ".el-drawer >> text='资产信息'"}},
            {"order": 5, "name": "等待页面加载", "action": "wait", "params": {"selector": ".el-table", "timeout": 20000}},
            {"order": 6, "name": "等待页面渲染", "action": "wait", "params": {"selector": "body", "timeout": 3000}},
            {"order": 7, "name": "验证表格存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-table"}},
            {"order": 8, "name": "验证筛选器存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-select"}},
        ],
    })

    # ==================== 会务管理用例 ====================

    # 用例7: 会务统筹-页面访问验证
    cases.append({
        "name": "会务统筹-页面访问验证",
        "description": "验证通过功能中心访问会务统筹页面，确认表格和筛选器正常加载",
        "module_id": CONFERENCE_MODULE_ID,
        "priority": "high",
        "order": 100,
        "config": DEFAULT_CONFIG,
        "steps": [
            {"order": 1, "name": "等待首页加载", "action": "wait", "params": {"selector": ".block", "timeout": 15000}},
            {"order": 2, "name": "点击功能中心图标", "action": "click", "params": {"selector": ".home_nav_left"}},
            {"order": 3, "name": "等待抽屉打开", "action": "wait", "params": {"selector": ".el-drawer", "timeout": 10000}},
            {"order": 4, "name": "点击会务统筹", "action": "click", "params": {"selector": ".el-drawer >> text='会务统筹'"}},
            {"order": 5, "name": "等待页面加载", "action": "wait", "params": {"selector": ".el-table", "timeout": 20000}},
            {"order": 6, "name": "等待页面渲染", "action": "wait", "params": {"selector": "body", "timeout": 3000}},
            {"order": 7, "name": "验证表格存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-table"}},
        ],
    })

    # ==================== 维护工单用例 ====================

    # 用例8: 工单列表-页面访问验证
    cases.append({
        "name": "工单列表-页面访问验证",
        "description": "验证通过功能中心访问工单列表页面，确认表格、Tab和筛选器正常加载",
        "module_id": WORK_ORDER_MODULE_ID,
        "priority": "high",
        "order": 100,
        "config": DEFAULT_CONFIG,
        "steps": [
            {"order": 1, "name": "等待首页加载", "action": "wait", "params": {"selector": ".block", "timeout": 15000}},
            {"order": 2, "name": "点击功能中心图标", "action": "click", "params": {"selector": ".home_nav_left"}},
            {"order": 3, "name": "等待抽屉打开", "action": "wait", "params": {"selector": ".el-drawer", "timeout": 10000}},
            {"order": 4, "name": "点击工单列表", "action": "click", "params": {"selector": ".el-drawer >> text='工单列表'"}},
            {"order": 5, "name": "等待页面加载", "action": "wait", "params": {"selector": ".el-table", "timeout": 20000}},
            {"order": 6, "name": "等待页面渲染", "action": "wait", "params": {"selector": "body", "timeout": 3000}},
            {"order": 7, "name": "验证表格存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-table"}},
            {"order": 8, "name": "验证Tab页存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-tabs__item"}},
        ],
    })

    # 用例9: 通讯录-页面访问验证
    cases.append({
        "name": "通讯录-页面访问验证",
        "description": "验证通过功能中心访问通讯录页面，确认表格和树形结构正常加载",
        "module_id": WORK_ORDER_MODULE_ID,
        "priority": "medium",
        "order": 101,
        "config": DEFAULT_CONFIG,
        "steps": [
            {"order": 1, "name": "等待首页加载", "action": "wait", "params": {"selector": ".block", "timeout": 15000}},
            {"order": 2, "name": "点击功能中心图标", "action": "click", "params": {"selector": ".home_nav_left"}},
            {"order": 3, "name": "等待抽屉打开", "action": "wait", "params": {"selector": ".el-drawer", "timeout": 10000}},
            {"order": 4, "name": "点击通讯录", "action": "click", "params": {"selector": ".el-drawer >> text='通讯录'"}},
            {"order": 5, "name": "等待页面加载", "action": "wait", "params": {"selector": ".el-table", "timeout": 20000}},
            {"order": 6, "name": "等待页面渲染", "action": "wait", "params": {"selector": "body", "timeout": 3000}},
            {"order": 7, "name": "验证表格存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-table"}},
            {"order": 8, "name": "验证树形结构存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-tree"}},
        ],
    })

    # 用例10: 信息窗管理-页面访问验证
    cases.append({
        "name": "信息窗管理-页面访问验证",
        "description": "验证通过功能中心访问信息窗管理页面，确认表格正常加载",
        "module_id": WORK_ORDER_MODULE_ID,
        "priority": "medium",
        "order": 102,
        "config": DEFAULT_CONFIG,
        "steps": [
            {"order": 1, "name": "等待首页加载", "action": "wait", "params": {"selector": ".block", "timeout": 15000}},
            {"order": 2, "name": "点击功能中心图标", "action": "click", "params": {"selector": ".home_nav_left"}},
            {"order": 3, "name": "等待抽屉打开", "action": "wait", "params": {"selector": ".el-drawer", "timeout": 10000}},
            {"order": 4, "name": "点击信息窗管理", "action": "click", "params": {"selector": ".el-drawer >> text='信息窗管理'"}},
            {"order": 5, "name": "等待页面加载", "action": "wait", "params": {"selector": ".el-table", "timeout": 20000}},
            {"order": 6, "name": "等待页面渲染", "action": "wait", "params": {"selector": "body", "timeout": 3000}},
            {"order": 7, "name": "验证表格存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-table"}},
        ],
    })

    return cases


async def create_case(client: httpx.AsyncClient, case_data: dict):
    """创建单个测试用例"""
    try:
        response = await client.post(f"{BASE_URL}/api/cases", json=case_data, timeout=30.0)
        if response.status_code in (200, 201):
            result = response.json()
            print(f"  ✅ {case_data['name']} (ID: {result['id']})")
            return result
        else:
            print(f"  ❌ {case_data['name']} - {response.status_code}")
            return None
    except Exception as e:
        print(f"  ❌ {case_data['name']} - {e}")
        return None


async def main():
    print("=" * 60)
    print("补充功能中心/会议运维/资产管理/会务管理/维护工单用例")
    print("=" * 60)

    cases = build_all_cases()
    print(f"\n计划创建: {len(cases)} 个用例")

    async with httpx.AsyncClient() as client:
        for i, case in enumerate(cases, 1):
            print(f"\n[{i}/{len(cases)}] ", end="")
            await create_case(client, case)

    print("\n" + "=" * 60)
    print("✅ 用例创建完成")
    print("=" * 60)


if __name__ == "__main__":
    asyncio.run(main())
