<?php
include('session_validator.php');
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Class</title>
       <link rel="stylesheet" href="css/bootstrap.min.css" media="screen">
    <link rel="stylesheet" href="css/font-awesome.min.css" media="screen">
    <link rel="stylesheet" href="css/animate-css/animate.min.css" media="screen">
    <link rel="stylesheet" href="css/lobipanel/lobipanel.min.css" media="screen">
    <link rel="stylesheet" href="css/toastr/toastr.min.css" media="screen">
    <link rel="stylesheet" href="css/icheck/skins/line/blue.css">
    <link rel="stylesheet" href="css/icheck/skins/line/red.css">
    <link rel="stylesheet" href="css/icheck/skins/line/green.css">
    <link rel="stylesheet" href="css/main.css" media="screen">
    <script src="js/modernizr/modernizr.min.js"></script>


</head>

<body class="top-navbar-fixed">
    <div class="main-wrapper">
        <?php include('includes/topbar.php');?>
        <div class="content-wrapper">
            <div class="content-container">
                <?php include('includes/leftbar.php');?>
                <div class="main-page">
                    <div class="container-fluid">
        <h2>Edit Class Details</h2>

        <?php
        // Include the database configuration file
        include("includes/db.php");

        if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET["id"])) {
            $classId = $_GET["id"];

            // Fetch class data for the provided ID
            $query = "SELECT * FROM classes WHERE Class_id = $classId";
            $result = $conn->query($query);

            if ($result->num_rows == 1) {
                $row = $result->fetch_assoc();
                ?>
                <form method="POST" action="edit_class.php">
                    <input type="hidden" name="classId" value="<?php echo $row['Class_id']; ?>">
                    <div class="form-group">
                        <label for="className">Class Name:</label>
                        <input type="text" id="className" name="className" class="form-control" value="<?php echo $row['class_name']; ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="section">Section:</label>
                        <input type="text" id="section" name="section" class="form-control" value="<?php echo $row['section']; ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="class_teacher">Class Teacher:</label>
                        <input type="text" id="class_teacher" name="class_teacher" class="form-control" value="<?php echo $row['class_teacher']; ?>" required>
                    </div>
                    <button type="submit" class="btn btn-primary" name="submit">Update Class</button>
                </form>
            <?php
            } else {
                echo "Class not found!";
            }
        } else {
            echo "Invalid request!";
        }

        // Close the database connection
        $conn->close();
        ?>
    </div>


<?php
include("includes/db.php");

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["submit"])) {
    $classId = $_POST["classId"];
    $className = strtoupper($conn->real_escape_string($_POST["className"]));
    $section = strtoupper($conn->real_escape_string($_POST["section"]));
    $class_teacher = strtoupper($conn->real_escape_string($_POST["class_teacher"]));

    $query = "UPDATE classes SET class_name='$className', section='$section', class_teacher='$class_teacher' WHERE Class_id=$classId";

    if ($conn->query($query) === TRUE) {
        // Class updated successfully
        echo "<script>
                alert('Class edited successfully');
                window.location.href = 'manage-classes.php';
              </script>";
        exit();
    } else {
        // Error occurred while updating class
        echo "Error: " . $query . "<br>" . $conn->error;
    }
} else {
    echo "Invalid request!";
}

// Close the database connection
$conn->close();
?>
  </div>
    <script src="js/jquery/jquery-2.2.4.min.js"></script>
    <script src="js/jquery-ui/jquery-ui.min.js"></script>
    <script src="js/bootstrap/bootstrap.min.js"></script>
    <script src="js/pace/pace.min.js"></script>
    <script src="js/lobipanel/lobipanel.min.js"></script>
    <script src="js/iscroll/iscroll.js"></script>
    <script src="js/prism/prism.js"></script>
    <script src="js/waypoint/waypoints.min.js"></script>
    <script src="js/counterUp/jquery.counterup.min.js"></script>
    <script src="js/toastr/toastr.min.js"></script>
    <script src="js/icheck/icheck.min.js"></script>
    <script src="js/main.js"></script>
    <script src="js/production-chart.js"></script>
    <script src="js/traffic-chart.js"></script>
    <script src="js/task-list.js"></script>
    <script>

</body>

</html>
