<?php
/** @var array $documents @var array $stats @var string $statusFilter @var string $csrfToken */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$documents = $documents ?? [];
$stats = $stats ?? ['total'=>0,'pending'=>0,'generated'=>0,'rejected'=>0];
$statusFilter = $statusFilter ?? '';
$csrfToken = $csrfToken ?? '';
$sc = ['pending_approval'=>'#f39c12','pending'=>'#f39c12','draft'=>'#f39c12','generated'=>'#27ae60','approved'=>'#27ae60','rejected'=>'#e84118','cancelled'=>'#999'];
$sl = ['pending_approval'=>'Pending Approval','pending'=>'Pending','draft'=>'Draft','generated'=>'Approved','approved'=>'Approved','rejected'=>'Rejected','cancelled'=>'Cancelled'];
$dt = ['certificate'=>'📜 Certificate','marksheet'=>'📋 Marksheet'];
?>
<div class="admin-section">
    <div style="margin-bottom:20px;">
        <h1 style="margin:0;font-size:1.4rem;color:#2c1e4a;">📤 Document Approvals</h1>
        <p style="margin:6px 0 0;font-size:0.88rem;color:#666;">Review and approve certificate/marksheet requests. Click "Approve & PDF" to approve and generate PDF.</p>
    </div>
    <div style="display:grid;grid-template-columns:repeat(4,1fr);gap:10px;margin-bottom:20px;">
        <div style="background:#fff;border-radius:10px;padding:14px;text-align:center;box-shadow:0 2px 8px rgba(0,0,0,0.06);border-left:4px solid #2c1e4a;"><div style="font-size:1.6rem;font-weight:700;color:#2c1e4a;"><?= (int)$stats['total'] ?></div><div style="font-size:0.72rem;color:#999;text-transform:uppercase;">Total</div></div>
        <div style="background:#fff;border-radius:10px;padding:14px;text-align:center;box-shadow:0 2px 8px rgba(0,0,0,0.06);border-left:4px solid #f39c12;"><div style="font-size:1.6rem;font-weight:700;color:#f39c12;"><?= (int)$stats['pending'] ?></div><div style="font-size:0.72rem;color:#999;text-transform:uppercase;">Pending</div></div>
        <div style="background:#fff;border-radius:10px;padding:14px;text-align:center;box-shadow:0 2px 8px rgba(0,0,0,0.06);border-left:4px solid #27ae60;"><div style="font-size:1.6rem;font-weight:700;color:#27ae60;"><?= (int)$stats['generated'] ?></div><div style="font-size:0.72rem;color:#999;text-transform:uppercase;">Approved</div></div>
        <div style="background:#fff;border-radius:10px;padding:14px;text-align:center;box-shadow:0 2px 8px rgba(0,0,0,0.06);border-left:4px solid #e84118;"><div style="font-size:1.6rem;font-weight:700;color:#e84118;"><?= (int)$stats['rejected'] ?></div><div style="font-size:0.72rem;color:#999;text-transform:uppercase;">Rejected</div></div>
    </div>
    <div style="margin-bottom:14px;">
        <form method="GET" style="display:flex;gap:8px;align-items:center;">
            <span style="font-size:0.82rem;color:#666;">Filter:</span>
            <select name="status" onchange="this.form.submit()" style="padding:7px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.84rem;">
                <option value="">All Status</option>
                <option value="pending_approval" <?= $statusFilter==='pending_approval'?'selected':'' ?>>Pending Approval</option>
                <option value="generated" <?= $statusFilter==='generated'?'selected':'' ?>>Approved</option>
                <option value="rejected" <?= $statusFilter==='rejected'?'selected':'' ?>>Rejected</option>
            </select>
        </form>
    </div>
    <div style="background:#fff;border-radius:10px;overflow:hidden;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
        <?php if (empty($documents)): ?>
            <div style="text-align:center;padding:60px 20px;color:#999;">
                <div style="font-size:3rem;margin-bottom:10px;">📭</div>
                <p style="margin:0;font-size:1rem;color:#666;">No document requests found</p>
                <p style="margin:4px 0 0;font-size:0.84rem;">When a branch submits a result, it will appear here.</p>
            </div>
        <?php else: ?>
            <div style="overflow-x:auto;">
                <table style="width:100%;border-collapse:collapse;font-size:0.84rem;">
                    <thead><tr style="background:#f8f9fa;border-bottom:2px solid #dee2e6;">
                        <th style="padding:12px;text-align:left;">Doc Number</th>
                        <th style="padding:12px;text-align:left;">Type</th>
                        <th style="padding:12px;text-align:left;">Student</th>
                        <th style="padding:12px;text-align:left;">Branch</th>
                        <th style="padding:12px;text-align:center;">Grade</th>
                        <th style="padding:12px;text-align:center;">%</th>
                        <th style="padding:12px;text-align:center;">Payment</th>
                        <th style="padding:12px;text-align:center;">Status</th>
                        <th style="padding:12px;text-align:right;">Actions</th>
                    </tr></thead>
                    <tbody>
                    <?php foreach ($documents as $d):
                        $status = $d['status'] ?? '';
                        $isPending = !in_array($status, ['generated','approved','rejected','cancelled'], true);
                        $isApproved = in_array($status, ['generated','approved'], true);
                        $isRejected = ($status === 'rejected');
                    ?>
                        <tr style="border-bottom:1px solid #f0f0f0;<?= $isPending ? 'background:#fffdf5;' : '' ?>">
                            <td style="padding:12px;font-family:monospace;font-weight:600;color:#6c3fe8;"><?= htmlspecialchars($d['certificate_no'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                            <td style="padding:12px;"><?= $dt[$d['doc_type']] ?? htmlspecialchars(ucfirst($d['doc_type'] ?? ''), ENT_QUOTES, 'UTF-8') ?></td>
                            <td style="padding:12px;font-weight:600;color:#2c1e4a;"><?= htmlspecialchars($d['student_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
                            <td style="padding:12px;color:#666;font-size:0.8rem;"><?= htmlspecialchars($d['branch_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?> (<?= htmlspecialchars($d['branch_code'] ?? '', ENT_QUOTES, 'UTF-8') ?>)</td>
                            <td style="padding:12px;text-align:center;font-weight:600;color:#1a4fa0;"><?= htmlspecialchars($d['grade'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
                            <td style="padding:12px;text-align:center;font-weight:600;color:#16a085;"><?= htmlspecialchars($d['percentage'] ?? '0', ENT_QUOTES, 'UTF-8') ?>%</td>
                            <td style="padding:12px;text-align:center;font-size:0.76rem;">
                                <?= htmlspecialchars(ucfirst($d['payment_mode'] ?? '—'), ENT_QUOTES, 'UTF-8') ?>
                                <?php if (!empty($d['transaction_id'])): ?>
                                    <br><span style="color:#999;font-size:0.68rem;font-family:monospace;">Txn: <?= htmlspecialchars($d['transaction_id'], ENT_QUOTES, 'UTF-8') ?></span>
                                <?php endif; ?>
                            </td>
                            <td style="padding:12px;text-align:center;">
                                <span style="padding:3px 10px;border-radius:12px;font-size:0.72rem;font-weight:600;color:#fff;background:<?= $sc[$status] ?? '#999' ?>;">
                                    <?= htmlspecialchars($sl[$status] ?? ucfirst($status), ENT_QUOTES, 'UTF-8') ?>
                                </span>
                            </td>
                            <td style="padding:12px;text-align:right;white-space:nowrap;">
                                <?php if ($isPending): ?>
                                    <form method="POST" action="<?= $bp ?>/super-admin/documents/<?= (int)$d['id'] ?>/approve" style="display:inline;">
                                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken, ENT_QUOTES, 'UTF-8') ?>">
                                        <button type="submit" style="background:#27ae60;color:#fff;border:none;padding:8px 18px;border-radius:4px;font-size:0.78rem;font-weight:700;cursor:pointer;" onclick="return confirm('Approve and generate PDF?')">✓ Approve & PDF</button>
                                    </form>
                                    <form method="POST" action="<?= $bp ?>/super-admin/documents/<?= (int)$d['id'] ?>/reject" style="display:inline-block;margin-left:4px;" onsubmit="return confirm('Reject this document?')">
                                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken, ENT_QUOTES, 'UTF-8') ?>">
                                        <input type="text" name="reason" placeholder="Reason" style="padding:4px 8px;border:1px solid #ddd;border-radius:4px;font-size:0.74rem;width:80px;">
                                        <button type="submit" style="background:#e84118;color:#fff;border:none;padding:6px 12px;border-radius:4px;font-size:0.74rem;font-weight:600;cursor:pointer;">✗</button>
                                    </form>
                                <?php elseif ($isApproved): ?>
                                    <span style="color:#27ae60;font-size:0.78rem;font-weight:600;">✅ Approved</span>
                                <?php elseif ($isRejected): ?>
                                    <span style="color:#e84118;font-size:0.78rem;">✗ Rejected</span>
                                <?php else: ?>
                                    <span style="color:#999;font-size:0.74rem;">—</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>
