<?php
/**
 * Super Admin — Generated Results List
 *
 * Upload to: /gmind/super-admin/Views/cert_gen/index.php
 *
 * Lists all certificate/marksheet records created via the Super Admin panel
 * (i.e., branch_id = 0 or NULL). All entries are auto-approved with status='generated'.
 */

$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$csrf = htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8');
?>
<style>
    .sa-stats-card { background:#fff; border-radius:12px; padding:18px 22px; box-shadow:0 2px 8px rgba(0,0,0,0.04); display:flex; align-items:center; gap:14px; }
    .sa-stats-card .stats-icon { width:48px; height:48px; border-radius:12px; display:flex; align-items:center; justify-content:center; font-size:1.3rem; color:#fff; }
    .sa-stats-card .stats-num { font-size:1.5rem; font-weight:700; color:#2c1e4a; line-height:1; }
    .sa-stats-card .stats-label { font-size:0.82rem; color:#667085; margin-top:4px; }
    .sa-table-card { background:#fff; border-radius:12px; padding:24px; box-shadow:0 2px 8px rgba(0,0,0,0.04); margin-top:24px; }
    .sa-table { width:100%; }
    .sa-table thead th { background:#f8f4ff; color:#6c3fe8; font-weight:700; font-size:0.82rem; text-transform:uppercase; letter-spacing:0.5px; padding:12px 14px; border-bottom:2px solid #6c3fe8; }
    .sa-table tbody td { padding:12px 14px; border-bottom:1px solid #f1f1f1; vertical-align:middle; font-size:0.9rem; }
    .sa-table tbody tr:hover { background:#faf8ff; }
    .badge-status { padding:5px 12px; border-radius:20px; font-size:0.75rem; font-weight:700; text-transform:uppercase; letter-spacing:0.5px; }
    .badge-status.generated { background:#dcfce7; color:#16a34a; }
    .badge-status.pending_approval { background:#fef3c7; color:#d97706; }
    .badge-status.rejected { background:#fee2e2; color:#dc2626; }
    .badge-doc-type { padding:3px 10px; border-radius:6px; font-size:0.7rem; font-weight:700; background:#ede9fe; color:#6c3fe8; text-transform:uppercase; }
    .sa-action-btn { padding:6px 12px; border-radius:6px; font-size:0.78rem; font-weight:600; text-decoration:none; display:inline-flex; align-items:center; gap:5px; }
    .sa-action-btn-view   { background:#dbeafe; color:#2563eb; }
    .sa-action-btn-view:hover   { background:#bfdbfe; }
    .sa-action-btn-download { background:#dcfce7; color:#16a34a; }
    .sa-action-btn-download:hover { background:#bbf7d0; }
    .sa-action-btn-disabled { background:#f3f4f6; color:#9ca3af; cursor:not-allowed; }
</style>

<div class="container-fluid">
    <!-- Page Actions -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 style="color:#2c1e4a; font-weight:700; margin:0;">Generated Results</h2>
            <p class="text-muted mb-0" style="font-size:0.88rem;">All certificate/marksheet records created directly by Super Admin (auto-approved, no branch assigned).</p>
        </div>
        <a href="<?= $bp ?>/super-admin/cert-gen/new" class="btn btn-primary" style="background:#6c3fe8; border-color:#6c3fe8; padding:10px 20px; font-weight:600;">
            <i class="fas fa-plus"></i> Create New Result
        </a>
    </div>

    <!-- Stats Row -->
    <div class="row mb-3">
        <div class="col-md-3">
            <div class="sa-stats-card">
                <div class="stats-icon" style="background:linear-gradient(135deg,#6c3fe8,#4a2db8);"><i class="fas fa-file-alt"></i></div>
                <div>
                    <div class="stats-num"><?= (int)($stats['total'] ?? 0) ?></div>
                    <div class="stats-label">Total Documents</div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="sa-stats-card">
                <div class="stats-icon" style="background:linear-gradient(135deg,#16a34a,#15803d);"><i class="fas fa-check-circle"></i></div>
                <div>
                    <div class="stats-num"><?= (int)($stats['generated'] ?? 0) ?></div>
                    <div class="stats-label">Auto-Approved</div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="sa-stats-card">
                <div class="stats-icon" style="background:linear-gradient(135deg,#d97706,#b45309);"><i class="fas fa-clock"></i></div>
                <div>
                    <div class="stats-num"><?= (int)($stats['pending'] ?? 0) ?></div>
                    <div class="stats-label">Pending (should be 0)</div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="sa-stats-card">
                <div class="stats-icon" style="background:linear-gradient(135deg,#dc2626,#b91c1c);"><i class="fas fa-times-circle"></i></div>
                <div>
                    <div class="stats-num"><?= (int)($stats['rejected'] ?? 0) ?></div>
                    <div class="stats-label">Rejected</div>
                </div>
            </div>
        </div>
    </div>

    <!-- Filter -->
    <div class="sa-table-card">
        <form method="GET" class="d-flex align-items-center gap-2 mb-3" style="flex-wrap:wrap;">
            <label class="form-label mb-0" style="font-weight:600; color:#2c1e4a;">Filter by Status:</label>
            <select name="status" class="form-select" style="width:auto;" onchange="this.form.submit()">
                <option value="">All (<?= (int)($stats['total'] ?? 0) ?>)</option>
                <option value="generated" <?= ($statusFilter === 'generated') ? 'selected' : '' ?>>Generated (<?= (int)($stats['generated'] ?? 0) ?>)</option>
                <option value="pending_approval" <?= ($statusFilter === 'pending_approval') ? 'selected' : '' ?>>Pending (<?= (int)($stats['pending'] ?? 0) ?>)</option>
                <option value="rejected" <?= ($statusFilter === 'rejected') ? 'selected' : '' ?>>Rejected (<?= (int)($stats['rejected'] ?? 0) ?>)</option>
            </select>
            <?php if (!empty($statusFilter)): ?>
                <a href="<?= $bp ?>/super-admin/cert-gen" class="btn btn-sm btn-secondary">Clear Filter</a>
            <?php endif; ?>
        </form>

        <!-- Table -->
        <div class="table-responsive">
            <table class="sa-table">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Cert No / Enrollment</th>
                        <th>Student</th>
                        <th>Course</th>
                        <th>Type</th>
                        <th>Grade</th>
                        <th>%</th>
                        <th>Issued</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($documents)): ?>
                    <tr>
                        <td colspan="10" class="text-center text-muted py-5">
                            <i class="fas fa-inbox" style="font-size:2.5rem; opacity:0.3;"></i>
                            <p class="mt-2 mb-0">No results found. Click <strong>"Create New Result"</strong> to generate your first certificate + marksheet.</p>
                        </td>
                    </tr>
                    <?php else: foreach ($documents as $i => $d): ?>
                    <tr>
                        <td><?= $i + 1 ?></td>
                        <td>
                            <div style="font-weight:600; color:#2c1e4a;"><?= htmlspecialchars($d['certificate_no'] ?? '—', ENT_QUOTES) ?></div>
                            <div class="text-muted" style="font-size:0.78rem;"><?= htmlspecialchars($d['enrollment_no'] ?? '—', ENT_QUOTES) ?></div>
                        </td>
                        <td><?= htmlspecialchars($d['student_name'] ?? '—', ENT_QUOTES) ?></td>
                        <td>
                            <div><?= htmlspecialchars($d['course_name'] ?? '—', ENT_QUOTES) ?></div>
                            <div class="text-muted" style="font-size:0.78rem;">Branch: <?= htmlspecialchars($d['branch_code'] ?? 'GMBR01', ENT_QUOTES) ?></div>
                        </td>
                        <td><span class="badge-doc-type"><?= htmlspecialchars(ucfirst($d['doc_type'] ?? 'certificate'), ENT_QUOTES) ?></span></td>
                        <td><strong><?= htmlspecialchars($d['grade'] ?? '—', ENT_QUOTES) ?></strong></td>
                        <td><?= htmlspecialchars(number_format((float)($d['percentage'] ?? 0), 2), ENT_QUOTES) ?>%</td>
                        <td><?= htmlspecialchars(!empty($d['issue_date']) ? date('d M Y', strtotime($d['issue_date'])) : '—', ENT_QUOTES) ?></td>
                        <td>
                            <?php
                            $status = $d['status'] ?? 'unknown';
                            $cls = 'generated';
                            if ($status === 'pending_approval') $cls = 'pending_approval';
                            elseif ($status === 'rejected') $cls = 'rejected';
                            ?>
                            <span class="badge-status <?= $cls ?>"><?= htmlspecialchars(ucfirst(str_replace('_', ' ', $status)), ENT_QUOTES) ?></span>
                        </td>
                        <td>
                            <?php if (($d['status'] ?? '') === 'generated' && !empty($d['pdf_path'])): ?>
                                <a href="<?= $bp ?>/super-admin/cert-gen/download/<?= (int)$d['id'] ?>?mode=view" target="_blank" class="sa-action-btn sa-action-btn-view" title="View PDF">
                                    <i class="fas fa-eye"></i> View
                                </a>
                                <a href="<?= $bp ?>/super-admin/cert-gen/download/<?= (int)$d['id'] ?>?mode=download" class="sa-action-btn sa-action-btn-download mt-1" title="Download PDF">
                                    <i class="fas fa-download"></i> Download
                                </a>
                                <a href="<?= $bp ?>/super-admin/cert-gen/regenerate/<?= (int)$d['id'] ?>" class="sa-action-btn sa-action-btn-view mt-1" title="Regenerate PDF" style="background:#fef3c7; color:#d97706;">
                                    <i class="fas fa-redo"></i> Regenerate
                                </a>
                            <?php elseif (($d['status'] ?? '') === 'generated'): ?>
                                <a href="<?= $bp ?>/super-admin/cert-gen/regenerate/<?= (int)$d['id'] ?>" class="sa-action-btn" style="background:#fee2e2; color:#dc2626;" title="PDF was not generated. Click to retry.">
                                    <i class="fas fa-exclamation-triangle"></i> Generate PDF
                                </a>
                            <?php else: ?>
                                <span class="sa-action-btn sa-action-btn-disabled"><i class="fas fa-ban"></i> N/A</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
