<?php
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$courses = $courses ?? [];
?>
<div class="admin-section">
    <div style="margin-bottom:20px;">
        <h1 style="margin:0;font-size:1.4rem;color:#2c1e4a;">📚 Course Subjects</h1>
        <p style="margin:6px 0 0;font-size:0.88rem;color:#666;">Add subjects with max marks per course. These auto-load when branches create results for certificates/marksheets.</p>
    </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:60px;color:#999;">No courses found. Create courses first from Website Admin → Courses.</div>
        <?php else: ?>
            <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;">Course Name</th>
                    <th style="padding:12px;text-align:center;">Duration</th>
                    <th style="padding:12px;text-align:center;">Subjects</th>
                    <th style="padding:12px;text-align:left;">Subject Details</th>
                    <th style="padding:12px;text-align:right;">Action</th>
                </tr></thead>
                <tbody>
                <?php foreach ($courses as $c): ?>
                    <tr style="border-bottom:1px solid #f0f0f0;">
                        <td style="padding:12px;font-weight:600;color:#2c1e4a;"><?= htmlspecialchars($c['name'], ENT_QUOTES, 'UTF-8') ?></td>
                        <td style="padding:12px;text-align:center;color:#666;"><?= htmlspecialchars($c['duration'] ?? '—', ENT_QUOTES, 'UTF-8') ?></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:<?= $c['subject_count'] > 0 ? '#27ae60' : '#e84118' ?>;">
                                <?= (int)$c['subject_count'] ?> subject(s)
                            </span>
                        </td>
                        <td style="padding:12px;font-size:0.78rem;color:#666;">
                            <?php if (!empty($c['parsed_subjects'])): ?>
                                <?php foreach ($c['parsed_subjects'] as $s): ?>
                                    <span style="display:inline-block;background:#f0f0f0;padding:2px 8px;border-radius:4px;margin:2px;"><?= htmlspecialchars($s['name'], ENT_QUOTES, 'UTF-8') ?> (<?= (int)$s['max_marks'] ?>)</span>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <span style="color:#e84118;">No subjects added</span>
                            <?php endif; ?>
                        </td>
                        <td style="padding:12px;text-align:right;">
                            <a href="<?= $bp ?>/super-admin/course-subjects/<?= (int)$c['id'] ?>/edit" style="background:#6c3fe8;color:#fff;text-decoration:none;padding:6px 14px;border-radius:4px;font-size:0.76rem;font-weight:600;">✎ Edit Subjects</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        <?php endif; ?>
    </div>
</div>
