<?php
/**
 * Admin — Batch Schedule Form (Create/Edit)
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$batch = $batch ?? null;
$isEdit = $batch !== null;
?>
<div class="admin-section">
    <div style="margin-bottom:20px;">
        <a href="<?= $bp ?>/admin/batch-schedules" style="color:#4A6CF7; text-decoration:none; font-size:13px; display:inline-flex; align-items:center; gap:4px;">
            <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="15 18 9 12 15 6"/></svg>
            Back to Batch Schedules
        </a>
    </div>

    <form method="POST" action="<?= $isEdit ? $bp . '/admin/batch-schedules/' . (int)$batch['id'] : $bp . '/admin/batch-schedules' ?>" style="max-width:700px;">
        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">

        <div style="margin-bottom:16px;">
            <label style="display:block; font-weight:600; margin-bottom:6px; font-size:14px;">Batch Name *</label>
            <input type="text" name="batch_name" value="<?= htmlspecialchars($batch['batch_name'] ?? '', ENT_QUOTES, 'UTF-8') ?>" required placeholder="e.g., CCC Morning Batch July 2026"
                style="width:100%; padding:10px 14px; border:1px solid #ddd; border-radius:8px; font-size:14px; outline:none;">
        </div>

        <div style="margin-bottom:16px;">
            <label style="display:block; font-weight:600; margin-bottom:6px; font-size:14px;">Course</label>
            <select name="course_id" style="width:100%; padding:10px 14px; border:1px solid #ddd; border-radius:8px; font-size:14px; outline:none;">
                <option value="">— No Course Linked —</option>
                <?php foreach ($courses as $c): ?>
                <option value="<?= (int) $c['id'] ?>" <?= ($batch['course_id'] ?? '') == $c['id'] ? 'selected' : '' ?>><?= htmlspecialchars($c['name'], ENT_QUOTES, 'UTF-8') ?></option>
                <?php endforeach; ?>
            </select>
        </div>

        <div style="display:flex; gap:16px; margin-bottom:16px; flex-wrap:wrap;">
            <div style="flex:1; min-width:200px;">
                <label style="display:block; font-weight:600; margin-bottom:6px; font-size:14px;">Start Date *</label>
                <input type="date" name="start_date" value="<?= htmlspecialchars($batch['start_date'] ?? '', ENT_QUOTES, 'UTF-8') ?>" required
                    style="width:100%; padding:10px 14px; border:1px solid #ddd; border-radius:8px; font-size:14px; outline:none;">
            </div>
            <div style="flex:1; min-width:200px;">
                <label style="display:block; font-weight:600; margin-bottom:6px; font-size:14px;">End Date</label>
                <input type="date" name="end_date" value="<?= htmlspecialchars($batch['end_date'] ?? '', ENT_QUOTES, 'UTF-8') ?>"
                    style="width:100%; padding:10px 14px; border:1px solid #ddd; border-radius:8px; font-size:14px; outline:none;">
            </div>
        </div>

        <div style="display:flex; gap:16px; margin-bottom:16px; flex-wrap:wrap;">
            <div style="flex:1; min-width:200px;">
                <label style="display:block; font-weight:600; margin-bottom:6px; font-size:14px;">Timing</label>
                <input type="text" name="timing" value="<?= htmlspecialchars($batch['timing'] ?? '', ENT_QUOTES, 'UTF-8') ?>" placeholder="9:00 AM - 11:00 AM"
                    style="width:100%; padding:10px 14px; border:1px solid #ddd; border-radius:8px; font-size:14px; outline:none;">
            </div>
            <div style="flex:1; min-width:200px;">
                <label style="display:block; font-weight:600; margin-bottom:6px; font-size:14px;">Days</label>
                <input type="text" name="days" value="<?= htmlspecialchars($batch['days'] ?? '', ENT_QUOTES, 'UTF-8') ?>" placeholder="Mon-Wed-Fri"
                    style="width:100%; padding:10px 14px; border:1px solid #ddd; border-radius:8px; font-size:14px; outline:none;">
            </div>
        </div>

        <div style="display:flex; gap:16px; margin-bottom:16px; flex-wrap:wrap;">
            <div style="flex:1; min-width:120px;">
                <label style="display:block; font-weight:600; margin-bottom:6px; font-size:14px;">Total Seats</label>
                <input type="number" name="seats_total" value="<?= (int)($batch['seats_total'] ?? 0) ?>" min="0"
                    style="width:100%; padding:10px 14px; border:1px solid #ddd; border-radius:8px; font-size:14px; outline:none;">
            </div>
            <div style="flex:1; min-width:120px;">
                <label style="display:block; font-weight:600; margin-bottom:6px; font-size:14px;">Available Seats</label>
                <input type="number" name="seats_available" value="<?= (int)($batch['seats_available'] ?? 0) ?>" min="0"
                    style="width:100%; padding:10px 14px; border:1px solid #ddd; border-radius:8px; font-size:14px; outline:none;">
            </div>
            <div style="flex:1; min-width:120px;">
                <label style="display:block; font-weight:600; margin-bottom:6px; font-size:14px;">Fee (₹)</label>
                <input type="number" name="fee" value="<?= htmlspecialchars($batch['fee'] ?? '', ENT_QUOTES, 'UTF-8') ?>" min="0" step="0.01"
                    style="width:100%; padding:10px 14px; border:1px solid #ddd; border-radius:8px; font-size:14px; outline:none;">
            </div>
        </div>

        <div style="margin-bottom:16px;">
            <label style="display:block; font-weight:600; margin-bottom:6px; font-size:14px;">Description</label>
            <textarea name="description" rows="3" placeholder="Additional details about this batch..."
                style="width:100%; padding:10px 14px; border:1px solid #ddd; border-radius:8px; font-size:14px; outline:none; resize:vertical;"><?= htmlspecialchars($batch['description'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea>
        </div>

        <div style="display:flex; gap:16px; margin-bottom:16px; flex-wrap:wrap;">
            <div style="flex:1; min-width:140px;">
                <label style="display:block; font-weight:600; margin-bottom:6px; font-size:14px;">Status</label>
                <select name="status" style="width:100%; padding:10px 14px; border:1px solid #ddd; border-radius:8px; font-size:14px; outline:none;">
                    <option value="upcoming" <?= ($batch['status'] ?? 'upcoming') === 'upcoming' ? 'selected' : '' ?>>Upcoming</option>
                    <option value="running" <?= ($batch['status'] ?? '') === 'running' ? 'selected' : '' ?>>Running</option>
                    <option value="completed" <?= ($batch['status'] ?? '') === 'completed' ? 'selected' : '' ?>>Completed</option>
                    <option value="cancelled" <?= ($batch['status'] ?? '') === 'cancelled' ? 'selected' : '' ?>>Cancelled</option>
                </select>
            </div>
            <div style="flex:1; min-width:100px;">
                <label style="display:block; font-weight:600; margin-bottom:6px; font-size:14px;">Sort Order</label>
                <input type="number" name="sort_order" value="<?= (int)($batch['sort_order'] ?? 0) ?>" min="0"
                    style="width:100%; padding:10px 14px; border:1px solid #ddd; border-radius:8px; font-size:14px; outline:none;">
            </div>
        </div>

        <div style="display:flex; gap:24px; margin-bottom:20px; flex-wrap:wrap;">
            <label style="display:flex; align-items:center; gap:8px; font-size:14px; cursor:pointer;">
                <input type="checkbox" name="is_featured" value="1" <?= ($batch['is_featured'] ?? 0) ? 'checked' : '' ?> style="width:18px; height:18px; accent-color:#4A6CF7;">
                <span><strong>Featured</strong> — Show on homepage</span>
            </label>
            <label style="display:flex; align-items:center; gap:8px; font-size:14px; cursor:pointer;">
                <input type="checkbox" name="show_countdown" value="1" <?= ($batch['show_countdown'] ?? 0) ? 'checked' : '' ?> style="width:18px; height:18px; accent-color:#4A6CF7;">
                <span><strong>Show Countdown</strong> — Homepage countdown timer</span>
            </label>
        </div>

        <div style="display:flex; gap:12px; margin-top:20px;">
            <button type="submit" class="admin-btn" style="background:#4A6CF7; color:#fff; padding:10px 24px; border-radius:8px; font-size:14px; font-weight:600; border:none; cursor:pointer;">
                <?= $isEdit ? 'Update Batch' : 'Add Batch' ?>
            </button>
            <a href="<?= $bp ?>/admin/batch-schedules" style="padding:10px 24px; border:1px solid #ddd; border-radius:8px; font-size:14px; color:#666; text-decoration:none;">Cancel</a>
        </div>
    </form>
</div>
