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

$zdir = $dbname;
$zcomp = 1;
$zdbroot = $zdir;
$focusfield = "user";
if (isset($_POST["submit"])) {
    $udbf = $zdbroot . "passmain";
    $myip = $_SERVER['REMOTE_ADDR'];

    $loginpin = $_POST["loginpin1"] . $_POST["loginpin2"] . $_POST["loginpin3"] . $_POST["loginpin4"];

    $login = mysqli_query($conn, "SELECT * FROM `passmain` WHERE `usertype` = '1' AND loginpin= '" . mysqli_real_escape_string($conn, $loginpin) . "'") or die(mysqli_error($conn));


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

    $_SESSION["zcompany"] = '';
    $zroot1 = substr($zroot, 0, -4);

    if (mysqli_num_rows($login) == 1) {
        $getuser_dtl = mysqli_fetch_object($login);

        $_SESSION['username_' . $dbname] = $getuser_dtl->user;

        $_SESSION['logged-in'] = true;
        $usercode = $getuser_dtl->code;
        $admin = $getuser_dtl->admin;
        $usertype = $getuser_dtl->usertype;
        $_SESSION["password_" . $dbname] = $getuser_dtl->password;
        $_SESSION["series_" . $dbname] = $getuser_dtl->series;

        $_SESSION["admin_" . $dbname] = $admin;
        $_SESSION["usertype_" . $dbname] = $usertype;
        $_SESSION["spltype_" . $dbname] = $getuser_dtl->spltype;
        $_SESSION["zdbyear_$dbname"] = $dbyear;
        $spltype = $getuser_dtl->spltype;
        if ($usertype > 1) {
            $getcom = mysqli_query($conn, "Select * from `usercomp` where username = '$usercode' order by `lastdate` desc limit 0,1 ") or die(mysqli_error($conn));
            $fetcom = mysqli_fetch_array($getcom);
            $cono = $fetcom['cono'];
            $year = $fetcom['finyear'];
            $_SESSION["comp_" . $dbname] = $cono;
            $_SESSION[$myyear] = $year;

            $_SESSION["zdbyear_" . $dbname] = $zroot1 . $year;

            $_SESSION["tit_" . $dbname] = $year;
//            browse($_SESSION["zdbyear_" . $dbname]);

            if ($spltype == "1") {
                header("Location: menuapp.php");
            } else {
                header("Location: menu.php");
            }
        } else {
            $getcom = mysqli_query($conn, "Select * from `compyear` order by `lastdate` desc limit 0,1 ") or die(mysqli_error($conn));
            $fetcom = mysqli_fetch_array($getcom);
            $cono = $fetcom['cono'];
            $year = $fetcom['finyear'];
            $_SESSION["comp_" . $dbname] = $cono;
            $_SESSION[$myyear] = $year;

            $_SESSION["zdbyear_" . $dbname] = $zroot1 .  $year;

            $_SESSION["tit_" . $dbname] = $year;

            header("Location: menuapp.php");
//            header("Location: comp.php");
        }
    } else {
        if ($ipfail) {
//            browse($myip . "  this Ip Is Blocked");
            browse("This Ip Is Blocked");
        } else {
            browse("Invalid Login Credentials");
        }
    }
}
include("linkshead.php");
?>
<style>
    input[type = text]{
        border-right: 0px;
        border-left: 0px;
        border-top: 0px;
        text-align: center;
    }
</style>
</head>
<script src="index.js" type="text/javascript"></script>
<body class="animated fadeIn">
    <section id="login-container">

        <div class="row">
            <div class="col-lg-3 col-md-3 col-sm-9 col-xs-9 " id="login-wrapper">
                <div class="panel panel-primary animated flipInY">
                    <div class="panel-heading">
                        <h3 class="panel-title">     
                            <?php Echo $project_title; ?>
                        </h3>      
                    </div>
                    <div class="panel-body">
                        <p>Login Pin provided by Supervisor to access.</p>
                        <form class="form-horizontal" action='' method="POST">
                            <div class="form-group">
                                <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                                    <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3" >
                                        <input type="number" class="form-control loginpin" name='loginpin1' style="height: 35px;font-size: 20px" minlength="1" maxlength="1" autofocus>
                                    </div>
                                    <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3" >
                                        <input type="number" class="form-control loginpin" name='loginpin2' style="height: 35px;font-size: 20px" minlength="1" maxlength="1">
                                    </div>
                                    <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3" >
                                        <input type="number" class="form-control loginpin" name='loginpin3' style="height: 35px;font-size: 20px" minlength="1" maxlength="1">
                                    </div>
                                    <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3" >
                                        <input type="number" class="form-control loginpin" name='loginpin4' style="height: 35px;font-size: 20px" minlength="1" maxlength="1">
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                                    <input type='submit' name='submit' value="SUBMIT" class="btn btn-primary btn-block" >
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

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