<?php
ob_start();
$user_id = $_SESSION['user_id'];

// ========== 以下是正常页面显示 ==========
$app_id = isset($_GET['app_id']) ? intval($_GET['app_id']) : 0;
$page = isset($_GET['p']) ? intval($_GET['p']) : 1;
$perpage = 15;

// 获取应用列表用于筛选
$apps = $db->fetchAll("SELECT id, name FROM apps WHERE id IN (SELECT DISTINCT app_id FROM licenses WHERE user_id = $user_id)");

// 构建查询条件
$where = "user_id = $user_id";
if ($app_id > 0) {
    $where .= " AND app_id = $app_id";
}

// 获取总数
$total_result = $db->query("SELECT COUNT(*) as cnt FROM licenses WHERE $where");
$total = $total_result->fetch_assoc()['cnt'];
$offset = ($page - 1) * $perpage;

// 获取授权码列表
$licenses = $db->fetchAll("
    SELECT l.*, a.name as app_name 
    FROM licenses l 
    JOIN apps a ON l.app_id = a.id 
    WHERE $where 
    ORDER BY l.id DESC 
    LIMIT $offset, $perpage
");
?>

<div class="licenses-container">
    <!-- 页面头部 -->
    <div class="page-header">
        <div class="page-title-section">
            <h2>
                <img src="https://api.iconify.design/mdi:key.svg?color=%23667eea" width="26" height="26" style="vertical-align: middle; margin-right: 10px;" alt="">
                授权码管理
            </h2>
            <p class="page-desc">管理您的授权码，支持复制和绑定域名</p>
        </div>
        <a href="?page=buy_app" class="btn btn-primary">
            <img src="https://api.iconify.design/mdi:plus.svg?color=white" width="18" height="18" style="margin-right: 6px;" alt="">
            购买新授权
        </a>
    </div>
    
    <!-- 筛选栏 -->
    <div class="filter-bar">
        <div class="filter-left">
            <label>
                <img src="https://api.iconify.design/mdi:filter.svg?color=%236b7280" width="14" height="14" style="margin-right: 6px;" alt="">
                筛选应用：
            </label>
            <select id="appFilter" onchange="location.href='?page=licenses&app_id='+this.value">
                <option value="0">全部应用</option>
                <?php foreach ($apps as $app): ?>
                    <option value="<?php echo $app['id']; ?>" <?php echo $app_id == $app['id'] ? 'selected' : ''; ?>>
                        <?php echo htmlspecialchars($app['name']); ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="filter-right">
            <span class="total-count">
                <img src="https://api.iconify.design/mdi:key.svg?color=%23667eea" width="14" height="14" style="margin-right: 4px;" alt="">
                共 <?php echo $total; ?> 个授权码
            </span>
        </div>
    </div>
    
    <!-- 授权码列表 -->
    <div class="table-wrapper">
        <table class="data-table">
            <thead>
                <tr>
                    <th>授权码</th>
                    <th>应用名称</th>
                    <th>绑定域名</th>
                    <th>到期时间</th>
                    <th>状态</th>
                    <th>使用状态</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($licenses)): ?>
                    <tr>
                        <td colspan="7">
                            <div class="empty-state">
                                <img src="https://api.iconify.design/mdi:key-off.svg?color=%239ca3af" width="48" height="48" alt="">
                                <p>暂无授权码</p>
                                <a href="?page=buy_app" class="btn btn-primary btn-sm">
                                    <img src="https://api.iconify.design/mdi:cart.svg?color=white" width="14" height="14" style="margin-right: 4px;" alt="">
                                    购买授权
                                </a>
                            </div>
                        </td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($licenses as $lic): ?>
                        <tr id="license-row-<?php echo $lic['id']; ?>">
                            <td class="license-code">
                                <code><?php echo htmlspecialchars($lic['license_code']); ?></code>
                                <button class="copy-btn" onclick="copyToClipboard('<?php echo $lic['license_code']; ?>')" title="复制授权码">
                                    <img src="https://api.iconify.design/mdi:content-copy.svg?color=%239ca3af" width="16" height="16" alt="">
                                </button>
                            </td>
                            <td>
                                <span class="app-name">
                                    <img src="https://api.iconify.design/mdi:application.svg?color=%236b7280" width="14" height="14" alt="">
                                    <?php echo htmlspecialchars($lic['app_name']); ?>
                                </span>
                            </td>
                            <td class="domain-cell" id="domain-cell-<?php echo $lic['id']; ?>">
                                <?php if ($lic['domain']): ?>
                                    <span class="domain-badge">
                                        <img src="https://api.iconify.design/mdi:web.svg?color=%23667eea" width="12" height="12" alt="">
                                        <?php echo htmlspecialchars($lic['domain']); ?>
                                    </span>
                                <?php else: ?>
                                    <span class="domain-empty">
                                        <img src="https://api.iconify.design/mdi:minus-circle.svg?color=%239ca3af" width="12" height="12" alt="">
                                        未绑定
                                    </span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if ($lic['expire_date']): ?>
                                    <span class="expire-date">
                                        <img src="https://api.iconify.design/mdi:calendar.svg?color=%236b7280" width="12" height="12" alt="">
                                        <?php echo $lic['expire_date']; ?>
                                    </span>
                                <?php else: ?>
                                    <span class="expire-forever">
                                        <img src="https://api.iconify.design/mdi:infinity.svg?color=%2310b981" width="14" height="14" alt="">
                                        永久
                                    </span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <span class="status-badge status-active">
                                    <span class="status-dot"></span>
                                    有效
                                </span>
                            </td>
                            <td>
                                <?php if ($lic['used'] == 1): ?>
                                    <div class="used-info">
                                        <span class="status-badge status-used">
                                            <span class="status-dot"></span>
                                            已使用
                                        </span>
                                        <span class="used-domain">
                                            <img src="https://api.iconify.design/mdi:web.svg?color=%236b7280" width="10" height="10" alt="">
                                            <?php echo htmlspecialchars($lic['used_domain']); ?>
                                        </span>
                                    </div>
                                <?php else: ?>
                                    <span class="status-badge status-unused">
                                        <span class="status-dot"></span>
                                        未使用
                                    </span>
                                <?php endif; ?>
                            </td>
                            
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    
    <!-- 分页 -->
    <?php if ($total > $perpage): ?>
        <div class="pagination">
            <?php if ($page > 1): ?>
                <a href="?page=licenses&app_id=<?php echo $app_id; ?>&p=<?php echo $page - 1; ?>" class="page-link page-prev">
                    <img src="https://api.iconify.design/mdi:chevron-left.svg?color=currentColor" width="16" height="16" alt="">
                </a>
            <?php endif; ?>
            
            <?php
            $total_pages = ceil($total / $perpage);
            $start_page = max(1, $page - 2);
            $end_page = min($total_pages, $page + 2);
            
            if ($start_page > 1): ?>
                <a href="?page=licenses&app_id=<?php echo $app_id; ?>&p=1" class="page-link">1</a>
                <?php if ($start_page > 2): ?>
                    <span class="page-ellipsis">...</span>
                <?php endif; ?>
            <?php endif; ?>
            
            <?php for ($i = $start_page; $i <= $end_page; $i++): ?>
                <a href="?page=licenses&app_id=<?php echo $app_id; ?>&p=<?php echo $i; ?>" class="page-link <?php echo $i == $page ? 'active' : ''; ?>">
                    <?php echo $i; ?>
                </a>
            <?php endfor; ?>
            
            <?php if ($end_page < $total_pages): ?>
                <?php if ($end_page < $total_pages - 1): ?>
                    <span class="page-ellipsis">...</span>
                <?php endif; ?>
                <a href="?page=licenses&app_id=<?php echo $app_id; ?>&p=<?php echo $total_pages; ?>" class="page-link"><?php echo $total_pages; ?></a>
            <?php endif; ?>
            
            <?php if ($page < $total_pages): ?>
                <a href="?page=licenses&app_id=<?php echo $app_id; ?>&p=<?php echo $page + 1; ?>" class="page-link page-next">
                    <img src="https://api.iconify.design/mdi:chevron-right.svg?color=currentColor" width="16" height="16" alt="">
                </a>
            <?php endif; ?>
        </div>
    <?php endif; ?>
</div>

<!-- 绑定域名弹窗 -->
<div id="bindModal" class="modal" style="display: none;">
    <div class="modal-content">
        <div class="modal-header">
            <h3>
                <img src="https://api.iconify.design/mdi:web.svg?color=%23667eea" width="20" height="20" style="margin-right: 8px;" alt="">
                绑定域名
            </h3>
            <button type="button" class="modal-close" onclick="closeModal()">
                <img src="https://api.iconify.design/mdi:close.svg?color=%239ca3af" width="20" height="20" alt="">
            </button>
        </div>
        <form id="bindForm">
            <input type="hidden" name="license_id" id="bind_license_id">
            <div class="modal-body">
                <div class="form-group">
                    <label>
                        <img src="https://api.iconify.design/mdi:key.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                        授权码
                    </label>
                    <input type="text" id="bind_license_code" readonly class="readonly-input">
                </div>
                <div class="form-group">
                    <label>
                        <img src="https://api.iconify.design/mdi:web.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                        绑定域名
                    </label>
                    <input type="text" name="domain" id="bind_domain" placeholder="例如：example.com" required>
                    <small>
                        <img src="https://api.iconify.design/mdi:information.svg?color=%239ca3af" width="12" height="12" style="margin-right: 4px;" alt="">
                        支持域名格式，如：example.com 或 sub.example.com
                    </small>
                    <div class="warning-tip">
                        <img src="https://api.iconify.design/mdi:alert-circle.svg?color=%23f59e0b" width="14" height="14" style="margin-right: 6px;" alt="">
                        绑定后授权码将变为已使用状态，不可更改
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary cancel-btn" onclick="closeModal()">
                    <img src="https://api.iconify.design/mdi:close.svg?color=currentColor" width="16" height="16" style="margin-right: 4px;" alt="">
                    取消
                </button>
                <button type="submit" class="btn btn-primary">
                    <img src="https://api.iconify.design/mdi:content-save.svg?color=white" width="16" height="16" style="margin-right: 4px;" alt="">
                    保存绑定
                </button>
            </div>
        </form>
    </div>
</div>

<style>
/* 基础变量 */
:root {
    --primary-color: #667eea;
    --primary-hover: #5a67d8;
    --danger-color: #ef4444;
    --success-color: #10b981;
    --warning-color: #f59e0b;
    --bg-primary: #ffffff;
    --bg-secondary: #f9fafb;
    --text-primary: #1f2937;
    --text-secondary: #6b7280;
    --text-muted: #9ca3af;
    --border-color: #e5e7eb;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
}

.licenses-container {
    max-width: 100%;
    padding: 4px;
}

/* 页面头部 */
.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 28px;
    flex-wrap: wrap;
    gap: 16px;
}
.page-title-section h2 {
    display: flex;
    align-items: center;
    font-size: 24px;
    font-weight: 600;
    margin-bottom: 4px;
    color: var(--text-primary);
}
.page-desc {
    font-size: 14px;
    color: var(--text-secondary);
    margin-left: 36px;
}

/* 按钮 */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 20px;
    border-radius: var(--radius-md);
    font-size: 14px;
    font-weight: 500;
    text-decoration: none;
    cursor: pointer;
    transition: all 0.2s;
    border: 1px solid transparent;
}
.btn-primary {
    background: var(--primary-color);
    color: white;
    box-shadow: 0 2px 4px rgba(102, 126, 234, 0.2);
}
.btn-primary:hover {
    background: var(--primary-hover);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}
.btn-sm {
    padding: 8px 16px;
    font-size: 13px;
}
.btn-secondary {
    background: var(--bg-primary);
    color: var(--text-primary);
    border-color: var(--border-color);
}
.btn-secondary:hover {
    background: var(--bg-secondary);
}

/* 筛选栏 */
.filter-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
    flex-wrap: wrap;
    gap: 12px;
}
.filter-left {
    display: flex;
    align-items: center;
    gap: 12px;
}
.filter-left label {
    display: flex;
    align-items: center;
    font-size: 14px;
    color: var(--text-secondary);
}
.filter-left select {
    padding: 9px 16px;
    border: 1.5px solid var(--border-color);
    border-radius: var(--radius-md);
    background: var(--bg-primary);
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s;
}
.filter-left select:focus {
    outline: none;
    border-color: var(--primary-color);
}
.total-count {
    display: flex;
    align-items: center;
    font-size: 13px;
    color: var(--text-secondary);
    background: var(--bg-secondary);
    padding: 6px 14px;
    border-radius: 30px;
}

/* 表格 */
.table-wrapper {
    overflow-x: auto;
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-sm);
}
.data-table {
    width: 100%;
    border-collapse: collapse;
}
.data-table th {
    padding: 16px 16px;
    text-align: left;
    font-weight: 600;
    font-size: 13px;
    color: var(--text-secondary);
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    white-space: nowrap;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}
.data-table td {
    padding: 16px;
    border-bottom: 1px solid var(--border-color);
    font-size: 14px;
    vertical-align: middle;
}
.data-table tbody tr {
    transition: background 0.2s;
}
.data-table tbody tr:hover {
    background: var(--bg-secondary);
}
.data-table tbody tr:last-child td {
    border-bottom: none;
}

/* 单元格样式 */
.license-code {
    display: flex;
    align-items: center;
    gap: 8px;
}
.license-code code {
    font-family: 'Monaco', 'Menlo', 'Courier New', monospace;
    font-size: 13px;
    background: var(--bg-secondary);
    padding: 6px 10px;
    border-radius: var(--radius-sm);
    color: var(--primary-color);
    border: 1px solid var(--border-color);
}
.copy-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all 0.2s;
}
.copy-btn:hover {
    background: var(--bg-primary);
    border-color: var(--primary-color);
}
.app-name {
    display: flex;
    align-items: center;
    gap: 6px;
}
.domain-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: #e0e7ff;
    color: #4338ca;
    padding: 5px 12px;
    border-radius: 30px;
    font-size: 12px;
    font-weight: 500;
}
.domain-empty {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    color: var(--text-muted);
    font-size: 13px;
}
.expire-date {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    color: var(--text-secondary);
}
.expire-forever {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    color: var(--success-color);
}

/* 状态徽章 */
.status-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 5px 12px;
    border-radius: 30px;
    font-size: 12px;
    font-weight: 500;
    white-space: nowrap;
}
.status-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
}
.status-active {
    background: #d1fae5;
    color: #065f46;
}
.status-active .status-dot {
    background: #10b981;
}
.status-used {
    background: #fef3c7;
    color: #92400e;
}
.status-used .status-dot {
    background: #f59e0b;
}
.status-unused {
    background: #e0e7ff;
    color: #4338ca;
}
.status-unused .status-dot {
    background: #667eea;
}
.used-info {
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.used-domain {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 12px;
    color: var(--text-muted);
}

/* 操作按钮 */
.action-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 13px;
    color: var(--text-primary);
    transition: all 0.2s;
}
.action-btn:hover {
    background: var(--bg-primary);
    border-color: var(--primary-color);
    transform: translateY(-1px);
}
.disabled-action {
    color: var(--text-muted);
    font-size: 13px;
}

/* 空状态 */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 48px 20px;
    text-align: center;
}
.empty-state img {
    opacity: 0.4;
    margin-bottom: 12px;
}
.empty-state p {
    color: var(--text-muted);
    font-size: 14px;
    margin: 0 0 16px;
}

/* 分页 */
.pagination {
    display: flex;
    gap: 6px;
    justify-content: center;
    margin-top: 28px;
}
.page-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 38px;
    height: 38px;
    padding: 0 8px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    text-decoration: none;
    color: var(--text-primary);
    font-size: 14px;
    font-weight: 500;
    background: var(--bg-primary);
    transition: all 0.2s;
}
.page-link:hover {
    border-color: var(--primary-color);
    color: var(--primary-color);
    background: rgba(102, 126, 234, 0.05);
}
.page-link.active {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
}
.page-ellipsis {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 38px;
    height: 38px;
    color: var(--text-muted);
}

/* 弹窗 */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10000;
}
.modal-content {
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    width: 90%;
    max-width: 450px;
    box-shadow: var(--shadow-lg);
}
.modal-header {
    padding: 20px 24px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.modal-header h3 {
    display: flex;
    align-items: center;
    font-size: 18px;
    font-weight: 600;
    margin: 0;
}
.modal-close {
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px;
    border-radius: var(--radius-sm);
    transition: background 0.2s;
    display: flex;
}
.modal-close:hover {
    background: var(--bg-secondary);
}
.modal-body {
    padding: 24px;
}
.modal-footer {
    padding: 16px 24px 24px;
    display: flex;
    gap: 12px;
    justify-content: flex-end;
}
.form-group {
    margin-bottom: 20px;
}
.form-group label {
    display: flex;
    align-items: center;
    margin-bottom: 8px;
    font-weight: 500;
    font-size: 14px;
}
.form-group input {
    width: 100%;
    padding: 12px 16px;
    border: 1.5px solid var(--border-color);
    border-radius: var(--radius-md);
    font-size: 14px;
    background: var(--bg-primary);
    transition: all 0.2s;
}
.form-group input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}
.readonly-input {
    background: var(--bg-secondary) !important;
    cursor: not-allowed;
}
.form-group small {
    display: flex;
    align-items: center;
    margin-top: 8px;
    font-size: 12px;
    color: var(--text-muted);
}
.warning-tip {
    display: flex;
    align-items: center;
    margin-top: 12px;
    padding: 10px 14px;
    background: #fef3c7;
    border-radius: var(--radius-sm);
    font-size: 13px;
    color: #92400e;
}

/* 响应式 */
@media (max-width: 768px) {
    .page-header {
        flex-direction: column;
        align-items: flex-start;
    }
    .page-desc {
        margin-left: 0;
    }
    .filter-bar {
        flex-direction: column;
        align-items: flex-start;
    }
    .data-table th,
    .data-table td {
        padding: 12px;
    }
    .license-code {
        flex-wrap: wrap;
    }
    .action-btn {
        padding: 6px 12px;
    }
}
</style>

<script>
// 复制到剪贴板
function copyToClipboard(text) {
    navigator.clipboard?.writeText(text).then(() => {
        window.showToast && window.showToast('授权码已复制', 'success');
    }).catch(() => {
        const ta = document.createElement('textarea');
        ta.value = text;
        document.body.appendChild(ta);
        ta.select();
        document.execCommand('copy');
        document.body.removeChild(ta);
        window.showToast && window.showToast('授权码已复制', 'success');
    });
}

// 显示绑定弹窗
function showBindModal(id, code, currentDomain) {
    const modal = document.getElementById('bindModal');
    if (!modal) return;
    document.getElementById('bind_license_id').value = id;
    document.getElementById('bind_license_code').value = code;
    document.getElementById('bind_domain').value = currentDomain !== '未绑定' ? currentDomain : '';
    modal.style.display = 'flex';
}

// 关闭弹窗
function closeModal() {
    const modal = document.getElementById('bindModal');
    if (modal) modal.style.display = 'none';
}

document.addEventListener('DOMContentLoaded', function() {
    const modal = document.getElementById('bindModal');
    const bindForm = document.getElementById('bindForm');
    
    // 点击背景关闭
    if (modal) {
        modal.addEventListener('click', function(e) {
            if (e.target === modal) closeModal();
        });
    }
    
    // ESC 关闭
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Escape') closeModal();
    });
    
    // 表单提交
    if (bindForm) {
        bindForm.addEventListener('submit', function(e) {
            e.preventDefault();
            
            const licenseId = document.getElementById('bind_license_id').value;
            const domain = document.getElementById('bind_domain').value.trim();
            const submitBtn = this.querySelector('button[type="submit"]');
            
            if (!domain) {
                window.showToast && window.showToast('请输入域名', 'error');
                return;
            }
            
            const domainPattern = /^[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/;
            if (!domainPattern.test(domain)) {
                window.showToast && window.showToast('域名格式不正确', 'error');
                return;
            }
            
            const originalHtml = submitBtn.innerHTML;
            submitBtn.innerHTML = '<span class="loading-spinner"></span> 保存中...';
            submitBtn.disabled = true;
            
            fetch('/api/bind_domain.php', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: 'license_id=' + encodeURIComponent(licenseId) + '&domain=' + encodeURIComponent(domain)
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    window.showToast && window.showToast('域名绑定成功', 'success');
                    setTimeout(() => location.reload(), 1000);
                } else {
                    window.showToast && window.showToast(data.message || '绑定失败', 'error');
                    submitBtn.innerHTML = originalHtml;
                    submitBtn.disabled = false;
                }
                closeModal();
            })
            .catch(error => {
                console.error('绑定错误:', error);
                window.showToast && window.showToast('网络错误', 'error');
                submitBtn.innerHTML = originalHtml;
                submitBtn.disabled = false;
                closeModal();
            });
        });
    }
});

// 加载动画样式
const style = document.createElement('style');
style.textContent = `
    .loading-spinner {
        display: inline-block;
        width: 16px;
        height: 16px;
        border: 2px solid rgba(255, 255, 255, 0.3);
        border-radius: 50%;
        border-top-color: white;
        animation: spin 0.6s linear infinite;
        margin-right: 8px;
    }
    @keyframes spin {
        to { transform: rotate(360deg); }
    }
`;
document.head.appendChild(style);
</script>