<?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['stdVal'])) {
  $stdVal = $_GET['stdVal'];
  $decryptId = decryptData($stdVal);
}else{
  header('Location:my-students.php');
}
$className; // class name
$current_term; // this term
$current_session; // this session
$studentId = $decryptId;

// Get student information
$stdInfo = "SELECT * FROM student WHERE studId = ?";
$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, $current_term, $current_session]);
$comdes_status = $stmtComent->rowCount();
if ($comdes_status < 1) {
  $res = 'disabled=""';
}else{
  $res = '';
}

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


?>
<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">
            <form action="" method="POST" style="display: flex; gap: 20px; padding-left: 40px;">
              <button class="btn btn-sm primary" 
              onclick="switchToForm(); 
              event.preventDefault();">
              Edit Record 
              <i class="fa fa-edit"></i>
            </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.jpg" alt="Student Photo">
                  <h2><?php echo strtoupper($surname).', '.$othernames; ?></h2>
                </div>

                <!-- Already Entered Data -->
                <div id="student-data">
                  <?php
                  if ($comdes_status < 1) { ?>
                    <div id="no-data" class="info-box">
                      No record for student attendance and activities.
                    </div>
                  <?php }else{
                    $recordRow = $stmtComent->fetch(PDO::FETCH_ASSOC);
                    $presentNo = $recordRow['attendance'];
                    $comment  = $recordRow['comment'];
                    ?>
                    <div class="entered-data">
                      <p><strong>Attendance:</strong> <?=$presentNo?>/45</p>
                      <p><strong>Activities:</strong> Debate club member, Football team</p>
                      <p><strong>Comment:</strong> <?=$comment?>
                        <!-- Hardworking and respectful student. Keep it up! -->
                      </p>
                    </div>
                  <?php }
                  ?>
                  
                  <?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>Test Score</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'];
                          $total_score = $resultRow['total'];
                          $grade      = $resultRow['grade']; ?>
                        <tr>
                          <td><?=$subject?></td>
                          <td><?=$test_score?></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 ($comdes_status < 1) {?>
                <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>Student Activities <small></small></label>
                  <textarea placeholder="e.g. Debate, Football, Class prefect"></textarea>
                </div> -->

                <div class="form-group">
                  <label>Class Teacher's Comment</label>
                  <textarea name="comment" 
                  placeholder="e.g. Hardworking and respectful student."
                  required=""></textarea>
                </div>
                <!-- <div class="form-group">
                  <label>Subjects & Scores</label>
                  <textarea placeholder="Example:
                  English - Test:20, Exam:45
                  Math - Test:30, Exam:50"></textarea>
                </div> -->
                <button name="save-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>Student Activities <small></small></label>
                  <textarea placeholder="e.g. Debate, Football, Class prefect"></textarea>
                </div> -->

                <div class="form-group">
                  <label>Class Teacher's Comment</label>
                  <textarea name="comment"><?=$comment?></textarea>
                </div>
                <!-- <div class="form-group">
                  <label>Subjects & Scores</label>
                  <textarea placeholder="Example:
                  English - Test:20, Exam:45
                  Math - Test:30, Exam:50"></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
// Save New Record for student
if (isset($_POST['save-record'])) {
  $attendance = $_POST['attendance'];
  $comment    = $_POST['comment'];
  // Saved New comment
  $insertCom = "INSERT INTO class_teacher_inputs (student_id, term, session, comment, attendance)
                VALUES(?,?,?,?,?) ";
  $insertPdo = $pdo->prepare($insertCom);
  $insertPdo->execute([$studentId, $current_term, $current_session, $comment, $attendance]);

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

  <script type="text/javascript">
    setTimeout("location.href='my-students.php';", 2000);
  </script>
  <?php


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

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

  <script type="text/javascript">
    setTimeout("location.href='my-students.php';", 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: 22px;
  color: #222;
  font-family: sans-serif;
  font-weight: bold;
}

.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;
}

/*.disa{
  cursor: 
}     */
    
</style>

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

