<?php
    
    ////////////////////// POST ACTION TO CHECK STUDENTS RESULT IF THE CHECK RESULT BUTTON IS CLICKED //////////////////
    if(isset($_POST['check_result_btn'])){
        $result_regno = inject_checker($connection, $_POST['result_regno']);
        $result_class = inject_checker($connection, $_POST['result_class']);
        $result_term = inject_checker($connection, $_POST['result_term']);
        $result_session = inject_checker($connection, $_POST['result_session']);

        //////////////// ERROR CHECKING FOR EMPTY FIELDS /////////////////
        if(empty($result_regno)){
            $msg = "<p class='text-danger'>Error! Registration Number is Required</p>";
        }
        elseif($result_class == $select){
            $msg = "<p class='text-danger'>Error! Please Select Class to check Result</p>";
        }
        elseif($result_term == $select){
            $msg = "<p class='text-danger'>Error! Please Select the term for the Result</p>";
        }

        else{
            $query = " SELECT * FROM `results1` WHERE `reg_number` = '{$result_regno}' AND `class` = '{$result_class}' AND `term` = '{$result_term}' AND `session` = '{$result_session}' ";
            $run_query = mysqli_query($connection, $query);

            if($run_query == true){
                if(mysqli_num_rows($run_query) > 0){
                    while($result = mysqli_fetch_assoc($run_query)){
                        $result_id = $result['id'];
                        $_SESSION['result'] = $result_id;
                        header("Location: vp_result_page.php");
                    }
                }else{
                    $msg = "<p class='text-danger'><b>No Result Records Found !!!</b></p>";
                }
            }else{
                $msg = "<p class='text-danger'><b>Result Checking Failed !!!</b></p>";
            } 
        }
    }
?>
<div class="page-title">
  <div>
    <h1><i class="fa fa-calendar"></i> Check Result</h1>
  </div>
  <div>
    <ul class="breadcrumb">
      <li><a href='student_dashboard.php'><i class="fa fa-home fa-lg"></i></a></li>
      <li><a href="#">Check Result</a></li>
    </ul>
  </div>
</div>

<div class="card">
    <?php
        if(isset($msg)){
            echo $msg;
        }
    ?>
    <div class='row'>
        <div class='col-xs-12 col-sm-12 col-md-12'>
			<div class='panel panel-primary'>
                <div class='panel-heading'>
                    <h3 class='log-text'>Please Fill the form to check result</h3>
                </div>
                <div class='panel-body'>
                    <form method='POST' action=''>
                        <div class='input-group'>
                            <span class='input-group-addon' id='basic-addon2'>REG NO</span>
                            <input type='text' name='result_regno' placeholder='Enter Registration Number' class='form-control' />
                        </div>
                        <br />

                        <div class='input-group'>
                            <span class='input-group-addon' id='basic-addon2'>CLASS</span>
                            <select class='form-control' name='result_class'>
                                <option selected><?php echo $select; ?></option>
                                    <?php
                                        $query = " SELECT * FROM `classes` ";
                                        $run_query = mysqli_query($connection, $query);
                                        if(mysqli_num_rows($run_query) > 0){
                                            while($result = mysqli_fetch_assoc($run_query)){
                                                $publish_result_classes = $result['classes'];
                                                echo"
                                                    <option>{$publish_result_classes}</option>
                                                ";
                                            }
                                        }
                                    ?>
                            </select>
                        </div>
                        <br />

                        <div class='input-group'>
                            <span class='input-group-addon' id='basic-addon2'>TERM</span>
                            <select class='form-control' name='result_term'>
                                <option selected><?php echo $select; ?></option>
                                <?php
                                    $term_array = array("First Term", "Second Term", "Third Term");
                                    foreach($term_array as $term){
                                        echo "<option>{$term}</option>";
                                    }
                                ?>
                            </select>
                        </div>
                        <br />

                        <div class='input-group'>
                            <span class='input-group-addon' id='basic-addon2'>Session</span>
                            <select class='form-control' name='result_session'>
                                <option selected><?php echo $select; ?></option>
                                    <?php
                                        $query = " SELECT * FROM `sessions` ";
                                        $run_query = mysqli_query($connection, $query);
                                        if(mysqli_num_rows($run_query) > 0){
                                            while($result = mysqli_fetch_assoc($run_query)){
                                                $all_sessions = $result['sessions'];
                                                echo "<option>{$all_sessions}</option>";
                                            }
                                        }
                                    ?>
                            </select>
                        </div>
                        <br />

                        <p id='btnsubmit'><input type='submit' name='check_result_btn' id='submit' value='Check Result' class='btn btn-large btn-primary login_btn text-center' /></p>
                    </form>
                </div>		
            </div>
        </div>
    </div>
</div>