<?php
ob_start();
requireAdmin();

$user = currentUser();
$message = '';
$error = '';

// 批量删除卡密
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'batch_delete') {
    $ids = $_POST['ids'] ?? [];
    if (!empty($ids)) {
        $id_list = implode(',', array_map('intval', $ids));
        // 先查询要删除的数量
        $count_result = $db->fetchOne("SELECT COUNT(*) as cnt FROM card_codes WHERE id IN ($id_list) AND status = 0");
        $deleted = $count_result['cnt'] ?? 0;
        // 执行删除
        $db->query("DELETE FROM card_codes WHERE id IN ($id_list) AND status = 0");
        $message = "成功删除 {$deleted} 张卡密";
    }
}

// 生成卡密
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'generate') {
    $amount = floatval($_POST['amount']);
    $quantity = intval($_POST['quantity']);
    $expire_days = intval($_POST['expire_days']);
    
    if ($amount <= 0) {
        $error = '面额必须大于0';
    } elseif ($quantity <= 0 || $quantity > 500) {
        $error = '数量必须在1-500之间';
    } else {
        $expire_at = $expire_days > 0 ? date('Y-m-d H:i:s', strtotime("+{$expire_days} days")) : 'NULL';
        $success_count = 0;
        
        for ($i = 0; $i < $quantity; $i++) {
            $card_code = strtoupper(substr(md5(uniqid(mt_rand(), true)), 0, 16));
            $card_code = substr($card_code, 0, 4) . '-' . substr($card_code, 4, 4) . '-' . substr($card_code, 8, 4) . '-' . substr($card_code, 12, 4);
            
            $sql = "INSERT INTO card_codes (card_code, amount, created_by, expire_at) 
                    VALUES ('$card_code', $amount, {$user['id']}, " . ($expire_at === 'NULL' ? 'NULL' : "'$expire_at'") . ")";
            if ($db->query($sql)) {
                $success_count++;
            }
        }
        
        $message = "成功生成 {$success_count} 张卡密，面额 ¥{$amount}";
    }
}

// 删除单张卡密
if (isset($_GET['delete'])) {
    $id = intval($_GET['delete']);
    $db->query("DELETE FROM card_codes WHERE id = $id AND status = 0");
    $message = '删除成功';
}

// 获取卡密列表
$page = isset($_GET['p']) ? intval($_GET['p']) : 1;
$perpage = 20;
$offset = ($page - 1) * $perpage;

$status_filter = isset($_GET['status']) ? intval($_GET['status']) : -1;
$status_sql = $status_filter >= 0 ? "AND status = $status_filter" : "";

$total = $db->fetchOne("SELECT COUNT(*) as cnt FROM card_codes WHERE 1=1 $status_sql")['cnt'];
$cards = $db->fetchAll("SELECT * FROM card_codes WHERE 1=1 $status_sql ORDER BY id DESC LIMIT $offset, $perpage");

// 统计数据
$stats = [
    'total' => $db->fetchOne("SELECT COUNT(*) as cnt FROM card_codes")['cnt'],
    'unused' => $db->fetchOne("SELECT COUNT(*) as cnt FROM card_codes WHERE status = 0")['cnt'],
    'used' => $db->fetchOne("SELECT COUNT(*) as cnt FROM card_codes WHERE status = 1")['cnt'],
    'total_amount' => $db->fetchOne("SELECT COALESCE(SUM(amount), 0) as total FROM card_codes WHERE status = 1")['total'] ?? 0
];
?>

<div class="card-manager">
    <!-- 页面头部 -->
    <div class="page-header">
        <h2>
            <img src="https://api.iconify.design/mdi:ticket-confirmation.svg?color=%23667eea" width="26" height="26" style="vertical-align: middle; margin-right: 10px;" alt="">
            卡密充值管理
        </h2>
    </div>
    
    <!-- 统计卡片 -->
    <div class="stats-grid">
        <div class="stat-card">
            <div class="stat-icon">
                <img src="https://api.iconify.design/mdi:ticket.svg?color=%23667eea" width="28" height="28" alt="">
            </div>
            <div class="stat-content">
                <span class="stat-value"><?php echo $stats['total']; ?></span>
                <span class="stat-label">总卡密数</span>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon">
                <img src="https://api.iconify.design/mdi:ticket-outline.svg?color=%23f59e0b" width="28" height="28" alt="">
            </div>
            <div class="stat-content">
                <span class="stat-value"><?php echo $stats['unused']; ?></span>
                <span class="stat-label">未使用</span>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon">
                <img src="https://api.iconify.design/mdi:check-circle.svg?color=%2310b981" width="28" height="28" alt="">
            </div>
            <div class="stat-content">
                <span class="stat-value"><?php echo $stats['used']; ?></span>
                <span class="stat-label">已使用</span>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon">
                <img src="https://api.iconify.design/mdi:cash-multiple.svg?color=%238b5cf6" width="28" height="28" alt="">
            </div>
            <div class="stat-content">
                <span class="stat-value">¥<?php echo number_format($stats['total_amount'], 2); ?></span>
                <span class="stat-label">已充值金额</span>
            </div>
        </div>
    </div>
    
    <!-- 提示消息 -->
    <?php if ($message): ?>
        <div class="alert alert-success">
            <img src="https://api.iconify.design/mdi:check-circle.svg?color=%2310b981" width="20" height="20" style="margin-right: 8px;" alt="">
            <?php echo $message; ?>
        </div>
    <?php endif; ?>
    <?php if ($error): ?>
        <div class="alert alert-error">
            <img src="https://api.iconify.design/mdi:alert-circle.svg?color=%23ef4444" width="20" height="20" style="margin-right: 8px;" alt="">
            <?php echo $error; ?>
        </div>
    <?php endif; ?>
    
    <!-- 生成卡密卡片 -->
    <div class="generate-card">
        <div class="generate-header">
            <h3>
                <img src="https://api.iconify.design/mdi:plus-circle.svg?color=%23667eea" width="20" height="20" style="vertical-align: middle; margin-right: 8px;" alt="">
                生成卡密
            </h3>
            <span class="generate-tip">批量生成充值卡密，可设置面额和有效期</span>
        </div>
        <form method="POST" class="generate-form">
            <input type="hidden" name="action" value="generate">
            <div class="form-row-inline">
                <div class="form-group-inline">
                    <label>
                        <img src="https://api.iconify.design/mdi:cash.svg?color=%2310b981" width="16" height="16" style="margin-right: 4px;" alt="">
                        面额 (元)
                    </label>
                    <div class="input-with-prefix">
                        <span class="prefix">¥</span>
                        <input type="number" name="amount" step="0.01" min="0.01" placeholder="0.00" required>
                    </div>
                </div>
                <div class="form-group-inline">
                    <label>
                        <img src="https://api.iconify.design/mdi:numeric.svg?color=%23667eea" width="16" height="16" style="margin-right: 4px;" alt="">
                        数量
                    </label>
                    <input type="number" name="quantity" min="1" max="500" value="10" required>
                </div>
                <div class="form-group-inline">
                    <label>
                        <img src="https://api.iconify.design/mdi:calendar-clock.svg?color=%23f59e0b" width="16" height="16" style="margin-right: 4px;" alt="">
                        有效期
                    </label>
                    <select name="expire_days">
                        <option value="0">永久有效</option>
                        <option value="30">30 天</option>
                        <option value="90">90 天</option>
                        <option value="180">180 天</option>
                        <option value="365">365 天</option>
                    </select>
                </div>
                <div class="form-group-inline form-submit">
                    <button type="submit" class="btn btn-primary">
                        <img src="https://api.iconify.design/mdi:ticket.svg?color=white" width="16" height="16" style="margin-right: 6px;" alt="">
                        生成卡密
                    </button>
                </div>
            </div>
        </form>
    </div>
    
    <!-- 筛选标签和批量操作 -->
    <div class="filter-section">
        <div class="filter-tabs">
            <a href="admin.php?page=card_codes&status=-1" class="filter-tab <?php echo $status_filter == -1 ? 'active' : ''; ?>">
                <img src="https://api.iconify.design/mdi:ticket.svg?color=<?php echo $status_filter == -1 ? 'white' : '%236b7280'; ?>" width="16" height="16" style="margin-right: 6px;" alt="">
                全部
                <span class="count"><?php echo $stats['total']; ?></span>
            </a>
            <a href="admin.php?page=card_codes&status=0" class="filter-tab <?php echo $status_filter == 0 ? 'active' : ''; ?>">
                <img src="https://api.iconify.design/mdi:ticket-outline.svg?color=<?php echo $status_filter == 0 ? 'white' : '%236b7280'; ?>" width="16" height="16" style="margin-right: 6px;" alt="">
                未使用
                <span class="count"><?php echo $stats['unused']; ?></span>
            </a>
            <a href="admin.php?page=card_codes&status=1" class="filter-tab <?php echo $status_filter == 1 ? 'active' : ''; ?>">
                <img src="https://api.iconify.design/mdi:check-circle.svg?color=<?php echo $status_filter == 1 ? 'white' : '%236b7280'; ?>" width="16" height="16" style="margin-right: 6px;" alt="">
                已使用
                <span class="count"><?php echo $stats['used']; ?></span>
            </a>
        </div>
        
        <!-- 批量操作栏 -->
        <div class="batch-actions" id="batchActions" style="display: none;">
            <span class="selected-count">已选择 <span id="selectedCount">0</span> 张卡密</span>
            <button type="button" class="btn btn-danger btn-sm" id="batchDeleteBtn" onclick="batchDelete()">
                <img src="https://api.iconify.design/mdi:delete.svg?color=white" width="14" height="14" style="margin-right: 4px;" alt="">
                批量删除
            </button>
            <button type="button" class="btn btn-secondary btn-sm" onclick="clearSelection()">
                <img src="https://api.iconify.design/mdi:close.svg?color=currentColor" width="14" height="14" style="margin-right: 4px;" alt="">
                取消选择
            </button>
        </div>
    </div>
    
    <!-- 卡密列表 -->
    <form id="batchForm" method="POST">
        <input type="hidden" name="action" value="batch_delete">
        <div class="table-wrapper">
            <table class="data-table" id="cardTable">
                <thead>
                    <tr>
                        <th style="width: 4%;">
                            <input type="checkbox" id="selectAll" class="checkbox" onchange="toggleSelectAll(this)">
                        </th>
                        <th style="width: 6%;">ID</th>
                        <th style="width: 22%;">卡密</th>
                        <th style="width: 8%;">面额</th>
                        <th style="width: 8%;">状态</th>
                        <th style="width: 12%;">使用者</th>
                        <th style="width: 14%;">使用时间</th>
                        <th style="width: 12%;">过期时间</th>
                        <th style="width: 6%;">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($cards as $card): ?>
                    <tr id="row-<?php echo $card['id']; ?>">
                        <td>
                            <?php if ($card['status'] == 0): ?>
                                <input type="checkbox" name="ids[]" value="<?php echo $card['id']; ?>" class="checkbox card-checkbox" onchange="updateSelectedCount()">
                            <?php else: ?>
                                <input type="checkbox" disabled class="checkbox">
                            <?php endif; ?>
                        </td>
                        <td class="id-cell">#<?php echo $card['id']; ?></td>
                        <td>
                            <div class="card-code-wrapper">
                                <code class="card-code"><?php echo $card['card_code']; ?></code>
                                <button type="button" class="copy-btn" data-code="<?php echo $card['card_code']; ?>" title="复制卡密">
                                    <img src="https://api.iconify.design/mdi:content-copy.svg?color=%239ca3af" width="14" height="14" alt="">
                                </button>
                            </div>
                        </td>
                        <td><span class="amount">¥<?php echo number_format($card['amount'], 2); ?></span></td>
                        <td>
                            <?php if ($card['status'] == 1): ?>
                                <span class="status-badge status-used">
                                    <span class="status-dot"></span>已使用
                                </span>
                            <?php else: ?>
                                <span class="status-badge status-unused">
                                    <span class="status-dot"></span>未使用
                                </span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <?php if ($card['used_by']): ?>
                                <div class="user-info">
                                    <img src="https://api.iconify.design/mdi:account.svg?color=%236b7280" width="14" height="14" alt="">
                                    <?php echo htmlspecialchars($card['used_by']); ?>
                                </div>
                            <?php else: ?>
                                <span class="text-muted">-</span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <?php if ($card['used_at']): ?>
                                <span class="datetime"><?php echo date('Y-m-d H:i', strtotime($card['used_at'])); ?></span>
                            <?php else: ?>
                                <span class="text-muted">-</span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <?php if ($card['expire_at']): ?>
                                <?php 
                                    $expire_time = strtotime($card['expire_at']);
                                    $is_expired = $expire_time < time();
                                ?>
                                <span class="expire-date <?php echo $is_expired ? 'expired' : ''; ?>">
                                    <img src="https://api.iconify.design/mdi:calendar.svg?color=<?php echo $is_expired ? '%23ef4444' : '%236b7280'; ?>" width="12" height="12" alt="">
                                    <?php echo date('Y-m-d', $expire_time); ?>
                                </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>
                            <?php if ($card['status'] == 0): ?>
                                <a href="?page=card_codes&delete=<?php echo $card['id']; ?>&status=<?php echo $status_filter; ?>&p=<?php echo $page; ?>" class="delete-btn" onclick="return confirm('确定删除此卡密吗？')" title="删除">
                                    <img src="https://api.iconify.design/mdi:delete.svg?color=%23ef4444" width="16" height="16" alt="">
                                </a>
                            <?php else: ?>
                                <span class="action-disabled">-</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    <?php if (empty($cards)): ?>
                        <tr>
                            <td colspan="9">
                                <div class="empty-state">
                                    <img src="https://api.iconify.design/mdi:ticket-outline.svg?color=%239ca3af" width="48" height="48" alt="">
                                    <p>暂无卡密数据</p>
                                    <span>点击上方生成卡密开始创建</span>
                                </div>
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </form>
    
    <!-- 分页 -->
    <?php if ($total > $perpage): ?>
    <div class="pagination">
        <?php if ($page > 1): ?>
            <a href="?page=card_codes&p=<?php echo $page - 1; ?>&status=<?php echo $status_filter; ?>" 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=card_codes&p=1&status=<?php echo $status_filter; ?>" 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=card_codes&p=<?php echo $i; ?>&status=<?php echo $status_filter; ?>" 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=card_codes&p=<?php echo $total_pages; ?>&status=<?php echo $status_filter; ?>" class="page-link"><?php echo $total_pages; ?></a>
        <?php endif; ?>
        
        <?php if ($page < $total_pages): ?>
            <a href="?page=card_codes&p=<?php echo $page + 1; ?>&status=<?php echo $status_filter; ?>" 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>

<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);
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
}

.card-manager {
    max-width: 100%;
    padding: 4px;
}

/* 页面头部 */
.page-header {
    margin-bottom: 24px;
}
.page-header h2 {
    display: flex;
    align-items: center;
    font-size: 24px;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0;
}

/* 统计卡片 */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 20px;
    margin-bottom: 24px;
}
.stat-card {
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    padding: 20px;
    border: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 16px;
    transition: all 0.2s;
    box-shadow: var(--shadow-sm);
}
.stat-card:hover {
    box-shadow: var(--shadow-md);
    transform: translateY(-2px);
}
.stat-icon {
    width: 52px;
    height: 52px;
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(102, 126, 234, 0.05) 100%);
}
.stat-content {
    display: flex;
    flex-direction: column;
}
.stat-value {
    font-size: 26px;
    font-weight: 700;
    color: var(--text-primary);
    line-height: 1.2;
}
.stat-label {
    font-size: 13px;
    color: var(--text-muted);
    margin-top: 4px;
}

/* 提示消息 */
.alert {
    display: flex;
    align-items: center;
    padding: 14px 20px;
    border-radius: var(--radius-md);
    margin-bottom: 24px;
    font-size: 14px;
}
.alert-success {
    background: #d1fae5;
    color: #065f46;
    border: 1px solid #a7f3d0;
}
.alert-error {
    background: #fee2e2;
    color: #991b1b;
    border: 1px solid #fecaca;
}

/* 生成卡密卡片 */
.generate-card {
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    border: 1px solid var(--border-color);
    padding: 24px;
    margin-bottom: 24px;
    box-shadow: var(--shadow-sm);
}
.generate-header {
    margin-bottom: 20px;
}
.generate-header h3 {
    display: flex;
    align-items: center;
    font-size: 18px;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0 0 6px;
}
.generate-tip {
    font-size: 13px;
    color: var(--text-muted);
}
.generate-form {
    margin-top: 16px;
}
.form-row-inline {
    display: flex;
    gap: 20px;
    flex-wrap: wrap;
    align-items: flex-end;
}
.form-group-inline {
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.form-group-inline label {
    display: flex;
    align-items: center;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
}
.form-group-inline input,
.form-group-inline select {
    padding: 11px 16px;
    border: 1.5px solid var(--border-color);
    border-radius: var(--radius-sm);
    font-size: 14px;
    background: var(--bg-primary);
    color: var(--text-primary);
    transition: all 0.2s;
    min-width: 140px;
}
.form-group-inline input:focus,
.form-group-inline select:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}
.input-with-prefix {
    display: flex;
    align-items: center;
    border: 1.5px solid var(--border-color);
    border-radius: var(--radius-sm);
    overflow: hidden;
    transition: all 0.2s;
    background: var(--bg-primary);
}
.input-with-prefix:focus-within {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}
.input-with-prefix .prefix {
    padding: 11px 12px;
    background: var(--bg-secondary);
    color: var(--text-secondary);
    font-weight: 500;
    border-right: 1px solid var(--border-color);
}
.input-with-prefix input {
    flex: 1;
    border: none !important;
    border-radius: 0 !important;
    padding: 11px 16px;
    min-width: 100px;
}
.input-with-prefix input:focus {
    outline: none;
    box-shadow: none;
}
.form-submit {
    margin-left: auto;
}

/* 按钮 */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 11px 24px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 500;
    text-decoration: none;
    cursor: pointer;
    transition: all 0.2s;
    border: 1px solid transparent;
    gap: 6px;
}
.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(-1px);
    box-shadow: 0 4px 8px rgba(102, 126, 234, 0.3);
}
.btn-danger {
    background: var(--danger-color);
    color: white;
}
.btn-danger:hover {
    background: #dc2626;
    transform: translateY(-1px);
}
.btn-secondary {
    background: var(--bg-primary);
    color: var(--text-primary);
    border-color: var(--border-color);
}
.btn-secondary:hover {
    background: var(--bg-secondary);
    border-color: var(--text-muted);
}
.btn-sm {
    padding: 8px 16px;
    font-size: 13px;
}

/* 筛选区域 */
.filter-section {
    margin-bottom: 20px;
}
.filter-tabs {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}
.filter-tab {
    display: inline-flex;
    align-items: center;
    padding: 9px 18px;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 30px;
    font-size: 14px;
    font-weight: 500;
    color: var(--text-secondary);
    text-decoration: none;
    transition: all 0.2s;
}
.filter-tab:hover {
    border-color: var(--primary-color);
    color: var(--primary-color);
    background: rgba(102, 126, 234, 0.05);
}
.filter-tab.active {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
}
.filter-tab .count {
    margin-left: 6px;
    padding: 2px 8px;
    background: rgba(0, 0, 0, 0.1);
    border-radius: 20px;
    font-size: 12px;
}
.filter-tab.active .count {
    background: rgba(255, 255, 255, 0.2);
}

/* 批量操作栏 */
.batch-actions {
    display: flex;
    align-items: center;
    gap: 16px;
    margin-top: 16px;
    padding: 12px 16px;
    background: var(--bg-secondary);
    border-radius: var(--radius-md);
    border: 1px solid var(--border-color);
}
.selected-count {
    font-size: 14px;
    color: var(--text-secondary);
}
.selected-count span {
    font-weight: 600;
    color: var(--primary-color);
}

/* 复选框 */
.checkbox {
    width: 18px;
    height: 18px;
    cursor: pointer;
    accent-color: var(--primary-color);
    border-radius: 4px;
}
.checkbox:disabled {
    cursor: not-allowed;
    opacity: 0.4;
}

/* 表格 */
.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;
    min-width: 1100px;
}
.data-table th {
    padding: 16px 12px;
    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 12px;
    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;
}

/* 单元格样式 */
.id-cell {
    font-family: 'Monaco', 'Menlo', monospace;
    font-size: 12px;
    color: var(--text-muted);
    font-weight: 500;
}
.card-code-wrapper {
    display: flex;
    align-items: center;
    gap: 8px;
}
.card-code {
    font-family: 'Monaco', 'Menlo', 'Courier New', monospace;
    font-size: 13px;
    background: var(--bg-secondary);
    padding: 6px 12px;
    border-radius: var(--radius-sm);
    color: var(--primary-color);
    border: 1px solid var(--border-color);
    letter-spacing: 0.5px;
}
.copy-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all 0.2s;
    opacity: 0;
}
tr:hover .copy-btn {
    opacity: 1;
}
.copy-btn:hover {
    background: var(--bg-primary);
    border-color: var(--primary-color);
}
.amount {
    font-weight: 600;
    color: var(--success-color);
    font-size: 15px;
}

/* 状态徽章 */
.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-used {
    background: #d1fae5;
    color: #065f46;
}
.status-used .status-dot {
    background: #10b981;
    box-shadow: 0 0 0 2px rgba(16, 185, 129, 0.2);
}
.status-unused {
    background: #fef3c7;
    color: #92400e;
}
.status-unused .status-dot {
    background: #f59e0b;
    box-shadow: 0 0 0 2px rgba(245, 158, 11, 0.2);
}

/* 用户信息 */
.user-info {
    display: flex;
    align-items: center;
    gap: 6px;
    color: var(--text-secondary);
}
.datetime {
    font-size: 13px;
    color: var(--text-secondary);
}
.expire-date {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: var(--text-secondary);
}
.expire-date.expired {
    color: var(--danger-color);
}
.expire-forever {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: var(--success-color);
}
.text-muted {
    color: var(--text-muted);
}

/* 删除按钮 */
.delete-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    transition: all 0.2s;
}
.delete-btn:hover {
    background: #fef2f2;
    border-color: var(--danger-color);
    transform: translateY(-1px);
}
.action-disabled {
    color: var(--text-muted);
}

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

/* 分页 */
.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);
}

/* 响应式 */
@media (max-width: 1024px) {
    .stats-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}
@media (max-width: 768px) {
    .stats-grid {
        grid-template-columns: 1fr;
    }
    .form-row-inline {
        flex-direction: column;
        align-items: stretch;
    }
    .form-submit {
        margin-left: 0;
    }
    .form-group-inline input,
    .form-group-inline select,
    .input-with-prefix {
        width: 100%;
    }
    .filter-tabs {
        flex-wrap: wrap;
    }
    .batch-actions {
        flex-wrap: wrap;
    }
}
</style>

<script>
// 复制卡密功能
document.querySelectorAll('.copy-btn').forEach(btn => {
    btn.addEventListener('click', function() {
        const code = this.getAttribute('data-code');
        navigator.clipboard?.writeText(code).then(() => {
            this.innerHTML = '<img src="https://api.iconify.design/mdi:check.svg?color=%2310b981" width="14" height="14" alt="">';
            setTimeout(() => {
                this.innerHTML = '<img src="https://api.iconify.design/mdi:content-copy.svg?color=%239ca3af" width="14" height="14" alt="">';
            }, 1500);
        }).catch(() => {
            const textarea = document.createElement('textarea');
            textarea.value = code;
            document.body.appendChild(textarea);
            textarea.select();
            document.execCommand('copy');
            document.body.removeChild(textarea);
            this.innerHTML = '<img src="https://api.iconify.design/mdi:check.svg?color=%2310b981" width="14" height="14" alt="">';
            setTimeout(() => {
                this.innerHTML = '<img src="https://api.iconify.design/mdi:content-copy.svg?color=%239ca3af" width="14" height="14" alt="">';
            }, 1500);
        });
    });
});

// 全选/取消全选
function toggleSelectAll(checkbox) {
    const checkboxes = document.querySelectorAll('.card-checkbox:not(:disabled)');
    checkboxes.forEach(cb => cb.checked = checkbox.checked);
    updateSelectedCount();
}

// 更新选中数量
function updateSelectedCount() {
    const checked = document.querySelectorAll('.card-checkbox:checked').length;
    document.getElementById('selectedCount').textContent = checked;
    document.getElementById('batchActions').style.display = checked > 0 ? 'flex' : 'none';
    
    // 更新全选状态
    const allCheckboxes = document.querySelectorAll('.card-checkbox:not(:disabled)');
    const checkedCheckboxes = document.querySelectorAll('.card-checkbox:checked');
    document.getElementById('selectAll').checked = allCheckboxes.length > 0 && allCheckboxes.length === checkedCheckboxes.length;
}

// 清除选择
function clearSelection() {
    document.querySelectorAll('.card-checkbox').forEach(cb => cb.checked = false);
    document.getElementById('selectAll').checked = false;
    updateSelectedCount();
}

// 批量删除
function batchDelete() {
    const checked = document.querySelectorAll('.card-checkbox:checked');
    if (checked.length === 0) return;
    
    if (!confirm('确定要删除选中的 ' + checked.length + ' 张卡密吗？此操作不可恢复。')) return;
    
    document.getElementById('batchForm').submit();
}

// 监听复选框变化
document.addEventListener('DOMContentLoaded', function() {
    document.querySelectorAll('.card-checkbox').forEach(cb => {
        cb.addEventListener('change', updateSelectedCount);
    });
    updateSelectedCount();
});
</script>