<?php 
include 'head.php';
include('xcrud/xcrud.php');
$xcrud = Xcrud::get_instance();
echo Xcrud::load_css(); 
include 'nav.php'; 
?>
<div class="content-wrapper">
	<section class="content-header">
		<h1><?php if($_GET['type']=="approved"){ echo "Approved Data";}else if($_GET['type']=="selected"){ echo "Selected Data";}?></h1>
    </section>
    <section class="content">
		<div class="row">
		
			<div class="col-lg-12 col-xs-12">
			  <?php
				$xcrud->table('truckdetails');
				//$xcrud->relation('TruckNo','truckno','TruckId','TruckId');
				//$xcrud->join('TruckNo','truckno','TruckId');
				$xcrud->columns('truckdetails.TruckNo,truckdetails.bill_no,truckdetails.ChassisNo,truckdetails.ChassisImg,truckdetails.CylNo,truckdetails.KitNo,truckdetails.color,truckdetails.product');
				$xcrud->fields('CylNo,Cyl_Mfg,KitNo,Kit_Mfg,select_status');
				$xcrud->relation('Cyl_Mfg','cyl_mfg','name','name');
				$xcrud->relation('Kit_Mfg','kit_mfg','name','name');
				$xcrud->label('truckdetails.TruckNo','Truck No');
				$xcrud->label('truckdetails.ChassisNo','Chassis No');
				$xcrud->label('truckdetails.ChassisImg','Chassis Img');
				$xcrud->label('truckdetails.CylNo','Cyl No');
				$xcrud->label('truckdetails.KitNo','Kit No');
				$xcrud->change_type("select_status","select","approved","approved,selected");
				//$xcrud->change_type("truckno.Status","select","approved","approved,completed,selected");
				$xcrud->change_type('ChassisImg','image','',array('path'=>'/upload'));
				//$xcrud->where('truckno.show_status','ok');
				if($_GET['type']=="approved"){
					$xcrud->where("truckdetails.select_status='approved'");
					//$xcrud->where("truckno.show_status='ok' AND truckdetails.select_status IS NULL");
					
					if (!in_array('View', $permissionsArray['Approved'])) {
					$xcrud->unset_view();
					}
					if (!in_array('Edit', $permissionsArray['Approved'])) {
						$xcrud->unset_edit();
					}
					if (!in_array('Delete', $permissionsArray['Approved'])) {
						$xcrud->unset_remove();
					}
				}else if($_GET['type']=="selected"){
					//$xcrud->where("truckno.show_status='ok' AND truckdetails.select_status ='selected'");
					$xcrud->where('truckdetails.select_status','selected');
					
					if (in_array('Customer-Detail', $permissionsArray['Selected'])) {
						$xcrud->button('#','Customer Detail','glyphicon glyphicon-user','',array('data-toggle'=>'modal','data-target'=>'#customerDetailModal','onclick'=>'viewCustomerDetail("{truckdetails.ChassisNo}")'));
					}
					if (in_array('Revert-Status', $permissionsArray['Selected'])) {
						$xcrud->button('javascript:;','Revert Status','fa fa-mail-reply','',array('data-toggle'=>'modal','data-target'=>'#revertStatsuModal','onclick'=>'revertStatus({Id})'));
					}
					
					if (!in_array('View', $permissionsArray['Selected'])) {
					$xcrud->unset_view();
					}
					if (!in_array('Edit', $permissionsArray['Selected'])) {
						$xcrud->unset_edit();
					}
					if (!in_array('Delete', $permissionsArray['Selected'])) {
						$xcrud->unset_remove();
					}
				}
				if($_SESSION['ticl_user_type']=="user"){
					$xcrud->unset_edit();
				}
				
				$xcrud->unset_title();
				$xcrud->unset_add();
				
				echo $xcrud->render();
			  ?>
			</div>
		</div>
    </section>
</div>

<div id="customerDetailModal" class="modal fade" role="dialog">
  <div class="modal-dialog modal-sm">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Customer Detail</h4>
      </div>
      <div class="modal-body">
         <div id="customer_detail_div">
		 </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

<!-- Revert Status Modal -->
<div id="revertStatsuModal" class="modal fade" role="dialog">
  <div class="modal-dialog">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Do you really want to revert status.</h4>
      </div>
      <div class="modal-body">
		<form method="post">
			<input type="hidden" id="hidden_turckdetail_id" name="hidden_turckdetail_id">
			<button type="submit" name="submit_revert_status" class="btn btn-info" style="margin-top:5px">Submit</button>
		</form>
      </div>
    </div>
  </div>
</div>
<?php
if(isset($_POST['submit_revert_status'])){
	$bdd=new DB();
	$values=array();
	$bdd->execute("UPDATE truckdetails SET select_status='approved' WHERE Id='".$_POST['hidden_turckdetail_id']."'",$values);
	$crow=$bdd->getOne('SELECT ChassisNo FROM truckdetails WHERE Id='.$_POST['hidden_turckdetail_id'],$values);
	$bdd->disconnect();
	
	//update revert status in to customer databae
	$url= web_constant::$ip_scanner."/scanner/api.php";
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query(array('type' => 'revertSelectStatus','ChassisNo'=>$crow['ChassisNo'])));
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$output = curl_exec($ch);
	//echo "url:".$url.",output:".$output;
	curl_close($ch);
	
	echo "<script>window.location='approved?type=selected'</script>";
}

include 'footer.php';
echo Xcrud::load_js();
?>
<script>
function viewCustomerDetail(chassisNo){
	dataString='action=viewCustomerDetail&chassisNo='+chassisNo;
	$.ajax({
		type:'POST',
		url:'ajax_data.php',
		data:dataString,
		success:function(data){
			console.log(data);
			$("#customer_detail_div").html(data)
		}
	});
}
function revertStatus(id){
	$("#hidden_turckdetail_id").val(id);
}
</script>