<?php    
include ('includes/connection.php'); 
include('sms.php');

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

 
require 'vendor/autoload.php';

if(isset($_GET['book_id']))
{
$book_id=$_GET['book_id'];     
$student_roll_no = "TRCD" . rand(100000, 999999);
$student_id= $user_row['student_id'];
$student_email_id= $user_row['student_email_id'];
$student_contact_no= $user_row['student_contact_no'];
$student_name= $user_row['student_name'];

$book_1= mysqli_query($conn,"SELECT * FROM exam_booking WHERE booking_id='$book_id'")or die(mysqli_error());
$sub_book= mysqli_fetch_array($book_1); 
$subject_id2= $sub_book['subject_id'];
$class_id= $sub_book['class_id'];
$exam_date= $sub_book['exam_date'];
$cost= $sub_book['cost'];
$exam_name= $sub_book['exam_name'];
$exam_date= $sub_book['exam_date'];
$image_url = "assets/img/icon.png";
 
$check= mysqli_query($conn,"SELECT * FROM student_to_class_soes WHERE student_id='$student_id' AND class_id='$class_id' AND subject_id='$subject_id'")or die(mysqli_error());
$check_1= mysqli_fetch_array($check); 
$check_2=$check_1['student_id'];
if ($check_2!=$student_id) { 

$query=mysqli_query($conn,"INSERT INTO `student_to_class_soes`(`class_id`,`subject_id`,`booking_id`,`student_id`,`exam_date`,`student_roll_no`) VALUES('$class_id','$subject_id2','$book_id','$student_id','$exam_date','$student_roll_no')");
if($query){ 
      $student_contact_no = substr($student_contact_no, 1);
      $s_mobile = "255$student_contact_no";
      $message = "Thank you for booking your examination with Cybercode Academy. We have received your request for the $exam_name exam scheduled on $exam_date Kindly proceed with payment by accepting PIN completion on your mobile phone";
      send_message($s_mobile, $message);
      
       echo "<div style='display: none;'>"; 
                    $mail = new PHPMailer(true);

                    try {
                        //Server settings
                        $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
                        $mail->isSMTP();                                            //Send using SMTP
                        $mail->Host       = 'mail.cybercode.co.tz';                     //Set the SMTP server to send through
                        $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
                        $mail->Username   = 'support@cybercode.co.tz';            //SMTP username
                        $mail->Password   = 'CYBER@support@2024';                   //SMTP password
                        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
                        $mail->Port       = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

                        //Recipients
                        $mail->setFrom('support@cybercode.co.tz');
                        $mail->addAddress($student_email_id);

                        //Content
                        $mail->isHTML(true);                                  //Set email format to HTML
                        $mail->Subject = 'Exam Booking Confirmation';
                        $mail->Body    = ' 
                        
                        Dear '.$student_name.',<br><br>
                        

                        Thank you for booking your examination with Cybercode Academy. We have received your
                        request for the '.$exam_name.' exam scheduled on '.$exam_date.'. Kindly proceed with payment by accepting PIN completion on your mobile phone.<br>
                        in the invoice below. <br><br>
                        If you have note received the Notification to complete payment please update your phone number on the system starting with 255xxxxxx and try again.
                        To get your examination code.<br><br>
                        If the problem persist kindly contact us through +255686969536. Before making payment kindly read and understand our examination policy.<br><br>
                        Best regards,<br>
                        Cybercode Academy Team.<br>
                         
                         ';

			$mail->send();
// 			echo 'Message has been sent';
			} catch (Exception $e) {
			echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
			}
      
      ?>
      <script type="text/javascript">
          alert("Your Successfully Booking The examination");
           window.location.href = 'admin.php?page=fgtt5678532srr5568exMadt578jjhggfr448w2&Id_2=<?php echo $user_row['course']; ?>&book_id=<?php echo $book_id; ?>';
        </script>
        <?php  
      }  
      else{
        ?>  
        <script type="text/javascript">
         alert("Sorry Exam are not Booking Try Again");
        </script>
        <?php
    }
    
}else{
      ?>
         <script type="text/javascript">
             alert("Sorry examination Already Booked");
         </script>
    <?php 
  }
}

 
?> 
   
  <style>
    
    .reactive-table {
      width: 100%;
      border-collapse: collapse; 
      box-shadow: 0 0 0 transparent; /* No outer border */
    }

    .reactive-table th,
    .reactive-table td {
      padding: 12px 15px;
      border-bottom: 1px solid #ddd;
      text-align: left;
    }

    .reactive-table tr {
      transition: background-color 0.3s ease, transform 0.2s ease;
    }

    /* Hover Effect */
    .reactive-table tr:hover {
      background-color: #f1f1f1;
      transform: scale(1.04);
      cursor: pointer;
    }

    .reactive-table th {
      background-color: #f9f9f9;
      font-weight: bold;
    }
  </style>
<div class="container">
      <div class="page-inner">
            <div class="page-header">
              <h3 class="fw-bold mb-3">Take examination</h3>
              <ul class="breadcrumbs mb-3">
                <li class="nav-home">
                  <a href="admin.php?page=dashboard&Id_2=<?php echo $user_row['course']; ?>">
                    <i class="icon-home"></i>
                  </a>
                </li>
                <li class="separator">
                  <i class="icon-arrow-right"></i>
                </li>
                <li class="nav-item">
                  <a href="admin.php?page=dashboard&Id_2=<?php echo $user_row['course']; ?>">Take Examination</a>
                </li>
                <li class="separator">
                  <i class="icon-arrow-right"></i>
                </li>
                <li class="nav-item">
                  <a href="admin.php?page=dashboard&Id_2=<?php echo $user_row['course']; ?>">Booking</a>
                </li>
              </ul>
            </div>
            <div class="row">
              <div class="col-md-12">
                <div class="card">
                  <div class="card-header">
                    <h4 class="card-title">Booking Examination</h4>
                  </div>
                  <div class="card-body"> 
                    <div class="tab-content mt-2 mb-3" id="pills-tabContent">
                      <div class="row"> 
                         <div class="col-md-12">
                            <div class="card">
                              <div class="card-header">
                                <h4 class="card-title">Booking list for <?php echo $user_row['course']; ?></h4>
                              </div>
                              <div class="card-body"> 
                             <div class="table-responsive">
                            <table class=" table reactive-table" id="myTable">
                            <thead>
                              <tr>
                                <th>#</th>
                                <th>Exam Name</th>
                                <th>Course Name</th> 
                                <th>Date Of Examination</th>
                                <th>ACTION</th>
                              </tr>
                            </thead>
                            <tbody>
                                 <?php 
                                    $i = 1;
                                    $subject_code= $user_row['course'];
                                    $sub= mysqli_query($conn,"SELECT subject_id FROM `subject_soes` WHERE course_code='$subject_code'")or die(mysqli_error());
                                    $sub_code= mysqli_fetch_array($sub); 
                                    $subject_id1= $sub_code['subject_id'];
                                    
                                   $school_query = mysqli_query($conn,"SELECT * FROM exam_booking,class_soes,subject_soes WHERE exam_booking.class_id=class_soes.class_id AND exam_booking.subject_id=subject_soes.subject_id AND exam_booking.subject_id='$subject_id1';");
                                while($row = mysqli_fetch_array($school_query)){  
                             ?>
                            <tr>
                              <td class="text-center"><?php echo $i++ ?></td> 
                              <td><?php echo $row['exam_name']; ?></td>
                              <td><?php echo $row['subject_name']; ?></td> 
                              <td><?php echo $row['exam_date']; ?></td> 
                                <td> <a href="admin.php?page=booking&Id_2=<?php echo $user_row['course']; ?>&book_id=<?php echo $row['booking_id']; ?>" onclick="return confirm('Are you sure you want to Take this Exam?');" class="badge badge-secondary btn-flat">
                                  Take Exam
                                </a>   </td>
                                   
                                </tr>
                              <?php } ?> 
                            </tbody>
                          </table>
                         </div>
                        </div>
                        </div>  
                   </div> 
                      </div> 
                    </div>
                  </div>
                  <div class="card-footer"> 
                    <div class="row">
                    <div class="col-md-6"> 
                     </div> 
                          <div style="text-align: center;"> 
                               <h6><i class="fas fa-map-marker-alt"></i> Location: <?php echo $user_row['student_address'];  ?> </h6>
                          </div>
                      </div> 
                   </div>
                  </div>
                </div>
              </div> 
            </div>
          </div> 