<?php

if (!isset($_GET['token'])) {
    http_response_code(404);
    exit;
}

require_once "{$_SERVER['DOCUMENT_ROOT']}/login/php/authFunctions.php";

$token = htmlspecialchars($_GET['token'], ENT_QUOTES, 'UTF-8');
$stmt = $GLOBALS['conn']->prepare("SELECT * FROM system_users WHERE user_password_reset_token = ? AND user_password_reset_expires > UNIX_TIMESTAMP()");
$stmt->bind_param("s", $token);
$stmt->execute();
$result = $stmt->get_result();
$tokenfound = false;
$user_data = array();

if ($result->num_rows == 1) {
    $tokenfound = true;
    $user_data = $result->fetch_assoc();
} else {
    addLoginAttempts();
}

if (checkLoginAttempts() > 20) {
    header('Location: /login/');
    exit;
}

?>
<!DOCTYPE html>
<html data-coreui-theme="dark" lang="en">
<head>
    <base href="./">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <meta name="description" content="Sentri - Configure phone configs">
    <meta name="author" content="Marco Mooij">
    <meta name="keyword" content="sentri">
    <title>Sentri | Login</title>

    <!-- favicon -->
    <link rel="icon" type="image/png" href="/src/images/favicon/favicon-96x96.png" sizes="96x96">
    <link rel="icon" type="image/svg+xml" href="/src/images/favicon/favicon-96x96.png">
    <link rel="shortcut icon" href="/src/images/favicon/favicon.ico">
    <link rel="apple-touch-icon" sizes="180x180" href="/src/images/favicon/apple-touch-icon.png">
    <link rel="manifest" href="/src/images/favicon/site.webmanifest">

    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #1d222b;
        }

        .portal-image {
            width: 100%;
            height: auto;
            aspect-ratio: 3 / 1;
            object-fit: contain;
            display: block;
            min-height: 200px; /* Prevent layout shift during image load */
        }
    </style>

    <link rel="stylesheet" href="css/style.full.css" as="style">
</head>
<body>
<div class="bg-body-tertiary min-vh-100 d-flex flex-row align-items-center">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-lg-8 col-md-10 col-12 d-flex align-items-center justify-content-center">
                <div class="card-group d-block d-md-flex row">
                    <div class="card col-md-7 mb-0">
                        <div class="card-body p-3">
                            <div class="card-body text-center justify-content-center align-items-center">
                                <h1>Verify email</h1>
                                <hr>
                                <?php if ($user_data['user_verified_email'] == 1) { ?>
                                    <p>Your email is already verified, set a password to continue</p>
                                    <br>
                                    <a href="/login/resetPassword.php?token=<?php echo urlencode($_GET['token']); ?>" class="btn btn-primary px-4">
                                        <i class="fa-solid fa-check"></i> Set password
                                    </a>
                                <?php } elseif (!$tokenfound) { ?>
                                    <p>This link has expired or does not exist. Please contact your administrator to request a new one.</p>
                                    <br>
                                <?php } else { ?>
                                    <p>Click the following button to verify your account and set a password.</p>
                                    <br>
                                    <a href="/login/resetPassword.php?token=<?php echo urlencode($_GET['token']); ?>" class="btn btn-primary px-4">
                                        <i class="fa-solid fa-check"></i> Yup, its me!
                                    </a>
                                <?php } ?>
                            </div>
                        </div>
                    </div>
                    <div class="card col-md-5 text-white bg-transparent py-5">
                        <div class="card-body text-center pt-5">
                            <div>
                                <img class="pt-3 w-100 portal-image" alt="portal-image" src="" width="500" height="265">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
</body>
</html>