<?php

session_start();

if (isset($_SESSION["security_Level"]) && $_SESSION["security_Level"] < 2) {
    require_once "../Config/session_check.php";
} else {
    header("location: ../index.php");
}
$_SESSION["last_Active"] = time();

$ID = "";
if (isset($_GET["ID"])) {
    $ID = $_GET["ID"];

} else {
    header("location: ../index.php");
}


require_once "../Config/config.php";

$Table = "";


$SQL = "SELECT staff.*, users.Terminate_On, users.Job_Role, users.Security_Level,users.Locked
FROM staff 
	LEFT JOIN users ON users.Staff_ID = staff.Staff_ID
WHERE staff.Staff_ID = ?;";

if ($stmt = mysqli_prepare($link, $SQL)) {

    mysqli_stmt_bind_param($stmt, "s", $ID);

    if (mysqli_stmt_execute($stmt)) {
        // Store result

        mysqli_stmt_bind_result($stmt, $staffID, $forname, $surname, $email, $phoneNo, $DOB, $HourseNO, $street, $city, $postcode, $termD, $job, $sercurity, $status);
        $Delpage = "'DeleteRecord.php?ID=" . $ID . "'";
        $hasNoUserAcc = '';
        $userAccStatus = 'Lock user account';
        if (mysqli_stmt_fetch($stmt)) {
            if (empty($termD)) {
                $termD = 'User account not made.';
                $sercurity = 'N/A';
                $job = 'N/A';
                $hasNoUserAcc = 'hidden';
            }
            if ($status == '1') {
                $userAccStatus = 'Unlock user account';
            }
            $Table = '
                        <tr>
                            <td colspan="3" style="padding: 0;margin: 0;"><button ID="delBtn" class="btn btn-danger btn-block" style=" padding-bottom: 14px"  onclick="window.location=' . $Delpage . '" disabled>Delete Staff member</button></td>
                        </tr>
                        <tr>
                            <th> ID </th>
                            <td colspan="2">' . $staffID . '</td>
                        </tr>
                        <tr>
                        
                            <th> Full Name </th>
                            <td>' . $forname . ' ' . $surname . '</td>
                             <td style="padding: 0;margin: 0;"><a class="btn btn-dark btn-block" style=" padding-bottom: 14px"  href="ChangeName.php?ID=' . $ID . '" >Change name</a></td>
                            
                        </tr>
                        <tr>
                            <th> Email Address </th>
                            <td colspan="2">' . $email . '</td>
                        </tr>
                        <tr>
                            <th> Phone number </th>
                            <td colspan="2">' . $phoneNo . '</td>
                        </tr>
                        <tr>
                            <th> Date of birth</th>
                            <td colspan="2">' . $DOB . '</td>                       
                        </tr>
                        <tr>
                            <th> House Number </th>
                            <td colspan="2">' . $HourseNO . '</td>
                        </th>
                        </tr>
                        <tr>
                            <th> Street Name </th>
                            <td colspan="2">' . $street . '</td>
                        </tr>
                        <tr>
                            <th> City </th>
                            <td colspan="2">' . $city . '</td>
                        </tr>
                        <tr>
                            <th> postcode </th>
                            <td colspan="2">' . $postcode . '</td>
                        </tr>
                         <tr>
                         <td colspan="3"style="padding: 0;margin: 0; "><a class="btn btn-dark btn-block" style=" padding-bottom: 14px"  href="ChangeOther.php?ID=' . $ID . '">Change information</a></td>
                        </tr>
                        <tr>
                            <th> Termination Date </th>
                            <td colspan="2">' . $termD . '</td>
                        </tr>
                        <tr>
                                <th> Job Role </th>
                            <td colspan="2">' . $job . '</td>
                        </tr>                        
                        <tr>
                                <th> Security Level </th>
                            <td colspan="2">' . $sercurity . '</td>
                        </tr>        
                        <tr ' . $hasNoUserAcc . ' >    
                            <th colspan="1" style="padding: 0;margin: 0;"><a ID="deluserBtn" disabled="true" class="btn btn-danger btn-block" style=" padding-bottom: 14px" href="DeleteUserAcc.php?ID=' . $ID . ' " >Delete user account</a></th>
                            <td colspan="2" style="padding: 0;margin: 0;"><a ID="lockAccount" class="btn btn-primary btn-block" style=" padding-bottom: 14px"  href="lockAccount.php?ID=' . $ID . '&STATUS=' . $status . '" >' . $userAccStatus . '</a></td>
                        </tr>    
                ';


        }
    }


}


?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include "../Templates/Header.php"; ?>
    <title>Dashboard</title>
</head>
<body>
<script>
    function chkclicked() {
        var state = document.getElementById("chkbox");
        if (state.checked) {
            document.getElementById("delBtn").disabled = false;
        } else {
            document.getElementById("delBtn").disabled = true;
        }
    }
</script>

<div id="page-container">

    <div id="content-wrap">
        <?php include "../Templates/navBar.php"; ?>
        <div class="container-fluid">
            <div class="row">
                <?php include "../Templates/SideBar.php"; ?>

                <main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
                    <h1>Information:</h1>
                    <div class="form-check">
                        <input type="checkbox" class="form-check-input" id="chkbox" name="chkbox"
                               onchange="chkclicked();">
                        <label class="form-check-label" for="chkbox">Check to activate Delete button. Note: once delete
                            button is pressed this user will be removed, forever.</label>
                    </div>
                    <table class="table table-bordered">
                        <?php echo $Table; ?>
                    </table>

                </main>
            </div>
        </div>


    </div>
    <?php include "../Templates/Footer.php"; ?>

</div>

</body>
</html>
