<?php
include './login/connect.php';

if (isset($_GET['id'])) {
    $routine_id = $_GET['id'];
    
    // রুটিনটি লোড করতে SQL কোড
    $query = "SELECT * FROM class_routine WHERE id = $routine_id";
    $result = $conn->query($query);
    $row = $result->fetch_assoc();
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // রুটিন আপডেট করার কোড
    $class_id = $_POST['class_id'];
    $day = $_POST['day'];
    $period_1 = $_POST['period_1'];
    $period_2 = $_POST['period_2'];
    $period_3 = $_POST['period_3'];
    $period_4 = $_POST['period_4'];
    $period_5 = $_POST['period_5'];
    $period_6 = $_POST['period_6'];

    $update_query = "UPDATE class_routine SET 
                        class_id = '$class_id',
                        day = '$day',
                        period_1 = '$period_1',
                        period_2 = '$period_2',
                        period_3 = '$period_3',
                        period_4 = '$period_4',
                        period_5 = '$period_5',
                        period_6 = '$period_6'
                    WHERE id = $routine_id";
    
    if ($conn->query($update_query) === TRUE) {
        echo "Routine Updated Successfully";
    } else {
        echo "Error: " . $conn->error;
    }
}

// ক্লাস লিস্ট পেতে SQL কোড
$class_query = "SELECT * FROM classes";
$class_result = $conn->query($class_query);

// বিষয় লিস্ট পেতে SQL কোড
$subject_query = "SELECT * FROM subjects";
$subject_result = $conn->query($subject_query);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Class Routine</title>
</head>
<body>
    <h1>Edit Class Routine</h1>
    
    <form method="POST" action="">
        <label for="class_id">Select Class:</label>
        <select name="class_id" id="class_id">
            <?php while ($class_row = $class_result->fetch_assoc()): ?>
                <option value="<?= $class_row['id']; ?>" <?= ($class_row['id'] == $row['class_id']) ? 'selected' : ''; ?>><?= $class_row['class_name']; ?></option>
            <?php endwhile; ?>
        </select>
        <br><br>
        
        <label for="day">Day:</label>
        <select name="day" id="day">
            <option value="Saturday" <?= ($row['day'] == 'Saturday') ? 'selected' : ''; ?>>Saturday</option>
            <option value="Sunday" <?= ($row['day'] == 'Sunday') ? 'selected' : ''; ?>>Sunday</option>
            <option value="Monday" <?= ($row['day'] == 'Monday') ? 'selected' : ''; ?>>Monday</option>
            <option value="Tuesday" <?= ($row['day'] == 'Tuesday') ? 'selected' : ''; ?>>Tuesday</option>
            <option value="Wednesday" <?= ($row['day'] == 'Wednesday') ? 'selected' : ''; ?>>Wednesday</option>
            <option value="Thursday" <?= ($row['day'] == 'Thursday') ? 'selected' : ''; ?>>Thursday</option>
        </select>
        <br><br>

        <!-- পিরিয়ডের জন্য বিষয় নির্বাচন -->
        <label for="period_1">1st Period:</label>
        <select name="period_1" id="period_1">
            <?php while ($subject_row = $subject_result->fetch_assoc()): ?>
                <option value="<?= $subject_row['id']; ?>" <?= ($subject_row['id'] == $row['period_1']) ? 'selected' : ''; ?>><?= $subject_row['subject_name']; ?></option>
            <?php endwhile; ?>
        </select>
        <br><br>

        <label for="period_2">2nd Period:</label>
        <select name="period_2" id="period_2">
            <?php
            // Resetting the subject result for each period
            $subject_result = $conn->query($subject_query);
            while ($subject_row = $subject_result->fetch_assoc()): ?>
                <option value="<?= $subject_row['id']; ?>" <?= ($subject_row['id'] == $row['period_2']) ? 'selected' : ''; ?>><?= $subject_row['subject_name']; ?></option>
            <?php endwhile; ?>
        </select>
        <br><br>

        <label for="period_3">3rd Period:</label>
        <select name="period_3" id="period_3">
            <?php
            // Resetting the subject result for each period
            $subject_result = $conn->query($subject_query);
            while ($subject_row = $subject_result->fetch_assoc()): ?>
                <option value="<?= $subject_row['id']; ?>" <?= ($subject_row['id'] == $row['period_3']) ? 'selected' : ''; ?>><?= $subject_row['subject_name']; ?></option>
            <?php endwhile; ?>
        </select>
        <br><br>

        <label for="period_4">4th Period:</label>
        <select name="period_4" id="period_4">
            <?php
            // Resetting the subject result for each period
            $subject_result = $conn->query($subject_query);
            while ($subject_row = $subject_result->fetch_assoc()): ?>
                <option value="<?= $subject_row['id']; ?>" <?= ($subject_row['id'] == $row['period_4']) ? 'selected' : ''; ?>><?= $subject_row['subject_name']; ?></option>
            <?php endwhile; ?>
        </select>
        <br><br>

        <label for="period_5">5th Period:</label>
        <select name="period_5" id="period_5">
            <?php
            // Resetting the subject result for each period
            $subject_result = $conn->query($subject_query);
            while ($subject_row = $subject_result->fetch_assoc()): ?>
                <option value="<?= $subject_row['id']; ?>" <?= ($subject_row['id'] == $row['period_5']) ? 'selected' : ''; ?>><?= $subject_row['subject_name']; ?></option>
            <?php endwhile; ?>
        </select>
        <br><br>

        <label for="period_6">6th Period:</label>
        <select name="period_6" id="period_6">
            <?php
            // Resetting the subject result for each period
            $subject_result = $conn->query($subject_query);
            while ($subject_row = $subject_result->fetch_assoc()): ?>
                <option value="<?= $subject_row['id']; ?>" <?= ($subject_row['id'] == $row['period_6']) ? 'selected' : ''; ?>><?= $subject_row['subject_name']; ?></option>
            <?php endwhile; ?>
        </select>
        <br><br>

        <button type="submit">Update Routine</button>
    </form>
</body>
</html>
