<?php
/**
 * IBVTE - Student Downloads Section
 */

require_once 'config.php';

// Dummy downloads data
$downloads = [
    ['title' => 'December 2024 Examination Results', 'session' => '2024-25', 'category' => 'results', 'type' => 'PDF', 'size' => '2.5 MB', 'date' => '2025-01-15', 'views' => 1250],
    ['title' => 'January 2025 Examination Time Table', 'session' => '2024-25', 'category' => 'timetable', 'type' => 'PDF', 'size' => '850 KB', 'date' => '2024-12-20', 'views' => 890],
    ['title' => 'Admit Card - Annual Examination 2025', 'session' => '2024-25', 'category' => 'admit_card', 'type' => 'PDF', 'size' => '1.2 MB', 'date' => '2025-01-05', 'views' => 2100],
    ['title' => 'Sample Question Paper - Computer Diploma', 'session' => '2024-25', 'category' => 'sample_paper', 'type' => 'PDF', 'size' => '1.8 MB', 'date' => '2024-11-10', 'views' => 650],
    ['title' => 'Syllabus - All Courses 2024-25', 'session' => '2024-25', 'category' => 'syllabus', 'type' => 'PDF', 'size' => '5.2 MB', 'date' => '2024-07-01', 'views' => 3200],
    ['title' => 'Admission Form 2025-26', 'session' => '2025-26', 'category' => 'forms', 'type' => 'PDF', 'size' => '450 KB', 'date' => '2025-01-20', 'views' => 780],
    ['title' => 'Examination Guidelines', 'session' => 'All', 'category' => 'guidelines', 'type' => 'PDF', 'size' => '1.1 MB', 'date' => '2024-08-15', 'views' => 1500],
    ['title' => 'Affiliation Application Form', 'session' => '2024-25', 'category' => 'forms', 'type' => 'PDF', 'size' => '680 KB', 'date' => '2024-06-01', 'views' => 420],
];

$categoryLabels = [
    'results' => ['label' => 'Results', 'icon' => 'fa-trophy', 'color' => 'success'],
    'timetable' => ['label' => 'Time Table', 'icon' => 'fa-clock', 'color' => 'warning'],
    'admit_card' => ['label' => 'Admit Cards', 'icon' => 'fa-id-card', 'color' => 'info'],
    'sample_paper' => ['label' => 'Sample Papers', 'icon' => 'fa-file-alt', 'color' => 'secondary'],
    'syllabus' => ['label' => 'Syllabus', 'icon' => 'fa-book', 'color' => 'primary'],
    'forms' => ['label' => 'Forms', 'icon' => 'fa-file-signature', 'color' => 'dark'],
    'guidelines' => ['label' => 'Guidelines', 'icon' => 'fa-info-circle', 'color' => 'info'],
];

$filter = $_GET['category'] ?? '';
$search = $_GET['search'] ?? '';

// Filter downloads
$filteredDownloads = array_filter($downloads, function($d) use ($filter, $search) {
    if ($filter && $d['category'] !== $filter) return false;
    if ($search && stripos($d['title'], $search) === false) return false;
    return true;
});

include 'includes/header.php';
?>

<section class="py-5" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);">
    <div class="container">
        <div class="text-center text-white">
            <h1 class="fw-bold mb-3"><i class="fas fa-download me-2"></i>Student Downloads</h1>
            <p class="lead mb-0">Access all important documents, results, and resources</p>
        </div>
    </div>
</section>

<section class="py-5">
    <div class="container">
        <!-- Search & Filter -->
        <div class="row mb-4">
            <div class="col-md-6">
                <form method="GET" class="d-flex gap-2">
                    <select name="category" class="form-select" onchange="this.form.submit()">
                        <option value="">All Categories</option>
                        <?php foreach ($categoryLabels as $key => $info): ?>
                            <option value="<?= $key ?>" <?= $filter === $key ? 'selected' : '' ?>>
                                <?= $info['label'] ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                    <?php if ($filter): ?>
                        <a href="downloads.php" class="btn btn-outline-secondary">Clear</a>
                    <?php endif; ?>
                </form>
            </div>
            <div class="col-md-6">
                <form method="GET" class="d-flex gap-2">
                    <input type="hidden" name="category" value="<?= $filter ?>">
                    <input type="text" name="search" class="form-control" placeholder="Search documents..." value="<?= htmlspecialchars($search) ?>">
                    <button type="submit" class="btn btn-primary"><i class="fas fa-search"></i></button>
                </form>
            </div>
        </div>
        
        <!-- Downloads Table -->
        <div class="card border-0 shadow-lg">
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0">
                        <thead class="table-light">
                            <tr>
                                <th class="ps-4">Document Title</th>
                                <th>Category</th>
                                <th>Session</th>
                                <th>Type</th>
                                <th>Size</th>
                                <th>Date</th>
                                <th class="pe-4">Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (empty($filteredDownloads)): ?>
                                <tr>
                                    <td colspan="7" class="text-center py-5">
                                        <i class="fas fa-folder-open fa-3x text-muted mb-3"></i>
                                        <p class="text-muted">No documents found</p>
                                    </td>
                                </tr>
                            <?php else: ?>
                                <?php foreach ($filteredDownloads as $download): ?>
                                    <?php $catInfo = $categoryLabels[$download['category']] ?? ['label' => 'Other', 'icon' => 'fa-file', 'color' => 'secondary']; ?>
                                    <tr>
                                        <td class="ps-4">
                                            <div class="d-flex align-items-center">
                                                <div class="bg-<?= $catInfo['color'] ?> bg-opacity-10 text-<?= $catInfo['color'] ?> rounded p-2 me-3">
                                                    <i class="fas <?= $catInfo['icon'] ?> fa-lg"></i>
                                                </div>
                                                <div>
                                                    <h6 class="mb-0 fw-bold"><?= htmlspecialchars($download['title']) ?></h6>
                                                    <small class="text-muted"><i class="fas fa-eye me-1"></i><?= number_format($download['views']) ?> views</small>
                                                </div>
                                            </div>
                                        </td>
                                        <td>
                                            <span class="badge bg-<?= $catInfo['color'] ?> bg-opacity-10 text-<?= $catInfo['color'] ?>">
                                                <?= $catInfo['label'] ?>
                                            </span>
                                        </td>
                                        <td><?= htmlspecialchars($download['session']) ?></td>
                                        <td><span class="badge bg-secondary"><?= $download['type'] ?></span></td>
                                        <td><?= $download['size'] ?></td>
                                        <td><?= formatDate($download['date']) ?></td>
                                        <td class="pe-4">
                                            <a href="#" class="btn btn-primary btn-sm rounded-pill" onclick="downloadFile('<?= $download['title'] ?>')">
                                                <i class="fas fa-download me-1"></i>Download
                                            </a>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        
        <!-- Category Cards -->
        <div class="row mt-5">
            <div class="col-12 mb-4">
                <h4 class="fw-bold text-center">Browse by Category</h4>
            </div>
            <?php foreach ($categoryLabels as $key => $info): ?>
                <div class="col-md-3 mb-3">
                    <a href="?category=<?= $key ?>" class="text-decoration-none">
                        <div class="card h-100 border-0 shadow-sm text-center p-4 hover-card">
                            <div class="mx-auto mb-3 bg-<?= $info['color'] ?> bg-opacity-10 text-<?= $info['color'] ?> rounded-circle d-flex align-items-center justify-content-center" style="width: 70px; height: 70px;">
                                <i class="fas <?= $info['icon'] ?> fa-2x"></i>
                            </div>
                            <h5 class="mb-0"><?= $info['label'] ?></h5>
                            <small class="text-muted">
                                <?= count(array_filter($downloads, fn($d) => $d['category'] === $key)) ?> files
                            </small>
                        </div>
                    </a>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
</section>

<script>
    function downloadFile(title) {
        // Simulate download
        alert('Downloading: ' + title + '\n\nIn production, this will download the actual file.');
    }
</script>

<style>
    .hover-card {
        transition: all 0.3s ease;
    }
    .hover-card:hover {
        transform: translateY(-5px);
        box-shadow: 0 10px 30px rgba(0,0,0,0.15) !important;
    }
</style>

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