<file_sep><?php
include 'config.php';
include 'user.php';
$response=array();
$user = new User($pdo);
$list = $user->check_login_details($_POST["email"],$_POST["password"]);
 if($list)
 {
 	$response['success']=$list;
 	$response['msg']="Valid user";
 }
 else
 {
 	$response['success']=0;
 	$response['msg']="invalid user";
 }
echo json_encode($response);
?><file_sep><?php
class User {
    /* Properties */
    private $conn;

    /* Get database access */
    public function __construct(\PDO $pdo) {
        $this->conn = $pdo;
    }

    /* List all users */
    public function get_users() {
     
        $stm = $this->conn->prepare("SELECT username, password FROM users");
        $stm->execute();
        return $data = $stm->fetchAll();
    }


    public function get_user_details() {
     
        $stm = $this->conn->prepare("SELECT * FROM users");
        $stm->execute();
        return $data = $stm->fetchAll();
    }

     public function getUserDropdownDetails() {
     
        $stm = $this->conn->prepare("SELECT username,id FROM users WHERE id!=1");
        $stm->execute();
        return $data = $stm->fetchAll();
    }

    public function check_login_details($email,$password) {
    $stm = $this->conn->prepare("SELECT id FROM users WHERE email=:email AND password=:password ");
    $stm->execute(['email'=>$email,'password'=>$password]);
    return $data = $stm->fetchAll();
    }

    public function save_user_details($username,$password,$mobile,$email,$address) {
    $stm = $this->conn->prepare("INSERT INTO users (username,password,mobile,email,address) VALUES ( :username, :password, :mobile, :email, :address)");
    $data=$stm->execute([':username'=>$username,':password'=>$password,':mobile'=>$mobile,':email'=>$email,':address'=>$address]);
    return $data;
    }
     
     public function save_logistics_details($user_id,$unitcost,$quantity,$total) {
    $stm = $this->conn->prepare("INSERT INTO logistics_details (user_id,unitcost,qunatity,total_amount) VALUES ( :user_id, :unitcost, :quantity, :total_amount)");
    $data=$stm->execute([':user_id'=>$user_id,':unitcost'=>$unitcost,':quantity'=>$quantity,':total_amount'=>$total]);
    return $data;
    }


 public function save_update_logistics_details($user_id,$unitcost,$quantity,$total,$id) {
    $stm = $this->conn->prepare("UPDATE logistics_details SET user_id = :user_id,unitcost = :unitcost,qunatity = :qunatity,total_amount = :total_amount WHERE id = :id");
    $data=$stm->execute([':user_id'=>$user_id,':unitcost'=>$unitcost,':qunatity'=>$quantity,':total_amount'=>$total,':id'=>$id]);
    return $data;
    }

    public function get_logistics_info_details($user_id) {
    $stm = $this->conn->prepare("SELECT ld.*,u.username,u.is_allowed FROM logistics_details AS ld LEFT JOIN users AS u ON ld.user_id=u.id WHERE ld.user_id=$user_id");
    $stm->execute();
    return $data = $stm->fetchAll();
    }

    public function get_all_logistics_info_details() {
    $stm = $this->conn->prepare("SELECT ld.*,u.username,u.is_allowed FROM logistics_details AS ld LEFT JOIN users AS u ON ld.user_id=u.id");
    $stm->execute();
    return $data = $stm->fetchAll();
    }


    public function get_logistics_info_based_on_id($id) {
    $stm = $this->conn->prepare("SELECT ld.*,u.username,u.is_allowed FROM logistics_details AS ld LEFT JOIN users AS u ON ld.user_id=u.id WHERE ld.id=$id");
    $stm->execute();
    return $data = $stm->fetchAll();
    }

   

     public function update_allow_status($id,$is_allowed) {
    $stm = $this->conn->prepare("UPDATE users SET is_allowed = :is_allowed WHERE id = :id");
    $data=$stm->execute([':is_allowed'=>$is_allowed,':id'=>$id]);
    return $data;
    }

    public function delete_logistics_details($id) {
    $stm = $this->conn->prepare("DELETE FROM logistics_details WHERE id = :id");
    $data=$stm->execute([':id'=>$id]);
    return $data;
    }
    
}

?><file_sep><!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="assets/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="jquery.cookie.js"></script>
<style>
body {font-family: Arial, Helvetica, sans-serif;}
form {border: 3px solid #f1f1f1;}

input[type=text], input[type=password] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
box-sizing: border-box;
}

button {
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
cursor: pointer;
width: 100%;
}

button:hover {
opacity: 0.8;
}

.imgcontainer {
text-align: center;
margin: 24px 0 12px 0;
}

img.avatar {
width: 40%;
border-radius: 50%;
}

.container {
padding: 16px;
}

span.psw {
float: right;
padding-top: 16px;
}

/* Change styles for span and cancel button on extra small screens */
@media screen and (max-width: 300px) {
span.psw {
display: block;
float: none;
}

}
</style>

<script>
$(document).ready(function(){
$(".login_details").click(function(){
var email=	$("#email").val().trim();
var password=	$("#password").val().trim();

email_length=email.length
password_length=password.length

var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
valid_email= emailReg.test(email);

if(valid_email==false){

alert("Please enter valid email id")
$("#email").val("")

return
}
if(email=="" || email_length==0){

alert("Please enter email id")
$("#email").val("")
return

}
if(password== "" || password_length==0)
{
alert("please enter Password")
return false
}
$.ajax({
url: 'login.php',
type: 'post',
data: "email=" + email + "&password="+ password,
success: function(response){
response = JSON.parse(response);

if(response.success!=0 || response.success!="")
{
$.cookie("user_id", response.success[0].id);
if(response.success[0].id==1)
{
	window.location.href="dashboard.php";
}else{
	window.location.href="getLogisticInfoBasedOnUser.php";
}

}else{
alert("Invalid email and password")
location.reload()
}



}
});

});


$(".signup").click(function(){

window.location.href="add_users_details.php";


});

});
</script>
</head>
<body>

<div class="imgcontainer">
<h4>Login Form</h4>
</div>

<div class="container">
<label for="email"><b>Email</b></label>
<input type="text" placeholder="Enter email" id="email" required>

<label for="password"><b>Password</b></label>
<input type="password" placeholder="Enter Password" id="password" required>
<p>&nbsp;</p>
<button type="button" class="btn btn-success login_details">Login</button>
<button type="button" class="btn btn-primary signup">Sign up</button>
</div>






</body>
</html><file_sep><?php
include 'config.php';
include 'user.php';
$response=array();
$user = new User($pdo);
$is_save = $user->save_user_details($_POST["username"],$_POST["password"],$_POST["mobile"],$_POST["email"],$_POST["address"]);
if($is_save)
{
$response['success']=1;
$response['msg']="User details saved successfully";
}
else
{
$response['success']=0;
$response['msg']="Failed to saved user details";
}
echo json_encode($response);
?><file_sep><?php
include 'config.php';
include 'user.php';
$response=array();
$user = new User($pdo);
$list = $user->getUserDropdownDetails();


echo json_encode($list);
?><file_sep>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="assets/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="jquery.cookie.js"></script>

<script>

$(document).ready(function(){


user_id= $.cookie("user_id");


if(user_id===undefined)
{
	window.location.href="index.php";
}


$.ajax({
url: 'get_logistics_info_details.php',
type: 'post',
data:'user_id='+user_id,
success: function(response){
response = JSON.parse(response);

total_data=response.length
var row="";
var allowed="";
is_allowed=response[0].is_allowed;

if(is_allowed==1)
{
allowed=allowed+'<a href="add_logistics_details.php?is_user=1" class="btn btn-primary pull-right">Add Logistic</a>';
}else if(is_allowed==0){
allowed=allowed+'<a href="" class="btn btn-primary pull-right" onclick="abc()">Not Alloed to Add Logistic</a>';
}

for(i=0;i<total_data;i++){

j=i+1;


row=row+'<tr><td>'+j+'</td><td>'+response[i].username+'</td><td>'+response[i].unitcost+'</td><td>'+response[i].qunatity+'</td><td>'+response[i].total_amount+'</td><td><input type="button" onclick="update_logistics('+response[i].id+')" style="background-color:lightgreen" value="update"></td><td><input type="button" onclick="delete_logistics('+response[i].id+')" style="background-color:red" value="Delete"></td></tr>';

}



$(".logistics_details").html(row);
$(".allowed").html(allowed);

}
});

$(".logout").click(function(){
$.removeCookie('user_id');

window.location.href="index.php";


});

});

</script>
<script>
	function abc()
	{
		alert("Admin is not given permission to add logistics")
	}

	function update_logistics(id)
	{
            window.location.href="update_logistics_info.php?id="+id;
	}
	function delete_logistics(id)
	{
	var is_delete=confirm("Do you want to delete logistics info")
	if(is_delete)
	{
	$.ajax({
	url: 'delete_logistics_details.php',
	type: 'post',
	data: "id=" + id,
	success: function(response){

	alert("logistics details deleted successfully")
	location.reload();

	}
	});
	}


}
</script>

</head>
<body>

<div class="container">
<p>&nbsp;</p>

<div class="pull-right">
<button class="btn btn-danger logout" href="">Log Out</button> 
</div>
<p>&nbsp;</p>
<h2>Logistics Details</h2> 
<a class="allowed"></a>
<p>&nbsp;</p>


<table class="table">
<thead>
<tr>
<th>Sno.</th>
<th>User Name</th>
<th>Unit Cost</th>
<th>Quantity</th>
<th>Total</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tbody class="logistics_details">

</tbody>
</table>
</div>

</body>
</html>
<?php


?><file_sep><?php

$a=array(1,2,3,4,5,6,9);
print_r($a);
echo "Hello";


$a=array(1,2,3,4,5,6,9);
print_r($a);
echo "Hello";

$a=array(1,2,3,4,5,6,9);
print_r($a);
echo "Hello";

$a=array(1,2,3,4,5,6,9);
print_r($a);
echo "Hello";

$a=array(1,2,3,4,5,6,9);
print_r($a);
echo "Hello";


$a=array(1,2,3,4,5,6,9);
print_r($a);
echo "Hello";
?>
<file_sep><?php
include 'config.php';
include 'user.php';
$response=array();
$user = new User($pdo);
$list = $user->delete_logistics_details($_POST["id"]);
 if($list)
 {
 	$response['success']=1;
 	$response['msg']="updated";
 }
 else
 {
 	$response['success']=0;
 	$response['msg']="Failed to update";
 }
echo json_encode($response);


?><file_sep>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="assets/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="jquery.cookie.js"></script>

<script>

$(document).ready(function(){

user_id=$.cookie("user_id");

if(user_id===undefined)
{
	window.location.href="index.php";
}

$.ajax({
url: 'get_user_details.php',
type: 'post',

success: function(response){
response = JSON.parse(response);
total_data=response.length
var row="";
for(i=0;i<total_data;i++){

j=i+1;


row=row+'<tr><td>'+j+'</td><td>'+response[i].username+'</td><td>'+response[i].mobile+'</td><td>'+response[i].email+'</td><td>'+response[i].address+'</td>';

if(response[i].is_allowed==1)
{
	row=row+'<td><input type="button" onclick="is_allowed('+response[i].id+','+response[i].is_allowed+')" value="Allowed"> </td>';
}else if(response[i].is_allowed==0)
{
	row=row+'<td><input type="button" onclick="is_allowed('+response[i].id+','+response[i].is_allowed+')" value="Not Allowed"> </td>'
}

row=row+'</tr>';

}



$(".user_details").html(row)

}
});

$(".logout").click(function(){
$.removeCookie('user_id');

window.location.href="index.php";


});

});

</script>
<script>
function is_allowed(id, allowed_status)
{


var is_allowed=0

if(allowed_status==1)
{
is_allowed=0;
}else if(allowed_status==0){
is_allowed=1;
}

var status_confirm=confirm("Do you want to revoked access ?")

if(status_confirm)
{

$.ajax({
url: 'update_allow_status.php',
type: 'post',
data: "id=" + id + "&is_allowed=" +  is_allowed,
success: function(response){

if(response.success!=0 || response.success!="")
{
if(is_allowed==1)
{
alert("Access allowed")
}else if(is_allowed==0){
alert("Access has been revoked")
}

location.reload();

}else{
alert("Failed to allowed Access")
location.reload()
}

}
});

}else{
location.reload();
}



}
</script>
</head>
<body>

<div class="container">
<p>&nbsp;</p>

<div class="pull-right">
<button class="btn btn-danger logout" href="">Log Out</button> 
</div>
<p>&nbsp;</p>
<h2>User Details</h2> 
<a href="get_all_logistics_details.php" class="btn btn-primary pull-center">View Logistic Details</a>
<a href="add_logistics_details.php" class="btn btn-primary pull-right">Add Logistic</a>
<p>&nbsp;</p>


<table class="table">
<thead>
<tr>
<th>Sno.</th>
<th>User Name</th>
<th>Mobile</th>
<th>Email</th>
<th>Address</th>
<th>Is Allowed</th>
</tr>
</thead>
<tbody class="user_details">

</tbody>
</table>
</div>

</body>
</html>
<?php


?><file_sep><?php
include 'config.php';
include 'user.php';
$response=array();
$user = new User($pdo);
$is_save = $user->save_update_logistics_details($_POST["user_id"],$_POST["unitcost"],$_POST["quantity"],$_POST["total"],$_POST["id"]);
if($is_save)
{
$response['success']=1;
$response['msg']="User details saved successfully";
}
else
{
$response['success']=0;
$response['msg']="Failed to saved user details";
}
echo json_encode($response);
?>