<?php

session_start();

// Check if the user is logged in, if not then redirect him to login page
require_once "../Config/session_check.php";

$_SESSION["last_Active"] = time();

$Table = '<table class="table table-hover" style="width:100%;" >
                        <thead>
                        <tr>
                        <th>Order ID</th>
                        <th>Customer Name</th>
                        <th>Employee name</th>
                        <th>Cost</th>
                        <th>Discount</th>
                        <th>VAT</th>
                        <th>Delivery Status</th>
                        </tr>
                        </thead>
                        <tbody>';


require_once "../Config/config.php";

$SQL = "SELECT orders.Order_ID, customers.Forename, customers.Surname, staff.Forename, staff.Surname, orders.Cost, orders.Discount, orders.VAT, orders.Delivery_Status
FROM orders 
	LEFT JOIN customers ON orders.Customer_ID = customers.Customer_ID 
	LEFT JOIN staff ON orders.User_ID = staff.Staff_ID;";
$stmt = mysqli_query($link, $SQL);

while ($row = mysqli_fetch_array($stmt)) {


    $DelStat = "";
    if ($row[8] == 1) {
        $DelStat = "Delivered";
    } else if ($row[8] == 0) {
        $DelStat = "On Its Way";
    }
    $linkerS = "window.location='DetailView.php?ID=" . $row[0] . "';";


    $Table = $Table . '<tr style="cursor: pointer" onclick="' . $linkerS . '" >
                        <td>' . $row[0] . '</td>
                        <td>' . $row[1] . ' ' . $row[2] . '</td>
                        <td>' . $row[3] . ' ' . $row[4] . '</td>
                        <td>' . $row[5] . '</td>
                        <td>' . $row[6] . '</td>
                        <td>' . $row[7] . '</td>
                        <td>' . $DelStat . '</td>
                        </tr>';
}
$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>Order Details:</h1>
                    <?php echo $Table; ?>

                </main>
            </div>
        </div>


    </div>
    <?php include "../Templates/Footer.php"; ?>

</div>

</body>
</html>
