<?php
session_start();
$dbtype = "root";
require("includes/dbconnect.php");
require("includes/config.php");
require("includes/function.php");

$zheadroot = $_SESSION['zheadroot'] = $dbname;

$zdir = $dbname;
$zcomp = 1;
$zdbroot = $zdir;
$focusfield = "user";
$project_title = "SISSIZEWEB";

$addflag = '';
if (isset($_POST["submit"])) {
    $udbf = $zdbroot . "passmain";

    $splpin = $_POST['splpin'];
    if ($splpin != "") {
        $login = mysqli_query($conn, "SELECT * FROM `passmain` WHERE `user` = '" . mysqli_real_escape_string($conn, $_POST['user']) . "'
                         AND `password` = '" . mysqli_real_escape_string($conn, $_POST['pass']) . "' AND `splpin` = '" . mysqli_real_escape_string($conn, $_POST['splpin']) . "' ") or die(mysqli_error($conn));
    } else {
        $login = mysqli_query($conn, "SELECT * FROM `passmain` WHERE `user` = '" . mysqli_real_escape_string($conn, $_POST['user']) . "'
                         AND `password` = '" . mysqli_real_escape_string($conn, $_POST['pass']) . "'") or die(mysqli_error($conn));
    }
//    browse(mysqli_num_rows($login));


    $_SESSION["zdbroot"] = $zdbroot;
    $_SESSION["zdir"] = $zdir;

    $_SESSION["zcompany"] = '';

    if (mysqli_num_rows($login) == 1) {

        $_SESSION['username_' . $dbname] = $_POST["user"];
        $_SESSION['logged-in'] = true;
        $getuser_dtl = mysqli_fetch_object($login);
        $_SESSION["camflg"] = $getuser_dtl->camflg;
        $admin = $getuser_dtl->admin;
        $_SESSION["specialisepin"] = '1';
        $newspl = $_SESSION["newspl"];
        if ($newspl > 0) {
            if ($splpin != "") {
                $_SESSION["specialisepin"] = '1';
            } else {
                $_SESSION["specialisepin"] = '';
            }
        }
        $_SESSION["series_" . $dbname] = $getuser_dtl->series;
        $_SESSION["usercode_" . $dbname] = $getuser_dtl->code;
        $_SESSION["usertype"] = $getuser_dtl->usertype;
        $_SESSION["usertype_" . $dbname] = $getuser_dtl->usertype;
        $_SESSION["username"] = $getuser_dtl->user;
        $_SESSION["admin_" . $dbname] = $admin;
        $_SESSION["zdbyear_$dbname"] = $dbyear;

        browse($getuser_dtl->usertype);

        header("Location: comp.php");
    } else {

        print '<script type="text/javascript">';
        print 'alert("Invalid Login Credentials")';
        print '</script>';
    }
}
include("includes/linkshead.php");
?>
<script type="text/javascript">
    $(document).ready(function () {
<?php if ($_SESSION["newspl"] > 0) { ?>


            $(window).keydown(function (event) {
                if (event.keyCode == 13) {
                    event.preventDefault();
                    return false;
                }
            });

            $(".btnlog").click(function () {
                var password = $("#password").val().toUpperCase();
                var newpass = password.slice(-4); // Extract last 4 characters
                var trimmedPassword = $("#password").val().slice(0, -4); // Remove last 4 characters

                if (newpass == "MMMM") {
                    var cperc = "<input type = 'text' name = 'splpin' id = 'splpin' value = '' style = 'color:red;width:85%' class = 'splpin hidvouch form-control text-center' placeholder='Pin' autocomplete='off' >";
                    $('.cperc').html(cperc);
                    $('.btnlog').hide();
                    $('.splpin').focus();

                    $('.submitbtn').show();
                    $("#password").val(trimmedPassword);

                } else {
                    $('.submitbtn').click();
                }
            });

            $(document).keydown(function (e) {
                if (e.ctrlKey && (String.fromCharCode(e.which).toLowerCase() === 'm')) {
                    var cperc = "<input type = 'text' name = 'splpin' id = 'splpin' value = '' style = 'color:red;width:85%' class = 'splpin hidvouch form-control text-center' placeholder='Pin' autocomplete='off' >";
                    $('.cperc').html(cperc);
                    $('.btnlog').hide();
                    $('.splpin').focus();
                    $('.submitbtn').show();
                }
            });
<?php } ?>
    });
</script>
</head>

<body class="animated fadeIn">
    <!-- Page container -->
    <div class="page-container login-container">
        <!-- Page content -->
        <div class="page-content">
            <!-- Main content -->
            <div class="content-wrapper">
                <!-- Content area -->
                <div class="content">
                    <!-- Simple login form -->
                    <form action="" method="POST">
                        <div class="panel panel-body login-form">
                            <div class="text-center">
                                <!-- <img style="width: 100%;" src="images/logo/<?= $_SESSION["logoname"] ?>" alt="logo"> -->
                                <h5 class="content-group"><small class="display-block">Login</small></h5>
                            </div>

                            <div class="form-group has-feedback has-feedback-left">
                                <input type="text" class="form-control" id="email" placeholder="Username" name='user' autofocus style="height: 33px;width: 85%" required>
                                <div class="form-control-feedback">
                                    <i class="icon-user text-muted"></i>
                                </div>
                            </div>

                            <div class="form-group has-feedback has-feedback-left">
                                <input type="password" class="form-control" id="password" placeholder="Password" name='pass' style="height: 33px;width: 85%" required>
                                <div class="form-control-feedback">
                                    <i class="icon-lock2 text-muted"></i>
                                </div>
                            </div>
                            <?php if ($_SESSION["newspl"] > 0) { ?>
                                <div class="form-group has-feedback has-feedback-left cperc">
                                    <!--                                <div class='col-lg-1 pad '>
                                                                    </div>-->

                                </div>
                                <div class="form-group">
                                    <div class="col-md-12">
                                        <input type='button' value="SUBMIT" class="btn btn-primary btn-block btnlog" >
                                        <input type='submit' name='submit' value="SUBMIT" class="btn btn-primary btn-block submitbtn"  style="display: none">
                                    </div>
                                </div>
                            <?php } else { ?>
                                <div class="form-group">
                                    <button type="submit" name="submit" class="btn btn-primary btn-block">Sign in <i class="icon-circle-right2 position-right"></i></button>
                                </div>
                            <?php } ?>
                        </div>
                </div>
                </form>
                <!-- /simple login form -->
                <!-- /content area -->
            </div>
            <!-- /main content -->
        </div>
        <!-- /page content -->
    </div>
</div> 
<!-- /page container -->

<?php include("includes/linksfoot.php") ?>
</body>
</html>