<?php

session_start();
require_once "../Config/session_check.php";
// Check if the user is logged in, if not then redirect him to login page


$_SESSION["last_Active"] = time();

$email = '';
$email_err = $emailV_err = '';
require_once "../Config/config.php";
if ($_SERVER["REQUEST_METHOD"] == "POST") {


    if (empty(trim($_POST["email"]))) {
        $email_err = 'please enter an email';
    }
    if (empty(trim($_POST["emailV"]))) {
        $emailV_err = 'please enter an email';
    } else if (trim($_POST["emailV"] == trim($_POST["email"]))) {

        $email = trim($_POST["email"]);
        $sql = "UPDATE staff set email_address=? where staff_ID=?";
        if ($stmt = mysqli_prepare($link, $sql)) {


            mysqli_stmt_bind_param($stmt, "ss", $email, $_SESSION["username"]);
            // Attempt to execute the prepared statement
            if (mysqli_stmt_execute($stmt)) {
                mysqli_close($link); // Close connection
                header("location: ../AccountSettings/");
                exit;
            }
            else
            {
                echo "Something got in the way";
            }
        }
    } else {
        $emailV_err = "emails do not match";
    }


}


?>
<!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">
                    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
                        <div class="form-group">
                            <label for="pwd">New Email:</label>
                            <input type="email"
                                   class="form-control <?php echo (!empty($email_err)) ? 'is-invalid' : ''; ?>"
                                   name="email" id="email" placeholder="Email">
                            <span><?php echo "<p class='error'>" . $emailV_err . "</p>"; ?></span>
                        </div>
                        <div class="form-group">
                            <label for="pwdr">Re-enter Email:</label>
                            <input type="email"
                                   class="form-control <?php echo (!empty($emailV_err)) ? 'is-invalid' : ''; ?>"
                                   name="emailV" id="emailV" placeholder="Re-enter Email">
                            <span id="pwd2p"></span>
                            <span><?php echo "<p class='error'>" . $emailV_err . "</p>"; ?></span>
                        </div>

                        <div class="form-group">
                            <input type="submit" class="btn btn-danger btn-block" value="Update">
                        </div>
                    </form>


                </main>
            </div>
        </div>


    </div>
    <?php include "../Templates/Footer.php"; ?>

</div>

</body>
</html>
