<?php
/**
 * Print Student Admit Card (Hall Ticket)
 */
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', 'college']);

$db = DB();
$id = $_GET['id'] ?? 0;

// Fetch student details
$student = $db->fetchOne("
    SELECT s.*, c.course_name, c.course_code, c.duration, 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.id = ? AND s.status = 'active'
", [$id]);

if (!$student) {
    die("Student not found or admission is not active.");
}

// Logo and Watermark Paths
$logoUrl = '../assets/images/logo.jpeg';
$watermarkUrl = '../assets/images/logo.jpeg'; 
?>
<!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($student['full_name']) ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f4f4f4;
            -webkit-print-color-adjust: exact !important;
            print-color-adjust: exact !important;
        }
        .page-a4 {
            width: 210mm;
            min-height: 297mm;
            padding: 20mm;
            margin: 10mm auto;
            border: 1px #D3D3D3 solid;
            border-radius: 5px;
            background: white;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            position: relative;
        }
        .watermark {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            opacity: 0.08;
            width: 60%;
            z-index: 0;
            pointer-events: none;
        }
        .content-layer {
            position: relative;
            z-index: 1;
        }
        .doc-header {
            border-bottom: 3px solid #3b0b7c;
            padding-bottom: 20px;
            margin-bottom: 30px;
        }
        .school-name {
            color: #3b0b7c;
            font-weight: 900;
            font-size: 24px;
            text-transform: uppercase;
        }
        .school-address {
            font-size: 14px;
            color: #555;
        }
        .doc-title {
            background-color: #3b0b7c;
            color: white;
            text-align: center;
            padding: 10px;
            font-size: 20px;
            font-weight: bold;
            border-radius: 50px;
            margin: 20px auto;
            width: 60%;
            text-transform: uppercase;
        }
        .student-details th {
            width: 30%;
            background-color: #f8f9fa;
        }
        .photo-box {
            width: 130px;
            height: 160px;
            border: 2px solid #3b0b7c;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #ccc;
            font-size: 12px;
            text-align: center;
        }
        .signature-box {
            margin-top: 50px;
            text-align: center;
        }
        .signature-line {
            border-top: 1px solid #000;
            width: 200px;
            margin: 0 auto 5px auto;
        }
        .instructions-list {
            font-size: 13px;
            line-height: 1.6;
        }
        @media print {
            body { background: none; margin: 0; padding: 0; }
            .page-a4 {
                margin: 0;
                border: none;
                box-shadow: none;
                width: 100%;
                padding: 0;
            }
            .no-print { display: none !important; }
        }
    </style>
</head>
<body>
    
    <div class="text-center mt-3 mb-3 no-print">
        <button class="btn btn-primary btn-lg px-5 shadow" onclick="window.print()">
            <i class="fas fa-print me-2"></i> Print Admit Card
        </button>
    </div>

    <div class="page-a4">
        <!-- Watermark -->
        <img src="<?= $watermarkUrl ?>" class="watermark" alt="Watermark">

        <div class="content-layer">
            <!-- Header -->
            <div class="row doc-header align-items-center">
                <div class="col-2 text-center">
                    <img src="<?= $logoUrl ?>" alt="Logo" style="max-width: 100px;">
                </div>
                <div class="col-10 text-center pe-5">
                    <div class="school-name">INDIAN BOARD OF VOCATIONAL & TECHNICAL EDUCATION</div>
                    <div class="school-address mt-1">An Autonomous Organization under Govt. of India Rules</div>
                    <div class="school-address fw-bold text-danger mt-1">Website: www.ibvte.org | Email: info@ibvte.org</div>
                </div>
            </div>

            <!-- Title -->
            <div class="doc-title">
                ADMIT CARD / HALL TICKET
            </div>

            <div class="row mt-5">
                <div class="col-9">
                    <table class="table table-bordered student-details">
                        <tr>
                            <th>Student Name</th>
                            <td class="fw-bold fs-5 text-uppercase text-primary"><?= htmlspecialchars($student['full_name']) ?></td>
                        </tr>
                        <tr>
                            <th>Father's Name</th>
                            <td><?= htmlspecialchars($student['father_name'] ?? 'N/A') ?></td>
                        </tr>
                        <tr>
                            <th>Mother's Name</th>
                            <td><?= htmlspecialchars($student['mother_name'] ?? 'N/A') ?></td>
                        </tr>
                        <tr>
                            <th>Date of Birth</th>
                            <td><?= $student['date_of_birth'] ? date('d M Y', strtotime($student['date_of_birth'])) : 'N/A' ?></td>
                        </tr>
                        <tr>
                            <th>Course Applied</th>
                            <td class="fw-bold"><?= htmlspecialchars($student['course_name'] ?? 'N/A') ?> (<?= htmlspecialchars($student['course_code'] ?? '') ?>)</td>
                        </tr>
                        <tr>
                            <th>Session Year</th>
                            <td><?= htmlspecialchars($student['session_year'] ?? date('Y')) ?></td>
                        </tr>
                        <tr>
                            <th>Institution / College</th>
                            <td><?= htmlspecialchars($student['institution_name'] ?? 'Direct Candidate') ?></td>
                        </tr>
                        <tr>
                            <th>Roll Number</th>
                            <td class="fw-bold text-danger"><?= htmlspecialchars($student['roll_number'] ?: 'TO BE ASSIGNED') ?></td>
                        </tr>
                        <tr>
                            <th>Registration No.</th>
                            <td class="fw-bold"><?= htmlspecialchars($student['registration_number'] ?: 'TO BE ASSIGNED') ?></td>
                        </tr>
                    </table>
                </div>
                <div class="col-3 d-flex flex-column align-items-center">
                    <div class="photo-box mb-3">
                        <?php if(!empty($student['profile_image'])): ?>
                            <img src="<?= htmlspecialchars($student['profile_image']) ?>" style="width:100%; height:100%; object-fit:cover;">
                        <?php else: ?>
                            Paste Recent<br>Passport Size<br>Photograph
                        <?php endif; ?>
                    </div>
                    <div style="border: 1px solid #ccc; width: 130px; height: 50px; display: flex; align-items: center; justify-content: center; font-size: 10px; color: #ccc;">
                        Candidate Signature
                    </div>
                </div>
            </div>

            <!-- Subject Details (if applicable) -->
            <div class="mt-4">
                <h6 class="fw-bold text-primary border-bottom pb-2">Examination Details</h6>
                <table class="table table-bordered table-sm mt-3">
                    <thead class="table-light">
                        <tr>
                            <th>Subject Code</th>
                            <th>Subject Name</th>
                            <th>Exam Date</th>
                            <th>Time</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr><td colspan="4" class="text-center text-muted fst-italic">As per official datesheet provided by the institution</td></tr>
                        <tr><td>-</td><td>-</td><td>-</td><td>-</td></tr>
                        <tr><td>-</td><td>-</td><td>-</td><td>-</td></tr>
                        <tr><td>-</td><td>-</td><td>-</td><td>-</td></tr>
                    </tbody>
                </table>
            </div>

            <!-- Instructions -->
            <div class="mt-4 p-3 rounded" style="background-color: #f8f9fa; border: 1px solid #ddd;">
                <h6 class="fw-bold text-danger mb-3"><i class="fas fa-exclamation-triangle me-2"></i>Important Instructions for Candidates</h6>
                <ol class="instructions-list mb-0">
                    <li>Candidates must bring this Admit Card along with a valid Original Photo ID (Aadhar Card/Voter ID) to the examination center.</li>
                    <li>Please report to the examination center at least 30 minutes before the commencement of the exam.</li>
                    <li>Electronic devices, mobile phones, calculators, and smartwatches are strictly prohibited inside the examination hall.</li>
                    <li>Candidates arriving 15 minutes after the start of the exam will not be permitted to enter.</li>
                    <li>Preserve this admit card carefully for future reference until results are declared.</li>
                </ol>
            </div>

            <div class="row mt-5 pt-4">
                <div class="col-4 text-center">
                    <div class="signature-box">
                        <div class="signature-line"></div>
                        <div class="fw-bold">Center Superintendent</div>
                        <div class="small text-muted">Seal & Signature</div>
                    </div>
                </div>
                <div class="col-4"></div>
                <div class="col-4 text-center">
                    <div class="signature-box">
                        <img src="../assets/images/logo.jpeg" style="width: 50px; opacity: 0.5; margin-bottom: -10px; position: relative; z-index: -1;">
                        <div class="signature-line"></div>
                        <div class="fw-bold text-primary">Controller of Examinations</div>
                        <div class="small text-muted">IBVTE Official</div>
                    </div>
                </div>
            </div>
            
            <div class="mt-5 text-center text-muted small border-top pt-3">
                System Generated Admit Card | Printed on: <?= date('d M Y, h:i A') ?>
            </div>
        </div>
    </div>
</body>
</html>
