 <?php 
 include ('../includes/connection.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'];
 $student_id=$user_row['student_id'];
 
     $time_query= mysqli_query($conn,"SELECT * FROM subject_soes,exam_soes,subject_wise_exam_detail WHERE exam_soes.exam_id=subject_wise_exam_detail.exam_id AND subject_soes.subject_id=subject_wise_exam_detail.subject_id AND subject_soes.course_code='$id';")or die(mysqli_error());
     $time = mysqli_fetch_array($time_query);
     $subject_exam_datatime=$time['subject_exam_datetime']; 
     $subject_exam_status=$time['subject_exam_status'];
     $exam_id=$time['exam_id'];
     $subject_name = $time['subject_name'];
     $subject_id=$time['subject_id'];
     $exam_subject_id=$time['exam_subject_id'];
     
 
     if($subject_exam_status=='Completed') {
     $student= mysqli_query($conn,"SELECT  SUM(marks) AS net_marks FROM `exam_subject_question_answer`,`exam_subject_question_soes` WHERE exam_subject_question_answer.exam_subject_question_id=exam_subject_question_soes.exam_subject_question_id AND exam_subject_question_soes.exam_id='$exam_id' AND exam_subject_question_soes.exam_subject_id='$exam_subject_id' AND exam_subject_question_answer.student_id='$student_id';")or die(mysqli_error());
     $row2 = mysqli_fetch_array($student);
     $check_net_marks=$row2['net_marks']; 
        
       
     }
       
    // else if ($subject_exam_status=='Pending'){ 
    //       //  $subject_exam_end_time="2025-10-31";
    //       date_default_timezone_set('Africa/Dar_es_Salaam');
    //           $subject_exam_end_time=$subject_exam_datatime;
     
    //      $remaining_minutes = strtotime($subject_exam_end_time) - time(); 
    //       }else if ($subject_exam_status=='Created'){ 
    //           date_default_timezone_set('Africa/Dar_es_Salaam');
    //          //  $subject_exam_end_time="2025-10-17";
    //          $subject_exam_end_time=$subject_exam_datatime;
     
    //       $remaining_minutes = strtotime($subject_exam_end_time) - time(); 
    //       }else {  
               date_default_timezone_set('Africa/Dar_es_Salaam');
               //  $subject_exam_end_time="2025-10-17";
                    $subject_exam_end_time=$subject_exam_datatime;
                 
                     $remaining_minutes = strtotime($subject_exam_end_time) - time();
            
                    //   if(time() > strtotime($subject_exam_end_time)){
                    //      header('location:orbital/view_subject_exam.php');
                    //  }
         
    //   }  
   
?>
<style>
    
    .reactive-table {
      width: 100%; 
      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.08);
      cursor: pointer;
    }

    .reactive-table th {
      background-color: #f9f9f9;
      font-weight: bold;
    }
    

.gradient-border-button {
  background: none; /* Remove default button background */
  border: none; /* Remove default button border */
  padding: 4px; /* Create space for the gradient border */
  position: relative;
  cursor: pointer;
  border-radius: 80px; /* Adjust for desired roundness */
  overflow: hidden; /* Crucial for containing the animated gradient */
}

.gradient-border-button span {
  display: block;
  padding: 15px 30px; /* Adjust padding as needed */
  background-color: #1a1a1a; /* Inner button background color */
  color: #ffffff; /* Text color */
  font-size: 16px;
  font-weight: bold;
  border-radius: 50px; /* Match outer border-radius */
  position: relative;
  z-index: 1; /* Ensure text is above the gradient */
}

.gradient-border-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(90deg, #00FFA3, #DC1FFF, #00FFA3); /* Gradient colors */
  background-size: 300% 200%; /* Make gradient wider than button for animation */
  border-radius: 50px; /* Match outer border-radius */
  z-index: 0; /* Place behind the span */
  animation: gradient-border-animation 3s linear infinite; /* Animation properties */
}

@keyframes gradient-border-animation {
  0% {
    background-position: 0% 50%;
  }
  100% {
    background-position: 100% 50%;
  }
}
  </style>
      
 <div class="container">
  <div class="page-inner">
    <div
      class="d-flex align-items-left align-items-md-center flex-column flex-md-row pt-2 pb-4"
    >
      <div>
        <h3 class="fw-bold mb-3"></h3>
        <h6 class="op-7 mb-2">Welcome to Cybercode <?php echo date('Y'); ?></h6>
      </div>
      <div class="ms-md-auto py-2 py-md-0"> 
       <?php  if($subject_exam_status=='Pending'){?>
       <button type="button"  class="gradient-border-button" >
         <span>Time to Examination: <b id="countdown"></b></span>
        </button> 
         <?php }else if ($subject_exam_status=='Started'){?> 
           <a href="orbital/exam.php?frtj67hs34fki9dns3=<?php echo $subject_id?>" class="btn btn-success btn-round"><i class="fas fa-address-card"></i> Examination Portal</a>
         <?php } else { ?>
         
         <?php }?> 
      </div>
    </div>
    
     <div class="row">
    <div class="col-md-6">
       <div class="card">
      <div class="card-header">
        <h4 class="card-title">Result For <?php echo $id; ?></h4>
      </div>
      <div class="card-body">
          <div class="table-responsive">
                    <table id="example1" class="table table-striped display table-bordered" >
                      <thead>
                        <tr>
                          <th>#</th>  
                          <th>Examination Name</th>     
                          <th>Total score </th>  
                          <th>Remark</th>  
                          <th>Certificate</th>
                        </tr>
                        </thead>
                        <tbody>
                          <?php 
                                
                            $i = 1;
                            $school_query = mysqli_query($conn,"SELECT * FROM subject_soes,sms_result,subject_wise_exam_detail WHERE sms_result.exam_id=subject_wise_exam_detail.exam_id AND subject_soes.subject_id=subject_wise_exam_detail.subject_id AND subject_soes.course_code='$id' AND student_id='$student_id' ");
                                while($row5 = mysqli_fetch_array($school_query)){  
                             ?>
                            <tr>
                              <td class="text-center"><?php echo $i++ ?></td> 
                              <td><?php echo $row5['subject_name']; ?> </td> 
                              <td><?php echo $row5['total_marks']; ?></td> 
                              <td>
                              <?php
                               if($row5["total_marks"] > '60' )
                                 {?> <span class="badge badge-info"> PASS</span>
                                 <td><a href="orbital/certificate.php?Id_2=<?php echo $row5['course_code']; ?>&Id_3=<?php echo $row5['student_id']; ?>"><button class="btn btn-secondary"><i class="fas fa-download"></i></button></a></td>
                                  <?php } else{?> 
                                  <span class="badge badge-danger">FAIL</span>
                                  <td><i>Under Review.</i></td>
                                 <?php } ?> 
                                </td>   
                              </tr> 
                            <?php }?>
                            </tbody> 
                            <tfoot>
                            <tr>
                                <td colspan="5"><center><p>The Examination is: <?php echo $subject_exam_status; ?> </p></center></td>  
                            </tr>
                          </tfoot>
                             
                    </table> 
                 </div>  
              
        </div>
        </div>
      <div class="col-md-12">
        <div class="card card-round">
          <div class="card-header">
            <div class="card-head-row">
              <div class="card-title">Course Overview</div> 
            </div>
          </div>
          <div class="card-body">
            <div class="chart-container" style="min-height: 375px">
             <div class="embed-responsive embed-responsive-16by9">
               <iframe class="embed-responsive-item" src="https://www.youtube.com/embed/S7smySFl3xY" style="height: 500px; width: 100%;" allowfullscreen></iframe> 
             </div> 
            </div>
            <div id="myChartLegend"></div>
          </div>
        </div>
      </div>
      </div>
     <div class="col-md-6">
    <div class="card">
      <div class="card-header">
        <h4 class="card-title">Your Progress on <?php echo $id; ?></h4>
      </div>
      <div class="card-body">  
      <div class="table-responsive">
    <table class="reactive-table" id="myTable">
    <thead>
      <tr>
        <th>#</th>
        <th>Sub Course</th>
        <th>Progress</th> 
        <th>Q-Marks</th>
        <th>Remark</th>
      </tr>
    </thead>
     <tbody>
         <?php
          $i = 1;
          $course1=$user_row['course']; 
          $student_id=$user_row['student_id'];
          $school_query = mysqli_query($conn,"SELECT DISTINCT module_soes.code,user_progress.module_id,user_progress.q_marks,user_progress.student_id,user_progress.subject_id,user_progress.progress_percent,module_soes.course_code FROM module_soes,user_progress WHERE module_soes.module_id = user_progress.module_id AND module_soes.course_code='$id' AND user_progress.student_id='$student_id'");
          while($row2 = mysqli_fetch_array($school_query)){  
          $subject_id=$row2['subject_id'];
          $module_id=$row2['module_id']; 
          $q_marks_1=$row2['q_marks'];
        ?>
      <tr>
        <td><?php echo $i++ ?></td>
        <td><a href="admin.php?page=fgtt5678532srr5567yfsszadt578jjhggfr443w2&Id_2=<?php echo $user_row['course']; ?>&Id_3=<?php echo $row2['code']; ?>" style="color: black;"><?php echo $row2['code']; ?></a></td>
        <?php if ($row2['progress_percent'] < 100) { ?>
        <td><span class="badge badge-danger"><?php echo $row2['progress_percent']; ?> %</span></td> 
        <?php } else{?>
        <td><span class="badge badge-info"><?php echo $row2['progress_percent']; ?> %</span></td>
        <?php }  
            $quiz = mysqli_query($conn,"SELECT SUM(marks) AS T_MARKS 
                                        FROM `quiz_subject_question_answer` 
                                        WHERE subject_id='$subject_id' 
                                          AND student_id='$student_id' 
                                          AND module_id='$module_id'");
            $quiz_r = mysqli_fetch_array($quiz); 
            $q_marks = $quiz_r['T_MARKS'];
            ?>
            
            <!-- DISPLAY MARKS -->
            <?php if($q_marks !== NULL) { ?>
                <td><?php echo $q_marks; ?> %</td>
            <?php } else { ?>
                <td><i>--NIL--</i></td>
            <?php } ?>
            
            <!-- DISPLAY STATUS -->
            <?php if($q_marks !== NULL) { 
                if ($q_marks <= 50) { ?>
                    <td style="color: red;">
                        <a href="orbital/update_quiz_exam.php?Id_2=<?php echo $module_id; ?>">Retake Quiz</a>
                    </td>
                <?php } else { ?>
                    <td>Pass</td>
                <?php } 
            } else { ?>
                <td><i>--NIL--</i></td>
            <?php } ?>

        </tr>
      <?php } ?> 
    </tbody>
          </table>
          </div>
          </div>
        </div>  
         
        </div>
      </div> 
    
    </div>
</div> 
 
<script>
// Get the target time from PHP (in milliseconds)
var targetTime = <?php echo strtotime($subject_exam_end_time) * 1000; ?>;

// Update the countdown every second
var timer = setInterval(function() {
    var now = new Date().getTime();
    var distance = targetTime - now;

    if (distance <= 0) {
        clearInterval(timer);
        document.getElementById("countdown").innerHTML = "Time's up!";
        window.location.href = "orbital/exam.php?frtj67hs34fki9dns3=<?php echo $subject_id?>"; // redirect when time over
    } else {
        var days    = Math.floor(distance / (1000 * 60 * 60 * 24));
        var hours   = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        var seconds = Math.floor((distance % (1000 * 60)) / 1000);

        // Add leading zeros
        hours = String(hours).padStart(2, '0');
        minutes = String(minutes).padStart(2, '0');
        seconds = String(seconds).padStart(2, '0');

        document.getElementById("countdown").innerHTML =
            days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
    }
}, 1000);
</script>
  