<?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.approval_status = 'approved'
    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 'Approved' 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">
                                            <div class="btn-group shadow-sm">
                                                <a href="print-admit-card.php?id=<?= $s['id'] ?>" class="btn btn-sm btn-primary px-3" target="_blank" title="Generate PDF" onclick="showNotification('<?= htmlspecialchars(addslashes($s['full_name'])) ?>')">
                                                    <i class="fas fa-print"></i> Generate
                                                </a>
                                                <a href="edit-admit-card-full.php?id=<?= $s['id'] ?>" class="btn btn-sm btn-outline-primary px-3" title="Edit Details">
                                                    <i class="fas fa-edit"></i> Edit
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </main>
        </div>
    </div>
    
    <!-- Toast Notification Container -->
    <div class="toast-container position-fixed top-0 end-0 p-4 mt-2" style="z-index: 1055;"></div>

    <script>
    function showNotification(studentName) {
        let container = document.querySelector('.toast-container');
        let toastEl = document.createElement('div');
        toastEl.className = 'toast align-items-center text-white bg-success border-0 shadow-lg rounded-3 mb-2';
        toastEl.setAttribute('role', 'alert');
        toastEl.setAttribute('aria-live', 'assertive');
        toastEl.setAttribute('aria-atomic', 'true');
        
        toastEl.innerHTML = `
            <div class="d-flex p-1">
                <div class="toast-body fw-bold fs-6">
                    <i class="fas fa-check-circle me-2 fs-5"></i> Admit Card successfully generating for ${studentName}...
                </div>
                <button type="button" class="btn-close btn-close-white me-3 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
            </div>
        `;
        
        container.appendChild(toastEl);
        let toast = new bootstrap.Toast(toastEl, { delay: 4000 });
        toast.show();
        
        toastEl.addEventListener('hidden.bs.toast', () => {
            toastEl.remove();
        });
    }
    </script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>