<?php
/** @var array $document @var array $timeline @var array $subjectsData @var float $walletBalance @var string $csrfToken */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$document = $document ?? [];
$timeline = $timeline ?? [];
$subjectsData = $subjectsData ?? [];
$walletBalance = $walletBalance ?? 0;
$csrfToken = $csrfToken ?? '';
$sc = ['pending_approval'=>'#f39c12','generated'=>'#27ae60','rejected'=>'#e84118'];
$sl = ['pending_approval'=>'Pending Approval','generated'=>'Approved & PDF Generated','rejected'=>'Rejected'];
?>
<div class="admin-section">
    <div style="margin-bottom:14px;">
        <a href="<?= $bp ?>/super-admin/documents" style="color:#6c3fe8;text-decoration:none;font-size:0.85rem;">← Back to Documents</a>
    </div>
    <div style="background:linear-gradient(135deg,#2c1e4a,#4a2db8);color:#fff;border-radius:12px;padding:20px;margin-bottom:20px;">
        <h1 style="margin:0;font-size:1.2rem;"><?= htmlspecialchars($document['certificate_no'] ?? '', ENT_QUOTES, 'UTF-8') ?></h1>
        <p style="margin:6px 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') ?> ·
            <?= htmlspecialchars($document['branch_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($sl[$document['status']] ?? ucfirst($document['status'] ?? ''), ENT_QUOTES, 'UTF-8') ?></span>
        </p>
    </div>
    <div style="display:grid;grid-template-columns:1fr 1fr;gap:16px;">
        <div>
            <div style="background:#fff;border-radius:10px;padding:18px;margin-bottom:16px;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
                <h3 style="margin:0 0 14px;font-size:0.95rem;color:#2c1e4a;">📋 Student Details</h3>
                <table style="width:100%;font-size:0.86rem;">
                    <tr><td style="padding:5px 0;color:#999;width:40%;">Student Name</td><td style="padding:5px 0;font-weight:600;"><?= htmlspecialchars($document['student_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
                    <tr><td style="padding:5px 0;color:#999;">Enrollment No</td><td style="padding:5px 0;font-weight:600;font-family:monospace;"><?= htmlspecialchars($document['enrollment_no'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
                    <tr><td style="padding:5px 0;color:#999;">Father's Name</td><td style="padding:5px 0;font-weight:600;"><?= htmlspecialchars($document['father_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
                    <tr><td style="padding:5px 0;color:#999;">Course</td><td style="padding:5px 0;font-weight:600;"><?= htmlspecialchars($document['course_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
                    <tr><td style="padding:5px 0;color:#999;">Branch</td><td style="padding:5px 0;font-weight:600;"><?= htmlspecialchars($document['branch_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?> (<?= htmlspecialchars($document['branch_code'] ?? '', ENT_QUOTES, 'UTF-8') ?>)</td></tr>
                </table>
            </div>
            <div style="background:#fff;border-radius:10px;padding:18px;margin-bottom:16px;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
                <h3 style="margin:0 0 14px;font-size:0.95rem;color:#2c1e4a;">📊 Result Details</h3>
                <div style="display:grid;grid-template-columns:repeat(4,1fr);gap:10px;margin-bottom:14px;">
                    <div style="text-align:center;padding:10px;background:#eaf0fb;border-radius:8px;"><div style="font-size:1.3rem;font-weight:700;color:#1a4fa0;"><?= htmlspecialchars($document['grade'] ?? '—', ENT_QUOTES, 'UTF-8') ?></div><div style="font-size:0.7rem;color:#999;">Grade</div></div>
                    <div style="text-align:center;padding:10px;background:#eafaf1;border-radius:8px;"><div style="font-size:1.3rem;font-weight:700;color:#27ae60;"><?= htmlspecialchars($document['percentage'] ?? '0', ENT_QUOTES, 'UTF-8') ?>%</div><div style="font-size:0.7rem;color:#999;">Percentage</div></div>
                    <div style="text-align:center;padding:10px;background:#fff5e6;border-radius:8px;"><div style="font-size:0.92rem;font-weight:700;color:#f39c12;"><?= htmlspecialchars($document['division'] ?? '—', ENT_QUOTES, 'UTF-8') ?></div><div style="font-size:0.7rem;color:#999;">Division</div></div>
                    <div style="text-align:center;padding:10px;background:#f4f0fa;border-radius:8px;"><div style="font-size:0.92rem;font-weight:700;color:#6c3fe8;"><?= htmlspecialchars($document['doc_type'] ?? '—', ENT_QUOTES, 'UTF-8') ?></div><div style="font-size:0.7rem;color:#999;">Type</div></div>
                </div>
                <?php if (!empty($subjectsData['subjects'])): ?>
                <table style="width:100%;font-size:0.82rem;border-collapse:collapse;">
                    <thead><tr style="background:#f8f9fa;"><th style="padding:6px;text-align:left;border:1px solid #ddd;">Subject</th><th style="padding:6px;text-align:center;border:1px solid #ddd;width:80px;">Max</th><th style="padding:6px;text-align:center;border:1px solid #ddd;width:80px;">Obtained</th></tr></thead>
                    <tbody>
                        <?php for ($i = 0; $i < count($subjectsData['subjects']); $i++): ?>
                        <tr><td style="padding:6px;border:1px solid #ddd;"><?= htmlspecialchars($subjectsData['subjects'][$i] ?? '', ENT_QUOTES, 'UTF-8') ?></td><td style="padding:6px;text-align:center;border:1px solid #ddd;"><?= (int)($subjectsData['max_marks'][$i] ?? 0) ?></td><td style="padding:6px;text-align:center;border:1px solid #ddd;font-weight:600;color:#27ae60;"><?= (int)($subjectsData['obtained_marks'][$i] ?? 0) ?></td></tr>
                        <?php endfor; ?>
                    </tbody>
                </table>
                <?php endif; ?>
                <?php if (!empty($subjectsData['from_label']) || !empty($subjectsData['to_label'])): ?>
                <p style="margin:10px 0 0;font-size:0.82rem;color:#666;">Session: <strong><?= htmlspecialchars($subjectsData['from_label'] ?? '', ENT_QUOTES, 'UTF-8') ?></strong> to <strong><?= htmlspecialchars($subjectsData['to_label'] ?? '', ENT_QUOTES, 'UTF-8') ?></strong></p>
                <?php endif; ?>
            </div>
        </div>
        <div>
            <div style="background:#fff;border-radius:10px;padding:18px;margin-bottom:16px;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
                <h3 style="margin:0 0 14px;font-size:0.95rem;color:#2c1e4a;">💳 Payment Details</h3>
                <table style="width:100%;font-size:0.86rem;">
                    <tr><td style="padding:5px 0;color:#999;width:50%;">Payment Mode</td><td style="padding:5px 0;font-weight:600;"><?= htmlspecialchars(ucfirst($document['payment_mode'] ?? '—'), ENT_QUOTES, 'UTF-8') ?></td></tr>
                    <tr><td style="padding:5px 0;color:#999;">Amount</td><td style="padding:5px 0;font-weight:600;">₹<?= number_format((float)($document['amount_paid'] ?? 0), 0) ?></td></tr>
                    <tr><td style="padding:5px 0;color:#999;">Transaction ID</td><td style="padding:5px 0;font-weight:600;font-family:monospace;"><?= htmlspecialchars($document['transaction_id'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
                    <tr><td style="padding:5px 0;color:#999;">Payment Date</td><td style="padding:5px 0;font-weight:600;"><?= !empty($document['payment_date']) ? date('d/m/Y', strtotime($document['payment_date'])) : '—' ?></td></tr>
                    <tr><td style="padding:5px 0;color:#999;">Branch Wallet</td><td style="padding:5px 0;font-weight:600;color:<?= $walletBalance >= 200 ? '#27ae60' : '#e84118' ?>;">₹<?= number_format($walletBalance, 0) ?></td></tr>
                </table>
            </div>
            <?php
            $status = $document['status'] ?? '';
            $isPending = !in_array($status, ['generated','approved','rejected','cancelled'], true);
            ?>
            <?php if ($isPending): ?>
            <div style="background:#fff;border-radius:10px;padding:18px;margin-bottom:16px;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
                <h3 style="margin:0 0 14px;font-size:0.95rem;color:#2c1e4a;">⚡ Actions</h3>
                <form method="POST" action="<?= $bp ?>/super-admin/documents/<?= (int)$document['id'] ?>/approve">
                    <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken, ENT_QUOTES, 'UTF-8') ?>">
                    <button type="submit" style="width:100%;padding:14px;background:linear-gradient(135deg,#27ae60,#1e8e4f);color:#fff;border:none;border-radius:8px;font-weight:700;font-size:0.92rem;cursor:pointer;margin-bottom:10px;" onclick="return confirm('Approve and generate PDF? This will deduct ₹<?= (float)($document['amount_paid'] ?? 0) ?> from branch wallet (if online).')">✓ Approve & Generate PDF</button>
                </form>
                <form method="POST" action="<?= $bp ?>/super-admin/documents/<?= (int)$document['id'] ?>/reject">
                    <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken, ENT_QUOTES, 'UTF-8') ?>">
                    <input type="text" name="reason" placeholder="Reason for rejection" style="width:100%;padding:8px;border:1px solid #ddd;border-radius:6px;font-size:0.84rem;margin-bottom:8px;">
                    <button type="submit" style="width:100%;padding:10px;background:#e84118;color:#fff;border:none;border-radius:8px;font-weight:600;font-size:0.86rem;cursor:pointer;" onclick="return confirm('Reject?')">✗ Reject</button>
                </form>
            </div>
            <?php elseif (in_array($status, ['generated','approved'], true)): ?>
            <div style="background:#eafaf1;border:1px solid #27ae60;border-radius:10px;padding:18px;margin-bottom:16px;">
                <h3 style="margin:0 0 6px;font-size:0.95rem;color:#1a7a4f;">✅ Approved & PDF Generated</h3>
                <p style="margin:0 0 14px;font-size:0.82rem;color:#666;">Branch can now download the PDF.</p>
                <!-- ✅ PDF Download/View Buttons -->
                <div style="display:flex;gap:8px;flex-wrap:wrap;margin-bottom:10px;">
                    <a href="<?= $bp ?>/super-admin/documents/<?= (int)$document['id'] ?>/pdf?mode=view"
                       target="_blank" style="background:#007bff;color:#fff;text-decoration:none;padding:10px 16px;border-radius:6px;font-size:0.82rem;font-weight:600;flex:1;text-align:center;">
                        👁 View PDF
                    </a>
                    <a href="<?= $bp ?>/super-admin/documents/<?= (int)$document['id'] ?>/pdf?mode=download"
                       style="background:#27ae60;color:#fff;text-decoration:none;padding:10px 16px;border-radius:6px;font-size:0.82rem;font-weight:600;flex:1;text-align:center;">
                        ⬇ Download PDF
                    </a>
                </div>
                <a href="<?= $bp ?>/super-admin/documents/<?= (int)$document['id'] ?>/combined"
                   style="background:linear-gradient(135deg,#16a085,#117a65);color:#fff;text-decoration:none;padding:12px 16px;border-radius:6px;font-size:0.82rem;font-weight:600;display:block;text-align:center;">
                    🔗 Download Combined (Cert + Marksheet)
                </a>
                <?php if (!empty($document['pdf_path'])): ?>
                <p style="margin:10px 0 0;font-size:0.72rem;color:#999;word-break:break-all;">
                    📁 <code><?= htmlspecialchars($document['pdf_path'], ENT_QUOTES, 'UTF-8') ?></code>
                </p>
                <?php endif; ?>
            </div>
            <!-- ✅ Inline PDF Preview -->
            <div style="background:#fff;border-radius:10px;overflow:hidden;box-shadow:0 2px 8px rgba(0,0,0,0.06);margin-bottom:16px;">
                <div style="background:#f8f9fa;padding:12px 18px;border-bottom:1px solid #e9ecef;">
                    <h3 style="margin:0;font-size:0.92rem;color:#2c1e4a;">📄 PDF Preview</h3>
                </div>
                <iframe src="<?= $bp ?>/super-admin/documents/<?= (int)$document['id'] ?>/pdf?mode=view#toolbar=1"
                        width="100%" height="500px"
                        style="border:none;display:block;"></iframe>
            </div>
            <?php endif; ?>
            <?php if (!empty($timeline)): ?>
            <div style="background:#fff;border-radius:10px;padding:18px;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
                <h3 style="margin:0 0 14px;font-size:0.95rem;color:#2c1e4a;">📅 Timeline</h3>
                <?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:#6c3fe8;margin-top:5px;flex-shrink:0;"></div>
                    <div>
                        <div style="font-weight:600;color:#2c1e4a;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;"><?= !empty($t['created_at']) ? date('d M Y, g:i A', strtotime($t['created_at'])) : '' ?></div>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>
