#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
修复新面板的 ID 命名，使其与 checkLoginForPanel 函数的期望一致
"""

PORTAL = r"D:\AI-Ad-Agent\portal\portal.html"

with open(PORTAL, 'r', encoding='utf-8') as f:
    content = f.read()

count = 0

# ====== 素材检索面板：matsearch-* → material-search-* ======
replacements = [
    # HTML IDs
    ('id="matsearch-login-required"', 'id="material-search-login-required"'),
    ('id="matsearch-content"', 'id="material-search-content"'),
    ('id="mat-prompt"', 'id="material-search-prompt"'),
    ('id="mat-industry"', 'id="material-search-industry"'),
    ('id="mat-type"', 'id="material-search-type"'),
    ('id="mat-color"', 'id="material-search-color"'),
    ('id="mat-other"', 'id="material-search-other"'),
    ('id="mat-search-btn"', 'id="material-search-btn"'),
    ('id="mat-loading"', 'id="material-search-loading"'),
    ('id="mat-results-card"', 'id="material-search-results-card"'),
    ('id="mat-copy-list"', 'id="material-search-copy-list"'),
    ('id="mat-materials-card"', 'id="material-search-materials-card"'),
    ('id="mat-material-grid"', 'id="material-search-material-grid"'),
    ('id="mat-material-empty"', 'id="material-search-material-empty"'),
    # JS references
    ('getElementById(\'mat-prompt\')', 'getElementById(\'material-search-prompt\')'),
    ('getElementById(\'mat-industry\')', 'getElementById(\'material-search-industry\')'),
    ('getElementById(\'mat-type\')', 'getElementById(\'material-search-type\')'),
    ('getElementById(\'mat-color\')', 'getElementById(\'material-search-color\')'),
    ('getElementById(\'mat-other\')', 'getElementById(\'material-search-other\')'),
    ('getElementById(\'mat-loading\')', 'getElementById(\'material-search-loading\')'),
    ('getElementById(\'mat-results-card\')', 'getElementById(\'material-search-results-card\')'),
    ('getElementById(\'mat-search-btn\')', 'getElementById(\'material-search-btn\')'),
    ('getElementById(\'mat-copy-list\')', 'getElementById(\'material-search-copy-list\')'),
    ('getElementById(\'mat-materials-card\')', 'getElementById(\'material-search-materials-card\')'),
    ('getElementById(\'mat-material-grid\')', 'getElementById(\'material-search-material-grid\')'),
    ('getElementById(\'mat-material-empty\')', 'getElementById(\'material-search-material-empty\')'),
]

for old, new in replacements:
    if old in content:
        content = content.replace(old, new)
        count += 1
        print(f"  ✅ {old} → {new}")

print(f"素材检索面板：已替换 {count} 处")

# ====== 案例推荐面板：case-* → case-recommend-* ======
count = 0
replacements2 = [
    # HTML IDs
    ('id="case-login-required"', 'id="case-recommend-login-required"'),
    ('id="case-content"', 'id="case-recommend-content"'),
    ('id="case-prompt"', 'id="case-recommend-prompt"'),
    ('id="case-industry"', 'id="case-recommend-industry"'),
    ('id="case-dimensions"', 'id="case-recommend-dimensions"'),
    ('id="case-platform"', 'id="case-recommend-platform"'),
    ('id="case-search-btn"', 'id="case-recommend-search-btn"'),
    ('id="case-loading"', 'id="case-recommend-loading"'),
    ('id="case-results-card"', 'id="case-recommend-results-card"'),
    ('id="case-grid"', 'id="case-recommend-grid"'),
    ('id="case-empty"', 'id="case-recommend-empty"'),
    # JS references
    ('getElementById(\'case-prompt\')', 'getElementById(\'case-recommend-prompt\')'),
    ('getElementById(\'case-industry\')', 'getElementById(\'case-recommend-industry\')'),
    ('getElementById(\'case-dimensions\')', 'getElementById(\'case-recommend-dimensions\')'),
    ('getElementById(\'case-platform\')', 'getElementById(\'case-recommend-platform\')'),
    ('getElementById(\'case-loading\')', 'getElementById(\'case-recommend-loading\')'),
    ('getElementById(\'case-results-card\')', 'getElementById(\'case-recommend-results-card\')'),
    ('getElementById(\'case-search-btn\')', 'getElementById(\'case-recommend-search-btn\')'),
    ('getElementById(\'case-grid\')', 'getElementById(\'case-recommend-grid\')'),
    ('getElementById(\'case-empty\')', 'getElementById(\'case-recommend-empty\')'),
]

for old, new in replacements2:
    if old in content:
        content = content.replace(old, new)
        count += 1
        print(f"  ✅ {old} → {new}")

print(f"案例推荐面板：已替换 {count} 处")

# ====== 账户搭建面板：acct-* → account-setup-* ======
count = 0
replacements3 = [
    # HTML IDs
    ('id="acct-login-required"', 'id="account-setup-login-required"'),
    ('id="acct-content"', 'id="account-setup-content"'),
    ('id="acct-prompt"', 'id="account-setup-prompt"'),
    ('id="acct-industry"', 'id="account-setup-industry"'),
    ('id="acct-platform"', 'id="account-setup-platform"'),
    ('id="acct-budget"', 'id="account-setup-budget"'),
    ('id="acct-age-range"', 'id="account-setup-age-range"'),
    ('id="acct-generate-btn"', 'id="account-setup-generate-btn"'),
    ('id="acct-loading"', 'id="account-setup-loading"'),
    ('id="acct-error-msg"', 'id="account-setup-error-msg"'),
    ('id="acct-result-card"', 'id="account-setup-result-card"'),
    ('id="acct-flow-steps"', 'id="account-setup-flow-steps"'),
    ('id="acct-copy-preview"', 'id="account-setup-copy-preview"'),
    ('id="acct-material-plan"', 'id="account-setup-material-plan"'),
    ('id="acct-budget-grid"', 'id="account-setup-budget-grid"'),
    ('id="acct-audience-tags"', 'id="account-setup-audience-tags"'),
    # JS references
    ('getElementById(\'acct-prompt\')', 'getElementById(\'account-setup-prompt\')'),
    ('getElementById(\'acct-industry\')', 'getElementById(\'account-setup-industry\')'),
    ('getElementById(\'acct-platform\')', 'getElementById(\'account-setup-platform\')'),
    ('getElementById(\'acct-budget\')', 'getElementById(\'account-setup-budget\')'),
    ('getElementById(\'acct-age-range\')', 'getElementById(\'account-setup-age-range\')'),
    ('getElementById(\'acct-loading\')', 'getElementById(\'account-setup-loading\')'),
    ('getElementById(\'acct-error-msg\')', 'getElementById(\'account-setup-error-msg\')'),
    ('getElementById(\'acct-result-card\')', 'getElementById(\'account-setup-result-card\')'),
    ('getElementById(\'acct-generate-btn\')', 'getElementById(\'account-setup-generate-btn\')'),
    ('getElementById(\'acct-flow-steps\')', 'getElementById(\'account-setup-flow-steps\')'),
    ('getElementById(\'acct-copy-preview\')', 'getElementById(\'account-setup-copy-preview\')'),
    ('getElementById(\'acct-material-plan\')', 'getElementById(\'account-setup-material-plan\')'),
    ('getElementById(\'acct-budget-grid\')', 'getElementById(\'account-setup-budget-grid\')'),
    ('getElementById(\'acct-audience-tags\')', 'getElementById(\'account-setup-audience-tags\')'),
]

for old, new in replacements3:
    if old in content:
        content = content.replace(old, new)
        count += 1
        print(f"  ✅ {old} → {new}")

print(f"账户搭建面板：已替换 {count} 处")

# 写回文件
with open(PORTAL, 'w', encoding='utf-8') as f:
    f.write(content)

print("\n🎉 ID 修复完成！")
