<?php $bp = defined('BASE_PATH') ? BASE_PATH : '/gmind'; $result = $result ?? []; $subjects = $subjects ?? []; $statusColors = ['draft'=>'#999','pending_approval'=>'#f39c12','approved'=>'#3498db','published'=>'#27ae60','rejected'=>'#e84118']; $divLabels = ['first'=>'First Division','second'=>'Second Division','third'=>'Third Division','fail'=>'Fail']; ?>
<div class="admin-section">
    <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px;flex-wrap:wrap;gap:10px;">
        <a href="<?= $bp ?>/branch/results" style="color:#16a085;text-decoration:none;font-size:0.85rem;">← Back</a>
        <div style="display:flex;gap:6px;">
            <?php if (($result['status'] ?? '') === 'draft' && ($result['fee_cleared'] ?? 0)): ?>
            <form method="POST" action="<?= $bp ?>/branch/results/<?= (int)($result['id']??0) ?>/publish" onsubmit="return confirm('Publish this result? Student will be able to see it.');"><input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>"><button type="submit" style="background:#27ae60;color:#fff;border:none;padding:6px 14px;border-radius:6px;font-size:0.82rem;cursor:pointer;">Publish</button></form>
            <?php endif; ?>
            <form method="POST" action="<?= $bp ?>/branch/results/<?= (int)($result['id']??0) ?>/delete" onsubmit="return confirm('Delete this result permanently?');"><input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>"><button type="submit" style="background:#e84118;color:#fff;border:none;padding:6px 14px;border-radius:6px;font-size:0.82rem;cursor:pointer;">Delete</button></form>
        </div>
    </div>
    <div style="background:linear-gradient(135deg,#16a085,#117a65);color:#fff;border-radius:12px;padding:20px;margin-bottom:16px;">
        <h2 style="margin:0;font-size:1.1rem;"><?= htmlspecialchars($result['student_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></h2>
        <p style="margin:4px 0 0;opacity:0.9;font-size:0.85rem;"><?= htmlspecialchars($result['enrollment_no'] ?? '', ENT_QUOTES, 'UTF-8') ?> · <?= htmlspecialchars($result['course_name'] ?? '', ENT_QUOTES, 'UTF-8') ?> · <span style="background:rgba(255,255,255,0.25);padding:2px 10px;border-radius:10px;font-size:0.76rem;"><?= htmlspecialchars(str_replace('_',' ',ucfirst($result['status'] ?? '')), ENT_QUOTES, 'UTF-8') ?></span></p>
    </div>
    <div style="display:grid;grid-template-columns:1fr 1fr;gap:16px;margin-bottom:16px;">
        <div style="background:#fff;border-radius:12px;padding:20px;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
            <h3 style="margin:0 0 12px;font-size:0.95rem;color:#0d3b3b;">Result Summary</h3>
            <div style="display:grid;grid-template-columns:1fr 1fr;gap:10px;">
                <div style="text-align:center;padding:14px;background:#eafaf1;border-radius:8px;"><div style="font-size:1.6rem;font-weight:700;color:#27ae60;"><?= (float)($result['percentage']??0) ?>%</div><div style="font-size:0.72rem;color:#999;">Percentage</div></div>
                <div style="text-align:center;padding:14px;background:#eaf0fb;border-radius:8px;"><div style="font-size:1.6rem;font-weight:700;color:#1a4fa0;"><?= htmlspecialchars($result['grade'] ?? '—', ENT_QUOTES, 'UTF-8') ?></div><div style="font-size:0.72rem;color:#999;">Grade</div></div>
            </div>
            <table style="width:100%;font-size:0.84rem;margin-top:12px;">
                <tr><td style="padding:5px 0;color:#999;">Marks Obtained</td><td style="padding:5px 0;text-align:right;font-weight:600;"><?= (float)($result['marks_obtained']??0) ?> / <?= (float)($result['total_marks']??0) ?></td></tr>
                <tr><td style="padding:5px 0;color:#999;">Division</td><td style="padding:5px 0;text-align:right;font-weight:600;"><?= htmlspecialchars($divLabels[$result['division']] ?? ucfirst($result['division'] ?? '—'), ENT_QUOTES, 'UTF-8') ?></td></tr>
                <tr><td style="padding:5px 0;color:#999;">Status</td><td style="padding:5px 0;text-align:right;font-weight:600;color:<?= ($result['result_status']??'')==='pass'?'#27ae60':'#e84118' ?>;"><?= htmlspecialchars(strtoupper($result['result_status'] ?? '—'), ENT_QUOTES, 'UTF-8') ?></td></tr>
                <tr><td style="padding:5px 0;color:#999;">Fee Cleared</td><td style="padding:5px 0;text-align:right;font-weight:600;"><?= ($result['fee_cleared']??0)?'<span style="color:#27ae60;">✓ Yes</span>':'<span style="color:#e84118;">✕ No (Due pending)</span>' ?></td></tr>
            </table>
        </div>
        <div style="background:#fff;border-radius:12px;padding:20px;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
            <h3 style="margin:0 0 12px;font-size:0.95rem;color:#0d3b3b;">Subject-wise Marks</h3>
            <?php if (empty($subjects)): ?><p style="color:#999;text-align:center;">No subject data.</p><?php else: ?>
            <table style="width:100%;font-size:0.82rem;border-collapse:collapse;">
                <thead><tr style="border-bottom:2px solid #dee2e6;"><th style="padding:6px;text-align:left;">Subject</th><th style="padding:6px;text-align:center;">Max</th><th style="padding:6px;text-align:center;">Obtained</th><th style="padding:6px;text-align:center;">%</th></tr></thead>
                <tbody>
                <?php foreach ($subjects as $s): $pct = $s['max']>0 ? round($s['obtained']/$s['max']*100) : 0; ?>
                <tr style="border-bottom:1px solid #f0f0f0;"><td style="padding:6px;"><?= htmlspecialchars($s['name'], ENT_QUOTES, 'UTF-8') ?></td><td style="padding:6px;text-align:center;"><?= (int)$s['max'] ?></td><td style="padding:6px;text-align:center;font-weight:600;"><?= (int)$s['obtained'] ?></td><td style="padding:6px;text-align:center;color:<?= $pct>=33?'#27ae60':'#e84118' ?>;"><?= $pct ?>%</td></tr>
                <?php endforeach; ?>
                </tbody>
            </table>
            <?php endif; ?>
            <?php if (!empty($result['remarks'])): ?><p style="font-size:0.8rem;color:#666;margin-top:10px;"><strong>Remarks:</strong> <?= htmlspecialchars($result['remarks'], ENT_QUOTES, 'UTF-8') ?></p><?php endif; ?>
        </div>
    </div>
</div>
