<?php
if (isset($_POST['result_date_btn'])) {
    $term_end_date  = inject_checker($connection, $_POST['term_end_date']);
    $next_term_date = inject_checker($connection, $_POST['next_term_date']);
    $date           = date('Y-m-d');

    if (empty($term_end_date)) {
        $_SESSION['msg']  = "Please Type End of Term Date.";
        $_SESSION['type'] = "error";
    } elseif (empty($next_term_date)) {
        $_SESSION['msg']  = "Please Type Next Resumption Date.";
        $_SESSION['type'] = "error";
    } else {
        $query = "SELECT * FROM `dates`";
        $run_query = mysqli_query($connection, $query);

        if (mysqli_num_rows($run_query) == 0) {
            $query = "INSERT INTO `dates`(`end_of_term`, `next_term_begins`, `date_declared`) 
                      VALUES('$term_end_date', '$next_term_date', '$date')";
            $run_query = mysqli_query($connection, $query);

            if ($run_query === true) {
                $_SESSION['msg']  = "Session Dates Declared Successfully.";
                $_SESSION['type'] = "success";
            } else {
                $_SESSION['msg']  = "Session Dates Declaration Failed.";
                $_SESSION['type'] = "error";
            }
        } else {
            $query = "UPDATE `dates` SET 
                      `end_of_term` = '{$term_end_date}', 
                      `next_term_begins` = '{$next_term_date}', 
                      `date_declared` = '{$date}'";
            $run_query = mysqli_query($connection, $query);

            if ($run_query === true) {
                $_SESSION['msg']  = "Session Dates Updated Successfully.";
                $_SESSION['type'] = "success";
            } else {
                $_SESSION['msg']  = "Session Dates Update Failed.";
                $_SESSION['type'] = "error";
            }
        }
    }

    header("Location: modules.php?save_resumption");
    exit();
}
?>


<div class='page-title'>
    <div>
        <h1><i class='fa fa-laptop'></i> Result Management</h1>
        <p>Save Resumption Date</p>
    </div>
</div>

<div class='card'>
    <form action='' method='POST'>
        <div class='panel panel-primary ch'>
            <div class='panel-heading'>
                <h4 class='text-center'>Save end of term and resumption date</h4>
            </div>
            <div class='panel-body'>
                <div class='row'>
                    <div class='col-xs-12 col-sm-12 col-md-5'>
                        <table class='table table-striped'>
                            <tr>
                                <td>Term Ends</td>
                                <td>
                                    <input name='term_end_date' type='date' class='form-control' required>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
                <div class='row'>
                    <div class='col-xs-12 col-sm-12 col-md-5'>
                        <table class='table table-striped'>
                            <tr>
                                <td>Resumption Date</td>
                                <td>
                                    <input name='next_term_date' type='date' class='form-control' required>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
                <div class='row'>
                    <div class='col-md-2'>
                        <button name='result_date_btn' class='btn btn-primary btn-lg' style='border-radius:10px;' type='submit'>SAVE</button>
                        <br><br>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
