 <?php 
   include('sms.php');
//   include('create_order.php');
   use PHPMailer\PHPMailer\PHPMailer;
   use PHPMailer\PHPMailer\SMTP;
   use PHPMailer\PHPMailer\Exception;

     
   require 'vendor/autoload.php';
   $id=$_GET['Id_2'];
   $book_id=$_GET['book_id'];
   $msg = "";
   $query= mysqli_query($conn,"SELECT DISTINCT * FROM subject_soes ,module_soes WHERE module_soes.course_code = subject_soes.course_code AND subject_soes.course_code='$id'")or die(mysqli_error());
   $row = mysqli_fetch_array($query);
   $subject_id=$row['subject_id'];
   $subject_name=$row['subject_name'];
   $student_name=$user_row['student_name'];
//   $student_contact_no="0757660790";
 

    $student_id=$user_row['student_id'];
    $invoice= mysqli_query($conn,"SELECT * FROM student_soes 
    INNER JOIN exam_invoice 
    ON exam_invoice.student_id = student_soes.student_id  WHERE exam_invoice.course_code ='$id' AND exam_invoice.student_id='$student_id' ")or die(mysqli_error());
    $invoice2 = mysqli_fetch_array($invoice); 

    if (isset($_POST['invoice'])){
		$student_id = mysqli_real_escape_string($conn, $_POST['student_id']);
        $email = mysqli_real_escape_string($conn, $_POST['student_email_id']);
        $course_code = mysqli_real_escape_string($conn, $_POST['course_code']);
        $description = mysqli_real_escape_string($conn, $_POST['description']);

		 $sql = "INSERT INTO `exam_invoice`(`student_id`,`course_code`,`description`) VALUES('{$student_id}','{$course_code}','{$description}')";
                $result = mysqli_query($conn, $sql);

            if ($result) {
              $student_contact_no = substr($student_contact_no, 1);
              $s_mobile = "255686969536";
              $message = "Cybercode Exam Confirmed: $student_name are requested the payment Code for $subject_name check and verify the invoice";
              send_message($s_mobile, $message);
			$msg = "<div class='alert bg-success-gradient' style='word-spacing: 5px; color: white;'>Your request is under review kindly wait while we process it.</div>";
			} else {
			$msg = "<div class='alert bg-danger-gradient' style='word-spacing: 5px; color: white;'>Something wrong went.</div>";
			}
		  }  
		  if (isset($_POST['update'])){
				$course_code = $_POST['course_code']; 
				$payment_code1 = $_POST['payment_code'];  
				$invoice_status='Enable';  
				$student_id = $_POST['student_id']; 

				$query1= mysqli_query($conn,"SELECT * FROM exam_invoice WHERE course_code='$course_code' AND student_id='$student_id'")or die(mysqli_error());
				$row1 = mysqli_fetch_array($query1);
				 $payment_code = $row1['payment_code'];  

				 if ($payment_code1 == "$payment_code" ){

				$query=mysqli_query($conn,"UPDATE exam_invoice set invoice_status='$invoice_status'  WHERE course_code='$course_code'");
				  if($query){ 
				      ?>
				      <script type="text/javascript">
				          alert("Succesfully submitted, Welcome to cybercode");
				           window.location.href = 'admin.php?page=fgtt5678532srr5568exMadt578jjhggfr448w2&Id_2=<?php echo $id?>&book_id=<?php echo $book_id?>';
				        </script>
				        <?php  
				      }  
				      else{
				        ?>  
				        <script type="text/javascript">
				         alert("Sorry Your Submition was not Successfuly ! Try Again");
				        </script>
				        <?php
				    }
				} else{
				        ?>  
				        <script type="text/javascript">
				         alert("Sorry the Payment verification code is not correct ! Check all codes sent Incase you requested more than Once, or contact us for support");
				        </script>
				        <?php
				    }
			}   
    ?> 
<style>
    
    .banner {
      width: 100%; 
      margin: 0 auto; 
      overflow: hidden;
    }
    .banner img {
      width: 100%;
      display: block;
    } 
  </style>
   <div class="container">
      <div class="page-inner">
            <div class="page-header">
              <h3 class="fw-bold mb-3"><?php echo $user_row['course']; ?></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']; ?>"><?php echo $row['subject_name']; ?></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']; ?>"><?php echo $row['code']; ?></a>
                </li>
              </ul>
            </div>
            <div class="row">
              <div class="col-md-12">
                <div class="card">
                  <div class="card-header">
                    <h4 class="card-title"><?php echo $row['subject_name']; ?> -(<?php echo $user_row['course']; ?>)</h4>
                  </div>
                  <div class="card-body">
                  	<?php echo $msg; ?>
                     <div class="col-md-12">  
                        <!--<div class="alert bg-info-gradient " style="word-spacing: 5px; color: white;">-->
                        <!--  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><i class="fas fa-bullhorn"></i>  | Before starting the exam ! make  sure your clear the Quiz per each Sub Topics provided in your course, and make a payment by follow the procedure below-->
                        <!--  </div>-->
                      </div><br>
					 <h5 class="fw-bold mb-3">Examination Invoice</h5>
					  <hr>
                      <div class="row">
			              <!--<div class="col-md-4">-->
			              <!--  <div class="card card-secondary bg-secondary-gradient">-->
			              <!--    <div class="card-body skew-shadow">-->
			              <!--       <h3 class="fw-bold mb-1">Lipa Kwa M-Pesa</h3>-->
			              <!--      <h2 class="py-4 mb-0">36216580</h2>-->
			              <!--      <div class="row">-->
			              <!--        <div class="col-8 pe-0">-->
			              <!--          <h3 class="fw-bold mb-1">Lipa Cybercode</h3>-->
			              <!--          <div class="text-small text-uppercase fw-bold op-8">-->
			              <!--            Cybercode-->
			              <!--          </div>-->
			              <!--        </div>-->
			              <!--        <div class="col-4 ps-0 text-end">-->
			              <!--          <h3 class="fw-bold mb-1">24/25</h3>-->
			              <!--          <div class="text-small text-uppercase fw-bold op-8">-->
			              <!--            Duration-->
			              <!--          </div>-->
			              <!--        </div>-->
			              <!--      </div>-->
			              <!--    </div>-->
			              <!--  </div>-->
			              <!--</div>-->
			              <!--<div class="col-md-4">-->
			              <!--  <div class="card card-secondary bg-secondary-gradient">-->
			              <!--    <div class="card-body bubble-shadow">-->
			              <!--      <h3 class="fw-bold mb-1">Mix By Yas</h3>-->
			              <!--      <h2 class="py-4 mb-0">+255 673 153 074</h2>-->
			              <!--      <div class="row">-->
			              <!--        <div class="col-8 pe-0">-->
			              <!--          <h3 class="fw-bold mb-1">Zephania Ndoshi</h3>-->
			              <!--          <div class="text-small text-uppercase fw-bold op-8">-->
			              <!--            Yas-->
			              <!--          </div>-->
			              <!--        </div>-->
			              <!--        <div class="col-4 ps-0 text-end">-->
			              <!--          <h3 class="fw-bold mb-1">24/25</h3>-->
			              <!--          <div class="text-small text-uppercase fw-bold op-8">-->
			              <!--            Duration-->
			              <!--          </div>-->
			              <!--        </div>-->
			              <!--      </div>-->
			              <!--    </div>-->
			              <!--  </div>-->
			              <!--</div>-->
			              <div class="row md-5 p-4">
			               <div class="col-md-8 ps-md-3">  
			                <div class="card-pricing2 card-secondary  gradient">
			                  <div class="pricing-header">
			                    <h3 class="fw-bold mb-4"><?php echo $row['subject_name']; ?></h3>
			                    <span class="sub-title"><?php echo $user_row['course']; ?></span>
			                  </div>
			                  <div class="price-value">
			                    <div class="value"> 
			                    <div class="banner">
			                      <img class="img-circle" src="assets/img/icon.png" alt="User Avatar">
			                    </div>
			                    </div>
			                  </div>
			                  <br> 
			                  <?php 
			                   
                               $invoice_no = "INV" . date("Ymd") . rand(100, 999);

                              
                                $result = $conn->query("SELECT * FROM `exam_booking` WHERE subject_id='$subject_id' AND booking_id='$book_id'");
                                
                                if ($result->num_rows > 0) {
                                    $row = $result->fetch_assoc();
                                    $grand_total = $row['cost'];
                                    $grand_total = $row['cost'];
                                    $description = "Examination Fee for ";
                                
                                    echo " 
                                    <div class='card-body'>
                                    <div class='table-responsive'>
                                    <table border='0' class='table' >
                                      <tr> 
                                        <td style='text-align: right;'><strong>Date:</strong> " . date("Y-m-d", strtotime($row['exam_created_on'])) . "</td>
                                      </tr> 
                                    </table>
                                    <br>
                                    <table class='table table-bordered' style='font-family: Arial;'>
                                      <tr style='background-color: #f2f2f2;'>
                                        <th>Description</th>
                                        <th>Quantity</th>
                                        <th>Unit Price</th>
                                        <th>Total</th>
                                      </tr>
                                      <tr>
                                        <td style='text-align: left;'>{$description}{$row['exam_name']}</td>
                                        <td style='text-align: right;'>01</td>
                                        <td style='text-align: right;'>{$row['cost']}</td>
                                        <td style='text-align: right;'>".$row['cost']."</td>
                                      </tr>
                                      <tr>
                                        <td colspan='3' style='text-align: right;'><strong>Grand Total</strong></td>
                                        <td style='text-align: right;'><strong>".$grand_total."</strong></td>
                                      </tr>
                                    </table>
                                     </div>
                                    </div>
                                     <a href='orbital/create_order.php?couid=".$id."&sdid=".$student_id."&sbid=".$subject_id."&book_id=".$book_id."' class='btn btn-secondary btn-round float-left'>Make Payment</a>
                                    ";
                                } else {
                                    echo "  
                                      <div class='alert bg-info-gradient ' style='word-spacing: 5px; color: white;'>
                                         <i class='fas fa-bullhorn'></i>  | Invoice not found
                                         </div>
                                         
                                        
                                 ";
                                }
                                ?>   
			               </div>
			             </div> 
			         <!--    <div class="col-md-4 ps-md-0 pe-md-0">-->
			         <!--     	<div class="card">-->
			         <!--     	 <div class="card-header">-->
			         <!--     		<h6>Payment Code</h6>-->
			         <!--     		</div>-->
			         <!--     		<div class="card-body"> -->
			         <!--     		<div class="row"> -->
			         <!--            <form action="" method="post">-->
			                    
			         <!--            <div class="col-md-12">  -->
			         <!--             	<br>-->
			         <!--            <input type="hidden" name="course_code"  class="form-control" value="<?php echo $user_row['course']; ?>">-->
			         <!--             <input type="hidden" name="student_id"  class="form-control" value="<?php echo $user_row['student_id']; ?>">-->
					       <!--       <input type="text" name="payment_code"  class="form-control" placeholder="Past Payment code..." required> -->
					       <!--       </div> -->
					       <!--       <br>-->
					       <!--       <div class="col-md-12" style="text-align: center;"> -->
					       <!--       <button type="submit" name="update" class="btn btn-secondary btn-border btn-lg w-75 fw-bold  ">  Submit</button>-->
					       <!--       </div>-->
					               
					       <!--    </form>-->
					       <!--  </div>-->
					       <!--</div>-->
			         <!--     </div> -->
			         <!--   </div>-->
			            
			         </div> 
			         <br> 
		                  <div class="card-footer"> 
		                  	<div class="row">  
                            <div class="col-md-12"  style="text-align: center;"> 
                        	 <form action="" method="post">  
					              <input type="hidden" name="description"  class="form-control" value="Payment for the course of <?php echo $subject_name; ?>">   
					              <input type="hidden" name="student_id"  class="form-control" value="<?php echo $user_row['student_id']; ?>"> 
					               <input type="hidden" name="student_email_id"  class="form-control" value="<?php echo $user_row['student_email_id']; ?>">
					               <input type="hidden" name="course_code"  class="form-control" value="<?php echo $user_row['course']; ?>">
					              <?php if (!$invoice2){?>
					             <p>Welcome to cybercode </p> 
					              <?php }else if ($invoice2["invoice_status"] == 'Enable'){?>
					              	<a href="orbital/exam.php" class="btn btn-success btn-round"><i class="fas fa-address-card"></i> Examination Portal</a>  
					              <?php }else{?>
					               <p>Your request is under review kindly wait while we process it.</p>
					              <?php  }?>
					         </form> 
                          </div>
                      </div> 
				      </div>
                  </div>
                </div>
              </div> 
            </div>
          </div>
        </div> 
 
