<?php
/**
 * IBVTE Admin - Admit Cards Management
 */
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';

if (!checkDBConnection()) exit;
Auth()->requireRole(['super_admin', 'admin']);

$db = DB();
$flash = getFlashMessage();

// Fetch all active students for admit card generation
$students = $db->fetchAll("
    SELECT s.*, c.course_name, c.course_code, u.institution_name, u.institution_code 
    FROM students s 
    LEFT JOIN courses c ON s.course_id = c.id 
    LEFT JOIN users u ON s.institution_id = u.id 
    WHERE s.status = 'active'
    ORDER BY s.created_at DESC
");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Admit Cards - <?= APP_SHORT_NAME ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="../assets/css/admin-style.css" rel="stylesheet">
</head>
<body>
    <?php include __DIR__ . '/../includes/admin_header.php'; ?>
    <div class="container-fluid">
        <div class="row">
            <?php include __DIR__ . '/../includes/admin_sidebar.php'; ?>
            
            <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4 py-4">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h2 class="h3"><i class="fas fa-id-card-alt me-2 text-primary"></i>Student Admit Cards</h2>
                </div>

                <?php if ($flash): ?>
                    <div class="alert alert-<?= $flash['type'] ?> alert-dismissible fade show shadow-sm">
                        <?= $flash['message'] ?>
                        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                    </div>
                <?php endif; ?>

                <div class="alert alert-info border-0 shadow-sm rounded-4 mb-4 d-flex align-items-center">
                    <i class="fas fa-info-circle fa-2x me-3"></i>
                    <div>
                        <h6 class="fw-bold mb-1">Hall Ticket / Admit Card Generation</h6>
                        <p class="mb-0 small">Only students with an 'Active' admission status are eligible for Admit Card generation. Click 'Generate/View' to print the official document.</p>
                    </div>
                </div>

                <div class="card shadow-sm border-0" style="border-radius:15px;">
                    <div class="card-body p-0">
                        <div class="table-responsive">
                            <table class="table table-hover align-middle mb-0">
                                <thead class="bg-light">
                                    <tr>
                                        <th class="ps-4">Student Name & ID</th>
                                        <th>Course / Program</th>
                                        <th>Institution</th>
                                        <th>Session</th>
                                        <th class="text-end pe-4">Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php if(empty($students)): ?>
                                        <tr><td colspan="5" class="text-center py-5 text-muted">No active students found for admit cards.</td></tr>
                                    <?php endif; ?>
                                    
                                    <?php foreach ($students as $s): ?>
                                    <tr>
                                        <td class="ps-4">
                                            <div class="d-flex align-items-center">
                                                <div class="bg-primary bg-opacity-10 text-primary rounded-circle d-flex align-items-center justify-content-center" style="width: 40px; height: 40px;">
                                                    <i class="fas fa-user"></i>
                                                </div>
                                                <div class="ms-3">
                                                    <div class="fw-bold fs-6"><?= htmlspecialchars($s['full_name']) ?></div>
                                                    <div class="small text-muted">Reg: <?= htmlspecialchars($s['registration_number'] ?: 'N/A') ?></div>
                                                </div>
                                            </div>
                                        </td>
                                        <td>
                                            <div class="fw-bold text-dark"><?= htmlspecialchars($s['course_name'] ?? 'N/A') ?></div>
                                            <div class="small text-muted">Code: <?= htmlspecialchars($s['course_code'] ?? 'N/A') ?></div>
                                        </td>
                                        <td>
                                            <div class="fw-bold text-primary"><?= htmlspecialchars($s['institution_name'] ?? 'Direct') ?></div>
                                            <div class="small text-muted">Code: <?= htmlspecialchars($s['institution_code'] ?? 'N/A') ?></div>
                                        </td>
                                        <td><?= htmlspecialchars($s['session_year'] ?? date('Y')) ?></td>
                                        <td class="text-end pe-4">
                                            <a href="print-admit-card.php?id=<?= $s['id'] ?>" class="btn btn-sm btn-primary px-3 rounded-pill shadow-sm" target="_blank">
                                                <i class="fas fa-print me-1"></i> Generate
                                            </a>
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </main>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>