<?php
session_start();
require_once '../include/Constants.php';
require_once "../vendor/autoload.php";
require_once "../include/DatabaseConn.php";
require_once "../models/User.php";
require_once '../models/USettings.php';
require_once '../models/Settings.php';
require_once '../include/GoogleAuthenticator.php';

$db = new DatabaseConn();
$link = $db->connect();

$user = User::getCurrentUser($link);
if (is_null($user)) {
    $url = $_SERVER['REQUEST_URI'];
    header("location: ../login?" . RETURN_URL . "=" . $url);
    die();
}

if (!is_null($user->getFa())) {
    header("location: index.php");
    die();
}

$ga = new PHPGangsta_GoogleAuthenticator();

if (isset($_POST['code'])) {

    $code = filter_input(INPUT_POST, "code");
    $secret = $user->getSecret();
    $checkResult = $ga->verifyCode($secret, $code, 2);
    if ($checkResult) {
        $db = new DatabaseConn();
        $link = $db->connect();
        $email = $user->getEmail();
        mysqli_query($link, "update users set `fa` = 'OK' WHERE email = '$email'");
        die("OK");
    } else {
        die('FAILED');
    }
}

if (is_null($user->getSecret())) {
    $secret = $ga->createSecret();
    $db = new DatabaseConn();
    $link = $db->connect();
    $email = $user->getEmail();
    mysqli_query($link, "update users set secret = '$secret' WHERE email = '$email'");
} else {
    $secret = $user->getSecret();
}

$qrCodeUrl = $ga->getQRCodeGoogleUrl(APP_NAME_ABBR, $secret);
?>
<!DOCTYPE html>
<html lang="en">
    <head>

        <?php require_once './partials/head_.php'; ?>
        <link rel="stylesheet" href="assets/css/style.css">
         <style>
            table#style-4 thead tr th {
                color: rgb(245, 222, 179) !important;
            }
            #style-4_info {
                color: white !important;
            }
           #style-4_previous a.page-link, #style-4_next a.page-link {
                padding: 5px;
            }
            img[src='https://hynportal.io/img/favicon.png'] {
                position: relative !important;
                left: 15px !important;
            }
            #dark .settings .card {
    background: #232629;
}
.main-panel.container-fluid .profile-item.container-fluid .card {
    height: 100%;
    overflow-x: hidden;
    color: #727274;
    background-color: #1f2225;
    border-color: #232629;
}
.settings .wallet .tab-content button {
    padding: 12px 60px;
    margin-right: 10px;
    margin-bottom: 20px;
    width: 200px;
}
        </style>
        <link href="../css/form.css" rel="stylesheet" type="text/css"/>
        <style>
            #select2-list-container {
                background-color: #2a2e39;
                border: 1px solid #2a2e39;
                color: #fff !important;
                border-radius: 2px;
            }
        </style>
         <style>
            span.select2-selection.select2-selection--single {
                background-color: #232629;
                border: 1px solid #232629;
                color: #ffffff;
                height: 45px;
                padding: 0.55rem .75rem;
            }
            span.select2-selection__arrow {
                margin-top: 9px !important;
            }
            span#select2-country-container {
                color: white;
            }
            div.iti.iti--allow-dropdown {
                width: 100%;
            }
            .settings-profile .custom-file {
    display: inline-block;
    width: 27%;
    margin-left: 20px;
}
 .settings .custom-file-label {
    background: #2a2e39;
    border: 1px solid #2a2e39;
    color: #616d75;
}
#dark .settings label {
    color: #fff;
}
.settings-profile .custom-file label {
    border: 1px solid #e0e3eb;
}
.custom-control-label::before, .custom-file-label, .custom-select {
    transition: background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out;
}
.custom-file-label {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    z-index: 1;
    height: calc(1.5em + .75rem + 2px);
    padding: .375rem .75rem;
    font-weight: 400;
    line-height: 1.5;
    color: #495057;
    background-color: #fff;
    border: 1px solid #ced4da;
    border-radius: .25rem;
}
.custom-file-input:lang(en) ~ .custom-file-label::after {
    content: "Browse";
}
.settings-profile .custom-file label::after {
    background: #ffcd05;
    color: #fff;
    font-weight: 600;
}
.custom-file-label::after {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    z-index: 3;
    display: block;
    height: calc(1.5em + .75rem);
    padding: .375rem .75rem;
    line-height: 1.5;
    color: #495057;
    content: "Browse";
    background-color: #e9ecef;
    border-left: inherit;
    border-radius: 0 .25rem .25rem 0;
}
.custom-file {
    position: relative;
    display: inline-block;
    width: 100%;
    height: calc(1.5em + .75rem + 2px);
    margin-bottom: 0;
}
.settings-profile input, .settings-profile select {
    border: 1px solid #e0e3eb;
    border-radius: 5px;
    font-size: 14px;
    height: 45px;
}
.custom-file-input {
    position: relative;
    z-index: 2;
    width: 100%;
    height: calc(1.5em + .75rem + 2px);
    margin: 0;
    opacity: 0;
}
#dark .form-control, #dark .settings-profile select {
    background-color: #232629;
    border: 1px solid #232629;
    color: #ffffff;
}
        </style>
    </head>
    <body id="dark" style="background: #1F2225;">
        <div id="root">
             <style>
            div#cover {
                position: fixed;
                top: 0;
                bottom: 0;
                width: 100%;
                height: 100%;
                background-color: #000000;
                opacity: 0.6;
                z-index: 10000000;
            }
            .hide{
                display: none !important;
            }
        </style>
        <div id="cover" class="hide"></div>
            <div class="main-panel container-fluid">
                <div id="top-banner" style="min-height:unset" class="row"></div>
                <div class="flex-wrap flex-md-nowrap no-gutters row">
                    <div class="d-block menu-panel navigation__tab-container navigation__tab-container_hidden col-12 col-md-auto">
                        <?php require_once './partials/navbar.php'; ?>

                    </div>
                    <div class="col-12 col-md">
                        <div class="profile-item container-fluid">
                            <div class="settings  settings-profile mtb15" style="margin-left: 25px;margin-right: 25px;">
                                <div class="container-fluid">
                                    <div class="row">

                                        <div class="tab-pane col-md-12 col-lg-12" id="settings-wallet" role="tabpanel" aria-labelledby="settings-wallet-tab">
                        <div class="wallet">
                            <div class="">
                                <div class="tab-content">
                                    <div class="tab-pane fade show active" id="coinBTC" role="tabpanel">
                                         <div class="card">
                                                    <div class="card-body">
                                <div class="_2zmy">
                                    <h1 class="_2zmz" style="font-weight: 400; font-size: 24px">Add Google Authenticator to continue</h1>
                                    <style>
                                        ul.ins li {
                                            margin-bottom: 1rem;
                                        }
                                        ul.ins li img.qr {
                                            margin-top: 1.5rem;
                                        }
                                        ul.ins, h1 {
                                            color: white !important;
                                        }

                                    </style>
                                    <ul class="ins" style="list-style: decimal">
                                        <li>
                                            <div class="_2zn8"> Download the Google authenticator app on your mobile platform.</div>
                                            <div class="_2zn9 _2z2g">
                                                <ul style="list-style: none">
                                                    <li style="margin-bottom: 1rem; margin-top: 1rem">
                                                        <a style="font-size: 14px;" class="_36or _36op" href="https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2" target="_blank" rel="noopener"><img class="_36oq" src="https://static.whatsapp.net/rsrc.php/yE/r/TDpp7fh8s7a.svg"> Android</a>
                                                    </li>
                                                    <li >
                                                        <a style="font-size: 14px;" class="_36or _36op" href="https://apps.apple.com/us/app/google-authenticator/id388497605" target="_blank" rel="noopener"><img class="_36oq" src="https://static.whatsapp.net/rsrc.php/yX/r/QBqjMTNuxPz.svg"> iPhone</a>
                                                    </li>

                                                </ul>
                                            </div>
                                        </li>
                                        <li>
                                            <div class="_2zn8"> Scan the QR code below with the Google authenticator app.</div>
                                            <img class="qr" src="<?php echo "$qrCodeUrl"; ?>" />
                                        </li>
                                        <li>
                                            <div class="form-group">
                                                <div class="_2zn8">Enter code</div>
                                                <input class="form-control" id="code" type="text" placeholder="Enter code" name="amount" style="width: auto; margin-top: 1.5rem;" required="">
                                                <button class="btn btn-primary btn-sm" id="confirm" style="background-color: #009688; margin-top: 1.5rem; " type="submit" name="select_btn">CONFIRM</button>
                                                <div id="report" style="display: none;vertical-align: bottom;margin-left: 10px;color: red;"></div>
                                            </div>

                                        </li>
                                    </ul>
                                </div>
                            </div>
                                                </div>
                                    </div>
                                  
                                </div>
                            </div>
                        </div>
                    </div>

                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>
         <?php require_once './partials/scripts.php'; ?>
         <script src="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/js/select2.min.js"></script>
          <script>
            $(document).ready(function () {
                $('button#confirm').click(function () {
                    var report = $('div#report');
                    var cover = $('div#cover');
                    var code = $('input#code').val().trim();
                    if (code.length > 1) {
                        cover.removeClass("hide");
                        report.css("display", "none");
                        $.post('add-2fa', {code: code}, function (data) {

                            if (data.trim() == "OK") {
                                report.text("Google 2fa added to your account!");
                                report.css("display", "inline-block");
                                report.css("color", "green");
                                cover.addClass("hide");
                                setTimeout(function () {
                                    window.location.href = "index";
                                }, 3000);
                            } else {
                                cover.addClass("hide");
                                report.text("Invalid code");
                                report.css("display", "inline-block");
                            }
                        });
                    } else {
                        cover.addClass("hide");
                        report.text("Invalid code");
                        report.css("display", "inline-block");
                    }
                });
            });
        </script>
        <script>
            $('#list').select2({
                placeholder: "Make a Selection"
            });
        </script>
    </body>
</html>