<?php
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$documents = $documents ?? [];
$typeFilter = $typeFilter ?? '';
$statusFilter = $statusFilter ?? '';
$typeLabels = ['certificate'=>'Certificate','marksheet'=>'Marksheet','id_card'=>'ID Card','admit_card'=>'Admit Card','affiliation'=>'Affiliation','authorization'=>'Authorization','renewal'=>'Renewal','franchise_agreement'=>'Franchise Agreement'];
$statusColors = ['pending_approval'=>'#f39c12','generated'=>'#27ae60','printed'=>'#3498db','delivered'=>'#16a085','issued'=>'#27ae60','rejected'=>'#e84118','cancelled'=>'#999'];
?>
<div class="admin-section">
    <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:20px;flex-wrap:wrap;gap:12px;">
        <form method="GET" style="display:flex;gap:8px;">
            <select name="type" onchange="this.form.submit()" style="padding:8px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.85rem;">
                <option value="">All Types</option>
                <?php foreach ($typeLabels as $v=>$l): ?><option value="<?= $v ?>" <?= $typeFilter===$v?'selected':'' ?>><?= $l ?></option><?php endforeach; ?>
            </select>
            <select name="status" onchange="this.form.submit()" style="padding:8px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.85rem;">
                <option value="">All Status</option>
                <option value="pending_approval" <?= $statusFilter==='pending_approval'?'selected':'' ?>>Pending</option>
                <option value="generated" <?= $statusFilter==='generated'?'selected':'' ?>>Generated</option>
                <option value="rejected" <?= $statusFilter==='rejected'?'selected':'' ?>>Rejected</option>
            </select>
        </form>
        <a href="<?= $bp ?>/branch/documents/create" class="admin-btn" style="background:#16a085;color:#fff;text-decoration:none;padding:10px 20px;border-radius:8px;font-size:0.88rem;font-weight:600;">+ Generate Document</a>
    </div>

    <div class="admin-card" style="background:#fff;border-radius:10px;padding:0;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;">
                <p style="font-size:16px;margin:0 0 6px;">No documents yet</p>
                <p style="font-size:13px;margin:0;">Generate your first certificate or marksheet.</p>
            </div>
        <?php else: ?>
            <div style="overflow-x:auto;">
                <table class="admin-table" style="width:100%;border-collapse:collapse;font-size:0.86rem;">
                    <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;">Student</th>
                        <th style="padding:12px;text-align:left;">Type</th>
                        <th style="padding:12px;text-align:left;">Course</th>
                        <th style="padding:12px;text-align:center;">Status</th>
                        <th style="padding:12px;text-align:left;">Date</th>
                        <th style="padding:12px;text-align:right;">Actions</th>
                    </tr></thead>
                    <tbody>
                    <?php foreach ($documents as $d): ?>
                        <tr style="border-bottom:1px solid #f0f0f0;">
                            <td style="padding:12px;font-family:monospace;font-weight:600;color:#16a085;"><?= htmlspecialchars($d['certificate_no'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                            <td style="padding:12px;"><?= htmlspecialchars($d['student_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
                            <td style="padding:12px;"><?= htmlspecialchars($typeLabels[$d['doc_type']] ?? ucfirst($d['doc_type'] ?? ''), ENT_QUOTES, 'UTF-8') ?></td>
                            <td style="padding:12px;color:#666;font-size:0.8rem;"><?= htmlspecialchars($d['course_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
                            <td style="padding:12px;text-align:center;"><span style="padding:3px 10px;border-radius:20px;font-size:0.72rem;font-weight:600;color:#fff;background:<?= $statusColors[$d['status']] ?? '#999' ?>;"><?= htmlspecialchars(str_replace('_',' ',ucfirst($d['status'] ?? '')), ENT_QUOTES, 'UTF-8') ?></span></td>
                            <td style="padding:12px;font-size:0.8rem;color:#999;"><?= date('M j, Y', strtotime($d['created_at'] ?? 'now')) ?></td>
                            <td style="padding:12px;text-align:right;">
                                <a href="<?= $bp ?>/branch/documents/<?= (int)$d['id'] ?>" class="admin-btn" style="background:#3498db;color:#fff;text-decoration:none;padding:5px 10px;border-radius:4px;font-size:0.74rem;">View</a>
                                <?php if (in_array($d['status'] ?? '', ['generated','issued','printed','delivered'], true)): ?>
                                    <a href="<?= $bp ?>/branch/documents/<?= (int)$d['id'] ?>/download" class="admin-btn" style="background:#27ae60;color:#fff;text-decoration:none;padding:5px 10px;border-radius:4px;font-size:0.74rem;">PDF</a>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>
