<?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 admit card details if available
$admitCard = $db->fetchOne("SELECT * FROM admit_cards WHERE student_id = ? ORDER BY created_at DESC LIMIT 1", [$id]);

// If no admit card in DB, use dummy session info
$examSession = $admitCard['exam_session'] ?? $s['session_year'];
$examType = $admitCard['exam_type'] ?? 'Annual';
$examCentre = $admitCard['exam_centre'] ?? ($s['institution_name'] . " (SELF CENTER)");

// 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 (fallback if not in DB)
$baseDate = !empty($admitCard['exam_date_from']) ? strtotime($admitCard['exam_date_from']) : 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://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: #000; margin: 0; padding: 20px; }
        
        .admit-card-container {
            width: 210mm;
            height: 297mm;
            padding: 8mm;
            margin: 0 auto;
            background: white;
            box-shadow: 0 0 20px rgba(0,0,0,0.1);
            position: relative;
            box-sizing: border-box;
        }
        
        .border-wrapper {
            border: 2px solid #000;
            padding: 2px;
            height: 100%;
            box-sizing: border-box;
        }

        .inner-border {
            border: 1px solid #000;
            padding: 8px;
            height: 100%;
            box-sizing: border-box;
        }
        
        .header-table {
            width: 100%;
            border-bottom: 2px solid #000;
            border-bottom-style: double;
            margin-bottom: 10px;
            padding-bottom: 5px;
        }
        
        .header-table td {
            vertical-align: middle;
        }

        .logo-td {
            width: 90px;
            text-align: center;
        }

        .college-logo { 
            width: 75px; 
            height: 75px; 
            object-fit: contain; 
        }
        
        .header-text {
            text-align: center;
            padding: 0 10px;
        }
        
        .header-en { font-size: 14px; font-weight: 900; margin: 0; padding: 1px 0; white-space: nowrap; }
        .header-hi { font-size: 18px; font-weight: bold; margin: 0; padding: 1px 0; white-space: nowrap; }
        .header-sub { font-size: 12px; font-weight: bold; margin: 0; padding: 1px 0; }
        .header-session { font-size: 13px; font-weight: bold; margin: 0; padding: 2px 0; }
        
        .details-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 8px;
            border: 1px solid #000;
        }
        
        .details-table td, .details-table th {
            border: 1px solid #000;
            padding: 4px 6px;
            font-size: 11px;
            vertical-align: middle;
        }

        .details-label {
            width: 110px;
            font-weight: bold;
        }

        .photo-cell {
            width: 120px;
            text-align: center;
            vertical-align: top;
            padding: 4px !important;
        }
        
        .student-photo { 
            width: 95px; 
            height: 110px; 
            object-fit: cover; 
            border: 1px solid #000; 
        }
        
        .signature-box {
            border: 1px solid #000;
            margin-top: 4px;
            height: 30px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 11px;
        }

        .note-box {
            font-size: 11px;
            padding: 4px;
            border: 1px solid #000;
            margin-bottom: 8px;
            font-weight: bold;
        }
        
        .exam-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 8px;
        }
        
        .exam-table th, .exam-table td {
            border: 1px solid #000;
            padding: 4px 6px;
            text-align: center;
            font-size: 10px;
        }
        
        .exam-table th { font-weight: bold; }
        
        .rules-box {
            border: 1px solid #000;
            padding: 6px 12px;
            margin-bottom: 8px;
            font-size: 10px;
            line-height: 1.4;
        }
        
        .instructions-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 15px;
        }
        
        .instructions-table td {
            border: 1px solid #000;
            padding: 4px 6px;
            font-size: 10px;
        }

        .instructions-table td.num {
            width: 25px;
            text-align: center;
            font-weight: bold;
        }

        .footer-sig {
            text-align: right;
            padding-top: 15px;
            padding-right: 20px;
            font-size: 11px;
            margin-bottom: 10px;
        }
        
        .print-time {
            text-align: center;
            font-size: 9px;
            border-top: 1px solid #000;
            padding-top: 4px;
        }
        
        .btn-print {
            margin: 20px auto;
            display: block;
            background: #333;
            color: #fff;
            border: none;
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            border-radius: 5px;
            width: fit-content;
        }

        @media print {
            body { background: none; padding: 0; margin: 0; }
            .admit-card-container { 
                margin: 0 auto; 
                box-shadow: none; 
                width: 210mm; 
                height: 297mm; 
                padding: 8mm;
                box-sizing: border-box;
                page-break-after: avoid; 
            }
            .btn-print { display: none; }
            @page { size: A4; margin: 0; }
        }
    </style>
</head>
<body>

<button onclick="window.print()" class="btn-print"><i class="fas fa-print"></i> Print Admit Card</button>

<div class="admit-card-container">
    <div class="border-wrapper">
        <div class="inner-border">
            
            <!-- Header -->
            <table class="header-table">
                <tr>
                    <td class="logo-td">
                        <img src="../assets/images/logo.jpeg" class="college-logo" alt="Logo">
                    </td>
                    <td class="header-text">
                        <div class="header-en">INDIAN BOARD OF VOCATIONAL AND TECHNICAL EDUCATION (NEW DELHI)</div>
                        <div class="header-hi">भारतीय व्यावसायिक एवं तकनीकी शिक्षा बोर्ड (नई दिल्ली)</div>
                        <div class="header-sub">औपबंधिक प्रवेश पत्र / PROVISIONAL ADMIT CARD</div>
                        <div class="header-session">REGULAR EXAMINATION <?= htmlspecialchars($examSession) ?></div>
                    </td>
                </tr>
            </table>

            <!-- Student Details -->
            <table class="details-table">
                <tr>
                    <td class="details-label">Roll Number :-</td>
                    <td><b><?= formatRollNumber($s['roll_number']) ?></b></td>
                    <td class="details-label">Enrollment No :-</td>
                    <td><b><?= htmlspecialchars($s['registration_number']) ?></b></td>
                    <td rowspan="3" 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="Photo">
                        <div class="signature-box">Signature</div>
                    </td>
                </tr>
                <tr>
                    <td class="details-label">Candidate Name :-</td>
                    <td><b><?= strtoupper($s['full_name']) ?></b></td>
                    <td class="details-label">Father / Mother's Name</td>
                    <td><b><?= strtoupper($s['father_name']) ?> / <?= strtoupper($s['mother_name']) ?></b></td>
                </tr>
                <tr>
                    <td class="details-label">Course :-</td>
                    <td><b><?= strtoupper($s['course_name']) ?></b></td>
                    <td class="details-label">Gender</td>
                    <td><b><?= ucfirst($s['gender']) ?></b></td>
                </tr>
                <tr>
                    <td class="details-label">College Name :-</td>
                    <td colspan="4"><b><?= strtoupper($s['institution_name']) ?> (<?= $s['institution_code'] ?>)</b></td>
                </tr>
                <tr>
                    <td class="details-label">Examination Centre Name :-</td>
                    <td colspan="4"><b><?= strtoupper($examCentre) ?></b></td>
                </tr>
            </table>

            <div class="note-box">
                Note :- परीक्षा तिथि एवं परिणाम को बोर्ड की वेबसाइट <a href="https://www.ibvte.com" style="color:blue; text-decoration:underline;">www.ibvte.com</a> पर जांच अवश्य करें।
            </div>

            <!-- Exam Schedule -->
            <table class="exam-table">
                <thead>
                    <tr>
                        <th width="60">Paper</th>
                        <th width="100">Paper Code</th>
                        <th>Subject</th>
                        <th>Paper Name</th>
                        <th width="90">Date</th>
                        <th width="110">Time</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    $i = 0;
                    foreach($subjects as $sub): 
                        $i++;
                        $examDate = date('d-m-Y', strtotime("+$i days", $baseDate));
                    ?>
                    <tr>
                        <td>Paper <?= str_pad($i, 2, '0', STR_PAD_LEFT) ?></td>
                        <td><?= htmlspecialchars($sub['subject_code'] ?? 'A050401T') ?></td>
                        <td><?= htmlspecialchars($sub['subject_name']) ?></td>
                        <td><?= htmlspecialchars($sub['subject_name']) ?></td>
                        <td><?= $examDate ?></td>
                        <td>10:00 to 1:00</td>
                    </tr>
                    <?php endforeach; ?>
                    <?php if(empty($subjects)): ?>
                        <tr><td colspan="6">No subjects mapped for this course.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>

            <!-- Rules Note -->
            <div class="rules-box">
                <div>Note :-</div>
                <div>1. परीक्षार्थी द्वारा परीक्षा फॉर्म में दिये गये अर्हता विवरण में अनियमितता या कूट रचित तथ्य पाये जाने पर परीक्षाफल निरस्त कर दिया जायेगा।</div>
                <div>2. विश्वविद्यालय द्वारा विशेष परिस्थितियों में परीक्षा तिथि एवं समय में परिवर्तन किया जा सकता है।</div>
                <div>3. परीक्षा के समय परीक्षार्थी को विश्वविद्यालय द्वारा जारी प्रवेश पत्र के साथ सरकार द्वारा प्रदत्त फोटो पहचान पत्र साथ रखना अनिवार्य है।</div>
                <div>4. यह प्रवेश पत्र अस्थायी है। परीक्षार्थी का परिणाम उसकी पात्रता के अधीन रहेगा।</div>
            </div>

            <div class="footer-sig">
                Controller of<br>examination
            </div>

            <!-- Instructions -->
            <table class="instructions-table">
                <tr><td class="num">1.</td><td>परीक्षार्थी परीक्षा प्रारम्भ होने से कम से कम 30 मिनट पूर्व परीक्षा केंद्र पर उपस्थित हों।</td></tr>
                <tr><td class="num">2.</td><td>प्रवेश पत्र एवं एक वैध फोटो पहचान पत्र साथ लाना अनिवार्य है।</td></tr>
                <tr><td class="num">3.</td><td>प्रवेश पत्र के बिना परीक्षा कक्ष में प्रवेश नहीं दिया जाएगा।</td></tr>
                <tr><td class="num">4.</td><td>रोल नंबर, परीक्षा तिथि, समय एवं विषय विवरण ध्यानपूर्वक जाँच लें।</td></tr>
                <tr><td class="num">5.</td><td>मोबाइल फोन, स्मार्ट वॉच, कैलकुलेटर, नोट्स या अन्य इलेक्ट्रॉनिक सामग्री प्रतिबंधित है।</td></tr>
                <tr><td class="num">6.</td><td>परीक्षार्थी परीक्षा प्रारम्भ होने से कम से कम 30 मिनट पूर्व परीक्षा केंद्र पर उपस्थित हों।</td></tr>
                <tr><td class="num">7.</td><td>प्रवेश पत्र एवं एक वैध फोटो पहचान पत्र साथ लाना अनिवार्य है।</td></tr>
            </table>

            <div class="print-time">
                Print Time :- <?= date('d-m-Y H:i:s') ?>
            </div>

        </div>
    </div>
</div>

</body>
</html>
