<?php
session_start();

require_once './include/Constants.php';
require_once "./vendor/autoload.php";
require_once "./include/DatabaseConn.php";
require_once './models/Settings.php';
require_once "./models/User.php";


if (!isset($_SESSION[EMAIL])) {
    header("location: register");
    die();
}

if (isset($_POST['token'])) {
    $token = filter_input(INPUT_POST, 'token', FILTER_SANITIZE_STRING);
    if ($token == null || $token == FALSE) {
        $error = "Invalid code";
    } else {
        $token = trim($token);
        $db = new DatabaseConn();
        $link = $db->connect();
        $stmt = mysqli_prepare($link, "select `user` from token where token = ?");
        $stmt->bind_param("s", $token);
        $stmt->execute();
        $stmt->store_result();
        if ($stmt->num_rows == 1) {
            $stmt->bind_result($user);
            $stmt->fetch();
            $stmt = mysqli_prepare($link, "update users set status = ? where email = ?");
            $newUser = NEW_USER;
            $stmt->bind_param("ss", $newUser, $user);
            $stmt->execute();
            $stmt->store_result();
            User::notifyAdmin(EMAIL_SUPPORT, $user);
            $_SESSION[USER] = $user;
            header("location: dashboard/");
            die();
        } else {
            $error = "Invalid code";
        }
    }
}
?>
<!DOCTYPE html>
<html lang="zxx">

<head>
    <?php require_once './partials/head.php'; ?>
    <style>
        #footer_section {
            padding-top: 135px;
        }
    </style>
</head>

<body>
<div class="sub-banner-section-outer position-relative">
    <header>
        <?php require_once './partials/header.php'; ?>
    </header>
      <section class="contact_info_section position-relative">
        <div class="container">
            <div class="row">
                <div class="col-lg-6 col-md-6 col-sm-12 col-12">
                    <div class="banner-section-content">
                        <h1 class="text-white" data-aos="fade-up">Confirm Code</h1>
                        <p class="text-white" data-aos="fade-right">We sent a verification email to <?php echo $_SESSION[EMAIL]; ?>. Enter the code inside to get started!</p>
                  </div>
                </div>
                <div class="col-lg-6 col-md-6 col-sm-12 col-12">
                    <div class="banner_image position-relative">
                        <div class="contact_info_form_content">
                    <form id="contactpage" style="height: 450px;" action="confirm" method="POST" >
                        <?php if (isset($error)) { ?>
                             <p class="error" style="color: red">Error: <?php echo "$error"; ?></p>
                        <?php } ?>
                        <div class="row">
                            <div class="col-lg-12 col-md-12 col-sm-12">
                                <div class="form-group mb-0">
                                 <input type="text" value="<?php
                                        if (isset($_POST['token'])) {
                                            echo ($_POST['token']);
                                        }
                                        ?>" name="token" placeholder="Enter code" class="form-control" required="required">
                                </div>
                            </div>
                           </div>
                        <div class="btn_wrapper">
                            <button type="submit" name="get_started" id="started" class="default-btn">Verify code <i class="fa-solid fa-angle-right"></i></button>
                        </div>
                    </form>
                </div>
                        <figure class="banner_image_bottom_shape mb-0 position-absolute top_bottom_shape">
                            <img src="images/sub_banner_image_bottom_shape.png" alt class="img-fluid">
                        </figure>
                    </div>
                </div>
            </div>
        </div>
    </section>
     
</div>

 <?php require_once './partials/footer.php'; ?>
</body>
</html>