<?php
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$result = $result ?? [];

$pct = (float)($result['percentage'] ?? 0);
$grade = $result['grade'] ?? 'A';
$division = $result['division'] ?? 'First Division';
$certNumber = 'CERT-' . ($result['branch_code'] ?? '') . '-' . $result['id'] . '-' . date('Y');

$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'] ?? 'your-website.com';
$verificationUrl = $protocol . '://' . $host . $bp . '/verify/' . rawurlencode($certNumber);
?>
<style>
@media print {
    .no-print { display: none !important; }
    body { background: #fff !important; }
    @page { margin: 8mm; size: landscape; }
}
.cert-ornament {
    background: linear-gradient(135deg, #d4a017 0%, #f1c40f 50%, #d4a017 100%);
    height: 6px; width: 100%; border-radius: 3px;
}
.cert-ornament-bottom { background: linear-gradient(135deg, #0d3b3b 0%, #16a085 50%, #0d3b3b 100%); }
</style>
<div class="admin-section">
    <div class="no-print" style="display:flex;justify-content:space-between;align-items:center;margin-bottom:14px;">
        <a href="<?= $bp ?>/branch/exam-results/<?= (int)$result['exam_id'] ?>" style="color:#16a085;text-decoration:none;font-size:0.85rem;">← Back to Results</a>
        <button onclick="window.print()" style="background:#16a085;color:#fff;border:none;padding:8px 20px;border-radius:8px;font-size:0.85rem;cursor:pointer;font-weight:600;">🖨 Print / Save as PDF</button>
    </div>

    <div style="max-width:900px;margin:0 auto;background:#fff;border:8px double #d4a017;border-radius:18px;padding:0;box-shadow:0 8px 32px rgba(0,0,0,0.12);position:relative;overflow:hidden;">
        <!-- Top gold ornament -->
        <div class="cert-ornament"></div>

        <!-- Inner border -->
        <div style="border:2px solid #0d3b3b;margin:14px;padding:30px;text-align:center;">

            <!-- Certificate header -->
            <div style="font-size:0.84rem;color:#999;letter-spacing:4px;text-transform:uppercase;margin-bottom:6px;">Gmind Institute</div>
            <h1 style="margin:0;font-family:'Poppins',sans-serif;font-size:2.2rem;color:#0d3b3b;letter-spacing:2px;font-weight:700;">CERTIFICATE OF COMPLETION</h1>
            <p style="margin:8px 0 0;font-size:0.86rem;color:#666;font-style:italic;">This is to certify that</p>

            <!-- Student name (prominent) -->
            <h2 style="margin:18px 0 4px;font-size:1.8rem;color:#16a085;font-weight:700;font-family:'Poppins',sans-serif;border-bottom:2px solid #d4a017;display:inline-block;padding:0 24px 6px;">
                <?= htmlspecialchars($result['student_name'] ?? '', ENT_QUOTES, 'UTF-8') ?>
            </h2>

            <p style="margin:14px 0 0;font-size:0.94rem;color:#444;line-height:1.7;max-width:680px;margin-left:auto;margin-right:auto;">
                Son/Daughter of <strong><?= htmlspecialchars($result['father_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></strong>,
                bearing Enrollment No <strong style="font-family:monospace;"><?= htmlspecialchars($result['enrollment_no'] ?? '', ENT_QUOTES, 'UTF-8') ?></strong>,
                has successfully <strong style="color:#27ae60;">PASSED</strong> the
            </p>

            <!-- Exam name (prominent) -->
            <h3 style="margin:12px 0 4px;font-size:1.3rem;color:#0d3b3b;font-weight:700;">
                <?= htmlspecialchars($result['exam_title'] ?? '', ENT_QUOTES, 'UTF-8') ?>
            </h3>
            <?php if (!empty($result['title_hindi'])): ?>
            <p style="margin:0 0 14px;font-size:1.05rem;color:#666;font-family:'Noto Sans Devanagari',sans-serif;"><?= htmlspecialchars($result['title_hindi'], ENT_QUOTES, 'UTF-8') ?></p>
            <?php endif; ?>

            <p style="margin:8px 0 0;font-size:0.94rem;color:#444;">
                conducted by <strong><?= htmlspecialchars($result['branch_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></strong>
                (Branch Code: <?= htmlspecialchars($result['branch_code'] ?? '', ENT_QUOTES, 'UTF-8') ?>)
                on <strong><?= !empty($result['completed_at']) ? date('d F Y', strtotime($result['completed_at'])) : '—' ?></strong>
            </p>

            <!-- Score block -->
            <div style="display:flex;justify-content:center;gap:14px;margin:20px 0;">
                <div style="padding:12px 22px;background:#eaf0fb;border-radius:8px;border:1px solid #1a4fa0;">
                    <div style="font-size:1.5rem;font-weight:700;color:#1a4fa0;font-family:monospace;"><?= (float)$result['total_obtained'] ?>/<?= (float)$result['total_marks'] ?></div>
                    <div style="font-size:0.72rem;color:#999;text-transform:uppercase;letter-spacing:1px;">Marks</div>
                </div>
                <div style="padding:12px 22px;background:#eafaf1;border-radius:8px;border:1px solid #27ae60;">
                    <div style="font-size:1.5rem;font-weight:700;color:#27ae60;"><?= $pct ?>%</div>
                    <div style="font-size:0.72rem;color:#999;text-transform:uppercase;letter-spacing:1px;">Percentage</div>
                </div>
                <div style="padding:12px 22px;background:#fff8e1;border-radius:8px;border:1px solid #d4a017;">
                    <div style="font-size:1.5rem;font-weight:700;color:#d4a017;"><?= htmlspecialchars($grade, ENT_QUOTES, 'UTF-8') ?></div>
                    <div style="font-size:0.72rem;color:#999;text-transform:uppercase;letter-spacing:1px;">Grade</div>
                </div>
                <div style="padding:12px 22px;background:#f4f9f9;border-radius:8px;border:1px solid #16a085;">
                    <div style="font-size:1.0rem;font-weight:700;color:#16a085;"><?= htmlspecialchars($division, ENT_QUOTES, 'UTF-8') ?></div>
                    <div style="font-size:0.72rem;color:#999;text-transform:uppercase;letter-spacing:1px;">Division</div>
                </div>
            </div>

            <p style="font-size:0.86rem;color:#666;font-style:italic;margin:0 0 24px;">
                We wish him/her success in all future endeavours.
            </p>

            <!-- Signatures + cert number -->
            <div style="display:flex;justify-content:space-between;align-items:end;margin-top:30px;">
                <div style="text-align:center;font-size:0.84rem;color:#666;">
                    <div style="margin-bottom:30px;">Cert. No: <strong style="font-family:monospace;"><?= htmlspecialchars($certNumber, ENT_QUOTES, 'UTF-8') ?></strong></div>
                    <div style="font-size:0.74rem;color:#999;"><?= !empty($result['approved_at']) ? 'Approved on ' . date('d M Y', strtotime($result['approved_at'])) : date('d M Y') ?></div>
                </div>
                <div style="text-align:center;font-size:0.92rem;color:#0d3b3b;">
                    <div style="font-family:'Brush Script MT', cursive;font-size:1.6rem;color:#16a085;font-style:italic;margin-bottom:2px;">Authorized</div>
                    <div style="border-top:1.5px solid #0d3b3b;width:180px;margin:0 auto;"></div>
                    <strong style="margin-top:4px;display:inline-block;">Branch Director</strong>
                    <div style="font-size:0.78rem;color:#999;margin-top:2px;"><?= htmlspecialchars($result['branch_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
                </div>
            </div>

            <!-- Verification footer -->
            <div style="margin-top:24px;padding-top:14px;border-top:1px dashed #ccc;font-size:0.72rem;color:#999;text-align:center;">
                Verify this certificate at <span style="font-family:monospace;color:#1a4fa0;"><?= htmlspecialchars($verificationUrl, ENT_QUOTES, 'UTF-8') ?></span>
            </div>
        </div>

        <!-- Bottom navy-teal ornament -->
        <div class="cert-ornament cert-ornament-bottom"></div>
    </div>
</div>
