<?php
/**
 * Phase 5 — Marksheets Module
 *
 * Dedicated view for marksheet-type documents. Shows:
 *   - Stats cards (total / pending / generated / rejected)
 *   - "Generate Marksheet" quick-action button (links to /branch/documents/create?type=marksheet)
 *   - Filtered list of marksheets only
 *   - Status filter dropdown
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$documents = $documents ?? [];
$statusFilter = $statusFilter ?? '';
$stats = $stats ?? ['total' => 0, 'pending' => 0, 'generated' => 0, 'rejected' => 0];
$templates = $templates ?? [];
$csrfToken = $csrfToken ?? '';

$statusColors = [
    'pending_approval' => '#f39c12',
    'generated'        => '#27ae60',
    'printed'          => '#3498db',
    'delivered'        => '#16a085',
    'rejected'         => '#e84118',
    'cancelled'        => '#999',
];
$statusLabels = [
    'pending_approval' => 'Pending Approval',
    'generated'        => 'Generated',
    'printed'          => 'Printed',
    'delivered'        => 'Delivered',
    'rejected'         => 'Rejected',
    'cancelled'        => 'Cancelled',
];
?>
<div class="admin-section">
    <!-- Header with quick-action -->
    <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:20px;flex-wrap:wrap;gap:12px;">
        <div>
            <h2 style="margin:0;font-size:1.2rem;color:#0d3b3b;">📋 Marksheets</h2>
            <p style="margin:4px 0 0;font-size:0.82rem;color:#666;">Generate and manage student marksheets with subject-wise marks. Online = instant PDF, offline = pending Super Admin approval.</p>
        </div>
        <a href="<?= $bp ?>/branch/documents/create?type=marksheet" style="background:linear-gradient(135deg,#16a085,#117a65);color:#fff;text-decoration:none;padding:11px 22px;border-radius:8px;font-size:0.86rem;font-weight:600;box-shadow:0 4px 12px rgba(22,160,133,0.25);">
            + Generate Marksheet
        </a>
    </div>

    <!-- Stats cards -->
    <div style="display:grid;grid-template-columns:repeat(4,1fr);gap:10px;margin-bottom:18px;">
        <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 #0d3b3b;">
            <div style="font-size:1.6rem;font-weight:700;color:#0d3b3b;"><?= (int)$stats['total'] ?></div>
            <div style="font-size:0.72rem;color:#999;text-transform:uppercase;letter-spacing:0.5px;">Total Marksheets</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;letter-spacing:0.5px;">Pending Approval</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;letter-spacing:0.5px;">Generated (Ready)</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;letter-spacing:0.5px;">Rejected</div>
        </div>
    </div>

    <!-- Templates available (info box) -->
    <?php if (!empty($templates)): ?>
    <div style="background:#eaf0fb;border-left:4px solid #1a4fa0;padding:12px 16px;border-radius:8px;margin-bottom:16px;font-size:0.82rem;color:#1a4fa0;line-height:1.6;">
        <strong>📋 Available Marksheet Templates:</strong>
        <?php foreach ($templates as $t): ?>
            <span style="display:inline-block;background:#fff;padding:3px 10px;border-radius:12px;font-size:0.76rem;margin:3px;border:1px solid #d0d7e3;"><?= htmlspecialchars($t['name'], ENT_QUOTES, 'UTF-8') ?></span>
        <?php endforeach; ?>
        <br>
        <span style="font-size:0.76rem;color:#666;margin-top:4px;display:inline-block;">To add more templates, contact the Super Admin.</span>
    </div>
    <?php else: ?>
    <div style="background:#fff5f5;border-left:4px solid #e84118;padding:12px 16px;border-radius:8px;margin-bottom:16px;font-size:0.82rem;color:#e84118;line-height:1.6;">
        <strong>⚠ No marksheet templates available.</strong> Please ask the Super Admin to create marksheet templates first (Super Admin → Certificate Templates → Create, type = Marksheet).
    </div>
    <?php endif; ?>

    <!-- Status filter -->
    <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 by status:</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>
                <?php foreach ($statusLabels as $v => $l): ?>
                    <option value="<?= $v ?>" <?= $statusFilter === $v ? 'selected' : '' ?>><?= $l ?></option>
                <?php endforeach; ?>
            </select>
            <?php if ($statusFilter): ?>
                <a href="<?= $bp ?>/branch/marksheets" style="color:#999;text-decoration:none;font-size:0.78rem;">✕ Clear filter</a>
            <?php endif; ?>
        </form>
    </div>

    <!-- Marksheets table -->
    <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="font-size:1rem;margin:0 0 6px;color:#666;">No marksheets yet</p>
                <p style="font-size:0.84rem;margin:0 0 16px;">Click "Generate Marksheet" above to create your first marksheet.</p>
                <a href="<?= $bp ?>/branch/documents/create?type=marksheet" style="background:#16a085;color:#fff;text-decoration:none;padding:9px 20px;border-radius:6px;font-size:0.82rem;font-weight:600;">+ Generate Marksheet</a>
            </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:11px;text-align:left;">Marksheet No</th>
                        <th style="padding:11px;text-align:left;">Student</th>
                        <th style="padding:11px;text-align:left;">Course</th>
                        <th style="padding:11px;text-align:center;">Division</th>
                        <th style="padding:11px;text-align:center;">%</th>
                        <th style="padding:11px;text-align:center;">Status</th>
                        <th style="padding:11px;text-align:left;">Generated</th>
                        <th style="padding:11px;text-align:right;">Actions</th>
                    </tr></thead>
                    <tbody>
                    <?php foreach ($documents as $d): ?>
                        <tr style="border-bottom:1px solid #f0f0f0;">
                            <td style="padding:11px;font-family:monospace;font-weight:600;color:#16a085;"><?= htmlspecialchars($d['certificate_no'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                            <td style="padding:11px;font-weight:600;color:#0d3b3b;"><?= htmlspecialchars($d['student_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?><br><span style="font-weight:400;color:#999;font-size:0.74rem;"><?= htmlspecialchars($d['enrollment_no'] ?? '', ENT_QUOTES, 'UTF-8') ?></span></td>
                            <td style="padding:11px;color:#666;font-size:0.8rem;"><?= htmlspecialchars($d['course_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
                            <td style="padding:11px;text-align:center;font-weight:600;color:#1a4fa0;"><?= htmlspecialchars($d['grade'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
                            <td style="padding:11px;text-align:center;font-weight:600;color:#16a085;"><?= htmlspecialchars($d['percentage'] ?? '0', ENT_QUOTES, 'UTF-8') ?>%</td>
                            <td style="padding:11px;text-align:center;">
                                <span style="padding:3px 10px;border-radius:12px;font-size:0.72rem;font-weight:600;color:#fff;background:<?= $statusColors[$d['status']] ?? '#999' ?>;">
                                    <?= htmlspecialchars($statusLabels[$d['status']] ?? ucfirst($d['status'] ?? ''), ENT_QUOTES, 'UTF-8') ?>
                                </span>
                            </td>
                            <td style="padding:11px;font-size:0.76rem;color:#999;"><?= !empty($d['created_at']) ? date('d M Y', strtotime($d['created_at'])) : '—' ?></td>
                            <td style="padding:11px;text-align:right;white-space:nowrap;">
                                <a href="<?= $bp ?>/branch/documents/<?= (int)$d['id'] ?>" style="background:#3498db;color:#fff;text-decoration:none;padding:4px 10px;border-radius:4px;font-size:0.72rem;font-weight:600;">View</a>
                                <?php if (in_array($d['status'] ?? '', ['generated','printed','delivered'], true)): ?>
                                    <a href="<?= $bp ?>/branch/documents/<?= (int)$d['id'] ?>/download" style="background:#27ae60;color:#fff;text-decoration:none;padding:4px 10px;border-radius:4px;font-size:0.72rem;font-weight:600;">PDF</a>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>

    <!-- Workflow help -->
    <div style="margin-top:18px;padding:14px;background:#eafaf1;border-radius:8px;font-size:0.8rem;color:#0d3b3b;line-height:1.6;">
        <strong>📌 Marksheet Generation Workflow:</strong>
        <ol style="margin:6px 0 0 18px;padding:0;">
            <li>Click <strong>"+ Generate Marksheet"</strong> above.</li>
            <li>Select a student → their details auto-fill.</li>
            <li>Select the marksheet template (set up by Super Admin).</li>
            <li>Enter subject-wise marks (subject name, max marks, obtained marks).</li>
            <li>System auto-calculates: total marks, percentage, grade, division.</li>
            <li>Choose payment mode:
                <ul>
                    <li><strong>Online/UPI/Wallet</strong> → cost deducted from branch wallet → PDF generated instantly.</li>
                    <li><strong>Offline</strong> → status = "Pending Approval" → Super Admin reviews → approves → PDF unlocked.</li>
                </ul>
            </li>
            <li>Once approved, the marksheet PDF can be downloaded and printed.</li>
            <li>Each marksheet has a unique marksheet number + QR code + verification URL (anyone can verify at /verify/MS_NO).</li>
        </ol>
    </div>
</div>
