<?php

session_start();

// Check if the user is logged in, if not then redirect him to login page
if (isset($_SESSION["security_Level"]) && $_SESSION["security_Level"] < 2) {
    require_once "../Config/session_check.php";
} else {
    header("location: ../index.php");
}

$_SESSION["last_Active"] = time();


$Table = '<table class="table table-hover" style="width:100%;" >
                        <a class="btn btn-dark btn-block" style=" padding-bottom: 14px"  href="AddStaff.php" >Add new employee</a>
                        <thead>
                        <tr>
                        <th>Staff ID</th>
                        <th>Full Name</th>
                        <th>Phone number</th>
                        <th>D.O.B</th>
                        <th>House number</th>
                        <th>Street</th>
                        <th>City</th>
                        <th>Postcode</th>
                        <th>Job Role</th>
                        </tr>
                        </thead>
                        <tbody>
                        ';


require_once "../Config/config.php";

$SQL = "SELECT staff.*, users.Job_Role, users.Security_Level
FROM staff 
	LEFT JOIN users ON users.Staff_ID = staff.Staff_ID;";
$stmt = mysqli_query($link, $SQL);


while ($row = mysqli_fetch_array($stmt)) {

    $useraccount = '';
    $linkerS = "window.location='DetailView.php?ID=" . $row[0] . "';";
    if (empty($row[10])) {
        $useraccount = 'N/A';
    } else {
        $useraccount = $row[10];
    }

    $Table = $Table . '<tr style="cursor: pointer" onclick="' . $linkerS . '" >
                        <td>' . $row[0] . '</td>
                        <td>' . $row[1] . ' ' . $row[2] . '</td>
                        <td>' . $row[4] . '</td>
                        <td>' . $row[5] . '</td>
                        <td>' . $row[6] . '</td>
                        <td>' . $row[7] . '</td>
                        <td>' . $row[8] . '</td>
                        <td>' . $row[9] . '</td>
                        <td>' . $useraccount . '</td>
                        </tr></a>';
}
$Table = $Table . "</tbody></table>";


?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include "../Templates/Header.php"; ?>
    <title>Dashboard</title>
</head>
<body>

<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>Staff:</h1>
                    <?php echo $Table; ?>


                </main>
            </div>
        </div>


    </div>
    <?php include "../Templates/Footer.php"; ?>

</div>

</body>
</html>
