<?php
include '../includes/db.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $course_id = $_POST['course_id'];
    $delivery_mode = $_POST['delivery_mode'];
    $reg_open = $_POST['registration_open_date'];
    $batch_start = $_POST['batch_starting_date'];
    $reg_close = $_POST['registration_close_date'];
    $batch_end = $_POST['batch_ending_date'];
    $batch_name = $_POST['batch_name'];
    $description = $_POST['description'];
    $language = $_POST['language'];
    $total_seat_count = $_POST['total_seat_count'];
    $is_enforced = isset($_POST['is_enforced']) ? 1 : 0;

    $stmt = $conn->prepare("
        INSERT INTO batches 
        (course_id, delivery_mode, registration_open_date, batch_starting_date, 
         registration_close_date, batch_ending_date, batch_name, description, 
         language, total_seat_count, is_enforced) 
        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    ");
    $stmt->bind_param(
        "issssssssii", 
        $course_id, $delivery_mode, $reg_open, $batch_start, 
        $reg_close, $batch_end, $batch_name, $description, 
        $language, $total_seat_count, $is_enforced
    );

    if ($stmt->execute()) {
        header("Location: batches.php?success=1");
        exit();
    } else {
        echo "Error: " . $stmt->error;
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Create Batch</title>
</head>
<body>

<h2>Create Batch</h2>
<form method="POST">
    <label>Course:</label>
    <select name="course_name" required>
        <option value="">Select Course</option>
        <?php
        $courses = $conn->query("SELECT  course_name FROM courses");
        while ($c = $courses->fetch_assoc()) {
            echo "<option value='{$c['id']}'>{$c['course_name']}</option>";
        }
        ?>
    </select><br><br>

    <label>Delivery Mode:</label>
    <select name="delivery_mode" required>
        <option value="">Select Delivery Mode</option>
        <option value="Online">Online</option>
        <option value="Offline">Offline</option>
        <option value="Hybrid">Hybrid</option>
    </select><br><br>

    <label>Registration Opening Date:</label>
    <input type="date" name="registration_open_date" required><br><br>

    <label>Batch Starting Date:</label>
    <input type="date" name="batch_starting_date" required><br><br>

    <label>Registration Closing Date:</label>
    <input type="date" name="registration_close_date" required><br><br>

    <label>Batch Ending Date:</label>
    <input type="date" name="batch_ending_date" required><br><br>

    <label>Batch Name:</label>
    <input type="text" name="batch_name" required><br><br>

    <label>Description:</label>
    <textarea name="description"></textarea><br><br>

    <label>Medium of Instruction:</label>
    <input type="radio" name="language" value="English" checked> English
    <input type="radio" name="language" value="Sinhala"> Sinhala
    <input type="radio" name="language" value="Tamil"> Tamil<br><br>

    <label>Total Seat Count:</label>
    <input type="number" name="total_seat_count" min="10" required><br><br>

    <label>Is Enforced:</label>
    <input type="checkbox" name="is_enforced" value="1"><br><br>

    <button type="submit">Save</button>
</form>

</body>
</html>
