<?php
    session_start();
    include 'config.php';
    $submit = $_POST['submit'];
    if(isset($submit))
    {
        // var_dump($_POST);die;
        $uname      = $_POST['username'];
        $password   = $_POST['password'];
        $regime_type = $_POST['regime_type'];
        $cargo_channel = $_POST['cargo_channel'];
        if($uname != ' ' || $password !=  ' ' )
        {
            $sql_stmt = "SELECT * FROM user_master where username = '$uname' and password ='$password'"; 
            $result = mysqli_query($con,$sql_stmt);
            
            if(!$result)
            {
                $error = die("Database access failed: " . mysqli_error()); 
            }
            else if($regime_type != -1 )
            {
                $rows = mysqli_num_rows($result); 
                if($rows == 1)
                { 

                    $row = mysqli_fetch_array($result);
                    // $sql = "INSERT INTO activity_log (user_id,company_id, module, login_date_time,public_ip) VALUES (".$row['id'].",".$row['company_code'].", $regime_type,'$date','$public_ip')";
                    // if ($con->query($sql) === TRUE)
                    // {
                        $_SESSION['userid'] = $row['id'];
                        $_SESSION['uname'] = $row['username'];
                        $_SESSION['regime_type'] = $regime_type;
                        $_SESSION['cargo_channel'] = $cargo_channel;
                        $_SESSION['company_code'] = $row['company_code'];
                        header("Location: exceltotext.php");
                    // } 
                    // else
                    // {
                    //     $error =  "Error: " . $sql . "<br>" . $con->error;
                    // }                    
                }
                else
                {
                    $error = "Invalid Credentials, try again!!"; 
                }
            }
            else
            {
                $error = "Please Fill All Fields!!!";   
            }   
        }
        else
        {
            $error = "Please Fill All Fields!!!";   
        }          	
    }
    else
    {
        $error = "Please Login to proceed!!!";   
    }
?>
<!DOCTYPE html>
<html>
    <head>
	<link href="css/style.css" rel="stylesheet">
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <title>Login</title>
        
    </head>

    <body>
	<div class="overlay">
        <div id="login">
            <h3 class="text-center text-white pt-5">Secure Login</h3>
            <div class="container">
                <div id="login-row" class="row justify-content-center align-items-center">
                    <div id="login-column" class="col-md-6">
                        <div id="login-box" class="col-md-12">
                            <form id="login-form" class="form" action="" method="post">
                                <h3 class="text-center text-info">Login</h3>
                                <span  class="text-center" style="color: red;"><?php echo $error; ?></span>
                                <div class="form-group">
                                    <label for="username" class="text-info">Username:</label><br>
                                    <input type="text" name="username" id="username" class="form-control">
                                </div>
                                <div class="form-group">
                                    <label for="password" class="text-info">Password:</label><br>
                                    <input type="password" name="password" id="password" class="form-control">
                                </div>
                                <div class="form-group">
                                    <label for="regime_type" class="text-info">Module:</label><br>
                                    <select name="regime_type" id="regime_type" class="form-control ">
                                        <option value="-1">--Select Module--</option>
                                        <?php
                                            $sql_stmt = "SELECT * FROM regime_master";
                                            $result = mysqli_query($con,$sql_stmt);
                                            while($row = mysqli_fetch_array($result))
                                            {
                                        ?>
                                        <option value="<?php echo $row['id']; ?>"><?php echo $row['type']; ?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label for="cargo_channel" class="text-info">Cargo channel:</label><br>
                                    <select name="cargo_channel" id="cargo_channel" class="form-control ">
                                        <option value="8">COURIER AIR</option>
                                        <option value="7">COURIER LAND</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <!--<label for="remember-me" class="text-info"><span>Remember me</span> <span><input id="remember-me" name="remember-me" type="checkbox"></span></label><br>-->
                                    <input type="submit" name="submit" class="btn btn-info btn-md" value="submit">
                                </div>
                                <div id="register-link" class="text-right">
                                    <!--<a href="#" class="text-info">Register here</a>-->
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
		</div>
    </body>
    <footer class="page-footer font-small blue">
        <div class="footer-copyright text-center py-3">© <?php echo date('Y'); ?> Copyright: m2clearance.com . Powered By
            <a href="http://m2clearance.com/"> m2clearance.com</a>
        </div>
    </footer>
    <script>
    document.onkeydown = function(e)
    {
        if(event.keyCode == 123)
        {
            return false;
        }
        if(e.ctrlKey && e.shiftKey && e.keyCode == 'I'.charCodeAt(0)) 
        {
            return false;
        }
        if(e.ctrlKey && e.shiftKey && e.keyCode == 'C'.charCodeAt(0)) 
        {
            return false;
        }
        if(e.ctrlKey && e.shiftKey && e.keyCode == 'J'.charCodeAt(0)) 
        {
            return false;
        }
        if(e.ctrlKey && e.keyCode == 'U'.charCodeAt(0))
        {
            return false;
        }
    }
    </script>
</html>
