<?php
/**
 * verify.php -- Verification Page
 */

$pageTitle = 'Verify Account';

//date_default_timezone_set('Africa/Lagos');

include 'inc/config.inc.php'; // Configuration

error_reporting(E_ALL);
ini_set('display_errors', 1);

//session_start();
/*if ( isset($_SESSION['student']) ){
	//header('Location: dashboard');
	header('Location: scholarships');
}*/

if ( (!empty($_GET['ver_code']) AND !empty($_GET['user']) ) AND (isset($_GET['ver_code'], $_GET['user'])) ){
    $ver_code = $_GET['ver_code'];
    $user_id = $_GET['user'];
    /*$stmt = $db_con->prepare("SELECT id, ver_code, verified FROM users WHERE id=:user_id AND ver_code=:ver_code");
    $stmt->execute(array(":user_id"=>$user_id, ":ver_code"=>$ver_code));
    $userRow = $stmt->fetch(PDO::FETCH_ASSOC);*/
?>

<!DOCTYPE html>
<html lang="en-uk" dir="ltr">
<head>
    <!-- Standard Meta -->
    <meta charset="utf-8">
    <meta name="description" content="">
    <meta name="keywords" content="investment, Forex, Trading, Bitcoin, Cryptocurrency, Global investment, Live Trade, Trading class, indics">
    <meta name="author" content="Paul HubWeb & Tech">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="theme-color" content="#313131" />
    <!-- Site Properties -->
    <title>Verify Account - criptohopper </title>
    <link rel="shortcut icon" href="public/images/logo.png" type="image/x-icon">
    <link rel="apple-touch-icon-precomposed" href="public/images/logo.png">
    <!-- Stylesheet -->
    <link rel="stylesheet" href="css/vendors/uikit.min.css">
    <link rel="stylesheet" href="css/style.css">

    <script src="plugins/sweetalerts/promise-polyfill.js"></script>
    <link href="plugins/sweetalerts/sweetalert.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <!-- preloader begin -->
    <div class="in-loader">
        <div style="background-image: linear-gradient(to right, rgb(0, 104, 0) , rgb(170, 212, 170));"></div>
        <div style="background-image: linear-gradient(to right, rgb(0, 104, 0) , rgb(170, 212, 170));"></div>
        <div style="background-image: linear-gradient(to right, rgb(0, 104, 0) , rgb(170, 212, 170));"></div>
    </div>
    <!-- preloader end -->
    <main>
        <!-- section content begin -->
        <div class="uk-section uk-padding-remove-vertical">
            <div class="uk-container uk-container-expand">
                <div class="uk-grid" data-uk-height-viewport="expand: true">
                    <div class="uk-width-3-5@m uk-background-cover uk-background-center-right uk-visible@m uk-box-shadow-xlarge" style="background-image: url(img/h.jpg);">
                    </div>
                    <div class="uk-width-expand@m uk-flex uk-flex-middle">
                        <div class="uk-grid uk-flex-center">
                            <div class="uk-width-3-5@m">
                                <div class="in-padding-horizontal@s" id="users">
                                    <!-- module logo begin -->
                                    <a class="uk-logo" href="<?= $BASE_URL; ?>">
                                        <img class="uk-margin-small-right in-offset-top-10" src="img/in-lazy.gif" data-src="public/images/logo.png" alt="wave" style="padding-top: 40px;" width="350" height="29" data-uk-img>
                                    </a>
                                    <!-- module logo begin -->
                                    <p class="uk-text-lead uk-margin-top uk-margin-remove-bottom">Verify your account</p>
                                    <p class="uk-text-small uk-margin-remove-top uk-margin-medium-bottom">Already have an account? <a href="signin" style="color:rgb(0, 104, 0)"><b>Login here</b></a></p>
                                    
                                    <!-- login form begin -->
                                    <form class="uk-grid uk-form" id="verifyForm" v-on:submit.prevent>
                                        <div class="uk-margin-small uk-width-1-1 uk-inline">
                                            <input class="uk-input uk-border-rounded" id="user_id" value="<?= $user_id; ?>" type="hidden" required>
                                        </div>
                                        <div class="uk-margin-small uk-width-1-1 uk-inline">
                                            <input class="uk-input uk-border-rounded" id="ver_code" type="hidden" value="<?= $ver_code; ?>" required>
                                        </div>

                                        <div class="uk-margin-small uk-width-1-1">
                                            <button class="uk-button uk-width-1-1 uk-button-primary uk-border-rounded uk-float-left" type="button" @click="verifyAccount()" style="background-image: linear-gradient(to right, rgb(0, 104, 0) , rgb(170, 212, 170));" id="btnVerify">Continue</button>
                                        </div>

                                    </form>
                                    <!-- login form end -->
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- section content end -->
    </main>
    <!-- Javascript -->
    <script src="plugins/jquery-3.1.1.min.js"></script>
    <script src="plugins/sweetalerts/sweetalert2.min.js"></script>
    <script src="https://unpkg.com/@popperjs/core@2/dist/umd/popper.js"></script>

    <script src="js/vendors/uikit.min.js"></script>
    <script src="js/vendors/indonez.min.js"></script>

    <script src="vuejs/vue.js"></script>
    <script src="vuejs/axios.js"></script>
    <script>
        // BASE URL and API Inclusion
        const BASE_URL = '<?= $BASE_URL; ?>';
        const API = `${BASE_URL}api/users.php`;
    </script>
    <!--<script src="vuejs/users.js"></script>-->
    <script>
        var app = new Vue({
            el: '#users',
            data: {
                verifyUser: { user_id:$('#user_id').val(), ver_code:$('#ver_code').val() }
            },

            methods: {

                // 2.3. Verify Account = Ver Code
                verifyAccount: function(){
                    var verFormData = app.toFormData(app.verifyUser);

                    if (!this.verifyUser.ver_code){ // check if ver_code is empty
                        this.swalEmptyNotice('ver_code');
                    }
                    else {
                        $('#btnVerify').prop('disabled', true); // disable verify button
                        axios.post(API+'?crud=verifyuser', verFormData)
                        .then( function( val ){

                            console.log( val );

                            $("#btnVerify").html('validating...'); // validating

                            if ( val.data.error==false && val.data.notice=='successful' ){
                                // 1. Verified
                                swal({
                                    html: `${val.data.message}`,
                                    type: 'success',
                                    timer: 3500,
                                    showConfirmButton: false,
                                    allowOutsideClick: false,
                                    allowEscapeKey: false,
                                }).then(function(){
                                    window.location.href = `${BASE_URL}`;
                                });
                            }
                            else if ( val.data.error==true && val.data.notice=='wrong' ){
                                // 2. Wrong VerCode AND 3. Already Verified
                                swal({
                                    html: `${val.data.message}`,
                                    type: 'error',
                                    timer: 3500,
                                    showConfirmButton: false,
                                    allowOutsideClick: false,
                                    allowEscapeKey: false,
                                }).then(function(){
                                    window.location.href = `${BASE_URL}`;
                                });
                            }
                            else if ( val.data.error==true && val.data.notice=='noaccount' ){
                                // 3. Error - Account Not Found
                                swal({
                                    html: `${val.data.message}`,
                                    type: 'error',
                                    timer: 3500,
                                    showConfirmButton: false,
                                    allowOutsideClick: false,
                                    allowEscapeKey: false,
                                }).then(function(){
                                    window.location.href = `${BASE_URL}`;
                                });
                            }
                        });
                    }
                },


                //---- Notices: BEFORE LOGIN
                // For Empty Input Fields - Sign In; Sign Up & Others
                swalEmptyNotice: function(field){
                    fieldAlert = field.split('_').join(' ').toUpperCase(); // convert to uppercase
                    swal({
                        html: `Sorry! <b>${fieldAlert}</b> cannot be left empty, please try again.`,
                        type: "error",
                        confirmButtonColor: "#c40000",
                        confirmButtonText: "Try again!",
                        allowOutsideClick: false,
                        allowEscapeKey: false,
                        padding: '2em'
                    }).then(function(){
                        document.getElementById(`${field}`).focus(); // send focus to input field
                    });
                },

                // Invalid Email & File Type
                swalIEFTNotice: function(field){
                    fieldAlert = field.split('_').join(' ').toUpperCase(); // convert to uppercase
                    swal({
                        //title: 'Are you sure you want to delete your account?',
                        html: `Sorry! <b>${fieldAlert}</b> is not valid, please try again.`,
                        type: "error",
                        confirmButtonColor: "#c40000",
                        confirmButtonText: 'Oops, sorry!',
                        allowOutsideClick: false,
                        allowEscapeKey: false,
                        padding: '2em'
                    });
                },

                toFormData: function(obj){
                    var form_data = new FormData();
                    for(var key in obj){
                        form_data.append(key, obj[key]);
                    }
                    return form_data;
                },
            }
        });
    </script>

</body>
</html>

<?php
} else {
    header('Location: ' . $BASE_URL);
}
?>