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

<style>
.card{
    height: 500px;
}
@media(max-width: 992px){
.insider{
    margin-top: -50px;
    padding-left: 10px;
    padding-right: 10px;
    }        
}
.card-header h3{
    font-size: 18px;
    font-weight: bold;
    font-family: sans-serif;
    color: #3C763D;
}
.card-header h5{
    font-size: 14px;
    /*font-weight: bold;*/
    font-family: sans-serif;

}
.cat-page-header h5{
    font-family: sans-serif;
    font-size: 14px;
    font-weight: bold;
}
.success_stutus{
    background: #E8FCEF; color: #4ECA74;
}
.error_status{
    background: #F8D7DA; color: #90151C;
}
.cat-page-header .warning-text{
    color: #90151C;
    font-weight: normal;
    /*font-style: italic;*/
}
.cat-page-header .success-text{
    color: #4ECA74;
    font-weight: normal;
    /*font-style: italic;*/
}
.negative{
    width: 100%;
    height: auto;
    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;
    text-align: center;
    font-weight: bold;
    font-family: sans-serif; 
}
.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;
}
</style>
<?php

// Print all Results
$sn = 0;
$allstd = "SELECT * FROM student WHERE class = ? 
        AND term = ? AND session = ?";
$stmt = $pdo->prepare($allstd);
$stmt->execute([$className, $current_term, $current_session]);
$valid = $stmt->rowCount();
if ($valid < 1) {
    $table = "display:none";
    $negat = "display:flex";
}else{
    $table = "display:block";
    $negat = "display:none";
}


// Get the status for result approval
$approRest = "SELECT status FROM result_approval 
            WHERE class = ? AND term = ? AND session = ?";
$queryRest = $pdo->prepare($approRest);
$queryRest->execute([$className, $current_term, $current_session]);
$countProval = $queryRest->rowCount();
$rowproval = $queryRest->fetch(PDO::FETCH_ASSOC);
$provalStatus = $rowproval['status'];

if ($countProval < 1) {
    $ca_color = 'warning-text';
    $sta_text = 'Not Submitted';
}else
if ($provalStatus =='Pending') {
    $ca_color = 'warning-text';
    $sta_text = 'Pending';
}else{
    $ca_color = 'success-text';
    $sta_text = 'Approved';
}

?>

<div id="notification-containers" class="notification-containers"></div>
<div class="insider">
    <div class="container">
        <div class="cat-pag">
            <div class="cat-page-header d-flex justify-content-between align-items-center">
                <h5>Status: <span class="<?=$ca_color?>"><?=$sta_text?></span>
                </h5>
                <form action="" method="POST">
                    <?php

                    if ($countProval < 1) {?>
                        <button name="confirm-btn" class="btn btn-sm info text-white">
                            Submit Result Now <i class="fa fa-user-plus"></i>
                        </button>
                    <?php }else if ($provalStatus == 'Pending') {?>
                        <button name="" class="btn btn-sm warning text-white">
                        Result Submitted
                        </button> 
                    <?php }else{?>
                        <button name="" class="btn btn-sm invoice text-white">
                        Result Approved
                        </button> 
                    <?php }

                    ?>
                </form>
                    
            </div>
            <div class="card mt-3">
                <div class="card-header text-center">
                    <h3>OBACCO COLLEGE</h3>
                    <h5 class="text-success"><?= $className?> | 
                        <?= $current_term?>Term <?= $current_session?> Academic Session
                    </h5>
                </div>
                <div class="card-body">
                    <!-- If Empty -->
                    <div class="negative" style="<?=$negat?>">
                        <i class="fa fa-envelope-open-text"></i>
                        <h5>No Results Yet.</h5>
                        <p class="text-muted text-center">This happens when the subject teacher has not graded this class or <br>they haven't done with the uploading. 
                        </p>
                    </div>
                    <form action="" method="POST" id="mainForm" style="<?=$table?>">        
                        <div class="table-responsive">
                            <table id="example" class="display table table-striped table-bordered">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>Surname</th>
                                        <th>Othernames</th>
                                        <th>Image</th>
                                        <th>Comments</th>
                                        <!-- <th>Total</th> -->
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                <!-- Display all student in this class -->
                                <?php
                                
                                while ($stdRow = $stmt->fetch(PDO::FETCH_ASSOC)) {
                                    $stdId      = $stdRow['studId'];
                                    $surName    = $stdRow['surname'];
                                    $otherNames  = $stdRow['otherNames'];
                                    $gender     = $stdRow['gender'];
                                    $sn = $sn + 1;
                                    $encryptId = encryptData($stdId);

                                    // Get student image
                                    $stdImg = "SELECT photo_path, dob 
                                            FROM student_image WHERE studentId = ?";
                                    $imgStmt = $pdo->prepare($stdImg);
                                    $imgStmt->execute([$stdId]);
                                    while ($imgRow = $imgStmt->fetch(PDO::FETCH_ASSOC)) {
                                        $theImg = $imgRow['photo_path'];
                                        $dob    = $imgRow['dob'];

                                // Get each student comment status
                                    $comstatus = "SELECT * FROM class_teacher_inputs 
                                    WHERE student_id = ? AND term = ? AND session = ? ";

                                    $stmtComent = $pdo->prepare($comstatus);
                                    $stmtComent->execute([$stdId, $current_term, $current_session]);
                                    $comdes_status = $stmtComent->rowCount();
                                    if ($comdes_status > 0) {
                                      $respond_to = 'Filled';
                                    }else{
                                      $respond_to = 'No Record';
                                    }
                                ?>
                                    <tr>
                                        <td><?= $sn ?></td>
                                        <td>
                                            <?=$surName?>
                                        </td>
                                        <td>
                                            <?=$otherNames?>
                                        </td>
                                        <td>
                                            <img alt="Student Image" src="../student/images/avatar/<?=$theImg?>" id = "image-<?=$stdId?>" width="60px" height="40px" >
                                        </td>
                                        <td><?=$respond_to?></td>
                                        <td>
                                            <a href="compile.php?stdVal=<?= $encryptId?>" class="btn btn-sm btn-primary">
                                                Compile <i class="fa fa-pen"></i>
                                            </a>
                                        </td>
                                    </tr>
                                   <?php } } ?> 
                                </tbody>
                            </table>
                        </div>
                        
                    </form>
                </div>
            </div>
        </div>
   
    </div>
</div>
<?php
// Confirm Result before sending
if (isset($_POST['confirm-btn'])) {
  
  $msgTitle = "<b>Confirm Request</b>";
  $msgBody = " <center>
              Are you sure you want to submit the results 
              to the Vice Principal for final approval? 
              <br>This means you have crossed checked the scores, performance, comment and attendance to be very okay.</center>
              ";
  $msgCloseBtn = "No, Cancel";
  $msgActionBtn = "Yes, Submit";
  include'includes/confirm-alert.php'; 
}

// Send Results for approval
if (isset($_POST['send-result-btn'])) {
  $created = date('Y-m-d');
  $sendResult = "INSERT INTO result_approval (class, term, session, modified)
                VALUES(?,?,?,?)";
  $sendStmt = $pdo->prepare($sendResult);
  $sendStmt->execute([$className, $current_term, $current_session, $created]);

  $_SESSION['message'] = "Sent for approval";
  $_SESSION['type'] = "success";
}
?>


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

<!-- DataTables & jQuery -->
<link rel="stylesheet" href="https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js"></script>

<script>
    $(document).ready(function () {
        $('#example').DataTable();
        $('#SelectAllBoxes').click(function () {
            $('.checkBoxes').prop('checked', this.checked);
        });
    });

    
</script>




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

