<?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";
        }
    }
}

$title = 'Confirm Account ' . APP_NAME;
?>


<!DOCTYPE html>
<html lang="en">
    <head>
        <?php require_once './partials/head.php'; ?>
    </head>
    <body>
        <style>
            @media only screen and (max-width: 768px) {
                .ll {
                    color: white !important;
                    background-color: #1452F5 !important;
                }
            }
        </style>
        <nav class="navbar navbar-expand-lg navbar-togglable navbar-dark bg-primary d-block">
              <div class="container">
                <a class="navbar-brand" href="index">
                    <img srcset="" class="sc-EHOje lkgfgG sc-cpmKsF iJpaCn" color="auto" sizes="" src="/logo.png" style="display: block;/*! height: 20px; */margin-left: 0px;width: auto;" width="auto" height="auto">
                    </a>


            </div>
        </nav>
        <main class="page-content" aria-label="Content">
            <section class="pt-4 pb-8 bg-gradient-primary">
                <div class="container">
                    <div class="row align-items-center justify-content-center justify-content-md-between">
                        <div class="col-12 col-lg-5 mb-4 text-center text-lg-left text-white">
                            <h1 class="display-3 font-weight-bold">The secure wallet to store crypto</h1>
                            <p class="text-white-75 lead mb-4">The easiest and most secure crypto wallet.</p>
                        </div>
                        <div class="col-12 col-lg-6 offset-md-1 text-center">
                            <div class="card shadow-lg">
                                <div class="card-body">
                                    <div class="row">
                                        <div class="col-12">
                                            <h3 class="font-weight-bold mb-1">Email Verification</h3>
                                            <p class="text-gray-700 pb-4">We sent a verification email to <?php echo $_SESSION[EMAIL]; ?>. Enter the code inside to get started!</p>
                                        </div>
                                    </div>
                                    <form action="confirm" method="POST">
                                       <?php if (isset($error)) { ?>
                                        <p style="margin-bottom: 16px; color: red !important;" class="error" >Error: <?php echo "$error"; ?></p>
                                    <?php } ?>
                                        <div class="input-group mb-6">
                                            <input id="amount" type="text" value="<?php
                                               if (isset($_POST['token'])) {
                                                   echo ($_POST['token']);
                                               }
                                               ?>" name="token"  class="form-control mt-4" placeholder="Enter code*" >
                                        </div>
                                        <button type="submit" id="downloadapp"  class="btn btn-block btn-primary">
                                            Verify Code
                                        </button>
                                     
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>
        </main>
          <?php require_once './partials/footer.php'; ?>
    </body>
</html>
