<?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/Settings.php';
require_once '../models/USettings.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();
}

$settings = USettings::getSystemSettings($link, $user->getEmail());
if ($settings->getTwofa() && is_null($user->getFa())) {
    header("location: add-2fa");
    die();
}

if (isset($_POST['settings'])) {
    $tor = is_null(filter_input(INPUT_POST, "tor")) ? 0 : 1;
    $twofa = is_null(filter_input(INPUT_POST, "twofa")) ? 0 : 1;
    $news = is_null(filter_input(INPUT_POST, "news")) ? 0 : 1;

    $r = mysqli_query($link, "select * from usettings where `user` = '{$user->getEmail()}'");
    if (mysqli_num_rows($r) == 1) {
        mysqli_query($link, "update usettings set `tor` = $tor, twofa = $twofa, news = $news WHERE `user` = '{$user->getEmail()}' ");
    } else {
        mysqli_query($link, "insert INTO usettings(tor,twofa,news,`user`) VALUES ($tor, $twofa, $news, '{$user->getEmail()}')");
    }

    if (mysqli_affected_rows($link) > 0) {
        $success = "Settings saved";
        if($twofa == 1){
            header("location: settings");
            die();
        }
    } else {
        $error = "Nothing changed";
    }
}
$settings = USettings::getSystemSettings($link, $user->getEmail());
?>
<!Doctype html>
<html class="gr__">
    <head>
        <?php require_once './partials/head.php'; ?>
        <style>
            /*!
             * Bootstrap v4.4.1 (https://getbootstrap.com/)
             * Copyright 2011-2019 The Bootstrap Authors
             * Copyright 2011-2019 Twitter, Inc.
             * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
             */[tabindex="-1"]:focus:not(:focus-visible){outline:0!important}h5{margin-top:0;margin-bottom:.5rem}p{margin-top:0;margin-bottom:1rem}ul{margin-top:0;margin-bottom:1rem}label{display:inline-block;margin-bottom:.5rem}input{margin:0;font-family:inherit;font-size:inherit;line-height:inherit}input{overflow:visible}[type=submit]{-webkit-appearance:button}[type=button]:not(:disabled),[type=reset]:not(:disabled),[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}[type=submit]::-moz-focus-inner{padding:0;border-style:none}input[type=checkbox]{box-sizing:border-box;padding:0}::-webkit-file-upload-button{font:inherit;-webkit-appearance:button}h5{margin-bottom:.5rem;font-weight:500;line-height:1.2}h5{font-size:1.25rem}.container-fluid{width:100%;padding-right:15px;padding-left:15px;margin-right:auto;margin-left:auto}.row{display:-ms-flexbox;display:flex;-ms-flex-wrap:wrap;flex-wrap:wrap;margin-right:-15px;margin-left:-15px}.col-lg-9,.col-md-12{position:relative;width:100%;padding-right:15px;padding-left:15px}@media (min-width:768px){.col-md-12{-ms-flex:0 0 100%;flex:0 0 100%;max-width:100%}}@media (min-width:992px){.col-lg-9{-ms-flex:0 0 75%;flex:0 0 75%;max-width:75%}}.custom-control-input.is-valid:focus:not(:checked)~.custom-control-label::before,.was-validated .custom-control-input:valid:focus:not(:checked)~.custom-control-label::before{border-color:#28a745}.custom-control-input.is-invalid:focus:not(:checked)~.custom-control-label::before,.was-validated .custom-control-input:invalid:focus:not(:checked)~.custom-control-label::before{border-color:#dc3545}.btn-primary:not(:disabled):not(.disabled).active,.btn-primary:not(:disabled):not(.disabled):active{color:#fff;background-color:#0062cc;border-color:#005cbf}.btn-primary:not(:disabled):not(.disabled).active:focus,.btn-primary:not(:disabled):not(.disabled):active:focus{box-shadow:0 0 0 .2rem rgba(38,143,255,.5)}.btn-secondary:not(:disabled):not(.disabled).active,.btn-secondary:not(:disabled):not(.disabled):active{color:#fff;background-color:#545b62;border-color:#4e555b}.btn-secondary:not(:disabled):not(.disabled).active:focus,.btn-secondary:not(:disabled):not(.disabled):active:focus{box-shadow:0 0 0 .2rem rgba(130,138,145,.5)}.btn-success:not(:disabled):not(.disabled).active,.btn-success:not(:disabled):not(.disabled):active{color:#fff;background-color:#1e7e34;border-color:#1c7430}.btn-success:not(:disabled):not(.disabled).active:focus,.btn-success:not(:disabled):not(.disabled):active:focus{box-shadow:0 0 0 .2rem rgba(72,180,97,.5)}.btn-info:not(:disabled):not(.disabled).active,.btn-info:not(:disabled):not(.disabled):active{color:#fff;background-color:#117a8b;border-color:#10707f}.btn-info:not(:disabled):not(.disabled).active:focus,.btn-info:not(:disabled):not(.disabled):active:focus{box-shadow:0 0 0 .2rem rgba(58,176,195,.5)}.btn-warning:not(:disabled):not(.disabled).active,.btn-warning:not(:disabled):not(.disabled):active{color:#212529;background-color:#d39e00;border-color:#c69500}.btn-warning:not(:disabled):not(.disabled).active:focus,.btn-warning:not(:disabled):not(.disabled):active:focus{box-shadow:0 0 0 .2rem rgba(222,170,12,.5)}.btn-danger:not(:disabled):not(.disabled).active,.btn-danger:not(:disabled):not(.disabled):active{color:#fff;background-color:#bd2130;border-color:#b21f2d}.btn-danger:not(:disabled):not(.disabled).active:focus,.btn-danger:not(:disabled):not(.disabled):active:focus{box-shadow:0 0 0 .2rem rgba(225,83,97,.5)}.btn-light:not(:disabled):not(.disabled).active,.btn-light:not(:disabled):not(.disabled):active{color:#212529;background-color:#dae0e5;border-color:#d3d9df}.btn-light:not(:disabled):not(.disabled).active:focus,.btn-light:not(:disabled):not(.disabled):active:focus{box-shadow:0 0 0 .2rem rgba(216,217,219,.5)}.btn-dark:not(:disabled):not(.disabled).active,.btn-dark:not(:disabled):not(.disabled):active{color:#fff;background-color:#1d2124;border-color:#171a1d}.btn-dark:not(:disabled):not(.disabled).active:focus,.btn-dark:not(:disabled):not(.disabled):active:focus{box-shadow:0 0 0 .2rem rgba(82,88,93,.5)}.btn-outline-primary:not(:disabled):not(.disabled).active,.btn-outline-primary:not(:disabled):not(.disabled):active{color:#fff;background-color:#007bff;border-color:#007bff}.btn-outline-primary:not(:disabled):not(.disabled).active:focus,.btn-outline-primary:not(:disabled):not(.disabled):active:focus{box-shadow:0 0 0 .2rem rgba(0,123,255,.5)}.btn-outline-secondary:not(:disabled):not(.disabled).active,.btn-outline-secondary:not(:disabled):not(.disabled):active{color:#fff;background-color:#6c757d;border-color:#6c757d}.btn-outline-secondary:not(:disabled):not(.disabled).active:focus,.btn-outline-secondary:not(:disabled):not(.disabled):active:focus{box-shadow:0 0 0 .2rem rgba(108,117,125,.5)}.btn-outline-success:not(:disabled):not(.disabled).active,.btn-outline-success:not(:disabled):not(.disabled):active{color:#fff;background-color:#28a745;border-color:#28a745}.btn-outline-success:not(:disabled):not(.disabled).active:focus,.btn-outline-success:not(:disabled):not(.disabled):active:focus{box-shadow:0 0 0 .2rem rgba(40,167,69,.5)}.btn-outline-info:not(:disabled):not(.disabled).active,.btn-outline-info:not(:disabled):not(.disabled):active{color:#fff;background-color:#17a2b8;border-color:#17a2b8}.btn-outline-info:not(:disabled):not(.disabled).active:focus,.btn-outline-info:not(:disabled):not(.disabled):active:focus{box-shadow:0 0 0 .2rem rgba(23,162,184,.5)}.btn-outline-warning:not(:disabled):not(.disabled).active,.btn-outline-warning:not(:disabled):not(.disabled):active{color:#212529;background-color:#ffc107;border-color:#ffc107}.btn-outline-warning:not(:disabled):not(.disabled).active:focus,.btn-outline-warning:not(:disabled):not(.disabled):active:focus{box-shadow:0 0 0 .2rem rgba(255,193,7,.5)}.btn-outline-danger:not(:disabled):not(.disabled).active,.btn-outline-danger:not(:disabled):not(.disabled):active{color:#fff;background-color:#dc3545;border-color:#dc3545}.btn-outline-danger:not(:disabled):not(.disabled).active:focus,.btn-outline-danger:not(:disabled):not(.disabled):active:focus{box-shadow:0 0 0 .2rem rgba(220,53,69,.5)}.btn-outline-light:not(:disabled):not(.disabled).active,.btn-outline-light:not(:disabled):not(.disabled):active{color:#212529;background-color:#f8f9fa;border-color:#f8f9fa}.btn-outline-light:not(:disabled):not(.disabled).active:focus,.btn-outline-light:not(:disabled):not(.disabled):active:focus{box-shadow:0 0 0 .2rem rgba(248,249,250,.5)}.btn-outline-dark:not(:disabled):not(.disabled).active,.btn-outline-dark:not(:disabled):not(.disabled):active{color:#fff;background-color:#343a40;border-color:#343a40}.btn-outline-dark:not(:disabled):not(.disabled).active:focus,.btn-outline-dark:not(:disabled):not(.disabled):active:focus{box-shadow:0 0 0 .2rem rgba(52,58,64,.5)}.custom-control{position:relative;display:block;min-height:1.5rem;padding-left:1.5rem}.custom-control-input{position:absolute;left:0;z-index:-1;width:1rem;height:1.25rem;opacity:0}.custom-control-input:checked~.custom-control-label::before{color:#fff;border-color:#007bff;background-color:#007bff}.custom-control-input:focus~.custom-control-label::before{box-shadow:0 0 0 .2rem rgba(0,123,255,.25)}.custom-control-input:focus:not(:checked)~.custom-control-label::before{border-color:#80bdff}.custom-control-input:not(:disabled):active~.custom-control-label::before{color:#fff;background-color:#b3d7ff;border-color:#b3d7ff}.custom-control-input:disabled~.custom-control-label{color:#6c757d}.custom-control-input:disabled~.custom-control-label::before{background-color:#e9ecef}.custom-control-label{position:relative;margin-bottom:0;vertical-align:top}.custom-control-label::before{position:absolute;top:.25rem;left:-1.5rem;display:block;width:1rem;height:1rem;pointer-events:none;content:"";background-color:#fff;border:#adb5bd solid 1px}.custom-control-label::after{position:absolute;top:.25rem;left:-1.5rem;display:block;width:1rem;height:1rem;content:"";background:no-repeat 50%/50% 50%}.custom-switch{padding-left:2.25rem}.custom-switch .custom-control-label::before{left:-2.25rem;width:1.75rem;pointer-events:all;border-radius:.5rem}.custom-switch .custom-control-label::after{top:calc(.25rem + 2px);left:calc(-2.25rem + 2px);width:calc(1rem - 4px);height:calc(1rem - 4px);background-color:#adb5bd;border-radius:.5rem;transition:background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out,-webkit-transform .15s ease-in-out;transition:transform .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out;transition:transform .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out,-webkit-transform .15s ease-in-out}@media (prefers-reduced-motion:reduce){.custom-switch .custom-control-label::after{transition:none}}.custom-switch .custom-control-input:checked~.custom-control-label::after{background-color:#fff;-webkit-transform:translateX(.75rem);transform:translateX(.75rem)}.custom-switch .custom-control-input:disabled:checked~.custom-control-label::before{background-color:rgba(0,123,255,.5)}.custom-control-label::before{transition:background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out}@media (prefers-reduced-motion:reduce){.custom-control-label::before{transition:none}}.card{position:relative;display:-ms-flexbox;display:flex;-ms-flex-direction:column;flex-direction:column;min-width:0;word-wrap:break-word;background-color:#fff;background-clip:border-box;border:1px solid rgba(0,0,0,.125);border-radius:.25rem}.card-body{-ms-flex:1 1 auto;flex:1 1 auto;min-height:1px;padding:1.25rem}.card-title{margin-bottom:.75rem}.close:not(:disabled):not(.disabled):focus,.close:not(:disabled):not(.disabled):hover{opacity:.75}@supports ((position:-webkit-sticky) or (position:sticky)){}@media print{*,::after,::before{text-shadow:none!important;box-shadow:none!important}p{orphans:3;widows:3}@page{size:a3}body{min-width:992px!important}}
            /*# sourceMappingURL=bootstrap.min.css.map */
            .mtb15 {
                margin: 15px 0;
            }

            .mt15 {
                margin-top: 15px;
            }

            .mb15 {
                margin-bottom: 15px;
            }

            .ml15 {
                margin-left: 15px;
            }

            .mr15 {
                margin-right: 15px;
            }

            .ptb70 {
                padding: 70px 0;
            }

            .pt70 {
                padding-top: 70px;
            }

            .pb40 {
                padding-bottom: 40px;
            }
            .custom-control-input:focus ~ .custom-control-label::before {
                box-shadow: none;
            }

            .settings .card {
                box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 14px -6px;
                border-radius: 5px;
                border: none;
                margin-bottom: 15px;
            }

            .settings .card-title {
                font-size: 16px;
                margin-bottom: 30px;
            }

            .settings-profile input {
                border: 1px solid #e0e3eb;
                border-radius: 5px;
                font-size: 14px;
                height: 45px;
            }

            .settings-profile input[type="submit"] {
                background: #ffcd05;
                color: #fff;
                font-weight: 600;
                padding: 10px 25px;
                height: inherit;
                font-size: 16px;
                margin-top: 10px;
            }

            .settings-notification li {
                display: flex;
                justify-content: space-between;
                padding: 15px 0;
                border-bottom: 1px solid #f0f3fa;
                align-items: center;
            }

            .settings-notification li:first-child {
                padding-top: 0;
            }

            .settings-notification li:last-child {
                padding-bottom: 0;
                border-bottom: 0;
            }

            .settings-notification li span {
                color: #18214d59;
            }

            .settings-notification li p {
                margin-bottom: 0;
            }

            .settings .custom-control-label {
                cursor: pointer;
            }


            #dark .custom-control-input:checked ~ .custom-control-label::before {
                color: #fff;
                border-color: #ffcd05;
                background-color: #ffcd05;
            }

            #dark .settings .card {
                background: #1e222d;
            }

            #dark .settings .card-title {
                color: #fff;
            }

            #dark .settings-notification li {
                border-bottom: 1px solid #2a2e39;
            }

            #dark .settings-notification li:last-child {
                border-bottom: 0;
            }

            #dark .settings-notification p {
                color: #ffffff;
            }

            #dark .settings-notification span {
                color: #4f5966;
            }

            #dark .settings label {
                color: #fff;
            }

            #dark .settings-profile input[type="submit"] {
                border: 1px solid #ffcd05;
            }
            #dark .custom-control-input:checked ~ .custom-control-label::before {
                color: #fff;
                border-color: #31b993;
                background-color: #31b993;
            }
            #dark .settings-profile input[type="submit"] {
                border: 1px solid #31b993;
            }
            .settings-profile input[type="submit"] {
                background: #31b993;
            }
            /*# sourceMappingURL=style.css.map */
        </style>
    </head>
    <body cz-shortcut-listen="true" data-gr-c-s-loaded="true">
        <div id="app">
            <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="sc-eIHaNI jXnkjm" style="">
                <div class="sc-bsbRJL jBomjO">
                    <span></span>
                </div>
                <div></div>
                <?php require_once './partials/navbar.php'; ?>
                <div class="sc-eTpRJs chWNvr">
                    <div data-e2e="page-home" class="sc-iomxrj lkDWON">
                        <div class="sc-ugnQR ikKNan">
                            <div class="sc-dDPtig fBozlL">
                                <section class="sc-jAarDj jmkFbA">
                                    <div class="sc-fNbuCz sc-bqsUL dgojan">
                                        <div class="sc-dArhMf fOVmDi">
                                            <div class="sc-hEsumM BCdqX sc-jYIdPM gLdSNN" style="background-color: transparent;">
                                                <span width="33%" data-e2e="totalTab" class="sc-ktHwxA iBBsrM">
                                                    <span>User Settings</span>
                                                </span>
                                            </div>
                                            <div class="sc-bHwgHz EpALc">
                                                <div class="card-body" style='padding: 30px;font-size: 15px;'>
                                                    <form action="user-settings" method="post">
                                                        <div class="settings-notification">
                                                            <ul>
                                                                <li>
                                                                    <div class="notification-info">
                                                                        <p style="font-family: 'Inter';">Block Tor Request</p>
                                                                        <span style="font-family: 'Inter';">Prevent unauthorized login request from unknown IP</span>
                                                                    </div>
                                                                    <div class="custom-control custom-switch">
                                                                        <input type="checkbox" <?php echo (($settings->getTor()) ? "checked" : ""); ?> name="tor" class="custom-control-input" id="notification1">
                                                                        <label class="custom-control-label" for="notification1"></label>
                                                                    </div>
                                                                </li>
                                                                <li>
                                                                    <div class="notification-info">
                                                                        <p style="font-family: 'Inter';">2FA</p>
                                                                        <span style="font-family: 'Inter';">Two factor authentication service</span>
                                                                    </div>
                                                                    <div class="custom-control custom-switch">
                                                                        <input type="checkbox" <?php echo (($settings->getTwofa()) ? "checked" : ""); ?> name="twofa" class="custom-control-input" id="notification2">
                                                                        <label class="custom-control-label" for="notification2"></label>
                                                                    </div>
                                                                </li>
                                                                <li>
                                                                    <div class="notification-info">
                                                                        <p style="font-family: 'Inter'; ">Latest news</p>
                                                                        <span style="font-family: 'Inter';">Get the latest news in your mail</span>
                                                                    </div>
                                                                    <div class="custom-control custom-switch">
                                                                        <input  type="checkbox" <?php echo (($settings->getNews()) ? "checked" : ""); ?> name="news" class="custom-control-input" id="notification3">
                                                                        <label class="custom-control-label" for="notification3"></label>
                                                                    </div>
                                                                </li>
                                                            </ul>
                                                            <div class="row" style="margin-top: 10px;">
                                                                <div class="col-md-12">
                                                                    <input type="submit" name="settings" class="sc-bdVaJa OngNg sc-kwgWOH fLeWXT" value="Save Settings" style="float: right;">
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </form>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="sc-fNbuCz sc-eRjRog jUkgna">
                                        <div class="sc-gEGaab dwfEmX dodo">
                                            <div style="height:560px; background-color: #FFFFFF; overflow:hidden; box-sizing: border-box; border: 1px solid #56667F; border-radius: 4px; text-align: right; line-height:14px; font-size: 12px; font-feature-settings: normal; text-size-adjust: 100%; box-shadow: inset 0 -20px 0 0 #56667F;padding:1px;padding: 0px; margin: 0px; width: 100%;"><div style="height:540px; padding:0px; margin:0px; width: 100%;"><iframe src="https://widget.coinlib.io/widget?type=chart&theme=light&coin_id=859&pref_coin_id=1505" width="100%" height="536px" scrolling="auto" marginwidth="0" marginheight="0" frameborder="0" border="0" style="border:0;margin:0;padding:0;line-height:14px;"></iframe></div><div style="color: #FFFFFF; line-height: 14px; font-weight: 400; font-size: 11px; box-sizing: border-box; padding: 2px 6px; width: 100%; font-family: Verdana, Tahoma, Arial, sans-serif;"></div></div>
                                        </div>
                                    </div>
                                </section>
                            </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>
            var clipboard = new ClipboardJS('.upgrade-to-text');
            clipboard.on('success', function (e) {
                Snackbar.show({
                    text: 'Copied!',
                    pos: 'top-right'
                });
            });
            $('a[href="settings"]').addClass('active');
        </script>


    </body>
</html>