<?php
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';

Auth()->requireRole(['college', 'admin', 'super_admin']);
$db = DB();

$id = intval($_GET['id'] ?? 0);
// Fetch student with course and college details
$s = $db->fetchOne("
    SELECT s.*, c.course_name, c.course_code, 
           u.institution_name, u.institution_code, u.email as college_email, u.mobile as college_mobile,
           u.address as college_address, u.profile_image as college_logo
    FROM students s 
    JOIN courses c ON s.course_id = c.id 
    JOIN users u ON s.institution_id = u.id
    WHERE s.id = ?
", [$id]);

if (!$s) {
    die("Student not found.");
}

// Fetch subjects for the course
$subjects = $db->fetchAll("SELECT * FROM subjects WHERE course_id = ? ORDER BY type, subject_name", [$s['course_id']]);

// Helper to generate exam dates (semi-dynamic for demo)
$baseDate = strtotime('next monday');
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admit Card - <?= htmlspecialchars($s['full_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">
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;700;900&display=swap');
        
        body { background: #f4f4f4; font-family: 'Roboto', sans-serif; color: #333; }
        
        .admit-card-container {
            width: 210mm;
            min-height: 297mm;
            padding: 15mm;
            margin: 20px auto;
            background: white;
            box-shadow: 0 0 20px rgba(0,0,0,0.1);
            border: 2px solid #333;
            position: relative;
        }
        
        .header-section {
            border-bottom: 2px solid #333;
            padding-bottom: 15px;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
        }
        
        .college-logo { width: 100px; height: 100px; object-fit: contain; }
        .college-info { flex: 1; text-align: center; }
        .college-name { font-size: 24px; font-weight: 900; color: #000; text-transform: uppercase; margin: 0; }
        .college-details { font-size: 13px; margin: 2px 0; font-weight: 400; }
        
        .title-bar {
            background: #e0e0e0;
            text-align: center;
            padding: 10px;
            border: 1px solid #333;
            margin-bottom: 20px;
        }
        .title-bar h3 { margin: 0; font-weight: 700; font-size: 18px; text-transform: uppercase; }
        .title-bar p { margin: 0; font-size: 14px; }
        
        .student-info-grid {
            display: grid;
            grid-template-columns: 1fr 1fr 140px;
            border: 1px solid #333;
            margin-bottom: 20px;
        }
        
        .info-cell { padding: 10px; border: 0.5px solid #333; }
        .info-label { font-size: 12px; color: #555; text-transform: uppercase; display: block; font-weight: 700; }
        .info-value { font-size: 15px; font-weight: 700; color: #000; }
        
        .photo-cell {
            grid-row: span 2;
            display: flex;
            align-items: center;
            justify-content: center;
            border-left: 1px solid #333;
            background: #f9f9f9;
        }
        .student-photo { width: 120px; height: 140px; object-fit: cover; border: 1px solid #ccc; }
        
        .exam-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 25px;
        }
        .exam-table th, .exam-table td {
            border: 1px solid #333;
            padding: 10px;
            text-align: center;
            font-size: 13px;
        }
        .exam-table th { background: #444; color: white; text-transform: uppercase; }
        .exam-table tr:nth-child(even) { background: #f2f2f2; }
        
        .instructions-box {
            border: 1px solid #333;
            padding: 15px;
            margin-bottom: 30px;
        }
        .instructions-box h5 {
            text-align: center;
            font-weight: 900;
            text-decoration: underline;
            margin-bottom: 10px;
            font-size: 16px;
        }
        .instructions-list { font-size: 11.5px; padding-left: 20px; margin: 0; line-height: 1.5; }
        
        .footer-grid {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 10px;
            margin-top: 40px;
        }
        .sign-box {
            text-align: center;
            border-top: 1px dashed #333;
            padding-top: 5px;
            font-size: 12px;
            font-weight: 700;
        }
        
        .stamp-area {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%) rotate(-30deg);
            opacity: 0.05;
            z-index: 0;
            pointer-events: none;
        }
        .stamp-area img { width: 450px; }

        @media print {
            body { background: none; padding: 0; }
            .admit-card-container { margin: 0; box-shadow: none; border: 2px solid #000; }
            .btn-print { display: none; }
            @page { size: A4; margin: 0; }
            .stamp-area { opacity: 0.07; } /* Slightly more visible on print */
        }
    </style>
</head>
<body>

<div class="text-center mt-4 mb-4">
    <button onclick="window.print()" class="btn btn-dark btn-lg px-5 shadow btn-print">
        <i class="fas fa-print me-2"></i>Print Admit Card
    </button>
</div>

<div class="admit-card-container">
    <div class="stamp-area">
        <img src="../assets/images/logo.jpeg" alt="Watermark">
    </div>
    <!-- Header -->
    <div class="header-section">
        <img src="../<?= $s['college_logo'] ?: 'assets/images/logo.jpeg' ?>" class="college-logo" alt="Logo">
        <div class="college-info">
            <h1 class="college-name"><?= htmlspecialchars($s['institution_name']) ?></h1>
            <p class="college-details">Affiliation No - <?= htmlspecialchars($s['institution_code']) ?></p>
            <p class="college-details"><?= htmlspecialchars($s['college_address']) ?></p>
            <p class="college-details">Mobile: <?= htmlspecialchars($s['college_mobile']) ?> | Email: <?= htmlspecialchars($s['college_email']) ?></p>
        </div>
    </div>

    <!-- Title -->
    <div class="title-bar">
        <h3>Examination Admit Card</h3>
        <p>(Academic Session <?= htmlspecialchars($s['session_year']) ?>) - Final Examination</p>
    </div>

    <!-- Student Info -->
    <div class="student-info-grid">
        <div class="info-cell">
            <span class="info-label">Admission / Roll No :</span>
            <span class="info-value"><?= htmlspecialchars($s['roll_number'] ?: $s['registration_number']) ?></span>
        </div>
        <div class="info-cell">
            <span class="info-label">Course / Class :</span>
            <span class="info-value"><?= htmlspecialchars($s['course_name']) ?></span>
        </div>
        <div class="photo-cell">
            <?php 
            $photo = $s['photo_path'] ?: 'assets/images/avatar.png';
            $photoUrl = (strpos($photo, 'http') === 0) ? $photo : '../' . $photo;
            ?>
            <img src="<?= $photoUrl ?>" class="student-photo" alt="Student">
        </div>
        <div class="info-cell">
            <span class="info-label">Student's Name :</span>
            <span class="info-value"><?= strtoupper($s['full_name']) ?></span>
        </div>
        <div class="info-cell">
            <span class="info-label">Father's Name :</span>
            <span class="info-value">MR. <?= strtoupper($s['father_name']) ?></span>
        </div>
        <div class="info-cell" style="grid-column: span 3;">
            <span class="info-label">College Name :</span>
            <span class="info-value"><?= strtoupper($s['institution_name']) ?></span>
        </div>
        <div class="info-cell" style="grid-column: span 3;">
            <span class="info-label">Examination Center Name :</span>
            <span class="info-value"><?= strtoupper($s['institution_name']) ?> (DISTRICT OFFICE / SELF CENTER)</span>
        </div>
    </div>

    <!-- Exam Table -->
    <table class="exam-table">
        <thead>
            <tr>
                <th>Subject</th>
                <th>Examination Date</th>
                <th>Day</th>
                <th>Timing</th>
                <th>Checked By</th>
            </tr>
        </thead>
        <tbody>
            <?php 
            $i = 0;
            foreach($subjects as $sub): 
                $examDate = date('d-M-Y', strtotime("+$i days", $baseDate));
                $day = date('l', strtotime("+$i days", $baseDate));
                $i++;
            ?>
            <tr>
                <td style="text-align: left; padding-left: 20px; font-weight: 700;"><?= htmlspecialchars($sub['subject_name']) ?></td>
                <td><?= $examDate ?></td>
                <td><?= $day ?></td>
                <td>09.00 AM to 12.00 PM</td>
                <td>........................</td>
            </tr>
            <?php endforeach; ?>
            <?php if(empty($subjects)): ?>
                <tr><td colspan="5">No subjects found for this course.</td></tr>
            <?php endif; ?>
        </tbody>
    </table>

    <!-- Instructions -->
    <div class="instructions-box">
        <h5>General/Exam Instructions for Students</h5>
        <ol class="instructions-list">
            <li>Check the exam timetable carefully.</li>
            <li>Students must bring their own <strong>Admit Card</strong> and show it to the invigilator(s) on duty.</li>
            <li>Preserve this admit card for future requirements.</li>
            <li>Mobile Phone, Calculator, Digital Watch or any Electronic Device is <strong>NOT ALLOWED</strong>.</li>
            <li>Arrive at the Examination Center at least 30 minutes before the start of the examination.</li>
            <li>Student is barred from entering the examination hall 15 minutes after the written examination starts.</li>
            <li>Ensure that you use the washroom before arriving for your exam as you will not be permitted to leave during the first hour.</li>
            <li>Normally, you are required to answer questions using blue or black ink.</li>
            <li>Keep your eyes on your own paper. Remember, copying is cheating!</li>
            <li>You must remain silent until after you have exited the school/college building.</li>
            <li>Result to be published on the official website in due time.</li>
        </ol>
    </div>

    <!-- Footer -->
    <div class="footer-grid">
        <div class="text-center">
            <div style="font-size: 11px; margin-bottom: 30px;">Date of Issue</div>
            <div class="sign-box"><?= date('d-M-Y') ?></div>
        </div>
        <div class="text-center">
            <div style="height: 45px;"></div>
            <div class="sign-box">Signature of Guardian</div>
        </div>
        <div class="text-center">
            <div style="height: 45px;"></div>
            <div class="sign-box">Class Teacher</div>
        </div>
        <div class="text-center">
            <div style="height: 45px;"></div>
            <div class="sign-box">Principal / Controller</div>
        </div>
    </div>

    </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/js/all.min.js"></script>
</body>
</html>
