<?php
include('libraryPHP/site_config.php');
//include('libraryPHP/session_check_url.php');
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
    <title><?php echo $appName ?> | <?php echo $pageTitle ?></title>
    <!-- Favicon -->
    <?php echo $appFaviconHtml; ?>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="assets/css/bootstrap.min.css">
    <!-- Fontawesome CSS -->
    <link rel="stylesheet" href="assets/css/font-awesome.min.css">
    <!-- Main CSS -->
    <link rel="stylesheet" href="assets/css/style.css">
    <!-- My Custom -->
    <link rel="stylesheet" href="custom.css">
    <!-- font awesome -->
    <link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous" />
    <style>
        .eye {
            position: relative;
            left: 98%;
            transform: translate(0, -176%);
            top: auto;
            cursor: pointer;

        }

        body {
            color: #000;
            height: 100% !important;
        }

        #togglePassword1 {
            position: absolute;
            transform: translate(0, -176%);
            top: auto;
            cursor: pointer;
        }

        #togglePassword2 {
            position: absolute;
            transform: translate(0, -174%);
            top: auto;
            cursor: pointer;
        }

        .error {
            color: red;
        }
    </style>
    <!--[if lt IE 9]>
			<script src="assets/js/html5shiv.min.js"></script>
			<script src="assets/js/respond.min.js"></script>
		<![endif]-->
</head>

<body>
    <!-- Main Wrapper -->
    <div class="main-wrapper login-body">
        <div class="login-wrapper">
            <div class="container">
                <div class="loginbox">
                    <div class="login-left">
                        <img class="img-fluid" src="assets/images/logo1.png" alt="Logo">
                    </div>
                    <div class="login-right">
                        <div class="login-right-wrap">
                            <h1>Login</h1>
                            <p class="account-subtitle">Access to our dashboard</p>
                            <!-- Form -->
                            <form action="" method="post" id="form_validation_login">
                                <input required="required" type="hidden" id="action" name="action" value="signin">
                                <input required="required" type="hidden" id="player_id" name="player_id" value="123456" />
                                <div class="form-group">
                                    <input class="form-control" type="text" placeholder="Email" name="email" id="email">
                                </div>
                                <div class="form-group" style="margin-bottom: 0px;">
                                    <input class="form-control" type="password" name="password" id="password1" placeholder="Password" maxlength="16">
                                    <span class="eye" onclick="myfunction1()">
                                        <i class="far fa-eye-slash" id="togglePassword1" style="
                                        margin-left: -1.5rem;"></i>
                                        <i class="far fa-eye" id="togglePassword2" style=" margin-left: -1.5rem;"></i>
                                    </span>
                                </div>
                                <label id="password1-error" class="error" for="password1" style="display: none;">Please enter your password </label>
                                <div class="my-2 d-flex justify-content-between align-items-center">
                                    <div class="form-check">
                                        <label class="form-check-label text-muted">
                                            <input type="checkbox" id="remember_admin" class="form-check-input" style="color:black;"> Keep me signed in <i class="input-helper"></i>
                                        </label>
                                    </div>
                                    <a href="forgot_password.php" class="auth-link ">Forgot Password?</a>
                                </div>
                                <div class="form-group">
                                    <button class="btn btn-primary w-100" type="submit">Login</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- /Main Wrapper -->
    <!-- jQuery -->
    <!-- Custom JS -->
    <?php include('commonjs.php') ?>
    <!-- extra js -->
    <?php include_once('extrajs.php') ?>
   
   
    
    <script>
        
        
        /****** Remember Code START ******/
        var remember_email = localStorage.getItem("remember_email");
        var remember_password = localStorage.getItem("remember_password");
        if (remember_email != 'null' && remember_email != null && remember_email != '') {
            if (remember_password != 'null' && remember_password != null && remember_password != '') {
                var email = $('#email').val(remember_email);
                var password = $('#password1').val(remember_password);
                $('#remember_admin').prop('checked', true);
            }
        }

       
    </script>
    <script>
        //---------------------- start for validations ---------------------
        $(document).ready(function() {
            $("#form_validation_login").validate({
                rules: {
                    email: {
                        required: true,
                        // minlength: 3,
                        maxlength: 100,
                        email: true
                    },
                    password: {
                        required: true,
                        minlength: 6,
                        maxlength: 16,
                    },
                },

                messages: {
                    email: {
                        required: msgValidation.emptyEmail[language],
                        // minlength: msgValidation.validMinLength[language],
                        maxlength: msgValidation.validMaxLength[language],
                        email: msgValidation.validEmail[language],
                    },
                    password: {
                        required: msgValidation.emptyPassword[language],
                        minlength: msgValidation.validMinLengthpass[language],
                        maxlength: msgValidation.validMaxlengthpass[language],
                    },
                },
                submitHandler: function(form) {
                   
                    var link = URLAPI + 'auth.php';
                    var data = $(form).serialize();
                    var messsage = msgLoading.blankMessage[language];
                    consoleProvider.log('messsage', messsage);
                    var email = $('#email').val();
                    var password = $('#password1').val();
                    if ($('#remember_admin').is(':checked')) {
                        console.log('checked iff');
                        if (email != '' && password != '') {
                            localStorage.setItem("remember_email", email);
                            localStorage.setItem("remember_password", password);
                        }
                    } else {
                        console.log('checked else');
                        localStorage.removeItem("remember_email");
                        localStorage.removeItem("remember_password");
                    }
                    apiRequest.post(link, data, messsage).then(function(obj) {
                        consoleProvider.log('obj', obj);
                        if (obj.success === true) {
                            var title = msgTitleOperation.loginStatus[language];
                            var user_role =obj.data.role_id_get;
                            var messsage = obj.msg[language];

                            if(user_role == 1){
                                 swal({
                                title: title,
                                text: messsage,
                                icon: "success", //error, success, info
                                button: "Ok",
                            }).then((value) => {
                                window.location.href = "manage_inspection_request.php";
                            });;

                            }else{
                              swal({
                                title: title,
                                text: messsage,
                                icon: "success", //error, success, info
                                button: "Ok",
                            }).then((value) => {
                                window.location.href = "dashboard.php";
                            });;
                            }
                           
                            return false;
                        } else {
                            //alert(obj.msg);
                            var title = msgTitleOperation.loginStatus[language];
                            var messsage = obj.msg[language];
                            swal({
                                title: title,
                                text: messsage,
                                icon: "error", //error, success, info
                                button: "Ok",
                            });
                            return false;
                        }
                    }, function(err) {
                        consoleProvider.log('err', err);
                        var title = msgTitleOperation.errorOnServer[language];
                        var messsage = msgOperations.errorOnServer[language];
                        swal({
                            title: title,
                            text: messsage,
                            icon: "warning", //error, success, info
                            button: "Ok",
                        });
                    });
                }
            });
        });
    </script>
    <script>
        function myfunction1() {
            var x = document.getElementById("password1");
            var y = document.getElementById("togglePassword1");
            var z = document.getElementById("togglePassword2");
            if (x.type === 'password') {
                x.type = "text";
                y.style.display = "none";
                z.style.display = "block";
            } else {
                x.type = "password";
                y.style.display = "block";
                z.style.display = "none";
            }
        }
    </script>
</body>

</html>