<?php
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$courses = $courses ?? [];
$catColors = ['computer'=>'#1a4fa0','accounting'=>'#27ae60','programming'=>'#f39c12','design'=>'#e84118','other'=>'#9b59b6'];
?>
<div class="admin-section">
    <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px;">
        <h2 style="margin:0;font-size:1.1rem;color:#2c1e4a;">Courses (Global)</h2>
        <a href="<?= $bp ?>/super-admin/courses/create" style="background:#27ae60;color:#fff;text-decoration:none;padding:10px 20px;border-radius:8px;font-size:0.86rem;font-weight:600;">+ Add Course</a>
    </div>

    <div style="background:#eaf0fb;border-left:4px solid:#6c3fe8;padding:10px 14px;border-radius:6px;margin-bottom:14px;font-size:0.82rem;color:#2c1e4a;">
        <strong>ℹ️</strong> Courses are created here (global). Branch admins can only set the fee for their center — they cannot add/edit courses.
    </div>

    <div style="background:#fff;border-radius:10px;overflow:hidden;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
        <?php if (empty($courses)): ?>
            <div style="text-align:center;padding:50px 20px;color:#999;"><p style="margin:0;">No courses yet. Create your first course.</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:10px;text-align:left;">Course Name</th>
                        <th style="padding:10px;text-align:left;">Category</th>
                        <th style="padding:10px;text-align:left;">Duration</th>
                        <th style="padding:10px;text-align:right;">Default Fee</th>
                        <th style="padding:10px;text-align:center;">Subjects</th>
                        <th style="padding:10px;text-align:center;">Active</th>
                        <th style="padding:10px;text-align:right;">Actions</th>
                    </tr></thead>
                    <tbody>
                    <?php foreach ($courses as $c):
                        $subjects = [];
                        if (!empty($c['subjects'])) { $decoded = json_decode($c['subjects'], true); if (is_array($decoded)) $subjects = $decoded; }
                    ?>
                        <tr style="border-bottom:1px solid #f0f0f0;">
                            <td style="padding:10px;font-weight:600;color:#2c1e4a;">
                                <?php if (!empty($c['image'])): ?><img src="<?= $bp ?>/<?= htmlspecialchars($c['image'], ENT_QUOTES, 'UTF-8') ?>" alt="" style="width:30px;height:30px;border-radius:4px;object-fit:cover;vertical-align:middle;margin-right:6px;"><?php endif; ?>
                                <?= htmlspecialchars($c['name'], ENT_QUOTES, 'UTF-8') ?>
                            </td>
                            <td style="padding:10px;"><span style="padding:2px 8px;border-radius:10px;font-size:0.7rem;font-weight:600;color:#fff;background:<?= $catColors[$c['category']] ?? '#999' ?>;"><?= htmlspecialchars($c['category'], ENT_QUOTES, 'UTF-8') ?></span></td>
                            <td style="padding:10px;color:#666;"><?= htmlspecialchars($c['duration'], ENT_QUOTES, 'UTF-8') ?></td>
                            <td style="padding:10px;text-align:right;font-weight:600;">₹<?= number_format((float)$c['fee'], 0) ?></td>
                            <td style="padding:10px;text-align:center;color:#666;"><?= count($subjects) ?> subjects</td>
                            <td style="padding:10px;text-align:center;"><?= $c['active'] ? '<span style="color:#27ae60;">✓</span>' : '<span style="color:#999;">✕</span>' ?></td>
                            <td style="padding:10px;text-align:right;">
                                <a href="<?= $bp ?>/super-admin/courses/<?= (int)$c['id'] ?>/edit" style="background:#f39c12;color:#fff;text-decoration:none;padding:4px 10px;border-radius:4px;font-size:0.74rem;">Edit</a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>
