<?php $basePath = defined('BASE_PATH') ? BASE_PATH : '/gmind'; ?>
<!-- Courses Management -->
<div class="admin-section">
    <?php if (!($showForm ?? false)): ?>
    <div class="admin-section-header">
        <div class="admin-filter-group">
            <select class="admin-form-select" id="categoryFilter">
                <option value="">All Categories</option>
                <option value="computer" <?= (($_GET['category'] ?? '') === 'computer') ? 'selected' : '' ?>>Computer</option>
                <option value="accounting" <?= (($_GET['category'] ?? '') === 'accounting') ? 'selected' : '' ?>>Accounting</option>
                <option value="programming" <?= (($_GET['category'] ?? '') === 'programming') ? 'selected' : '' ?>>Programming</option>
                <option value="design" <?= (($_GET['category'] ?? '') === 'design') ? 'selected' : '' ?>>Design</option>
                <option value="other" <?= (($_GET['category'] ?? '') === 'other') ? 'selected' : '' ?>>Other</option>
            </select>
        </div>
        <a href="<?= $basePath ?>/admin/courses/create" class="admin-btn admin-btn-primary">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="18" height="18"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
            Add New Course
        </a>
    </div>
    <?php endif; ?>

    <?php if ($showForm ?? false): ?>
    <!-- Add/Edit Course Form -->
    <div class="admin-card" style="margin-bottom: 24px;">
        <div class="admin-card-header">
            <h2 class="admin-card-title"><?= $editCourse ? 'Edit Course' : 'Add New Course' ?></h2>
        </div>
        <div class="admin-card-body">
            <form method="POST" action="<?= $editCourse ? $basePath . '/admin/courses/' . (int)$editCourse['id'] : $basePath . '/admin/courses' ?>" enctype="multipart/form-data" class="admin-form" novalidate>
                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">

                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label for="name" class="admin-form-label">Course Name <span class="admin-required">*</span></label>
                        <input type="text" id="name" name="name" class="admin-form-input" required
                               value="<?= htmlspecialchars($editCourse['name'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                    <div class="admin-form-group">
                        <label for="category" class="admin-form-label">Category</label>
                        <select id="category" name="category" class="admin-form-select">
                            <option value="computer" <?= ($editCourse['category'] ?? 'computer') === 'computer' ? 'selected' : '' ?>>Computer</option>
                            <option value="accounting" <?= ($editCourse['category'] ?? '') === 'accounting' ? 'selected' : '' ?>>Accounting</option>
                            <option value="programming" <?= ($editCourse['category'] ?? '') === 'programming' ? 'selected' : '' ?>>Programming</option>
                            <option value="design" <?= ($editCourse['category'] ?? '') === 'design' ? 'selected' : '' ?>>Design</option>
                            <option value="other" <?= ($editCourse['category'] ?? '') === 'other' ? 'selected' : '' ?>>Other</option>
                        </select>
                    </div>
                </div>

                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label for="duration" class="admin-form-label">Duration</label>
                        <input type="text" id="duration" name="duration" class="admin-form-input" placeholder="e.g. 6 Months"
                               value="<?= htmlspecialchars($editCourse['duration'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                    <div class="admin-form-group">
                        <label for="fee" class="admin-form-label">Fee (INR)</label>
                        <input type="number" id="fee" name="fee" class="admin-form-input" min="0" step="0.01"
                               value="<?= htmlspecialchars($editCourse['fee'] ?? '0', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                </div>

                <div class="admin-form-group">
                    <label for="description" class="admin-form-label">Description</label>
                    <textarea id="description" name="description" class="admin-form-textarea" rows="4"><?= htmlspecialchars($editCourse['description'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea>
                </div>

                <div class="admin-form-group">
                    <label for="syllabus" class="admin-form-label">Syllabus</label>
                    <textarea id="syllabus" name="syllabus" class="admin-form-textarea" rows="6"><?= htmlspecialchars($editCourse['syllabus'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea>
                </div>

                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label for="image" class="admin-form-label">Course Image</label>
                        <input type="file" id="image" name="image" class="admin-form-input" accept="image/jpeg,image/png,image/gif,image/webp"
                               data-preview="imagePreview">
                        <?php if ($editCourse && !empty($editCourse['image'])): ?>
                        <div class="admin-current-image">
                            <span>Current:</span>
                            <img src="<?= htmlspecialchars(upload_url($editCourse['image']), ENT_QUOTES, 'UTF-8') ?>" alt="Current course" height="60">
                        </div>
                        <?php endif; ?>
                        <div id="imagePreview" class="admin-image-preview"></div>
                    </div>
                    <div class="admin-form-group">
                        <label class="admin-form-label">Options</label>
                        <label class="admin-toggle">
                            <input type="checkbox" name="is_featured" value="1" <?= ($editCourse['is_featured'] ?? 0) ? 'checked' : '' ?>>
                            <span class="admin-toggle-slider"></span>
                            <span class="admin-toggle-label">Featured</span>
                        </label>
                        <label class="admin-toggle" style="margin-top: 8px;">
                            <input type="checkbox" name="active" value="1" <?= ($editCourse['active'] ?? 1) ? 'checked' : '' ?>>
                            <span class="admin-toggle-slider"></span>
                            <span class="admin-toggle-label">Active</span>
                        </label>
                    </div>
                </div>

                <div class="admin-form-actions">
                    <button type="submit" class="admin-btn admin-btn-primary"><?= $editCourse ? 'Update Course' : 'Create Course' ?></button>
                    <a href="<?= $basePath ?>/admin/courses" class="admin-btn admin-btn-secondary">Cancel</a>
                </div>
            </form>
        </div>
    </div>
    <?php endif; ?>

    <!-- Courses Table -->
    <?php if (!empty($courses)): ?>
    <div class="admin-card">
        <div class="admin-card-body">
            <div class="admin-table-responsive">
                <table class="admin-table" data-search="true">
                    <thead>
                        <tr>
                            <th>Image</th>
                            <th>Name</th>
                            <th>Category</th>
                            <th>Duration</th>
                            <th>Fee</th>
                            <th>Featured</th>
                            <th>Active</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($courses as $course): ?>
                        <tr data-category="<?= htmlspecialchars($course['category'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                            <td>
                                <?php if (!empty($course['image'])): ?>
                                <img src="<?= htmlspecialchars(!empty($course['image']) ? upload_url($course['image']) : $basePath . '/images/course-default.jpg', ENT_QUOTES, 'UTF-8') ?>" alt="<?= htmlspecialchars($course['name'], ENT_QUOTES, 'UTF-8') ?>" class="admin-table-thumb">
                                <?php else: ?>
                                <span class="admin-no-image">—</span>
                                <?php endif; ?>
                            </td>
                            <td><strong><?= htmlspecialchars($course['name'], ENT_QUOTES, 'UTF-8') ?></strong></td>
                            <td><span class="admin-badge admin-badge-category"><?= htmlspecialchars(ucfirst($course['category'] ?? ''), ENT_QUOTES, 'UTF-8') ?></span></td>
                            <td><?= htmlspecialchars($course['duration'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                            <td>₹<?= number_format((float) ($course['fee'] ?? 0)) ?></td>
                            <td><?= $course['is_featured'] ? '<span class="admin-badge admin-badge-success">Yes</span>' : '—' ?></td>
                            <td>
                                <label class="admin-toggle admin-toggle-sm">
                                    <input type="checkbox" <?= $course['active'] ? 'checked' : '' ?>>
                                    <span class="admin-toggle-slider"></span>
                                </label>
                            </td>
                            <td>
                                <div class="admin-actions">
                                    <a href="<?= $basePath ?>/admin/courses/<?= (int) $course['id'] ?>/edit" class="admin-btn-icon" title="Edit">
                                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><path d="M11 4H4a2 2 0 00-2 2v14a2 2 0 002 2h14a2 2 0 002-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 013 3L12 15l-4 1 1-4 9.5-9.5z"/></svg>
                                    </a>
                                    <form method="POST" action="<?= $basePath ?>/admin/courses/<?= (int) $course['id'] ?>/delete" style="display:inline" onsubmit="return confirm('Delete this course? The image file will also be removed.')">
                                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                        <button type="submit" class="admin-btn-icon admin-btn-delete" title="Delete">
                                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 01-2 2H7a2 2 0 01-2-2V6m3 0V4a2 2 0 012-2h4a2 2 0 012 2v2"/></svg>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <?php elseif (!($showForm ?? false)): ?>
    <div class="admin-card">
        <div class="admin-card-body">
            <p class="admin-empty-text">No courses found. <a href="<?= $basePath ?>/admin/courses/create">Add your first course</a>.</p>
        </div>
    </div>
    <?php endif; ?>
</div>
