<?php 
session_start();
require_once("functions.php");

if(isset($_POST["username"]) && !empty($_POST["username"]) && isset($_POST["password"]) && !empty($_POST["password"])){
    $username = $_POST["username"];
    $password = $_POST["password"];
    $ip_address = $_SERVER['REMOTE_ADDR'];
    
    $array_tentativi_login = array(':ip_address' => $ip_address);
    $sql_tentativi_login ="select * FROM secure_login WHERE ip_address=:ip_address AND fallito = 1";
    list($status_tentativi_login,$contenuto_tentativi_login,$nrows_tentativi_login) = read_db_pdo($sql_tentativi_login,$array_tentativi_login);
    if($nrows_tentativi_login == 3) {
        write_admin_user_login($username,1);
        echo "KO1R";
        exit;
    }
    if($nrows_tentativi_login > 3) {
        if ($nrows_tentativi_login > 19) { 
            echo "KO10IP";
            exit;
        }
    }
    
    $pwd_hash = $password;
    $array_login = array(':username' => $username,':password' => $pwd_hash);
    $sql_login ="select * FROM rivenditori WHERE username=:username and password=:password LIMIT 1";
    list($status_login,$contenuto_login,$nrows_login) = read_db_pdo($sql_login,$array_login);
    $stato_rivenditore = $contenuto_login[0]["stato"];
    
    if( ($status_login==1) && ($nrows_login==1) ) {
        $id_utente = (int) $contenuto_login[0]["id_rivenditore"];
        if($id_utente < 1 ) {
            write_admin_user_login($username,1);
            echo "KO1";
            exit;
        }
        $user_logged = $contenuto_login[0]["username"];
        if($user_logged!=$username ) {
            write_admin_user_login($username,1);
            echo "KO2";
            exit;
        }
        $stato = $contenuto_login[0]["stato"];
        if($stato == 0 ) {
                echo "KO2000";
            exit;
        }
        $permessi = $contenuto_login[0]["permessi"];
        $oggi = date("d-m-Y"); 
        $server = $_SERVER['SERVER_NAME'];
        $informazioni_utente_logged = array("username"=>$user_logged,"id_utente"=>$id_utente, "data"=>$oggi, "code" => $permessi);
        $_SESSION["DAST"]["hash_informazioni_utente_logged"] = md5($user_logged.$oggi.$server);
        $_SESSION["DAST"]['informazioni_utente_logged'] = $informazioni_utente_logged;
        echo "OK";
        exit;
    } else {
        write_admin_user_login($username,1);
        $n_tentativi_login = check_admin_failed_login();
        if ($n_tentativi_login < 15) { 
            echo "KO100";
            exit;
        }else{
            echo "KO1000";
            exit;
        }
    }
    exit;
}
?>
<!DOCTYPE html>
<html lang="it">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>PANNELLO - NOW</title>
    <link rel="stylesheet" href="vendors/mdi/css/materialdesignicons.min.css">
    <link rel="stylesheet" href="vendors/css/vendor.bundle.base.css">
    <link rel="stylesheet" href="css/vertical-layout-dark/style.css">
    <link rel="shortcut icon" href="./images/now_icon.png" />
    <style>
        @keyframes buttonGradient {
            0% {
                background: linear-gradient(90deg, #00ffff, #ff00ff);
            }
            50% {
                background: linear-gradient(90deg, #ff00ff, #00ffff);
            }
            100% {
                background: linear-gradient(90deg, #00ffff, #ff00ff);
            }
        }

        body {
            height: 100vh;
            overflow: hidden;
            filter: drop-shadow(0 0 10px white);
            background-color: #000000;
        }

        .form-control, .asColorPicker-input, .dataTables_wrapper select, 
        .jsgrid .jsgrid-table .jsgrid-filter-row input[type=text], 
        .jsgrid .jsgrid-table .jsgrid-filter-row select, 
        .jsgrid .jsgrid-table .jsgrid-filter-row input[type=number], 
        .select2-container--default .select2-selection--single, 
        .select2-container--default .select2-selection--single .select2-search__field, 
        .typeahead, .tt-query, .tt-hint {
            border: 1px solid #ffffff;
            font-weight: 400;
            font-size: 0.875rem;
        }

        .auth .brand-logo img {
            width: 300px;
        }

        .auth-form-btn {
            background: linear-gradient(90deg, #00ffff, #ff00ff) !important;
            color: white !important;
            width: 100% !important;
            cursor: pointer !important;
            font-family: "Arial Black", sans-serif !important;
            border: none !important;
            padding: 12px !important;
            border-radius: 8px !important;
            text-transform: uppercase !important;
            font-weight: bold !important;
            animation: buttonGradient 3s ease infinite !important;
            position: relative !important;
            z-index: 1 !important;
            filter: drop-shadow(0 0 5px rgba(0, 255, 255, 0.5)) !important;
            transition: transform 0.3s ease, filter 0.3s ease !important;
        }

        .auth-form-btn::before {
            content: "" !important;
            position: absolute !important;
            top: -3px !important;
            left: -3px !important;
            right: -3px !important;
            bottom: -3px !important;
            animation: buttonGradient 3s ease infinite !important;
            z-index: -1 !important;
            filter: blur(10px) !important;
            opacity: 0.7 !important;
            border-radius: 10px !important;
        }

        .auth-form-btn:hover {
            transform: translateY(-2px) !important;
            filter: drop-shadow(0 0 10px rgba(255, 0, 255, 0.7)) !important;
        }

        .auth-form-btn:active {
            transform: translateY(1px) !important;
        }

        .form-control {
            background: rgba(255, 255, 255, 0.1) !important;
            border: 1px solid rgba(255, 255, 255, 0.2) !important;
            color: white !important;
        }

        .form-control:focus {
            background: rgba(255, 255, 255, 0.15) !important;
            border-color: rgba(255, 255, 255, 0.3) !important;
            box-shadow: 0 0 15px rgba(255, 255, 255, 0.1) !important;
        }

        .form-control::placeholder {
            color: rgba(255, 255, 255, 0.6) !important;
        }
    </style>
</head>

<body>
    <div class="container-scroller">
        <div class="container-fluid page-body-wrapper full-page-wrapper">
            <div class="content-wrapper d-flex align-items-center auth px-8" style="background-color: black;">
                <div class="row w-100 mx-0">
                    <div class="col-lg-4 mx-auto">
                        <div class="brand-logo">
                            <img src="./images/impulse.svg" alt="logo" style="display: block; margin: 0 auto;">
                        </div>

                        <div class="alert alert-fill-danger" role="alert" id="error_login" style="display:none">
                            <i class="mdi mdi-alert-circle"></i>ERRORE! Dati non validi
                        </div>
                        <form class="pt-3" method="POST" id="login_form">
                            <div class="form-group">
                                <input id="username" class="form-control form-control-danger" name="username" minlength="4" type="text" required placeholder="Username" aria-invalid="true">
                            </div>
                            <div class="form-group">
                                <input type="password" minlength="4" class="form-control form-control-lg" required name="password" id="password" placeholder="Password" aria-invalid="true">
                            </div>
                            <div class="mt-3">
                                <button type="submit" class="btn btn-block btn-lg font-weight-medium auth-form-btn">ACCEDI</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="vendors/js/vendor.bundle.base.js"></script>
    <script src="js/off-canvas.js"></script>
    <script src="js/hoverable-collapse.js"></script>
    <script src="js/template.js"></script>
    <script src="js/settings.js"></script>
    <script src="js/todolist.js"></script>
    <script src="vendors/jquery-validation/jquery.validate.min.js"></script>
    <script src="vendors/bootstrap-maxlength/bootstrap-maxlength.min.js"></script>
    <script src="js/form-validation.js"></script>
    <script src="js/bt-maxLength.js"></script>
    <script>
        $(document).ready(function(){
            function credenziali_nonvalide(){
                $("#error_login").html("<p>Credenziali non valide!</p>");
                $("#error_login").show();
            }
            
            $(document).on('submit','#login_form',function(e){
                e.preventDefault();
                $("#error_login").hide();
                if($("#username").val() == "") { credenziali_nonvalide(); return false;}
                if($("#password").val() == "") { credenziali_nonvalide(); return false;}
                $.ajax({
                    url: window.location.href,
                    type: "POST",
                    data: $(this).serialize(),
                    success: function(data) {
                        verifica = data.replace(/(\r\n|\n|\r)/gm,"");
                        switch (verifica) {
                            case "KO1000":
                                $("#error_login").html("<p>Troppi tentativi falliti!</p>");
                                $("#error_login").show();
                                location.reload();
                                break;
                            case "KO0":
                                $("#error_login").html("<p>Credenziali non valide!</p>");
                                $("#error_login").show();
                                break;
                            case "KO10IP":
                                $("#error_login").html("<p>IP BLOCCATO! RIPROVA TRA 5 MINUTI</p>");
                                $("#error_login").show();
                                break;
                            case "KO1R":
                                $("#error_login").html("<p>Credenziali non valide!</p>");
                                $("#error_login").show();
                                location.reload();
                                break;
                            case "KO2000":
                                $("#error_login").html("<p>Account Disabilitato - Contatta l'amministratore</p>");
                                $("#error_login").show();
                                location.reload();
                                break;
                            case "OK":
                                location.href="dashboard.php";
                                break;
                            default:
                                $("#error_login").html("<p>Credenziali non valide!</p>");
                                $("#error_login").show();
                                break;
                        }
                    },
                    error: function(data) {
                        $("#error_login").html("<p>Script non disponibile</p>");
                        $("#error_login").show()
                    }
                });
            });
        });
    </script>
    <script>
        function blocco_mousedx() { return(false); }
        document.oncontextmenu = blocco_mousedx;
    </script>
    <script>
        document.onkeydown = function(blocco_tasti) {
            if(event.keyCode == 123) { return false; }
            if(blocco_tasti.ctrlKey && blocco_tasti.shiftKey && blocco_tasti.keyCode == 'I'.charCodeAt(0)) { return false; }
            if(blocco_tasti.ctrlKey && blocco_tasti.shiftKey && blocco_tasti.keyCode == 'C'.charCodeAt(0)) { return false; }
            if(blocco_tasti.ctrlKey && blocco_tasti.shiftKey && blocco_tasti.keyCode == 'J'.charCodeAt(0)) { return false; }
            if(blocco_tasti.ctrlKey && blocco_tasti.keyCode == 'U'.charCodeAt(0)) { return false; } 
        }
    </script>    
</body>
</html>