<?php
// Get all active chambers
$chambers = $conn->query("SELECT * FROM chambers WHERE status='active'");

if(isset($_POST['save_schedule'])) {
    $chamber_id = intval($_POST['chamber_id']);
    // Clear existing schedule for this chamber
    $conn->query("DELETE FROM chamber_schedules WHERE chamber_id = $chamber_id");
    
    foreach($_POST['schedule'] as $day => $data) {
        if(isset($data['active']) && $data['active'] == 1) {
            $start = $conn->real_escape_string($data['start']);
            $end = $conn->real_escape_string($data['end']);
            $duration = intval($data['duration']);
            $free_camp = isset($data['free_camp']) ? 1 : 0;
            $conn->query("INSERT INTO chamber_schedules (chamber_id, day_of_week, start_time, end_time, slot_duration, is_free_camp) 
                          VALUES ($chamber_id, $day, '$start', '$end', $duration, $free_camp)");
        }
    }
    echo "<div class='alert success'>Schedule saved!</div>";
}
?>

<form method="post">
    <select name="chamber_id" id="chamber_select" required>
        <option value="">Select Chamber</option>
        <?php while($ch = $chambers->fetch_assoc()): ?>
            <option value="<?= $ch['id'] ?>"><?= htmlspecialchars($ch['name']) ?></option>
        <?php endwhile; ?>
    </select>
    
    <div id="schedule_table" style="display:none;">
        <table>
            <thead>
                <tr><th>Day</th><th>Active</th><th>Start Time</th><th>End Time</th><th>Slot (min)</th><th>Free Camp</th></tr>
            </thead>
            <tbody>
                <?php
                $days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
                foreach($days as $index => $day_name):
                ?>
                <tr>
                    <td><?= $day_name ?></td>
                    <td><input type="checkbox" name="schedule[<?= $index ?>][active]" class="day_active"></td>
                    <td><input type="time" name="schedule[<?= $index ?>][start]" disabled></td>
                    <td><input type="time" name="schedule[<?= $index ?>][end]" disabled></td>
                    <td><input type="number" name="schedule[<?= $index ?>][duration]" value="30" disabled></td>
                    <td><input type="checkbox" name="schedule[<?= $index ?>][free_camp]" disabled></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <button type="submit" name="save_schedule">Save Schedule</button>
    </div>
</form>

<script>
// Enable/disable fields when checkbox is toggled
document.querySelectorAll('.day_active').forEach(cb => {
    cb.addEventListener('change', function() {
        let row = this.closest('tr');
        let inputs = row.querySelectorAll('input:not(.day_active)');
        inputs.forEach(inp => inp.disabled = !this.checked);
    });
});
document.getElementById('chamber_select').addEventListener('change', function() {
    document.getElementById('schedule_table').style.display = this.value ? 'block' : 'none';
});
</script>