<?php
require_once __DIR__ . '/../../includes/LicenseClient.php';

try {
    $client = new LicenseClient();
    $result = $client->getResources();
    $resources = $result['resources'] ?? [];
} catch (Exception $e) {
    $resources = [];
    $error = $e->getMessage();
}
?>

<?php if (isset($error)): ?>
<div class="card" style="background: #fef2f2; color: #991b1b;">
    <img src="https://api.iconify.design/mdi:alert-circle.svg?color=%23ef4444" width="20" height="20" alt="">
    <?php echo htmlspecialchars($error); ?>
</div>
<?php endif; ?>

<?php foreach ($resources as $res): ?>
<div class="card">
    <div style="display: flex; gap: 12px;">
        <img src="https://api.iconify.design/mdi:package-variant.svg?color=%23667eea" width="40" height="40" alt="">
        <div style="flex:1;">
            <div style="font-weight: 600;"><?php echo htmlspecialchars($res['title']); ?></div>
            <div style="font-size: 12px; color: #999;"><?php echo htmlspecialchars($res['file_size']); ?></div>
        </div>
       <a href="/api/download_resource.php?id=<?php echo $res['id']; ?>" 
   class="btn btn-primary" 
   style="padding: 8px 16px;">
    <img src="https://api.iconify.design/mdi:download.svg?color=white" width="18" height="18" alt="">
    下载
</a>
    </div>
    <?php if ($res['description']): ?>
    <p style="font-size: 13px; color: #666; margin-top: 10px;"><?php echo htmlspecialchars($res['description']); ?></p>
    <?php endif; ?>
    <div style="display: flex; gap: 16px; margin-top: 8px; font-size: 12px; color: #999;">
        <?php if ($res['version']): ?>
        <span><img src="https://api.iconify.design/mdi:tag.svg?color=%23999" width="12" height="12" alt=""> v<?php echo htmlspecialchars($res['version']); ?></span>
        <?php endif; ?>
        <span><img src="https://api.iconify.design/mdi:download.svg?color=%23999" width="12" height="12" alt=""> <?php echo number_format($res['download_count']); ?></span>
    </div>
</div>
<?php endforeach; ?>

<?php if (empty($resources)): ?>
<div class="card" style="text-align: center; padding: 40px;">
    <img src="https://api.iconify.design/mdi:package-variant-closed.svg?color=%23999" width="48" height="48" alt="">
    <p style="color: #999;">暂无资源</p>
</div>
<?php endif; ?>