<?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();

$Phone_No = '';
$PhoneNo_err = '';
require_once "../Config/config.php";
if ($_SERVER["REQUEST_METHOD"] == "POST") {


    if (empty(trim($_POST["Phone"]))) {
        $PhoneNo_err = 'please enter a phone number';
    } else {
        $Phone_No = trim($_POST["Phone"]);

        if (preg_match("#[a-z]+#", $Phone_No)) {
            $PhoneNo_err = "Phone numbers only contain numbers";
        }
        else if (preg_match("#[A-Z]+#", $Phone_No)) {
            $PhoneNo_err = "Phone numbers only contain numbers";
        }
        else if (strlen($_POST["Phone"]) == 11) {

            $sql = "UPDATE staff set Phone_Number=? where staff_ID=?";
            if ($stmt = mysqli_prepare($link, $sql)) {


                mysqli_stmt_bind_param($stmt, "ss", $Phone_No, $_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{
            $PhoneNo_err = 'That is not a uk phone number.';
        }

    }
}


?>
<!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="Phone">New Phone number:</label>
                            <input type="text"
                                   class="form-control <?php echo (!empty($PhoneNo_err)) ? 'is-invalid' : ''; ?>"
                                   name="Phone" id="Phone" placeholder="Phone number">
                            <span><?php echo "<p class='error'>" . $PhoneNo_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>
