<?php
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']);

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

// CSRF Protection
if (!Auth()->verifyCSRFToken($_GET['csrf_token'] ?? '')) {
    die("Security session expired or invalid request.");
}

$student = $db->fetchOne("
    SELECT s.*, 
           co.course_name, co.course_code, co.duration as course_duration,
           u.institution_name as college_name,
           u.city as college_city,
           u.state as college_state
    FROM students s
    LEFT JOIN courses co ON s.course_id = co.id
    LEFT JOIN users u ON s.institution_id = u.id
    WHERE s.id = ? AND s.result_declared = 1
", [$id]);

if (!$student) {
    die("Student not found or result not declared yet.");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Certificate - <?= htmlspecialchars($student['full_name']) ?></title>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Cinzel:wght@700&family=Montserrat:wght@400;600;700&family=Playfair+Display:ital,wght@0,700;1,700&display=swap');
        
        * { box-sizing: border-box; }
        body { margin: 0; padding: 0; background: #555; font-family: 'Montserrat', sans-serif; }
        
        .print-btn {
            position: fixed; top: 20px; right: 20px; padding: 12px 25px;
            background: #d4af37; color: #000; border: none; border-radius: 5px;
            cursor: pointer; font-weight: bold; z-index: 999; box-shadow: 0 4px 10px rgba(0,0,0,0.3);
        }

        .certificate-container {
            width: 210mm; height: 297mm; /* A4 Portrait */
            background: #fff; margin: 20px auto; position: relative;
            box-shadow: 0 0 30px rgba(0,0,0,0.5); padding: 15mm;
            overflow: hidden;
        }

        /* Border System */
        .outer-border {
            border: 15px solid #0a2351; height: 100%; position: relative; padding: 5px;
        }
        .inner-border {
            border: 2px solid #d4af37; height: 100%; position: relative; padding: 10px;
        }
        
        .header-top {
            display: flex; justify-content: space-between; font-size: 11px; font-weight: 700; color: #333; margin-bottom: 10px;
        }

        .main-header { text-align: center; margin-top: 10px; }
        .board-name {
            font-family: 'Cinzel', serif; font-size: 26px; color: #b01c2e; margin: 10px 0;
            text-shadow: 1px 1px 1px rgba(0,0,0,0.1);
        }
        .board-tagline { font-size: 12px; font-weight: 700; color: #0a2351; margin-bottom: 5px; }
        .board-reg { font-size: 10px; color: #666; font-style: italic; }

        .logo-wrap { margin: 10px 0; }
        .main-logo { height: 80px; }

        .cert-type-box {
            border: 2px solid #b01c2e; display: inline-block; padding: 5px 15px;
            margin: 10px 0; background: #fff; position: relative;
            max-width: 300px; /* Narrower to avoid photo overlap */
        }
        .cert-type-title {
            font-family: 'Cinzel', serif; font-size: 16px; color: #b01c2e; margin: 0;
            letter-spacing: 1px;
        }
        .reg-no-strip {
            background: #b01c2e; color: white; padding: 2px 10px; font-size: 10px;
            margin-top: 3px; font-weight: bold;
        }

        .photo-box {
            position: absolute; top: 190px; right: 40px; width: 100px; height: 125px;
            border: 2px solid #0a2351; padding: 2px; background: #eee; z-index: 20;
            box-shadow: 2px 2px 5px rgba(0,0,0,0.1);
        }
        .student-photo { width: 100%; height: 100%; object-fit: cover; }

        .cert-content {
            margin-top: 25px; text-align: left; padding: 0 40px; line-height: 2.0;
            font-size: 14px; color: #333; position: relative; z-index: 10;
        }
        .field-label { font-weight: 400; }
        .field-value {
            font-family: 'Playfair Display', serif; font-weight: 700; color: #b01c2e;
            text-transform: uppercase; border-bottom: 1px dotted #888; display: inline-block;
            padding: 0 5px; min-width: 100px; text-align: center; font-style: italic;
        }

        .directions {
            margin-top: 30px; padding: 10px 40px; text-align: left;
            border-top: 1px solid #eee;
        }
        .dir-title { font-weight: 700; font-size: 11px; margin-bottom: 3px; }
        .dir-list { font-size: 10px; margin: 0; padding-left: 15px; color: #555; }

        .footer {
            margin-top: 30px; display: flex; justify-content: space-between; 
            align-items: flex-end; padding: 0 40px; position: relative;
        }
        .signature { text-align: center; flex: 1; }
        .sig-line { border-top: 1px solid #000; width: 140px; margin: 0 auto 5px auto; }
        .sig-text { font-size: 11px; font-weight: 700; color: #0a2351; }

        .qr-wrap { text-align: center; flex: 0 0 100px; margin: 0 20px; }
        .qr-code { width: 75px; height: 75px; border: 1px solid #ddd; padding: 2px; background: #fff; }
        .verify-text { font-size: 8px; margin-top: 3px; color: #888; font-weight: bold; }

        .watermark {
            position: absolute; top: 55%; left: 50%; transform: translate(-50%, -50%) rotate(-45deg);
            font-size: 120px; color: rgba(10, 35, 81, 0.03); font-weight: 900;
            pointer-events: none; white-space: nowrap; font-family: 'Montserrat', sans-serif;
        }

        @media print {
            body { background: none; padding: 0; }
            .print-btn { display: none; }
            .certificate-container { margin: 0; box-shadow: none; width: 210mm; height: 297mm; }
        }
    </style>
</head>
<body>
    <button class="print-btn" onclick="window.print()">
        <i class="fas fa-print me-2"></i>PRINT CERTIFICATE
    </button>

    <div class="certificate-container">
        <div class="watermark">IBVTE ORIGINAL</div>
        
        <div class="outer-border">
            <div class="inner-border">
                
                <div class="header-top">
                    <div>RECOGNISATION : <span style="border-bottom: 1px dotted #888; min-width: 100px; display: inline-block;">GLOBAL</span></div>
                    <div>VALIDITY : <span style="border-bottom: 1px dotted #888; min-width: 100px; display: inline-block;">LIFE TIME</span></div>
                </div>

                <div class="main-header">
                    <h1 class="board-name">Indian Board of Vocational and Technical Education</h1>
                    <div class="board-tagline">AN ISO 9001:2015 CERTIFIED ORGANIZATION</div>
                    <div class="board-reg">(Duly Registered by the Govt. of India | REG NO: IBVTE/2024/001)</div>
                    
                    <div class="logo-wrap">
                        <img src="../assets/images/logo.jpeg" class="main-logo" alt="Logo">
                    </div>

                    <div class="cert-type-box">
                        <h2 class="cert-type-title">REGISTRATION CERTIFICATE</h2>
                        <div class="reg-no-strip">REGISTRATION NO. - <?= htmlspecialchars($student['registration_number'] ?? 'N/A') ?></div>
                    </div>
                </div>

                <div class="photo-box">
                    <?php if ($student['photo_path']): ?>
                        <img src="../<?= htmlspecialchars($student['photo_path']) ?>" class="student-photo" alt="Photo">
                    <?php else: ?>
                        <div style="height:100%; display:flex; align-items:center; justify-content:center; color:#ccc; font-size:10px;">PHOTO HERE</div>
                    <?php endif; ?>
                </div>

                <div class="cert-content">
                    This is to Certified that Mr./Mrs./Km. <span class="field-value" style="min-width: 250px;"><?= htmlspecialchars($student['full_name']) ?></span> <br>
                    S/W/D/O/Shri <span class="field-value" style="min-width: 300px;"><?= htmlspecialchars($student['father_name']) ?></span> <br>
                    Resident of <span class="field-value" style="min-width: 450px;"><?= htmlspecialchars($student['address']) ?></span> <br>
                    State <span class="field-value" style="min-width: 150px;"><?= htmlspecialchars($student['college_state'] ?? 'U.P.') ?></span> 
                    Whose Date of Birth <span class="field-value" style="min-width: 150px;"><?= date('d/M/Y', strtotime($student['date_of_birth'])) ?></span> <br>
                    Qualification <span class="field-value" style="min-width: 300px;"><?= htmlspecialchars($student['course_name']) ?></span> <br>
                    From Institution <span class="field-value" style="min-width: 380px;"><?= htmlspecialchars($student['college_name'] ?? 'IBVTE DIRECT') ?></span> <br>
                    has been Registered in <span class="field-value" style="min-width: 100px;"><?= getDivisionLabel($student['division']) ?></span> 
                    with <span class="field-value" style="min-width: 100px;"><?= number_format($student['percentage'], 2) ?>% Marks.</span> <br>
                    as a Vocational Professional Under the Rule of IBVTE Council of India.
                </div>

                <div class="directions">
                    <div class="dir-title">DIRECTIONS :</div>
                    <ul class="dir-list">
                        <li>Holder of this Certificate can Practice as Vocational Technician Under any eminent Specialist in the Concern Field.</li>
                        <li>This certificate is issued for registration purposes based on successful completion of the course.</li>
                        <li>In Case any Change in address. Immediately Intimate to the Council.</li>
                    </ul>
                </div>

                <div class="footer">
                    <div class="signature">
                        <div style="font-size: 11px; margin-bottom: 5px; font-weight: bold;">Date: <?= date('d/M/Y', strtotime($student['result_declared_at'] ?? 'now')) ?></div>
                        <div class="sig-text">ISSUE DATE</div>
                    </div>
                    
                    <div class="qr-wrap">
                        <img src="https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=<?= urlencode(APP_URL . "verify-certificate.php?id=" . $student['id']) ?>" class="qr-code" alt="QR Code">
                        <div class="verify-text">SCAN TO VERIFY</div>
                    </div>

                    <div class="signature">
                        <div class="sig-line"></div>
                        <div class="sig-text">CHAIRMAN / DIRECTOR</div>
                        <div style="font-size: 10px; color: #555;">(Authorized Signatory)</div>
                    </div>
                </div>

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

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</body>
</html>
