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

// Compute display fields
$pct = (float)($result['percentage'] ?? 0);
$obt = (float)($result['total_obtained'] ?? 0);
$tot = (float)($result['total_marks'] ?? 0);
$grade = $result['grade'] ?? 'F';
$division = $result['division'] ?? 'Fail';
$status = $result['status'] ?? 'fail';

$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'] ?? 'your-website.com';
$verificationUrl = $protocol . '://' . $host . $bp . '/verify/' . rawurlencode($result['enrollment_no'] ?? '');
?>
<style>
@media print {
    .no-print { display: none !important; }
    body { background: #fff !important; }
    .marksheet-card { box-shadow: none !important; border: 2px solid #0d3b3b !important; }
    @page { margin: 12mm; }
}
</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 class="marksheet-card" style="max-width:800px;margin:0 auto;background:#fff;border:2px solid #0d3b3b;border-radius:14px;padding:34px;box-shadow:0 4px 20px rgba(0,0,0,0.08);">
        <!-- Header with logo + institute name -->
        <div style="text-align:center;border-bottom:3px double #0d3b3b;padding-bottom:16px;margin-bottom:18px;">
            <h1 style="margin:0;font-family:'Poppins',sans-serif;font-size:1.7rem;color:#0d3b3b;letter-spacing:1px;">GMIND INSTITUTE</h1>
            <p style="margin:6px 0 0;font-size:0.84rem;color:#666;"><?= htmlspecialchars($result['branch_name'] ?? '', ENT_QUOTES, 'UTF-8') ?> (<?= htmlspecialchars($result['branch_code'] ?? '', ENT_QUOTES, 'UTF-8') ?>) · <?= htmlspecialchars($result['branch_address'] ?? '', ENT_QUOTES, 'UTF-8') ?> · 📞 <?= htmlspecialchars($result['branch_phone'] ?? '', ENT_QUOTES, 'UTF-8') ?></p>
            <h2 style="margin:14px 0 0;font-size:1.25rem;color:#16a085;text-transform:uppercase;letter-spacing:3px;font-weight:700;">Marksheet</h2>
        </div>

        <!-- Student info -->
        <table style="width:100%;font-size:0.88rem;margin-bottom:18px;border-collapse:collapse;">
            <tr>
                <td style="padding:6px 0;color:#999;width:30%;">Student Name</td>
                <td style="padding:6px 0;font-weight:700;color:#0d3b3b;width:35%;"><?= htmlspecialchars($result['student_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                <td style="padding:6px 0;color:#999;width:20%;">Enrollment No</td>
                <td style="padding:6px 0;font-weight:700;font-family:monospace;color:#0d3b3b;"><?= htmlspecialchars($result['enrollment_no'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
            </tr>
            <tr>
                <td style="padding:6px 0;color:#999;">Father's Name</td>
                <td style="padding:6px 0;font-weight:600;"><?= htmlspecialchars($result['father_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
                <td style="padding:6px 0;color:#999;">Course</td>
                <td style="padding:6px 0;font-weight:600;"><?= htmlspecialchars($result['course_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
            </tr>
            <tr>
                <td style="padding:6px 0;color:#999;">Exam Title</td>
                <td style="padding:6px 0;font-weight:600;"><?= htmlspecialchars($result['exam_title'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                <td style="padding:6px 0;color:#999;">Duration</td>
                <td style="padding:6px 0;font-weight:600;"><?= (int)($result['duration_minutes'] ?? 60) ?> min</td>
            </tr>
            <tr>
                <td style="padding:6px 0;color:#999;">Exam Date</td>
                <td style="padding:6px 0;font-weight:600;"><?= !empty($result['start_time']) ? date('d M Y', strtotime($result['start_time'])) : '—' ?></td>
                <td style="padding:6px 0;color:#999;">Submitted At</td>
                <td style="padding:6px 0;font-weight:600;"><?= !empty($result['completed_at']) ? date('d M Y, g:i A', strtotime($result['completed_at'])) : '—' ?></td>
            </tr>
        </table>

        <!-- Marks summary table -->
        <table style="width:100%;font-size:0.92rem;border:2px solid #0d3b3b;border-collapse:collapse;margin-bottom:18px;">
            <thead>
                <tr style="background:#0d3b3b;color:#fff;">
                    <th style="padding:12px;text-align:left;border:1px solid #0d3b3b;">Subject / Exam</th>
                    <th style="padding:12px;text-align:center;border:1px solid #0d3b3b;">Max Marks</th>
                    <th style="padding:12px;text-align:center;border:1px solid #0d3b3b;">Obtained</th>
                    <th style="padding:12px;text-align:center;border:1px solid #0d3b3b;">%</th>
                    <th style="padding:12px;text-align:center;border:1px solid #0d3b3b;">Grade</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td style="padding:12px;border:1px solid #0d3b3b;font-weight:600;">
                        <?= htmlspecialchars($result['exam_title'] ?? '', ENT_QUOTES, 'UTF-8') ?>
                        <?php if (!empty($result['title_hindi'])): ?>
                            <br><span style="font-weight:400;color:#666;font-size:0.8rem;font-family:'Noto Sans Devanagari',sans-serif;"><?= htmlspecialchars($result['title_hindi'], ENT_QUOTES, 'UTF-8') ?></span>
                        <?php endif; ?>
                    </td>
                    <td style="padding:12px;text-align:center;border:1px solid #0d3b3b;font-weight:600;"><?= $tot ?></td>
                    <td style="padding:12px;text-align:center;border:1px solid #0d3b3b;font-weight:700;color:#16a085;font-size:1.1rem;"><?= $obt ?></td>
                    <td style="padding:12px;text-align:center;border:1px solid #0d3b3b;font-weight:600;"><?= $pct ?>%</td>
                    <td style="padding:12px;text-align:center;border:1px solid #0d3b3b;font-weight:700;color:#1a4fa0;"><?= htmlspecialchars($grade, ENT_QUOTES, 'UTF-8') ?></td>
                </tr>
                <tr style="background:#f9f9f9;font-weight:700;">
                    <td style="padding:12px;border:1px solid #0d3b3b;text-align:right;">TOTAL</td>
                    <td style="padding:12px;text-align:center;border:1px solid #0d3b3b;"><?= $tot ?></td>
                    <td style="padding:12px;text-align:center;border:1px solid #0d3b3b;color:#16a085;font-size:1.15rem;"><?= $obt ?></td>
                    <td style="padding:12px;text-align:center;border:1px solid #0d3b3b;color:#27ae60;"><?= $pct ?>%</td>
                    <td style="padding:12px;text-align:center;border:1px solid #0d3b3b;"><?= htmlspecialchars($grade, ENT_QUOTES, 'UTF-8') ?></td>
                </tr>
            </tbody>
        </table>

        <!-- Result declaration -->
        <div style="background:linear-gradient(135deg,#0d3b3b,#16a085);color:#fff;padding:18px;border-radius:10px;text-align:center;margin-bottom:18px;">
            <div style="font-size:0.86rem;opacity:0.85;letter-spacing:1px;">RESULT</div>
            <div style="font-size:1.8rem;font-weight:700;letter-spacing:3px;margin:6px 0;"><?= $status === 'pass' ? 'PASSED' : 'FAILED' ?></div>
            <div style="font-size:0.92rem;opacity:0.9;"><?= htmlspecialchars($division, ENT_QUOTES, 'UTF-8') ?> · Grade: <strong><?= htmlspecialchars($grade, ENT_QUOTES, 'UTF-8') ?></strong></div>
        </div>

        <?php if (!empty($result['admin_remarks'])): ?>
        <div style="padding:10px;background:#fff8e1;border-left:4px solid #f39c12;border-radius:6px;font-size:0.82rem;color:#666;margin-bottom:18px;">
            <strong>Remarks:</strong> <?= htmlspecialchars($result['admin_remarks'], ENT_QUOTES, 'UTF-8') ?>
        </div>
        <?php endif; ?>

        <!-- Verification + signatures -->
        <div style="display:flex;justify-content:space-between;align-items:end;margin-top:24px;">
            <div style="font-size:0.74rem;color:#999;line-height:1.5;">
                <div><strong>Verify at:</strong></div>
                <div style="font-family:monospace;color:#1a4fa0;"><?= htmlspecialchars($verificationUrl, ENT_QUOTES, 'UTF-8') ?></div>
                <div style="margin-top:6px;">Marksheet ID: <code><?= htmlspecialchars($result['enrollment_no'] . '-' . $result['id'], ENT_QUOTES, 'UTF-8') ?></code></div>
                <div>Generated: <?= date('d M Y, g:i A') ?></div>
            </div>
            <div style="text-align:center;font-size:0.84rem;color:#666;">
                <div>Authorized Signatory</div>
                <div style="margin:24px 0 4px;border-top:1px solid #666;width:200px;"></div>
                <strong><?= htmlspecialchars($result['branch_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></strong>
            </div>
        </div>
    </div>
</div>
