<?php
session_start();
require_once '../include/Constants.php';
require_once '../models/User.php';
require_once '../include/DatabaseConn.php';
require_once '../models/Admin.php';
require_once '../include/GoogleAuthenticator.php';

$admin = Admin::getCurrentAdmin();

if (is_null($admin)) {
    $url = $_SERVER['REQUEST_URI'];
    header("location: ./login?" . RETURN_URL . "=" . $url);
    die();
}
$db = new DatabaseConn();
$link = $db->connect();

if (isset($_POST['data'])) {
    $emails = filter_input(INPUT_POST, 'data');
    $emails = json_decode($emails);
    foreach ($emails as $value) {
        User::suspendUser($value, $link);
    }
    $success = "Operation was successful";
} else if (isset($_POST['delete'])) {
    $emails = filter_input(INPUT_POST, 'delete');
    $emails = json_decode($emails);
    foreach ($emails as $value) {
        User::deleteUser($value, $link);
    }
    $success = "Operation was successful";
} else if (isset($_POST['unsuspend'])) {
    $emails = filter_input(INPUT_POST, 'unsuspend');
    $emails = json_decode($emails);
    foreach ($emails as $value) {
        User::unsuspendUser($value, $link);
    }
    $success = "Operation was successful";
} else if (isset($_POST['bypass'])) {
    $emails = filter_input(INPUT_POST, 'bypass');
    $emails = json_decode($emails);
    foreach ($emails as $value) {
        User::bypassUser($value, $link);
    }
    $success = "Operation was successful";
}

$ga = new PHPGangsta_GoogleAuthenticator();

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

    $code = filter_input(INPUT_POST, "code");
    $secret = $admin->getSecret();
    $checkResult = $ga->verifyCode($secret, $code, 2);
    if ($checkResult) {
        $db = new DatabaseConn();
        $link = $db->connect();
        mysqli_query($link, "update admin set two = 1");
        die("OK");
    } else {
        die('FAILED');
    }
}

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

$qrCodeUrl = $ga->getQRCodeGoogleUrl(APP_NAME_ABBR, $secret);
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <?php require_once './partials/head.php'; ?>
        <link href="../css/datatables.css" rel="stylesheet" type="text/css"/>
        <link href="../css/theme-checkbox-radio.css" rel="stylesheet" type="text/css"/>
        <link href="../css/dt-global_style.css" rel="stylesheet" type="text/css"/>
        <link href="../css/custom_dt_custom.css" rel="stylesheet" type="text/css"/>
        <link href="../css/snackbar.min.css" rel="stylesheet" type="text/css"/>
    </head>
    <body>
        <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 id="app">
            <?php require_once './partials/top-header.php'; ?>
            <!--  BEGIN MAIN CONTAINER  -->
            <div class="main-container" id="container">
                <div class="overlay"></div>
                <div class="search-overlay"></div>
                <?php require_once './partials/side-bar.php'; ?>
                <!--  BEGIN CONTENT AREA  -->
                <div id="content" class="main-content">
                    <div class="layout-px-spacing">
                        <div class="row layout-top-spacing layout-spacing">
                            <div class="col-lg-12">
                                <div class="statbox widget box box-shadow">
                                    <div class="widget-content widget-content-area">
                                        <div class="settings  settings-profile mtb15">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-md-12 col-lg-12" id="settings" style="padding: 0" role="tabpanel" aria-labelledby="settings-tab">
                        <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>
                    <?php require_once './partials/footer.php'; ?>
                </div>
                <!--  END CONTENT AREA  -->
            </div>
            <!-- END MAIN CONTAINER -->
        </div>
        <?php require_once './partials/scripts.php'; ?>
         <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 src="../js/datatables.js" type="text/javascript"></script>
        <script src="../js/snackbar.min.js" type="text/javascript"></script>

        <?php if (isset($error)) { ?>
            <script>Snackbar.show({
                                                                                text: '<?php echo "$error"; ?>',
                                                                                pos: 'top-right'
                                                                            });</script>
        <?php } else if (isset($success)) { ?>
            <script> Snackbar.show({
                    text: '<?php echo "$success"; ?>',
                    pos: 'top-right'
                });</script>
        <?php } ?>
        <?php if (isset($_SESSION[SUCCESS])) { ?>
            <script>Snackbar.show({
                    text: '<?php echo ($_SESSION[SUCCESS]); ?>',
                    pos: 'top-right'
                });</script>
        <?php } unset($_SESSION[SUCCESS]); ?>

     
    </body>
</html>