<?php
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$document = $document ?? [];
$timeline = $timeline ?? [];
$statusColors = ['pending_approval'=>'#f39c12','generated'=>'#27ae60','rejected'=>'#e84118','cancelled'=>'#999'];
?>
<div class="admin-section">
    <div style="margin-bottom:20px;display:flex;justify-content:space-between;align-items:center;">
        <a href="<?= $bp ?>/branch/documents" style="color:#16a085;text-decoration:none;font-size:0.85rem;">← Back</a>
        <?php if (in_array($document['status'] ?? '', ['generated','issued','printed','delivered'], true)): ?>
            <a href="<?= $bp ?>/branch/documents/<?= (int)($document['id'] ?? 0) ?>/download" style="background:#27ae60;color:#fff;text-decoration:none;padding:8px 16px;border-radius:6px;font-size:0.85rem;font-weight:600;">⬇ Download PDF</a>
        <?php endif; ?>
    </div>

    <div class="admin-card" style="background:linear-gradient(135deg,#16a085,#117a65);color:#fff;border-radius:12px;padding:24px;margin-bottom:20px;">
        <h2 style="margin:0;font-size:1.2rem;"><?= htmlspecialchars($document['certificate_no'] ?? '', ENT_QUOTES, 'UTF-8') ?></h2>
        <p style="margin:4px 0 0;opacity:0.9;font-size:0.88rem;">
            <?= htmlspecialchars($document['student_name'] ?? '', ENT_QUOTES, 'UTF-8') ?> · <?= htmlspecialchars($document['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($document['status'] ?? '')), ENT_QUOTES, 'UTF-8') ?></span>
        </p>
    </div>

    <div class="admin-card" style="background:#fff;border-radius:12px;padding:20px;box-shadow:0 2px 8px rgba(0,0,0,0.06);margin-bottom:20px;">
        <h3 style="margin:0 0 14px;font-size:1rem;color:#0d3b3b;">Document Details</h3>
        <table style="width:100%;font-size:0.86rem;">
            <tr><td style="padding:6px 0;color:#999;">Document Number</td><td style="padding:6px 0;text-align:right;font-weight:600;font-family:monospace;"><?= htmlspecialchars($document['certificate_no'] ?? '', ENT_QUOTES, 'UTF-8') ?></td></tr>
            <tr><td style="padding:6px 0;color:#999;">Student Name</td><td style="padding:6px 0;text-align:right;font-weight:600;"><?= htmlspecialchars($document['student_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></td></tr>
            <tr><td style="padding:6px 0;color:#999;">Enrollment No</td><td style="padding:6px 0;text-align:right;font-weight:600;"><?= htmlspecialchars($document['enrollment_no'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
            <tr><td style="padding:6px 0;color:#999;">Course</td><td style="padding:6px 0;text-align:right;font-weight:600;"><?= htmlspecialchars($document['course_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
            <tr><td style="padding:6px 0;color:#999;">Grade</td><td style="padding:6px 0;text-align:right;font-weight:600;"><?= htmlspecialchars($document['grade'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
            <tr><td style="padding:6px 0;color:#999;">Percentage</td><td style="padding:6px 0;text-align:right;font-weight:600;"><?= htmlspecialchars($document['percentage'] ?? '0', ENT_QUOTES, 'UTF-8') ?>%</td></tr>
            <tr><td style="padding:6px 0;color:#999;">Issue Date</td><td style="padding:6px 0;text-align:right;font-weight:600;"><?= !empty($document['issue_date']) ? date('F j, Y', strtotime($document['issue_date'])) : '—' ?></td></tr>
            <tr><td style="padding:6px 0;color:#999;">Verification URL</td><td style="padding:6px 0;text-align:right;"><a href="<?= htmlspecialchars($document['verification_url'] ?? '#', ENT_QUOTES, 'UTF-8') ?>" target="_blank" style="color:#16a085;font-size:0.78rem;">Verify →</a></td></tr>
        </table>
    </div>

    <?php if (!empty($timeline)): ?>
    <div class="admin-card" style="background:#fff;border-radius:12px;padding:20px;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
        <h3 style="margin:0 0 14px;font-size:1rem;color:#0d3b3b;">Timeline</h3>
        <div style="display:flex;flex-direction:column;gap:8px;">
            <?php foreach ($timeline as $t): ?>
                <div style="display:flex;gap:10px;padding:8px 0;border-bottom:1px solid #f0f0f0;">
                    <div style="width:8px;height:8px;border-radius:50%;background:#16a085;margin-top:5px;flex-shrink:0;"></div>
                    <div>
                        <div style="font-weight:600;color:#0d3b3b;font-size:0.84rem;"><?= htmlspecialchars(ucfirst(str_replace('_',' ',$t['event'] ?? '')), ENT_QUOTES, 'UTF-8') ?></div>
                        <div style="font-size:0.76rem;color:#999;"><?= htmlspecialchars($t['description'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
                        <div style="font-size:0.72rem;color:#bbb;margin-top:2px;"><?= date('M j, Y g:i A', strtotime($t['created_at'] ?? 'now')) ?></div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
    <?php endif; ?>
</div>
