<?php
/**
 * User Downloads Page
 * Digital Marketplace
 */

require_once __DIR__ . '/../config/database.php';

require_login();

$page_title = 'My Downloads';

$user_id = get_current_user_id();

// Get downloads
$stmt = $pdo->prepare("SELECT oi.*, o.order_number, o.payment_status, p.file_path, p.download_url, p.slug 
                      FROM order_items oi 
                      JOIN orders o ON oi.order_id = o.id 
                      LEFT JOIN products p ON oi.product_id = p.id 
                      WHERE o.user_id = ? AND o.payment_status = 'completed' 
                      ORDER BY o.created_at DESC");
$stmt->execute([$user_id]);
$downloads = $stmt->fetchAll();

include __DIR__ . '/../includes/header.php';
?>

<section class="section">
    <div class="container">
        <div class="d-flex justify-content-between align-items-center mb-4" data-aos="fade-up">
            <h1><i class="fas fa-download me-2"></i>My Downloads</h1>
            <a href="<?php echo BASE_URL; ?>/products.php" class="btn btn-primary">
                <i class="fas fa-shopping-bag me-2"></i>Shop More
            </a>
        </div>
        
        <?php if (empty($downloads)): ?>
            <div class="card" data-aos="fade-up">
                <div class="card-body text-center py-5">
                    <i class="fas fa-download fa-4x text-muted mb-4"></i>
                    <h3>No downloads available</h3>
                    <p class="text-muted mb-4">You don't have any downloadable products yet.</p>
                    <a href="<?php echo BASE_URL; ?>/products.php" class="btn btn-primary">
                        <i class="fas fa-shopping-bag me-2"></i>Start Shopping
                    </a>
                </div>
            </div>
        <?php else: ?>
            <div class="row g-4">
                <?php foreach ($downloads as $download): ?>
                <div class="col-md-6 col-lg-4" data-aos="fade-up">
                    <div class="card download-card h-100">
                        <div class="card-body">
                            <div class="d-flex align-items-start mb-3">
                                <div class="download-icon me-3">
                                    <i class="fas fa-file-archive"></i>
                                </div>
                                <div class="flex-grow-1">
                                    <h5 class="card-title mb-1"><?php echo sanitize($download['product_name']); ?></h5>
                                    <small class="text-muted">Order: <?php echo $download['order_number']; ?></small>
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <div class="d-flex justify-content-between align-items-center mb-2">
                                    <small class="text-muted">Downloads</small>
                                    <span class="badge bg-<?php echo $download['download_count'] >= $download['download_limit'] ? 'danger' : 'success'; ?>">
                                        <?php echo $download['download_count']; ?>/<?php echo $download['download_limit']; ?>
                                    </span>
                                </div>
                                <div class="progress" style="height: 6px;">
                                    <div class="progress-bar bg-<?php echo $download['download_count'] >= $download['download_limit'] ? 'danger' : 'success'; ?>" 
                                         role="progressbar" 
                                         style="width: <?php echo ($download['download_count'] / $download['download_limit']) * 100; ?>%">
                                    </div>
                                </div>
                            </div>
                            
                            <div class="d-grid gap-2">
                                <?php if ($download['download_count'] < $download['download_limit']): ?>
                                    <a href="<?php echo BASE_URL; ?>/download.php?item=<?php echo $download['id']; ?>" 
                                       class="btn btn-primary">
                                        <i class="fas fa-download me-2"></i>Download Now
                                    </a>
                                <?php else: ?>
                                    <button class="btn btn-secondary" disabled>
                                        <i class="fas fa-ban me-2"></i>Download Limit Reached
                                    </button>
                                <?php endif; ?>
                                
                                <?php if ($download['slug']): ?>
                                    <a href="<?php echo BASE_URL; ?>/product.php?slug=<?php echo $download['slug']; ?>" 
                                       class="btn btn-outline-light btn-sm">
                                        <i class="fas fa-eye me-2"></i>View Product
                                    </a>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</section>

<style>
.download-card {
    border: 1px solid var(--dark-lighter);
    background: var(--gradient-card);
    transition: var(--transition);
}

.download-card:hover {
    border-color: var(--primary);
    transform: translateY(-5px);
}

.download-icon {
    width: 50px;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--gradient-primary);
    border-radius: var(--radius);
    color: var(--white);
    font-size: 1.25rem;
}
</style>

<?php include __DIR__ . '/../includes/footer.php'; ?>
