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

<style>
@media(max-width: 992px){
  .insider{
    margin-top: -50px;
    padding-left: 10px;
    padding-right: 10px;
  }        
}
.insider{
  height: 100vh;
  overflow-y: auto;
  padding-bottom: 30px;
  scrollbar-height:auto;
  -ms-overflow-style: none;
}
.insider::-webkit-scrollbar{
  display: none;
}

.result-sheet {
  background: #fff;
  max-width: 900px;
  margin: auto;
  padding: 25px;
  border: 1px solid #ccc;
  border-radius: 10px;
}

.result-sheet .header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  text-align: center;
  border-bottom: 2px solid #333;
  padding-bottom: 15px;
  margin-bottom: 20px;
}
.logo-left img,
.student-photo img {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: 10px;
  border: 2px solid #ccc;
}

.school-details {
  flex-grow: 1;
}

.school-details h2 {
  margin: 0;
  font-size: 24px;
}

.action-buttons {
  text-align: right;
  margin-top: 20px;
}

.action-buttons button {
    background: #062d8f;
    color: #fff;
    padding: 8px 16px;
    margin-left: 10px;
    border: none;
    border-radius: 5px;
    cursor: pointer;
}

.action-buttons button:hover {
    background: #1c4eff;
}
.school-header {
  text-align: center;
  border-bottom: 2px solid #333;
  padding-bottom: 10px;
  margin-bottom: 20px;
}
.school-header img {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: 50%;
}
.school-header h1 {
  font-size: 26px;
  margin-top: 10px;
}
.school-header h4 {
  font-size: 16px;
  font-weight: normal;
}

.student-info {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  margin-bottom: 20px;
  font-size: 15px;
}
.student-info p {
  margin: 5px 0;
}

table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;
}

table, th, td {
  border: 1px solid #555;
}

th, td {
  padding: 10px;
  text-align: center;
  font-size: 14px;
}

th {
  background-color: #062d8f;
  color: white;
}

.summary {
  margin-top: 15px;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}

.summary div {
  margin: 5px 0;
}

.comment-section {
  margin-top: 25px;
}

.comment-section p {
  font-size: 14px;
  line-height: 1.6;
}

.signatures {
  display: flex;
  justify-content: space-between;
  margin-top: 30px;
  font-size: 14px;
}

.sign-box {
  width: 45%;
  border-top: 1px solid #444;
  text-align: center;
  padding-top: 5px;
}

@media(max-width: 600px) {
  .student-info {
    flex-direction: column;
  }
  .summary {
    flex-direction: column;
  }
  .signatures {
    flex-direction: column;
    gap: 20px;
  }
  .sign-box {
    width: 100%;
  }
}
@media print {
  .action-buttons {
    display: none;
  }
  body * {
    visibility: hidden;
  }
  #result-sheet, #result-sheet *{
    visibility: visible;
  }
  .insider{
    padding-top: 0;
  }
  .insider{
    margin-top: 0!important;
    padding-left: none !important;
    padding-right: none !important;
  } 

}

@media(max-width: 992px){
.insider{
    margin-top: -50px;
    /*padding-left: 10px;
    padding-right: 10px;*/
    }        
}
:root {
  --primary-color: #084c61;
  --accent-color: #FFA726;
}


.negative{
  width: 100%;
  height: 400px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-top: 20px;
}
.negative i{
    font-size: 70px;
    color: #FE9C13;

}
.negative h5{
    font-size: 20px;
    color: #FE9C13;
}
.negative a{
    padding: 10px;
    color: #fff;
    text-decoration: none;
    text-align: center;
    font-size: 17px;
    border-radius: 5px;
    /*font-weight: bold;*/
}
.negative a i{
    font-size: 17px;
    color: #fff;
}
.negative button{
    padding: 10px;
    color: #fff;
    text-decoration: none;
    text-align: center;
    font-size: 17px;
    border-radius: 5px;
    outline: none;
    border:none;
    /*font-weight: bold;*/
}
.negative button i{
    font-size: 17px;
    color: #fff;
}
@media(max-width: 992px){
    .card-header h5{
        font-size: 15px;
    }
    .negative h5{
        font-size: 17px;
    }
    .negative p{
        text-align: center;
        font-size: 14px;
        font-family: sans-serif;
    }
    .negative a{
        font-size: 15px ;
    }
    .school-details h2{
      font-size: 15px;
    }
    .school-details p{
      font-size: 12px;
    }
    .text-sm{
      font-size: 12px;
  }
}

.card{
  height: auto;
}
</style>


<?php
$viewStatus = 'Approved';
$getscore = "SELECT status FROM result_approval WHERE status = ? 
            AND class = ? AND term = ? AND session = ? ";
$stmt = $pdo->prepare($getscore);
$stmt->execute([$viewStatus, $current_class, $current_term, $current_session]);


$valid = $stmt->rowCount();

if ($valid < 1) {
    $table = "display:none";
    $negat = "display:flex";
    $cont = "display:block";
}else{
    $table = "display:block";
    $negat = "display:none";
    $cont = "display:none";
}

// Get student avatar
$avat = "SELECT photo_path FROM student_image WHERE adminNo = ?";
$avastmt = $pdo->prepare($avat);
$avastmt->execute([$adminNo]);
$therow = $avastmt->FETCH(PDO::FETCH_ASSOC);
$stdImg = $therow['photo_path'];

// Class teacher input
$getCmt = "SELECT * FROM class_teacher_inputs WHERE student_id = ?
AND term = ? AND session = ?";
$comt = $pdo->prepare($getCmt);
$comt->execute([$adminNo, $current_term, $current_session]);
$commtRow = $comt->FETCH(PDO::FETCH_ASSOC);
$position = isset($commtRow['position']) ? $commtRow['position']: ' ';
$attendance = isset($commtRow['attendance']) ? $commtRow['attendance']:'';
$teacher_comment = isset($commtRow['comment']) ? $commtRow['comment']:'';
$vp_comment = isset($commtRow['vp_comment']) ? $commtRow['vp_comment']:'';

// Total Score and Average
$sql = "SELECT SUM(total) as total_score, AVG(total) as avg_score 
FROM results WHERE stdId = ?";
$query = $pdo->prepare($sql);
$query->execute([$adminNo]);
$avgRow = $query->fetch(PDO::FETCH_ASSOC);
$total_score = $avgRow['total_score'];
$average_score = $avgRow['avg_score'];

// school opening time
$sql_info = "SELECT times_opened FROM school_info WHERE term = ? AND session = ?";
$query_info = $pdo->prepare($sql_info);
$query_info->execute([$current_term, $current_session]);
$infoRow = $query_info->fetch(PDO::FETCH_ASSOC);
$opened_time = $infoRow['times_opened'];

?> 

<div id="notification-containers" class="notification-containers"></div>
<div class="insider">
  <div class="container" style="<?=$cont?>" >
    <div class="cat-pag">

      <div class="card mt-3" >
        <!-- If Empty -->
        <div class="negative" style="<?=$negat?>">
          <i class="fa fa-clock"></i>
          <h5>Results Not Yet Released</h5>
          <p class="text-muted">
            Your results for <b>First Term 2025 Session</b> have not been published yet. <br>
            Please check back later once they are available.
          </p>
          <a href="index.php" class="notifications">
            Okay, Got it <i class="fa fa-check-circle"></i>
          </a>
        </div>
      </div>
    </div>
  </div>
  <div class="" style="<?=$table?>">
    <div id="result-sheet" class="result-sheet">
        <div class="header">
            <div class="logo-left">
                <img src="logo/goodness.jpg" alt="School Logo" />
            </div>

            <div class="school-details">
               <?php SchoolAddress();?>
                <p><strong>Academic Result Sheet</strong></p>
            </div>

            <div class="student-photo">
                <img src="images/avatar/<?=$stdImg?>" alt="Student Photo" />
            </div>
        </div>
        <div class="student-info">
          <p><strong>Name:</strong> <?=$surName.' '.$otherName?></p>
          <p><strong>Class:</strong> <?=$current_class?></p>
          <p><strong>Admission No:</strong> <?=$adminNo?></p>
          <p><strong>Gender:</strong> <?=$gender?></p>
          <p><strong>Session:</strong> <?=$current_session?></p>
          <p><strong>Term:</strong> <?=$current_term?> Term</p>
        </div>

        <table>
          <thead>
            <tr>
              <th class="text-sm">Subject</th>
              <th class="text-sm">CA (40)</th>
              <th class="text-sm">Exam (60)</th>
              <th class="text-sm">Total (100)</th>
              <!-- <th>Grade</th> -->
              <th class="text-sm">Remark</th>
            </tr>
          </thead>
          <tbody>
            <?php
            $getscore = "SELECT * FROM results WHERE stdId = ? 
                        AND class = ? AND term = ? AND session = ? ";
            $stmt = $pdo->prepare($getscore);
            $stmt->execute([$adminNo, $current_class, $current_term, $current_session]);
            while ($row = $stmt->FETCH(PDO::FETCH_ASSOC)) {
                $subject    = $row['subject'];
                $test       = $row['test_score'];
                $secondTest = $row['second_test'];
                $exam       = $row['exam_score'];
                $total      = $row['total'];
                $remark     = $row['grade'];
                $assessment = $test + $secondTest;
            ?>
            <tr>
               <td class="text-sm"><?=$subject?></td>
               <td class="text-sm"><?=$test?></td>
               <td class="text-sm"><?=$exam?></td>
               <td class="text-sm"><?=$total?></td>
              <!--  <td>A</td> -->
               <td class="text-sm"><?=$remark?></td>
            </tr>
            <?php } ?>
            <!-- Add more rows as needed -->
          </tbody>
        </table>
        <?php
        

        ?>
        <div class="summary">
          <div><strong>Total Score:</strong> <?=$total_score?></div>
          <div><strong>Average:</strong> <?=$average_score?></div>
          <div><strong>Position:</strong> <?=$position?></div>
          <div><strong>Attendance:</strong> <?=$attendance?>/<?=$opened_time?></div>
        </div>

        <div class="comment-section">
            <p><strong>Teacher’s Comment:</strong> 
              <?=$teacher_comment?>
                
            </p>
          <p><strong>Principal’s Comment:</strong>
          <?=$vp_comment?> 
          </p>
        </div>

        <div class="signatures">
          <div class="sign-box">Class Teacher's Signature</div>
          <div class="sign-box">Principal's Signature</div>
        </div>
        <div class="action-buttons">
            <button onclick="window.print()">🖨️ Print</button>
            <button onclick="generatePDF()">📄 Download PDF</button>
        </div>
            
    </div>
    
  </div>

</div>


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

<script>
function generatePDF() {
    const element = document.getElementById('result-sheet');
    const opt = {
        margin:       0.5,
        filename:     'student-result.pdf',
        image:        { type: 'jpeg', quality: 0.98 },
        html2canvas:  { scale: 4 },
        jsPDF:        { unit: 'in', format: 'a4', orientation: 'portrait' }
    };
    html2pdf().set(opt).from(element).save();
}
</script>


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

