<?php 
include 'head.php';
include('xcrud/xcrud.php');
$xcrud = Xcrud::get_instance();
echo Xcrud::load_css(); 
include 'nav.php';

$x=1;
$bill_no=$_GET['bill_no'];
//$TruckNo = substr($truckId, 0, -8);

$totalRecordsQuery = "SELECT COUNT(*) as total FROM truckdetails WHERE bill_no = '$bill_no'";
$totalRecordsResult = mysqli_query($conn, $totalRecordsQuery);
$totalRecords = mysqli_fetch_assoc($totalRecordsResult)['total']; 
?>
<div class="content-wrapper">
	<section class="content-header">
		
		<h1>Truck Details of Bill No: <?php echo $bill_no; if(isset($_GET['type']) && $_GET['type']=="pending"){  echo " [ ".$totalRecords . " Record Required ]"; } ?></h1>
    </section>
    <section class="content">
		<div class="row">
		
			<div class="col-lg-12 col-xs-12">
			  <?php
				$xcrud->table('truckdetails');
				$xcrud->columns('TruckNo,ChassisNo,ChassisImg,CylNo,KitNo,EngineNo,color,product');
				$xcrud->fields('CylNo,Cyl_Mfg,KitNo,Kit_Mfg');
				$xcrud->relation('Cyl_Mfg','cyl_mfg','name','name');
				$xcrud->relation('Kit_Mfg','kit_mfg','name','name');
				$xcrud->change_type('ChassisImg','image','',array('path'=>'../upload'));
				$xcrud->modal('ChassisImg');
				//$xcrud->change_type('ChassisImg', 'image', false, array('width' => 450,'path' => '../upload','thumbs' => array(array('height' => 55,'width' => 120,'crop' => true,'marker' => '_th'))));
				$xcrud->label('TruckNo','Truck No');
				$xcrud->label('ChassisNo','Chassis No');
				$xcrud->label('ChassisImg','Chassis Img');
				$xcrud->label('CylNo','Cyl No');
				$xcrud->label('KitNo','Kit No');
				$xcrud->label('EngineNo','Engine No');
				$xcrud->where('bill_no',$bill_no);
				//$xcrud->where("select_status != 'approved' OR select_status IS null");
				if($_GET['type']=='complete'){
					$xcrud->where("select_status IS null");
				}
				$xcrud->button('#','Update Status','glyphicon glyphicon-check','',array('data-toggle'=>'modal','data-target'=>'#updateApprovedStatusModal','onclick'=>'setApproveId("{id}")'));
				$xcrud->unset_title();
				$xcrud->unset_add();
				$xcrud->unset_remove();
				echo $xcrud->render();
			  ?>
			</div>
		</div>
    </section>
</div>

<div id="updateApprovedStatusModal" 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">Update status to approved</h4>
      </div>
      <div class="modal-body">
         <input type="hidden" id="hidden_truck_id">
		 <button type="button" class="btn btn-success" onclick="updateApprovedStatus()" data-dismiss="modal">YES</button>
		 <button type="button" class="btn btn-danger" style="float: right;" data-dismiss="modal">No</button>
		 </div>
      </div>
      <!--<div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>-->
    </div>
  </div>
</div>
<?php
include 'footer.php';
echo Xcrud::load_js();
?>
<script>
function setApproveId(id){
	$("#hidden_truck_id").val(id)
}

function updateApprovedStatus(){
	var id=$("#hidden_truck_id").val()
	dataString='action=updateApprovedStatus&id='+id;
	$.ajax({
		type:'POST',
		url:'ajax_data.php',
		data:dataString,
		success:function(response){
			//alert(data);
			value=JSON.parse(response);
			if(value.status=="redirect")
				window.location.href = "Completed.php";
			else
				location.reload();
				
		}
	});
}
</script>