<?php
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$course = $course ?? [];
$subjects = $subjects ?? [];
$csrfToken = $csrfToken ?? '';
?>
<div class="admin-section">
    <div style="margin-bottom:14px;">
        <a href="<?= $bp ?>/super-admin/course-subjects" style="color:#6c3fe8;text-decoration:none;font-size:0.85rem;">← Back to Courses</a>
    </div>

    <div style="background:linear-gradient(135deg,#2c1e4a,#4a2db8);color:#fff;border-radius:12px;padding:18px;margin-bottom:18px;">
        <h1 style="margin:0;font-size:1.15rem;">📚 Subjects: <?= htmlspecialchars($course['name'] ?? '', ENT_QUOTES, 'UTF-8') ?></h1>
        <p style="margin:6px 0 0;opacity:0.9;font-size:0.84rem;">Add subjects and max marks. These will auto-load when branches create results.</p>
    </div>

    <form method="POST" action="<?= $bp ?>/super-admin/course-subjects/save" id="subjectForm">
        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken, ENT_QUOTES, 'UTF-8') ?>">
        <input type="hidden" name="course_id" value="<?= (int)$course['id'] ?>">

        <div style="background:#fff;border-radius:10px;padding:18px;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
            <table style="width:100%;border-collapse:collapse;font-size:0.86rem;" id="subjectsTable">
                <thead><tr style="background:#f8f9fa;border-bottom:2px solid #dee2e6;">
                    <th style="padding:10px;text-align:center;width:40px;">#</th>
                    <th style="padding:10px;text-align:left;">Subject Name</th>
                    <th style="padding:10px;text-align:center;width:120px;">Max Marks</th>
                    <th style="padding:10px;text-align:center;width:60px;">✕</th>
                </tr></thead>
                <tbody id="subjectsBody">
                    <?php if (empty($subjects)): ?>
                        <?php for ($i = 0; $i < 7; $i++): ?>
                        <tr>
                            <td style="padding:8px;text-align:center;color:#999;"><?= $i + 1 ?></td>
                            <td style="padding:8px;"><input type="text" name="subject_name[]" placeholder="e.g. Hindi, English, Maths" style="width:100%;padding:7px;border:1px solid #ddd;border-radius:4px;font-size:0.84rem;"></td>
                            <td style="padding:8px;text-align:center;"><input type="number" name="max_marks[]" value="100" min="1" style="width:80px;padding:7px;border:1px solid #ddd;border-radius:4px;font-size:0.84rem;text-align:center;"></td>
                            <td style="padding:8px;text-align:center;"><button type="button" onclick="this.closest('tr').remove()" style="background:none;border:none;color:#e84118;cursor:pointer;font-size:1rem;">✕</button></td>
                        </tr>
                        <?php endfor; ?>
                    <?php else: ?>
                        <?php foreach ($subjects as $i => $s): ?>
                        <tr>
                            <td style="padding:8px;text-align:center;color:#999;"><?= $i + 1 ?></td>
                            <td style="padding:8px;"><input type="text" name="subject_name[]" value="<?= htmlspecialchars($s['name'] ?? '', ENT_QUOTES, 'UTF-8') ?>" style="width:100%;padding:7px;border:1px solid #ddd;border-radius:4px;font-size:0.84rem;"></td>
                            <td style="padding:8px;text-align:center;"><input type="number" name="max_marks[]" value="<?= (int)($s['max_marks'] ?? 100) ?>" min="1" style="width:80px;padding:7px;border:1px solid #ddd;border-radius:4px;font-size:0.84rem;text-align:center;"></td>
                            <td style="padding:8px;text-align:center;"><button type="button" onclick="this.closest('tr').remove()" style="background:none;border:none;color:#e84118;cursor:pointer;font-size:1rem;">✕</button></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <button type="button" onclick="addRow()" style="margin-top:10px;padding:8px 16px;background:#6c3fe8;color:#fff;border:none;border-radius:6px;font-size:0.82rem;font-weight:600;cursor:pointer;">+ Add Subject</button>
        </div>

        <div style="display:flex;gap:12px;justify-content:flex-end;margin-top:16px;">
            <a href="<?= $bp ?>/super-admin/course-subjects" style="padding:12px 24px;border:1px solid #ddd;border-radius:8px;text-decoration:none;color:#666;font-weight:600;font-size:0.9rem;">Cancel</a>
            <button type="submit" style="padding:12px 32px;background:linear-gradient(135deg,#6c3fe8,#4a2db8);color:#fff;border:none;border-radius:8px;font-weight:600;font-size:0.9rem;cursor:pointer;">💾 Save Subjects</button>
        </div>
    </form>
</div>
<script>
var rowNum = document.querySelectorAll('#subjectsBody tr').length;
function addRow() {
    rowNum++;
    var tbody = document.getElementById('subjectsBody');
    var tr = document.createElement('tr');
    tr.innerHTML = '<td style="padding:8px;text-align:center;color:#999;">' + rowNum + '</td>' +
        '<td style="padding:8px;"><input type="text" name="subject_name[]" placeholder="Subject name" style="width:100%;padding:7px;border:1px solid #ddd;border-radius:4px;font-size:0.84rem;"></td>' +
        '<td style="padding:8px;text-align:center;"><input type="number" name="max_marks[]" value="100" min="1" style="width:80px;padding:7px;border:1px solid #ddd;border-radius:4px;font-size:0.84rem;text-align:center;"></td>' +
        '<td style="padding:8px;text-align:center;"><button type="button" onclick="this.closest(\'tr\').remove()" style="background:none;border:none;color:#e84118;cursor:pointer;font-size:1rem;">✕</button></td>';
    tbody.appendChild(tr);
}
</script>
