<?php
$user_id = $_SESSION['user_id'];
$licenses = $db->fetchAll("
    SELECT l.*, a.name as app_name 
    FROM licenses l 
    JOIN apps a ON l.app_id = a.id 
    WHERE l.user_id = $user_id 
    ORDER BY l.id DESC 
    LIMIT 30
");
?>

<?php foreach ($licenses as $lic): ?>
<div class="card">
    <div style="display: flex; justify-content: space-between; margin-bottom: 8px;">
        <span style="font-weight: 600;"><?php echo htmlspecialchars($lic['app_name']); ?></span>
        <span style="padding: 4px 10px; border-radius: 20px; font-size: 12px;
                     <?php echo $lic['status'] ? 'background:#d1fae5;color:#065f46;' : 'background:#fee2e2;color:#991b1b;'; ?>">
            <?php echo $lic['status'] ? '有效' : '失效'; ?>
        </span>
    </div>
    <code style="display: block; background: #f5f5f5; padding: 8px; border-radius: 6px; margin-bottom: 8px; font-size: 13px;">
        <?php echo htmlspecialchars($lic['license_code']); ?>
    </code>
    <div style="display: flex; gap: 16px; font-size: 12px; color: #666;">
        <span><img src="https://api.iconify.design/mdi:web.svg?color=%23666" width="12" height="12" alt=""> <?php echo $lic['domain'] ?: '未绑定'; ?></span>
        <span><img src="https://api.iconify.design/mdi:calendar.svg?color=%23666" width="12" height="12" alt=""> <?php echo $lic['expire_date'] ?: '永久'; ?></span>
    </div>
    <div style="margin-top: 10px;">
        <button onclick="copyToClipboard('<?php echo $lic['license_code']; ?>')" style="padding: 6px 12px; background: #f5f5f5; border: none; border-radius: 6px;">
            <img src="https://api.iconify.design/mdi:content-copy.svg?color=%23666" width="14" height="14" alt=""> 复制
        </button>
    </div>
</div>
<?php endforeach; ?>

<?php if (empty($licenses)): ?>
<div class="card" style="text-align: center; padding: 40px;">
    <img src="https://api.iconify.design/mdi:key-off.svg?color=%23999" width="48" height="48" alt="">
    <p style="color: #999;">暂无授权码</p>
    <a href="?page=buy_app" class="btn btn-primary" style="margin-top: 16px; text-decoration: none;">购买授权</a>
</div>
<?php endif; ?>

<script>
function copyToClipboard(text) {
    navigator.clipboard?.writeText(text).then(() => showToast('已复制'));
}
</script>