<?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>
.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: 18px;
    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;*/
}
.card-body h5{
    font-weight: bold;
    font-size: 15px;
    font-family: sans-serif;
}
.details{
    width: 100%;
    display: flex;
    grid-gap: 30px;
}
.details h5{
    text-align: left;
}
.overview{
    width: 100%;
    display: flex;
    flex-direction: column;
    gap:10px;
}
.title-head{
    width: 100%;
    height: auto;
   
    padding: 10px;
    border:1px solid #ccc;
    text-align: center;
}
.title-head h5{
    font-weight: bold;
    font-size: 25px;
    font-family: sans-serif;
}
.title-head h4{
    font-size: 15px;
    font-family: sans-serif;
    font-weight: bold;
}
.comment{
    height: auto; padding: 5px; 
    border-left: 1px solid #ccc; 
    border-bottom: 1px solid #ccc; 
    font-size: 16px; 
    font-family: sans-serif;
}
   
</style>
<?php

if (isset($_GET['val'])) {
    $stdId = $_GET['val'];
}
// Get all the result for this student
$clastd = "SELECT class FROM class_teacher WHERE staffId = ?";
$thepdo = $pdo->prepare($clastd);
$thepdo->execute([$staffId]);
$filter = $thepdo->rowCount();
if ($filter == FALSE) {
    header('Location:index.php');
}else{
    $classRow = $thepdo->fetch(PDO::FETCH_ASSOC);
    $myClass = $classRow['class'];
}

// Get student info
$stdInfo = "SELECT * FROM student WHERE studId = ? ";
$stmt = $pdo->prepare($stdInfo);
$stmt->execute([$stdId]);
$stdRow = $stmt->fetch(PDO::FETCH_ASSOC);
    $surName    = $stdRow['surname'];
    $otherNames  = $stdRow['otherNames'];
    $gender     = $stdRow['gender'];
$fullNames = $surName.' '.$otherNames;

$test_status = "pending";
$exam_status = "pending";

if ($test_status =='pending') {
    $ca_color = 'warning-text';
}else{
    $ca_color = 'success-text';
}

if ($exam_status =='pending') {
    $exam_color = 'warning-text';
}else{
    $exam_color = 'success-text';
}

// Check comment
$comms = "SELECT * FROM comments_tbl WHERE studId = ? AND session = ? AND term = ?";
$commsPdo = $pdo->prepare($comms);
$commsPdo->execute([$stdId, $current_session, $current_term]);

// sort out
$relv = $commsPdo->rowCount();
if ($relv == TRUE) {
    $commentRelv = TRUE; 
}else{
    $commentRelv = FALSE;
}

?>

<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>Name: <span class="<?= $ca_color?>"><?= $fullNames?></span> | 
                    SEX: <span class="<?= $exam_color?>"><?= $gender?></span>
                </h5> -->
                
                <div style="display: flex; align-items: center; justify-content: center; gap:20px">
                    <h5>Comments:</h5>
                    <?php
                    if ($commentRelv == TRUE) {?>
                        <button class="btn info btn-sm text-white" onclick="openModal('modal-update')"> View Comment <i class="fa fa-eye"></i>
                        </button>
                    <?php } else{?>
                        <button class="btn info btn-sm text-white" onclick="openModal('modal-add')" 
                            >Write Comment 
                            <i class="fa fa-pen"></i>
                        </button>
                    <?php }
                    ?>
                    
                </div>
            </div>


            <div class="card mt-3">
                
                <div class="card-body ">
                    <div class="title-head">
                        <h5>YOUR SCHOOL COLLEGE</h5>
                        <h4>BAPTIST HIGH SCHOOL AREA OGBOMOSO<br>OYO STATE NIGERIA</h4>
                    </div>
                    <table class="mt-4">
                      <tr>
                        <td><span style="font-weight: bold">Student Name:</span></td>
                        <td style="padding-left: 10px; "><?= $fullNames?></td>
                      </tr>
                      <tr>
                        <td><span style="font-weight: bold">Gender:</span></td>
                        <td style="padding-left: 10px; "><?= $gender?>.</td>
                      </tr>
                      <tr>
                        <td><span style="font-weight: bold">Class:</span></td>
                        <td style="padding-left: 10px; "><?= $myClass?>.</td>
                      </tr>
                      <tr>
                        <td><span style="font-weight: bold">Session:</span></td>
                        <td style="padding-left: 10px; "><?=$current_session?>.</td>
                      </tr>
                      <tr>
                        <td><span style="font-weight: bold">Term:</span></td>
                        <td style="padding-left: 10px; "><?= $current_term?>.</td>
                      </tr>
                    </table> 

                    <div class="table-responsive mt-4">
                        <table class="table table-striped table-bordered">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Subjects</th>
                                    <th>Test Score</th>
                                    <th>Exam Score</th>
                                    <th>Total</th>
                                    <th>Remarks</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $sn = 0;
                                $stdRst = "SELECT * FROM results WHERE class = ? 
                                        AND term = ? AND session = ? AND stdId = ?";
                                $stmt = $pdo->prepare($stdRst);
                                $stmt->execute([$myClass, $current_term, 
                                                $current_session, $stdId]);
                                while ($stdRow = $stmt->fetch(PDO::FETCH_ASSOC)) {
                                    $subject    = $stdRow['subject'];
                                    $test_score = $stdRow['test_score'];
                                    $exam_score = $stdRow['exam_score'];
                                    $total      = $stdRow['total'];
                                    $grade      = $stdRow['grade']; 
                                    $sn = $sn + 1;
                                ?>
                                 <tr>
                                     
                                     <td><?= $sn ?></td>
                                     <td>
                                         <?=$subject?>
                                     </td>
                                     <td>
                                         <?= $test_score?>
                                     </td>
                                     <td>
                                         <?= $exam_score?>
                                     </td>
                                     <td><?=$total?></td>
                                     <td>
                                         <?= $grade?>
                                     </td>
                                 </tr>
                                <?php } ?>
                            </tbody>
                        </table>
                    </div> 
                    <div class="overview">
                        <h5><u>Summary:</u></h5>
                        <span>Total Subject: 40</span>
                        <span>Total Score: 300</span>
                        <span>Position: 1</span>
                        <span>Total Student: 50</span>
                        <h5>Comments:</h5>
                        <div class="comment">

                        <?php
                        if ($commentRelv == TRUE) {
                            $commtRow = $commsPdo->fetch(PDO::FETCH_ASSOC);
                            $commtId = $commtRow['commentId'];
                            $comments = $commtRow['comments'];
                            ?>
                          <?= $comments?>
                        <?php }
                        ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
   
    </div>
</div>

<?php
// Save Comments Query
if (isset($_POST['add-commt'])) {
    $comments = $_POST['comments'];

    $commt = "INSERT INTO comments_tbl (comments, studId, session, term )
            VALUES(?, ?, ?, ?)";
    $commPdo = $pdo->prepare($commt);
    $commPdo->execute([$comments, $stdId, $current_session, $current_term]);

    // Success Message
    $_SESSION['message'] = 'Comments Inserted Successfully';
    $_SESSION['type'] = 'success';?>

    <!-- Refresh the page in two minutes -->
    <script type="text/javascript">
      setTimeout("location.href='javascript:history.back()';", 2000);
    </script>
    <?php 
}


// Update Comments Query 
if (isset($_POST['upd-commt'])) {
    $keywords = $_POST['comments'];
    $commtId  = $_POST['commtId'];

    $upds = "UPDATE comments_tbl SET comments = ? WHERE commentId = ? ";
    $updsPdo = $pdo->prepare($upds);
    $updsPdo->execute([$keywords, $commtId]);

    // Success Message
    $_SESSION['message'] = 'Comments Updated Successfully';
    $_SESSION['type'] = 'success';?>

    <!-- Refresh the page in two minutes -->
    <script type="text/javascript">
      setTimeout("location.href='javascript:history.back()';", 2000);
    </script>

    <?php 


}
?>

<!-- Add New Comments Modal -->
<div id="modal-add" class="modal">
    <div class="modal-content" style="margin-top:50px;">
        <div class="modal-header text-success">Write comments for <?= $fullNames;?> <i class="fa fa-pen"></i></div>
        <div class="modal-body">
            <form action="" method="POST">
                <div>
                    <textarea name="comments" placeholder="Write Comments here"></textarea>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger btn-sm" onclick="closeModal('modal-add')">Cancel <i class="fa fa-cancel"></i> &times;</button>
                    <!-- Add product button -->
                    <button name="add-commt" class="btn btn-success btn-sm">Add Comments <i class="fa fa-plus-circle"></i></button>
                </div>
            </form>
        </div>
        
    </div>
</div>

<!-- Modal Update -->
<div id="modal-update" class="modal">
    <div class="modal-content" style="margin-top:50px;">
        <div class="modal-header text-success">Update comments for <?= $fullNames;?> <i class="fa fa-pen"></i></div>
        <div class="modal-body">
            <form action="" method="POST">
                <div>
                    <textarea name="comments" placeholder="Write Comments here"><?=$comments?></textarea>
                        <input type="hidden" name="commtId" value="<?= $commtId?>">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger btn-sm" onclick="closeModal('modal-update')">Cancel <i class="fa fa-cancel"></i> &times;</button>
                    <!-- update button -->
                    <button name="upd-commt" class="btn btn-success btn-sm">Update Comments <i class="fa fa-edit"></i></button>
                </div>
            </form>
        </div>
        
    </div>
</div>



<?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();
        
    });
    function openModal(id) {
        document.getElementById(id).style.display = "block";
    }

    function closeModal(id) {
        document.getElementById(id).style.display = "none";
    }

    function handleDelete() {
        const checked = document.querySelectorAll("input[name='checkBoxArray[]']:checked");
        if (checked.length === 0) {
            alert("No products selected!");
            return;
        }
        openModal('modal-delete');
    }
   
</script>


<!-- Modal Styling -->
<style>
textarea{
    width: 100%;
    height: 100px;
    border:2px solid #ccc;
    border-radius: 5px;
    padding-left: 10px;
    padding-right: 10px;
    padding-top: 10px;
}
    .modal {
        display: none;
        position: fixed;
        z-index: 99999;
        left: 0; top: 0;
        width: 100%; height: 100%;
        overflow: auto;
        background-color: rgba(0,0,0,0.5);

    }

    .modal-content {
        background-color: #fff;
        margin: auto;
        padding: 20px;
        border-radius: 8px;
        width: 80%;
        max-width: 500px;
        animation: fadeIn 0.3s;
    }

    .modal-header, .modal-footer {
        font-weight: bold;
    }

    @keyframes fadeIn {
        from { opacity: 0; transform: translateY(-20px); }
        to { opacity: 1; transform: translateY(0); }
    }

    .label-wraps{
        width: 100%;
        padding-left: 30px;
        padding-right: 30px;
        display: flex;
        justify-content: space-between;
      
    }
    .input-wraps{
        width: 100%;
        display: flex;
        gap:8px;
        padding: 5px;
    }
    .price-box{
        width: 50%;
        height: 55px;
        /*border-bottom: 2px solid #ddd;*/
        border:2px solid #ddd;
        padding: 15px;
        border-radius: 10px;
    }
    .price-box span{
        font-size: 18px;
        color: #666;
        /*font-weight: bold;*/
    }
    .price-box input{
        width: 85%;
        
        border:none;
        outline: none;
        font-size: 16px;
        color: 888;
       /* height: 20px;*/
    }
    .quant-box{
        width: 50%;
        height: 55px;
        /*border-bottom: 2px solid #ddd;*/
        border:2px solid #ddd;
        padding: 15px;
        border-radius: 10px;
    }
    .quant-box input{
        width: 100%;
        border:none;
        outline: none;
        font-size: 16px;
        color: 888;
    }
</style>

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

