<?php session_start();?>
<?php include'includes/header.php'; ?>
<?php include'includes/sidebar.php';?>
<?php include'includes/functions.php';?>
<?php include'includes/navbar.php'; ?>

<!-- Handle Add Slider -->
<style>

@media(max-width: 992px){
    .insider{
        margin-top: -50px;
    }
    .card{
      height: 500px;
    }
    .negative{
      height: 500px;
    } 
    .card-header h5{
      font-size: 12px;
    }
}

.card{
    height: 600px;
}


</style>

<?php
if (isset($_GET['viewVal'])) {
  $package = $_GET['viewVal'];
  $unpack = explode('*', $package);

  $className = $unpack[0];
  $term = $unpack[1];
  $session = $unpack[2];
  $stdId  = $unpack[3];

  $decryptId = decryptData($stdId);
}else{
  header('Location:my-students.php');
}
$className; // class name
$term; // this term
$session; // this session
$studentId = $decryptId;

// Go back link
$back_link = $className.'*'.$term.'*'.$session;

// Get student information
$stdInfo = "SELECT * FROM student WHERE adminNo = ?";
$infoStmt = $pdo->prepare($stdInfo);
$infoStmt->execute([$studentId]);
$infoRow = $infoStmt->fetch(PDO::FETCH_ASSOC);
$surname = $infoRow['surname'];
$othernames = $infoRow['otherNames'];

// Get comments details for this student
$comdes = "SELECT * FROM class_teacher_inputs WHERE student_id = ? 
          AND term = ? AND session = ? ";
$stmtComent = $pdo->prepare($comdes);
$stmtComent->execute([$studentId, $term, $session]);
$recordRow = $stmtComent->fetch(PDO::FETCH_ASSOC);

$presentNo = $recordRow['attendance'];
$comment  = $recordRow['comment'];
$vp_comment = $recordRow['vp_comment'];
$check_status = $recordRow['check_status'];
$position     = $recordRow['position'];


// Get the results details
$resultNow = "SELECT * FROM results WHERE stdId = ? 
            AND class = ? AND term = ? AND session = ? ";
$stmtResult = $pdo->prepare($resultNow);
$stmtResult->execute([$studentId, $className, $term, $session]); 
$currentResult = $stmtResult->rowCount();

// Get student image
$getImg = "SELECT photo_path FROM student_image WHERE adminNo = ?";
$imgPdo = $pdo->prepare($getImg);
$imgPdo->execute([$studentId]);
$imgRow = $imgPdo->FETCH(PDO::FETCH_ASSOC);
$theImg = $imgRow['photo_path'];

// Get the school opening time for this session
$getInfo = "SELECT times_opened FROM school_info WHERE term = ? AND session = ?";
$infoPdo = $pdo->prepare($getInfo);
$infoPdo->execute([$current_term, $current_session]);
$openedInfo = $infoPdo->FETCH(PDO::FETCH_ASSOC);
$times_opened = $openedInfo['times_opened'];
?>
<div id="notification-containers" class="notification-containers"></div>
<div class="insider">
  <div class="container">
    <div class="cat-pag">
      <div class="card mt-3">
        <div class="card-header" style="width: 100%; display: flex;">
          <form action="" method="POST" style="display: flex; gap: 20px; width: 100%;align-items: center; ">
            <button class="btn btn-sm primary" 
            onclick="switchToForm(); 
            event.preventDefault();">
            Edit Record 
            <i class="fa fa-edit"></i>
            </button>
            <?php
            if ($check_status == 'Pending') {?>
              <button 
              class="btn btn-sm info"
              name="mark-btn"> 
              Mark as checked 
              <i class="fa fa-check-circle"></i>
              </button>
            <?php }else{?>

              <button
              type="button" 
              class="btn btn-sm invoice"
              style="cursor: not-allowed;" 
              >
              Checked 
              <i class="fa fa-check-circle"></i>
              </button>

            <?php }?>
            <div style="">
            <a href="approved_review.php?rsltVal=<?=$back_link?>" 
              class="btn btn-sm notifications">
              <i class="fa fa-caret-right"></i> 
              Go Back
            </a>
            </div>
          </form>
            
          </div>
          <div class="card-body" style="background: #F7F9FB;">
            <div class="dashboard">

              <!-- Student Info Card -->
              <div class="student-card">
                <div class="student-header">
                  <img src="../../student/images/avatar/<?=$theImg?>" alt="Student Photo">
                  <div>
                    <h2><?php echo strtoupper($surname).', '.$othernames; ?></h2>
                    <h5><?=$studentId?></h5>
                  </div>
                </div>

                <!-- Already Entered Data -->
                <div id="student-data">
                  <div class="entered-data">
                    <p><strong>Attendance:</strong> <?=$presentNo.'/'.$times_opened?></p>
                    <p><strong>Comment:</strong> <?=$comment?> </p>
                    <p><strong>Position: <?=$position?></strong> </p>

                    <?php
                    if ($vp_comment == 'No comment yet!') { ?>
                      <div id="no-data" class="info-box">
                        No Comment yet from V/Principal to <?=$surname?>.
                        <button 
                        onclick="switchToForm()"
                        class="btn btn-sm primary text-white ">Add comment</button>
                      </div>
                    <?php }else{?>
                      <p><strong>VP's Comment:</strong> <?=$vp_comment?></p>
                      <!-- Hardworking and respectful student. Keep it up! -->
                    <?php }
                    ?>
                  </div>
                  
                  <?php
                  if ($currentResult =='') {?>
                    <div id="" class="info-box">
                      No result data entered yet for this student.
                    </div>
                  <?php }else{?>
                    <!-- Subjects Table -->
                    <table>
                      <thead>
                        <tr>
                          <th>Subject</th>
                          <th>First CA</th>
                          <th>Second CA</th>
                          <th>Exam Score</th>
                          <th>Total</th>
                          <th>Grade</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php
                        while ($resultRow = $stmtResult->fetch(PDO::FETCH_ASSOC)) {
                          $subject = $resultRow['subject'];
                          $test_score = $resultRow['test_score'];
                          $exam_score = $resultRow['exam_score'];
                          $second_test = $resultRow['second_test'];
                          $total_score = $resultRow['total'];
                          $grade      = $resultRow['grade']; ?>
                        <tr>
                          <td><?=$subject?></td>
                          <td><?=$test_score?></td>
                          <td><?=$second_test?></td>
                          <td><?=$exam_score?></td>
                          <td><?=$total_score?></td>
                          <td><?=$grade?></td>
                        </tr>
                        <?php } ?>
                      </tbody>
                    </table>
                    <!-- <button class="btn" onclick="switchToForm()">Edit Record</button> -->
                 <?php }
                  ?>
                </div>

              <!-- Form to enter data -->
              <form action="" method="POST" id="student-form" style="display:none; margin-top:15px;">
                <?php
                // Save New Record
                if ($vp_comment == 'No comment yet!') {?>
                <!-- <div class="form-group">
                  <label>Attendance (Present Times Only)</label>
                  <input type="text" placeholder="e.g. 42" name="attendance" required="">
                </div> -->

                <div class="form-group">
                  <label>Class Teacher's Comment</label>
                  <textarea name="teacher_comment"><?=$comment?></textarea>
                </div>

                <div class="form-group">
                  <label>Vice/Principal's Comment</label>
                  <textarea name="vp_comment" 
                  placeholder="e.g. Hardworking and respectful student."
                  required=""></textarea>
                </div>
                
                <button name="edit-record" type="submit" class="btn primary">Save Record</button>

                <!-- Already Exists, need updating -->
                <?php }else{?>
                <!-- <div class="form-group">
                  <label>Attendance (Present Times Only)</label>
                  <input type="text" placeholder="e.g. 42" name="attendance" value="<?=$presentNo?>">
                </div> -->

                <div class="form-group">
                  <label>Class Teacher's Comment</label>
                  <textarea name="teacher_comment"><?=$comment?></textarea>
                </div>

                <div class="form-group">
                  <label>Vice/Principal's Comment</label>
                  <textarea name="vp_comment"><?=$vp_comment?></textarea>
                </div>
               
                <button name="edit-record" type="submit" class="btn primary">Update Record</button>
                <?php }
                ?>
              </form>
            </div>
            <!-- Dashboard Ends -->
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<?php
// Mark as Read
if (isset($_POST['mark-btn'])) {
  $currently = 'Checked';
  // Saved New comment
  $updSta = "UPDATE class_teacher_inputs SET check_status = ? WHERE student_id = ?";
  $theupdPdo = $pdo->prepare($updSta);
  $theupdPdo->execute([$currently, $studentId]);

  $_SESSION['message'] = 'Marked as Checked Successfully';
  $_SESSION['type'] = 'success';?>

  <script type="text/javascript">
    setTimeout("location.href='javascript:history.back()';", 2000);
  </script>
  <?php


}
// Save Editing Records 
if (isset($_POST['edit-record'])) {
  $vp_comment         = $_POST['vp_comment'];
  $teacher_comment    = $_POST['teacher_comment'];
  // Update Record
  $update = "UPDATE class_teacher_inputs SET vp_comment = ?, comment = ? 
            WHERE student_id = ? ";
  $updateStmt = $pdo->prepare($update);
  $updateStmt->execute([$vp_comment, $comment, $studentId]);

  $_SESSION['message'] = "Record Updated Successfully";
  $_SESSION['type'] = "success";?>

  <script type="text/javascript">
    setTimeout("location.href='javascript:history.back()';", 2000);
  </script>
  <?php

}



?> 
<script>
  // Switch between view mode and form mode
  function switchToForm() {
    document.getElementById("student-data").style.display = "none";
    document.getElementById("student-form").style.display = "block";
  }
</script>


<?php include 'includes/footer.php'; ?>




<!-- Modal Styling -->
<style>
.dashboard {
  max-width: 900px;
  margin: auto;
}

/* Student Profile Card */
.student-card {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  margin-bottom: 20px;
}

.student-header {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.student-header img {
  width: 70px;
  height: 70px;
  border-radius: 50%;
  object-fit: cover;
  margin-right: 15px;
}

.student-header h2 {
  margin: 0;
  font-size: 18px;
  color: #222;
  font-family: sans-serif;
  font-weight: bold;
}
.student-header h5{
  font-size: 16px;
}

.info-box {
  background: #eef3fc;
  border-left: 4px solid #007bff;
  padding: 10px 15px;
  margin-bottom: 15px;
  color: #444;
  border-radius: 4px;
  font-size: 14px;
}

.entered-data p {
  margin: 6px 0;
  font-size: 14px;
}

/* Subject Table */
table {
  width: 100%;
  border-collapse: collapse;
  background: #fff;
  margin-top: 20px;
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

table thead {
  background: #007bff;
  color: #fff;
}

table th, table td {
  padding: 12px 15px;
  text-align: center;
  border-bottom: 1px solid #eee;
  font-size: 14px;
}

table tr:last-child td {
  border-bottom: none;
}

table tbody tr:hover {
  background: #f9f9f9;
}

/* Buttons */
.btn {
  display: inline-block;
  padding: 8px 15px;
  /*background: #007bff;*/
  color: #fff;
  border-radius: 6px;
  font-size: 14px;
  border: none;
  cursor: pointer;
  transition: background 0.3s;
  margin-top: 10px;
}

.btn:hover {
  /*background: #0056b3;*/
  color: #fff;
}

/* Form Styling */
.form-group {
  margin-bottom: 15px;
}

.form-group label {
  font-weight: 600;
  display: block;
  margin-bottom: 5px;
}

.form-group input, 
.form-group textarea {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 6px;
   background: #f9f9f9;
}

.form-group textarea {
  resize: vertical;
  min-height: 80px;
  font-size: 14px;
}

@media(max-width: 992px){
  .student-header h2{
    font-size: 14px;
  }
  .student-header h5{
    font-size: 13px;
  }
  .student-header img{
    width: 60px;
    height: 60px;
  }
}    
</style>

<?php include'includes/notification-alert-js.php';?>

