<?php

session_start();
require_once "../Config/session_check.php";

$_SESSION["last_Active"] = time();

$ID = "";
if (isset($_GET["ID"])) {
    $ID = $_GET["ID"];

} else {
    header("location: ../");
}


require_once "../Config/config.php";

$Table = "";


$SQL = "SELECT customers.*, addresses.*
FROM customers 
	LEFT JOIN addresses ON addresses.Customer_ID = customers.Customer_ID
WHERE customers.Customer_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, $CustomerID, $forname, $surname, $email, $phoneNo, $DOB, $addressID, $CustomerID, $HourseNO, $street, $city, $postcode);
        if (mysqli_stmt_fetch($stmt)) {
            $Delpage = "'DeleteRecord.php?ID=" . $ID . "'";
            $accessGrant = array('', '', '', '');
            if ($_SESSION['security_Level'] <= 2) {
                $accessGrant = array('<button ID="delBtn" class="btn btn-danger btn-block" style=" padding-bottom: 14px"  onclick="window.location=' . $Delpage . '" disabled>Delete Customer</button>', '<a class="btn btn-dark btn-block" style=" padding-bottom: 14px"  href="ChangeEmailForm.php?ID=' . $ID . '" >Change email</a>', '<td colspan="3"style="padding: 0;margin: 0; "><a class="btn btn-dark btn-block" style=" padding-bottom: 14px"  href="ChangeAddressForm.php?ID=' . $ID . '">Change information</a></td>', '<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 = '
                        <tr>
                            <td colspan="3" style="padding: 0;margin: 0;">' . $accessGrant[0] . '</td>
                        </tr>
                        <tr>
                            <th> ID </th>
                            <td  colspan="2">' . $CustomerID . '</td>
                        </tr>
                        <tr>
                        
                            <th> Full Name </th>
                            <td  colspan="2">' . $forname . ' ' . $surname . '</td>
                            
                            
                        </tr>
                        <tr>
                            <th> Email Address </th>
                            <td >' . $email . '</td>
                            <td style="padding: 0;margin: 0;">' . $accessGrant[1] . '</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>
                         ' . $accessGrant[2] . '
                        </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>
                       
                ';

        }
    } else {
        echo "something went wrong, try again later.";
    }
}


?>
<!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>
                    <?php echo $accessGrant[3]; ?>
                    <table class="table table-bordered">
                        <?php echo $Table; ?>
                    </table>

                </main>
            </div>
        </div>


    </div>
    <?php include "../Templates/Footer.php"; ?>

</div>

</body>
</html>
