<?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';
// require_once '../models/course_model.php';
// require_once '../models/fee_model.php';
// $student =  new Student($conn);
// $course =  new Course($conn);
// $fee =  new Fee($conn);
// $coursesData =  $course->getAllCourses();
// $studentsData = $student->getAllStudents();
// if (isset($_POST["enrollStudent"])) {
//   $enID = $_POST["enID"];
//   $enDate = $_POST["enDate"];
// $currentYear = date("Y", strtotime($enDate));
//   $courseID = $_POST["courseID"];
// //   $currentDate = Date("Y-m-d");
// //   $currentYear = date("Y");
//   $studentID = $student->getStudentIdByEnrollmentId($enID);
//   $course->createStudentCourse($studentID, $courseID, $enDate, $currentYear);
//   $success = $fee->createStudentFee($studentID,1);
//     header("Location: view_students.php?message=Student Enrolled successfully");
//     exit(); // Stop further execution
// }


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

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

<body>
  <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-md-6 grid-margin stretch-card">
            <div class="card">
              <div class="card-body">
                <h4 class="card-title">Enroll Student To Course</h4>
                <?php
                if (isset($succ)) {
                ?>
                  <div class='mb-4 alert alert-success'><?php echo $succ; ?> </div>
                <?php
                }
                if (isset($err)) {
                ?>
                  <div class='mb-4 alert alert-danger'>
                    </php echo $err; ?>
                  </div>
                <?php
                }
                ?>
                <p class="card-description">
                  Add Student to specific <code>Course</code>
                </p>
                <form action="enroll.php" method="POST">
                  <div class="form-group">
                    <label for="exampleFormControlSelect1">Student</label>
                    <input list="students" id="studentList" class="form-control" name="enID" required>
                    <datalist id="students">
                      <?php
                      $sellstudent = mysqli_query($conn, "SELECT * FROM students WHERE status = 'pending'");
                      while ($studentData = mysqli_fetch_array($sellstudent)) {
                      ?>
                        <option value="<?= $studentData['en_id'] ?>"><?= $studentData['fullname'] ?></option>
                      <?php } ?>
                    </datalist>
                  </div>
                  <div class="form-group">
                    <label for="exampleFormControlSelect2">Course</label>
                    <select class="form-control" id="exampleFormControlSelect2" name="courseID" required>
                        <option value="">---Select course---</option>
                      <?php
                      $coursesData = mysqli_query($conn, "SELECT * FROM fees");
                      while ($courseValue = mysqli_fetch_array($coursesData)) { ?>
                        <option value="<?= $courseValue['id'] ?>"><?= $courseValue['name'] ?></option>
                      <?php  } ?>
                    </select>
                  </div>

                  <div class="form-group">
                    <label for="exampleFormControlSelect1">Enrollement Date</label>
                    <input type='date' id="studentList" class="form-control" name="enDate" required>

                  </div>

                  <div class="col-sm-9">
                    <button type="submit" name="enrollStudent" class="btn btn-dark btn-lg ">Enroll Student</button>
                  </div>
              </div>
              </form>
            </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>