<?php
include 'header.php';

include('xcrud/xcrud.php');
echo Xcrud::load_css();
$bdd=new DB();
?>
<link rel="stylesheet" href="assets/plugins/datepicker/css/datepicker.css">
<div class="page-header">
	<h1>Dashboard</h1>
</div>
<!-- start: PAGE CONTENT -->

<div class="row">
	<div class="col-sm-12">
		<!-- start: RESPONSIVE ICONS BUTTONS PANEL -->
		<div class="panel panel-default">
			<div class="panel-heading">
				<i class="fa fa-external-link-square"></i>
				Customers Status
				<?php //print_r($permissionResult);?>
			</div>
			<div class="panel-body">
				<?php if(isset($nothing)){ ?>
					<h1><?=$nothing?></h1>
				<?php } ?>
				<div class="row">
				<?php if($type=='KYC' || $type=='admin'){  
						if ($type=='admin' || in_array('Add', $userPermissions)) {	
				?>
					<!--<div class="col-sm-4">
						<a href="add-customer.php">
						<button class="btn btn-icon btn-block">
							Add Customer
						</button>
						</a>
					</div>-->
				<?php  }
					if ($type=='admin' || in_array('KYC', $userPermissions)) {	
				?>	
					<!--<div class="col-sm-4">
						<a href="kyc-customer.php">
						<button class="btn btn-icon btn-block">
							KYC Customer
							<span class="badge badge-danger"><?php
							$row=$bdd->getOne("SELECT count(customer_id) as total FROM customer WHERE status='KYC'");
							echo $row['total'];
							?></span>
						</button>
						</a>
					</div>-->
				<?php
					}
				}
				?>
				<?php
				if($type!='restricted_user' && $type!='KYC'){
					
					if ($type=='admin' || in_array('Pending', $userPermissions)){	
				
				?>
					<div class="col-sm-4">
						<a href="customer?status=Pending">
						<button class="btn btn-icon btn-block">
							Pending
							<?php
							$row=$bdd->getOne("SELECT count(customer_id) as total FROM customer WHERE status='pending'");
							//$task_cust_row=$bdd->getAll("SELECT customer_id FROM customer WHERE status='pending' AND created_date < NOW() - INTERVAL 10 MINUTE");
							//$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='pending' AND created_date < NOW() - INTERVAL 10 MINUTE ) AS pending_customer_exists");
							$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='pending' AND created_date < NOW()) AS pending_customer_exists");
							if($task_cust_row['pending_customer_exists']==0 || $row['total']==0)
								$cust_badge='success';
							else
								$cust_badge='danger';
							?>
							<span class="badge badge-<?php echo $cust_badge;?>"> <?php echo $row['total']; ?></span>
						</button>
						</a>
					</div>
				<?php  }
					if ($type=='admin' || in_array('Allotment', $userPermissions)) {	
				?>
					<div class="col-sm-4">
						<a href="customer?status=Allotment">
						<button class="btn btn-icon btn-block">
							Allotment
							<?php
							$row=$bdd->getOne("SELECT count(customer_id) as total FROM customer WHERE status='Allotment'");
							
							//$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='Allotment' AND allotment_time < NOW() - INTERVAL 1 HOUR ) AS pending_customer_exists");
							$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='Allotment' AND allotment_time < NOW()) AS pending_customer_exists");
							if($task_cust_row['pending_customer_exists']==0 || $row['total']==0)
								$cust_badge='success';
							else
								$cust_badge='danger';
							?>
							<span class="badge badge-<?php echo $cust_badge;?>"><?php echo $row['total']; ?></span>
						</button>
						</a>
					</div>
				<?php  }
					if ($type=='admin' || in_array('Proceed', $userPermissions)) {	
				?>
					<div class="col-sm-4">
						<a href="customer?status=Proceed">
						<button class="btn btn-icon btn-block">
							Proceed
							<?php
							$row=$bdd->getOne("SELECT count(customer_id) as total FROM customer WHERE status='Proceed'");
							//$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='Proceed' AND  proceed_time < NOW() - INTERVAL CASE WHEN TIME(proceed_time) < '17:00:00' THEN 1 ELSE 18 END HOUR ) AS pending_customer_exists");
							$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='Proceed' AND  proceed_time < NOW()) AS pending_customer_exists");
							if($task_cust_row['pending_customer_exists']==0 || $row['total']==0)
								$cust_badge='success';
							else
								$cust_badge='danger';
							?>
							<span class="badge badge-<?php echo $cust_badge;?>"><?php echo $row['total']; ?></span>
						</button>
						</a>
					</div>
				<?php  }
					if ($type=='admin' || in_array('Upload', $userPermissions)) {	
				?>
					<div class="col-sm-4">
						<a href="customer?status=Upload">
						<button class="btn btn-icon btn-block">
							Upload
							<?php
							$row=$bdd->getOne("SELECT count(customer_id) as total FROM customer WHERE status='Upload'");
							//$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='Upload' AND upload_time < NOW() - INTERVAL 2 HOUR ) AS pending_customer_exists");
							$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='Upload' AND upload_time < NOW()) AS pending_customer_exists");
							if($task_cust_row['pending_customer_exists']==0 || $row['total']==0)
								$cust_badge='success';
							else
								$cust_badge='danger';
							?>
							<span class="badge badge-<?php echo $cust_badge;?>"><?php echo $row['total']; ?></span>
						</button>
						</a>
					</div>
				<?php  }
					if ($type=='admin' || in_array('Insp', $userPermissions)) {	
				?>
					<div class="col-sm-4">
						<a href="customer?status=Insp">
						<button class="btn btn-icon btn-block">
							Insp/Scan
							<?php
							$row=$bdd->getOne("SELECT count(customer_id) as total FROM customer WHERE status='Insp'");
							//$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='Insp' AND insp_time < NOW() - INTERVAL 1 HOUR ) AS pending_customer_exists");
							$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='Insp' AND insp_time < NOW()) AS pending_customer_exists");
							if($task_cust_row['pending_customer_exists']==0 || $row['total']==0)
								$cust_badge='success';
							else
								$cust_badge='danger';
							?>
							<span class="badge badge-<?php echo $cust_badge;?>"><?php echo $row['total']; ?></span>
						</button>
						</a>
					</div>
				<?php  }
					if ($type=='admin' || in_array('Payment', $userPermissions)) {	
				?>
					<div class="col-sm-4">
						<a href="customer?status=Payment">
						<button class="btn btn-icon btn-block">
							Payment
							<?php
							$row=$bdd->getOne("SELECT count(customer_id) as total FROM customer WHERE status='Payment'");
							//$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='Payment' AND payment_time < NOW() - INTERVAL 4 HOUR ) AS pending_customer_exists");
							$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='Payment' AND payment_time < NOW()) AS pending_customer_exists");
							if($task_cust_row['pending_customer_exists']==0 || $row['total']==0)
								$cust_badge='success';
							else
								$cust_badge='danger';
							?>
							<span class="badge badge-<?php echo $cust_badge;?>"><?php echo $row['total']; ?></span>
						</button>
						</a>
					</div>
					<?php } } ?>
					<?php if($type!='KYC'){ 
						
						if ($type=='admin' || in_array('Semi-Successfull', $userPermissions)) {	
					
						?>
					<div class="col-sm-4">
						<a href="customer?status=SemiSuccessfull">
						<button class="btn btn-icon btn-block">
							Semi Successfull
							<?php
							$row=$bdd->getOne("SELECT count(customer_id) as total FROM customer WHERE status='SemiSuccessfull'");
							//$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='SemiSuccessfull' AND semi_successfull_time < NOW() - INTERVAL 1 HOUR ) AS pending_customer_exists");
							$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='SemiSuccessfull' AND semi_successfull_time < NOW()) AS pending_customer_exists");
							if($task_cust_row['pending_customer_exists']==0 || $row['total']==0)
								$cust_badge='success';
							else
								$cust_badge='danger';
							?>
							<span class="badge badge-<?php echo $cust_badge;?>"><?php echo $row['total']; ?></span>
						</button>
						</a>
					</div>
				<?php  }
					if ($type=='admin' || in_array('HSRP', $userPermissions)) {	
				?>
					<div class="col-sm-4">
						<a href="customer?status=hsrp">
						<button class="btn btn-icon btn-block">
							HSRP
							<?php
							$row=$bdd->getOne("SELECT count(customer_id) as total FROM customer WHERE status='hsrp'");
							//$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='hsrp' AND hsrp_time < NOW() - INTERVAL 24 HOUR ) AS pending_customer_exists");
							$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='hsrp' AND hsrp_time < NOW()) AS pending_customer_exists");
							if($task_cust_row['pending_customer_exists']==0 || $row['total']==0)
								$cust_badge='success';
							else
								$cust_badge='danger';
							?>
							<span class="badge badge-<?php echo $cust_badge;?>"><?php echo $row['total']; ?></span>
						</button>
						</a>
					</div>
				<?php  }
					if ($type=='admin' || in_array('Fitment', $userPermissions)) {	
				?>
					<div class="col-sm-4">
						<a href="customer?status=Fitment">
						<button class="btn btn-icon btn-block">
							Fitment
							<?php
							$row=$bdd->getOne("SELECT count(customer_id) as total FROM customer WHERE status='Fitment'");
							//$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='Fitment' AND fitment_time < NOW() - INTERVAL 18 HOUR ) AS pending_customer_exists");
							$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='Fitment' AND fitment_time < NOW()) AS pending_customer_exists");
							if($task_cust_row['pending_customer_exists']==0 || $row['total']==0)
								$cust_badge='success';
							else
								$cust_badge='danger';
							?>
							<span class="badge badge-<?php echo $cust_badge;?>"><?php echo $row['total']; ?></span>
						</button>
						</a>
					</div>
				<?php  }
					if ($type=='admin' || in_array('Ready-to-go', $userPermissions)) {	
				?>
					<div class="col-sm-4">
						<a href="customer?status=Ready">
						<button class="btn btn-icon btn-block">
							Ready to go
							<?php
							$row=$bdd->getOne("SELECT count(customer_id) as total FROM customer WHERE status='Ready'");
							//$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='Ready' AND ready_time < NOW() - INTERVAL 2 HOUR ) AS pending_customer_exists");
							$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='Ready' AND ready_time < NOW()) AS pending_customer_exists");
							if($task_cust_row['pending_customer_exists']==0 || $row['total']==0)
								$cust_badge='success';
							else
								$cust_badge='danger';
							?>
							<span class="badge badge-<?php echo $cust_badge;?>"><?php echo $row['total']; ?></span>
						</button>
						</a>
					</div>
				<?php  }
					if ($type=='admin' || in_array('RC', $userPermissions)) {	
				?>
					<div class="col-sm-4">
						<a href="customer?status=RC">
						<button class="btn btn-icon btn-block">
							RC
							<?php
							$row=$bdd->getOne("SELECT count(customer_id) as total FROM customer WHERE status='RC'");
							//$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='RC' AND rc_time < NOW() - INTERVAL 4 HOUR ) AS pending_customer_exists");
							$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='RC' AND rc_time < NOW()) AS pending_customer_exists");
							if($task_cust_row['pending_customer_exists']==0 || $row['total']==0)
								$cust_badge='success';
							else
								$cust_badge='danger';
							?>
							<span class="badge badge-<?php echo $cust_badge;?>"><?php echo $row['total']; ?></span>
						</button>
						</a>
					</div>
				<?php  }
					if ($type=='admin' || in_array('Register', $userPermissions)) {	
				?>
					<div class="col-sm-4">
						<a href="customer?status=Register">
						<button class="btn btn-icon btn-block">
							Register
							<?php
							$row=$bdd->getOne("SELECT count(customer_id) as total FROM customer WHERE status='Register'");
							//$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='Register' AND register_time < NOW() - INTERVAL 1 WEEK ) AS pending_customer_exists");
							$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='Register' AND register_time < NOW()) AS pending_customer_exists");
							if($task_cust_row['pending_customer_exists']==0 || $row['total']==0)
								$cust_badge='success';
							else
								$cust_badge='danger';
							?>
							<span class="badge badge-<?php echo $cust_badge;?>"><?php echo $row['total']; ?></span>
						</button>
						</a>
					</div>
					<?php  }
					if ($type=='admin' || in_array('IssueDocs', $userPermissions)) {	
					?>
					<div class="col-sm-4">
						<a href="customer?status=IssueDocs">
						<button class="btn btn-icon btn-block">
							Issue Docs
							<?php
							$row=$bdd->getOne("SELECT count(customer_id) as total FROM customer WHERE status='IssueDocs'");
							//$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='IssueDocs' AND issue_docs_time < NOW() - INTERVAL 1 WEEK ) AS pending_customer_exists");
							$task_cust_row=$bdd->getOne("SELECT EXISTS ( SELECT 1 FROM customer WHERE status='IssueDocs' AND issue_docs_time < NOW()) AS pending_customer_exists");
							if($task_cust_row['pending_customer_exists']==0 || $row['total']==0)
								$cust_badge='success';
							else
								$cust_badge='danger';
							?>
							<span class="badge badge-<?php echo $cust_badge;?>"><?php echo $row['total']; ?></span>
						</button>
						</a>
					</div>

				<?php  }
					if ($type=='admin' || in_array('Full-Successfull', $userPermissions)) {	
				?>
					<div class="col-sm-4">
						<a href="customer?status=FullSuccessfull">
						<button class="btn btn-icon btn-block">
							Full Successfull
							<span class="badge" style="background:#3baf2b;"> <?php
							
							$row=$bdd->getOne("SELECT count(customer_id) as total FROM customer WHERE status='FullSuccessfull'");
							echo $row['total'];
							?> </span>
						</button>
						</a>
					</div>
				<?php
					}
				}
				if($type!='restricted_user' && $type!="KYC"){
					
					if ($type=='admin' || in_array('Update-Registration-No', $userPermissions)) {	
				
				?>
					<div class="col-sm-4">
						<a href="update-registration-no">
						<button class="btn btn-icon btn-block">
							Update Registration No
						</button>
						</a>
					</div>
				<?php } } ?>
				</div>
			</div>
		</div>
		<!-- end: RESPONSIVE ICONS BUTTONS PANEL -->
	</div>
</div>
<?php
if($type!="KYC"){
?>
<div class="col-sm-12">
	<div class="panel-body">
		<div class="alert alert-info">
			<h4 align="center">Search Customer</h4>
		</div>
	</div>
	
	<form method="post">
	<div class="col-sm-3">
		<input type="text" placeholder="Customer Name" name="cust_name" class="form-control" onkeyup="search_customer_record(this.value)">
	</div>
	<div class="col-sm-3">
		<input type="text" placeholder="Contact No" name="cust_name" class="form-control" onkeyup="search_customer_contactNO_record(this.value)">
	</div>
	<div class="col-sm-3">
		<input type="text" placeholder="Chassis No." name="cust_chassis_no" class="form-control" onkeyup="search_customer_chassisNo_record(this.value)">
	</div>
	<div class="col-sm-3">
		<input type="text" placeholder="Registration No." value="GJ-" name="cust_register_no" class="form-control" onkeyup="search_customer_registerNo_record(this.value)">
	</div>
	<!--<div class="col-sm-2">
		<input type="text" class="datepicker">
	</div>
	<div class="col-sm-2">
		<input type="submit" value="Search" class="btn btn-info" name="cust_submit">
	</div> -->
	
	</form>
</div>
<?php
}
?>
<div class="col-sm-12" id="cust_result" style="padding-top:10px">
</div>
<!-- end: PAGE CONTENT-->
</div>
</div>
<!-- end: PAGE -->
</div>
<!-- end: MAIN CONTAINER -->		
<?php
$bdd->disconnect();

include 'footer.php';
echo Xcrud::load_js();
?>

<!-- start: JAVASCRIPTS REQUIRED FOR THIS PAGE ONLY -->
<!--<script src="assets/plugins/bootstrap-datepicker/js/bootstrap-datepicker.js"></script> -->
<!-- end: JAVASCRIPTS REQUIRED FOR THIS PAGE ONLY -->
<script>
//FormElements.init();

/*$('.datepicker').datepicker({
    format: 'mm-dd-yyyy'
});*/
function search_customer_record(value){
	var dataString='action=search_customer_record&customer_name='+value;
	$.ajax({
		url:'ajax_data.php',
		data:dataString,
		type:'POST',
		success:function(data){
			$("#cust_result").html(data);
		}
	});
}

function search_customer_contactNO_record(value){
	var dataString='action=search_customer_contactNO_record&contact_no='+value;
	$.ajax({
		url:'ajax_data.php',
		data:dataString,
		type:'POST',
		success:function(data){
			$("#cust_result").html(data);
		}
	});
}

function search_customer_chassisNo_record(value){
	var dataString='action=search_customer_chassisNo_record&chassis_no='+value;
	$.ajax({
		url:'ajax_data.php',
		data:dataString,
		type:'POST',
		success:function(data){
			$("#cust_result").html(data);
		}
	});
}

function search_customer_registerNo_record(value){
	var dataString='action=search_customer_registerNo_record&registration_no='+value;
	$.ajax({
		url:'ajax_data.php',
		data:dataString,
		type:'POST',
		success:function(data){
			$("#cust_result").html(data);
		}
	});
}
</script>