<?php
require_once '../auth/auth.php';
require_once '../models/db_connection.php';

?>

<!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">Search Students</h4>
                <p class="card-description">
                  Search students to add <code>Marks</code>
                </p>
                <form action="generate_certificates.php" method="POST">
                    <div class="form-group">
                    <label for="exampleFormControlSelect2">Enrollment Year</label>
                  
                    <input name="year" required class="form-control" type="text">
                  </div> 
                  <div class="form-group">
                    <label for="exampleFormControlSelect2">From Date</label>
                    <input name="fromdate" required class="form-control" type="date">
                  </div>
                  <div class="form-group">
                    <label for="exampleFormControlSelect2">To Date</label>
                    <input name="todate" required class="form-control" type="date">
                  </div>
                  <div class="form-group">
                    <label for="exampleFormControlSelect2">Course</label>
                    <select class="form-control" id="exampleFormControlSelect2" name="courseID">
                      <?php
                      // foreach ($coursesData as $courseValue) { 
                      ?>
                        
                       <?php  //} 
                        ?> 
                    </select>
                  </div>

                  <div class="col-sm-9">
                    <button <?php //if ($_SESSION['role'] != 'admin') {
                            //echo "disabled";
                            //} 
                            ?> type="submit" name="search" class="btn btn-dark btn-lg ">Generate</button>
                  </div>
              </div>
              </form>
            </div>
          </div> -->

          <div class="col-lg-12 grid-margin stretch-card">
            <div class="card">
              <div class="card-body">
                <h4 class="card-title">View All Students</h4>

                <p class="card-description">
                  Students Table <code></code>
                </p>
                <!-- <div class="col-sm-9">
                  <a class="btn btn-dark btn-lg " href="../controllers/certificates_controller.php">Generate Certificates</a>
                </div> -->
                <div class="table-responsive pt-3">
                  <table class="table table-bordered display" id="example" style="width:100%">
                    <thead>
                      <tr>
                        <th> # </th>
                        <th>Enrollment ID</th>
                        <th>Identification Number</th>
                        <th> Fullname</th>
                        <th> Practical</th>
                        <th> Theory</th>
                        <th> Average</th>
                        <th>Course</th>
                        <th>Enrollment Date</th>
                        <th>Remarks</th>
                        <?php if ($_SESSION['role'] == 'admin') {  
                            ?>
                        <th> Action</th>
                        <?php
                        }
                        ?>

                      </tr>
                    </thead>
                    <tbody>
                      <?php
                      $x = 1;
                      $getstudent = mysqli_query($conn, "SELECT * 
                                                            FROM students 
                                                            JOIN student_exams ON students.student_id = student_exams.student
                                                            JOIN student_course ON students.en_id = student_course.s_id 
                                                            JOIN fees ON student_course.course_id = fees.id
                                                            WHERE student_course.course_id = student_exams.course AND student_exams.remark = 'passed'
                                                            ");

                      while ($fetch = mysqli_fetch_array($getstudent)) {
                        $endDate = date("Y-m-d", strtotime($fetch['description'], strtotime($fetch['en_date'])));
                      ?>
                        <tr>
                          <td> <?php echo $x ?></td>
                          <td><?php echo $fetch['en_id'] ?></td>
                          <td><?php echo $fetch['ident_number']; ?></td>
                          <td><?php echo $fetch['fullname'] ?></td>
                          <td><?php echo $fetch['practical'] ?></td>
                          <td><?php echo $fetch['theory']  ?></td>
                          <td>
                            <?php
                            echo $average = (($fetch['practical'] + $fetch['theory']) / 200) * 100;
                            ?>
                          </td>
                          <td><?php echo $fetch['name'] ?></td>
                          <td><?php echo $fetch['en_date'] ?></td>
                          <td>
                            <?php
                            $x++;
                            if ($average >= 70) {
                              echo  "<label class='badge badge-success'>Passed</label>";
                            } else {
                              echo   "<label class='badge badge-danger'>Failed</label>";
                            }
                            ?>
                          </td>
                          <?php if ($_SESSION['role'] == 'admin') {  
                            ?>
                          <td>
                              <a class="btn btn-outline-primary btn-icon-text" href="../controllers/singleCertificateController.php?id=<?= $fetch['student']  ?>&from=<?php echo $fetch['en_date'] ?>&to=<?php echo $endDate ?>">Generate</a>
                            
                          </td>
                          <?php
                            }
                            ?>
                        </tr>
                      <?php }
                      ?>


                    </tbody>
                  </table>
                </div>
              </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"); ?>
  <script>
    new DataTable('#example');
  </script>

</body>

</html>