<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once '../auth/auth.php';
require_once '../models/db_connection.php';
//require_once '../models/student_model.php';
//$student =  new Student($conn);
// Check if a student ID is provided in the URL
if (isset($_GET['id'])) {
  $course_id = $_GET['id'];
  $sql = mysqli_query($conn, "SELECT * FROM fees WHERE id = '$course_id'");
  $fetch = mysqli_fetch_array($sql);
}
if (isset($_POST['editcourse'])) {
  $updatecourse = mysqli_query($conn, "UPDATE fees SET name = '$_POST[coursename]', ammount ='$_POST[price]', description = '$_POST[description]' WHERE id = '$course_id'");
  if ($updatecourse) {
    echo "<script>
        alert('Course edited successfully');
        window.location.href = 'view_course.php';
    </script>";
  }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <title>Course | Edit Course </title>
  <?php include("../templates/header.php"); ?>
</head>

<body>
  <?php
  if (isset($_GET["message"])) {
    $message = $_GET["message"];
    echo "<script>alert('" . $message . "')</script>";
  }
  ?>
  <div class="container-scroller">
    <!-- partial:partials/_navbar.html -->
    <?php include("../templates/nav.php"); ?>
    <!-- partial -->
    <div class="container-fluid page-body-wrapper">
      <!-- partial:partials/_sidebar.html -->
      <?php include("../templates/sidenav.php"); ?>
      <!-- partial -->
      <div class="main-panel">
        <div class="content-wrapper">
          <div class="col-12 grid-margin">
            <div class="card">
              <div class="card-body">
                <h4 class="card-title">Edit Course Form</h4>
                <form class="form-sample" action="" method="POST" enctype="multipart/form-data">

                  <div class="row">
                    <div class="col-md-4">
                      <div class="form-group row">
                        <label class="col-form-label">Course name</label>
                        <div class="col-sm-9">
                          <input type="text" class="form-control" name="coursename" value="<?php echo $fetch['name']; ?>" required>
                        </div>
                      </div>
                    </div>
                    <!-- Other form fields with values filled from $work, $education, etc. -->
                    <div class="col-md-4">
                      <div class="form-group row">
                        <label class="col-form-label">Description</label>
                        <div class="col-sm-9">
                          <input type="text" class="form-control" name="description" value="<?php echo $fetch['description']; ?>" required>
                        </div>
                      </div>
                    </div>

                  </div>

                  <div class="row">
                    <div class="col-md-6">
                      <div class="form-group row">
                        <label class="col-sm-3 col-form-label">Course price</label>
                        <input type="text" name="price" class="form-control" value="<?php echo $fetch['ammount']; ?>" required>
                        <input type="hidden" name="id" value="<?= $_GET['id'] ?>">

                        <div class="col-sm-9">
                          <button type="submit" name="editcourse" class="btn btn-dark btn-lg btn-block">Edit Course</button>
                        </div>
                      </div>
                    </div>
                  </div>
                </form>
              </div>
            </div>
          </div>
        </div>
        <!-- content-wrapper ends -->
        <!-- partial:partials/_footer.html -->
        <?php include("../templates/footer.php"); ?>

        <!-- partial -->
      </div>
      <!-- main-panel ends -->
    </div>
    <!-- page-body-wrapper ends -->
  </div>
  <?php include("../templates/inc_scripts.php"); ?>
</body>

</html>