<?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";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $message = '';
    $nwemail = trim($_POST["email"]);
    $nwemail2 = trim($_POST["email2"]);

    if (empty($nwemail)) {
        $message = 'No email entered.';
    } else if (empty($nwemail2)) {
        $message = 'emails do not match';
    } else if ($nwemail != $nwemail2) {
        $message = 'emails do not match';
    } else if (!filter_var($nwemail, FILTER_VALIDATE_EMAIL)) {
        $message = 'enter valid email';

    }

    if (empty($message)) {
        echo 'dsds';

        $SQL = "UPDATE customers set emailAddress=? where Customer_ID=?";

        if ($stmt = mysqli_prepare($link, $SQL)) {


            mysqli_stmt_bind_param($stmt, "ss", $nwemail, $ID);
            // Attempt to execute the prepared statement
            if (mysqli_stmt_execute($stmt)) {
                // Close connection
                mysqli_close($link);
                header("location: ../Customer/DetailView.php?ID=" . $ID . "");
                exit;
            } else {
                echo "Something got in the way.";
            }
        }
    } else {
        echo $message;
    }

}
