<?php
/**
 * Generate Printable Document (NOC / Admit Card)
 */
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;
$type = $_GET['type'] ?? 'noc';

// Fetch the appropriate record
$record = null;
if ($type === 'noc') {
    $record = $db->fetchOne("SELECT * FROM noc_applications WHERE id = ?", [$id]);
    if (!$record || $record['status'] !== 'approved') {
        die("Record not found or not approved.");
    }
} else {
    // For future student admit cards
    die("This module is currently configured for NOC/ID cards.");
}

// 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>Generate Document - <?= htmlspecialchars($record['applicant_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.1;
            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: 80%;
            text-transform: uppercase;
        }
        .student-details th {
            width: 35%;
            background-color: #f8f9fa;
        }
        .photo-box {
            width: 120px;
            height: 150px;
            border: 2px solid #ccc;
            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;
        }
        @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 Document
        </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">
                <?php 
                if ($record['application_type'] === 'noc') echo 'NO OBJECTION CERTIFICATE (NOC)';
                elseif ($record['application_type'] === 'id_card') echo 'OFFICIAL ID CARD AUTHORIZATION';
                else echo 'NOC & ID CARD AUTHORIZATION';
                ?>
            </div>

            <div class="row mt-5">
                <div class="col-9">
                    <table class="table table-bordered student-details">
                        <tr>
                            <th>Application Number</th>
                            <td class="fw-bold text-danger"><?= htmlspecialchars($record['application_no']) ?></td>
                        </tr>
                        <tr>
                            <th>Applicant Name</th>
                            <td class="fw-bold fs-5"><?= htmlspecialchars($record['applicant_name']) ?></td>
                        </tr>
                        <tr>
                            <th>Email Address</th>
                            <td><?= htmlspecialchars($record['email']) ?></td>
                        </tr>
                        <tr>
                            <th>Mobile Number</th>
                            <td><?= htmlspecialchars($record['mobile']) ?></td>
                        </tr>
                        <tr>
                            <th>Document Requested</th>
                            <td><?= ucfirst(str_replace('_', ' ', $record['document_type'])) ?></td>
                        </tr>
                        <tr>
                            <th>Status</th>
                            <td class="text-success fw-bold">VERIFIED & APPROVED</td>
                        </tr>
                        <tr>
                            <th>Date of Approval</th>
                            <td><?= date('d M Y') ?></td>
                        </tr>
                    </table>
                </div>
                <div class="col-3 d-flex flex-column align-items-center">
                    <div class="photo-box mb-2">
                        Paste Recent<br>Passport Size<br>Photograph
                    </div>
                    <div style="border: 1px solid #ccc; width: 120px; height: 50px; display: flex; align-items: center; justify-content: center; font-size: 10px; color: #ccc;">
                        Signature
                    </div>
                </div>
            </div>

            <div class="mt-5 pt-3" style="font-size: 15px; line-height: 1.8; text-align: justify;">
                <p>This is to certify that <strong><?= htmlspecialchars($record['applicant_name']) ?></strong> has successfully completed the required verification process. The board has no objection to the issuance of the requested <strong><?= ucfirst(str_replace('_', ' ', $record['document_type'])) ?></strong>.</p>
                <p>This document is electronically generated and holds official status for verification purposes under the Indian Board of Vocational & Technical Education guidelines.</p>
            </div>

            <div class="row mt-5 pt-5">
                <div class="col-6 text-center">
                    <div class="signature-box">
                        <div class="signature-line"></div>
                        <div class="fw-bold">Applicant Signature</div>
                        <div class="small text-muted">Sign in the presence of authority</div>
                    </div>
                </div>
                <div class="col-6 text-center">
                    <div class="signature-box">
                        <div class="signature-line"></div>
                        <div class="fw-bold">Registrar / Secretary</div>
                        <div class="small text-muted">IBVTE Official Seal & Signature</div>
                    </div>
                </div>
            </div>
            
            <div class="mt-5 text-center text-muted small border-top pt-3">
                Printed on: <?= date('d M Y, h:i A') ?> | System Generated Document | Valid across all affiliated centers.
            </div>
        </div>
    </div>
</body>
</html>
