<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
require_once 'database.php';
$msg="";
if(isset($_GET['message']))
{
$msg = $_GET['message'];
$success = 1;
}
class index extends database
{
    public function login($data)
    {
		$where['username'] = $data['username'];
		$user = $this->fetchRow('user', $where);

        if(password_verify($data['password'], $user['password'])) 
		{

                    if($user['status'] ==1)
                    {
		/*if($user['user_type']=='admin')
			{
				$_SESSION['id']=$user['id'];
               $_SESSION['username']=$user['username'];
				$_SESSION['user_type']=$user['user_type'];
				
				return 1;
				exit;
			}
			else if($user['user_type']=='general')
			{ */
				$_SESSION['id']=$user['id'];
				$_SESSION['user_type']=$user['user_type'];
				$_SESSION['username']=$user['username'];
              
				if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
					$ip = $_SERVER['HTTP_CLIENT_IP'];
				} else {
					$ip = $_SERVER['REMOTE_ADDR'];
				}
				$data1['ip']=$ip;
				$data1['user_id']=$user['id'];
				$data1['login_time']=date('Y-m-d H:i:s');
				$in=$this->insert('user_log', $data1);
				return 2;
				exit;
			/*} */
			return 0;
			exit;
                    }
                    return 0;
                    exit;
		}
    }
}
if(isset($_POST['data']))
{
$msg="";
$success = 0;
	$data=$_POST['data'];
	if(trim($data['username']) == '')
	{
		$msg.= "Username is required<br/>";
	}
	if(trim($data['password']) == '')
	{
		$msg.= "Passowrd is required<br/>";
	}
	
	if($msg=="")
	{
		$index = new index();
		$loggedInUser = $index->login($data);
		
		if($loggedInUser)
		{
                    if($loggedInUser ==1)
                    {
			header('Location: orders.php');
				exit;
                    }
                  header('Location: index.php');
                    exit;
		}
	}
	$msg = 'Login Invalid';
}

?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Login</title>
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="css/new-style.css">
</head>
<body id="login">
<div class="container-fluid acp-header">
	<div class="container">
    	<div class="row">
        	<div class="col-md-12">
            	<div class="acp-logo">
                	<img src="images/logo.png" class="img-responsive" width="120"/>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="login-wrap">
	<div class="login-box">
	
    	<h2>Login</h2>
        <div class="login-box-iner">
		<?php if($msg !=''){echo '<span class="alert alert-danger">'.$msg.'</span>';}?>
        	<form method="post">
            	<label>Username</label>
                <input name="data[username]" type="text"  />
                <label>Password</label>
                <input name="data[password]" type="password" />
                <button type="submit">Login</button>
            </form>
        </div>
    </div>
</div>



<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="js/function.js"></script>


</body>
</html>
