<?php
session_start();

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

if (isset($_POST['register'])) {
    $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
    if ($email == null || $email == FALSE) {
        $error = 'Email is not valid';
    } else {
        $db = new DatabaseConn();
        $link = $db->connect();
        $email = strtolower($email);
        $stmt = mysqli_prepare($link, "select email from users WHERE  email = ?");
        $stmt->bind_param("s", $email);
        $stmt->execute();
        $stmt->store_result();
        if ($stmt->num_rows > 0) {
            $error = 'Email already exist!';
        }
    }
    $password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
    if ($password == null || $password == FALSE) {
        $error = 'Password is not valid';
    } else if (strlen($password) < 8) {
        $error = 'Password is weak';
    }
    
    $cpassword = filter_input(INPUT_POST, 'cpassword', FILTER_SANITIZE_STRING);
    if ($cpassword == null || $cpassword == FALSE) {
        $error = 'Confirmation Password is not valid';
    } else if (strcmp($password, $cpassword) != 0) {
        $error = 'Password does not match';
    }



    if (!isset($error)) {
        $db = new DatabaseConn();
        $link = $db->connect();
        $settings = Settings::getSystemSettings($link);
        date_default_timezone_set('Africa/Lagos');
        $time = time();
        $user = new User();
        $user->setCountry("n/a");
        $user->setDateJoined($time);
        $user->setEmail($email);
        $user->setName("n/a");
        $user->setPassword($password);
        $user->setPhone("n/a");
        $user->setStatus(($settings->getEmailVerification() == 1) ? NOT_CONFIRMED : NEW_USER);
        User::saveUser($user, $link);
        
        $response = (strcmp($user->getStatus(), NOT_CONFIRMED) == 0) ? User::sendEmailVerification($user, $link) : false;
        if ($response) {
            $_SESSION[EMAIL] = $user->getEmail();
            header("location: confirm");
            die();
        } else if (is_null($response)) {
            User::deleteUser($user->getEmail(), $link);
            $error = "We couldn't verify your email";
        } else {
            LoginRecord::saveUserRecords($user, $link);
            $_SESSION[USER] = $user->getEmail();
            header("location: dashboard/");
            die();
        }
    }
}


?>

<!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">Sign Up</h3>
                                            <p class="text-gray-700 pb-4">Already have an account? <a href="login">Sign in </a></p>
                                        </div>
                                    </div>
                                    <form action="create-wallet" 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"  value="<?php
                                               if (isset($_POST['email'])) {
                                                   echo ($_POST['email']);
                                               }
                                               ?>" name="email" type="email" class="form-control mt-4" placeholder="Email*" >
                                        </div>
                                        <div class="input-group mb-6">
                                            <input id="amount" type="password" name="password" class="form-control mt-4" placeholder="Password*" >
                                        </div>
                                        <div class="input-group mb-6">
                                            <input id="amount"  type="password" name="cpassword" class="form-control mt-4" placeholder=" Confirm Password*" >
                                        </div>
                                        <input type="hidden" name="register" style="display: none" />
                                        <button type="submit" id="downloadapp"  class="btn btn-block btn-primary">
                                            Create Wallet
                                        </button>
                                     
                                    </form>
                                    <p style="margin-top: 16px">By signing up, I agree to the terms of use and processing of my personal data as stated in the <a href="policy">privacy policy</a></p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>
        </main>
        <?php require_once './partials/footer.php'; ?>
    </body>
</html>
