<?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>Truck Intransit</h1>
    </section>
    <section class="content">      
		<div class="row">
			<div class="col-lg-12 col-xs-12">
				<form method="post" class="mb-3">
    <div class="form-row">
      <div class="col-md-4 mb-3">
        <label for="TruckNo">Truck No</label>
        <input type="text" class="form-control" id="TruckNo" name="TruckNo" required>
      </div>
      <div class="col-md-4 mb-3">
        <label for="Date">Date</label>
        <input type="date" class="form-control" id="Date" name="Date" required>
      </div>
      <div class="col-md-4 mb-3">
        <label>&nbsp</label>
        <button name="Search" type="submit" class="form-control btn btn-primary">Search</button>
      </div>
    </div>
  </form>
			</div>
			<div class="col-lg-12 col-xs-12">
				
<?php
$xcrud->table('TruckNo');
$xcrud->subselect('Qty','SELECT COUNT(*) FROM truckdetails WHERE TruckNo = {TruckId}');
$xcrud->columns('TruckNo,bill_no,Date,Qty',false);
$xcrud->fields('TruckNo,bill_no,Date',false);
$xcrud->label('TruckNo','Truck No');
$xcrud->change_type('Date','date');
$xcrud->where('Status=','pending');
$xcrud->unset_add();
$xcrud->unset_title();

//$xcrud->button('TruckData.php?truckID={truckID}&type=pending','Add Data','glyphicon glyphicon-plus','',array('target'=>'_self'));
if (in_array('Update-Status', $permissionsArray['PDI'])) {
	$xcrud->button('#','Update Status','glyphicon glyphicon-check','',array('data-toggle'=>'modal','data-target'=>'#updateCompleteStatusModal','onclick'=>'setCompleteDetail("{truckID}")'));
}
if (in_array('Truck-Detail', $permissionsArray['PDI'])) {
	$xcrud->button('#','Truck Detail','glyphicon glyphicon-eye-open','',array('data-toggle'=>'modal','data-target'=>'#truckModal','onclick'=>'viewTruckDetail("{bill_no}")'));
}

if (!in_array('View', $permissionsArray['PDI'])) {
	$xcrud->unset_view();
}
if (!in_array('Edit', $permissionsArray['PDI'])) {
	$xcrud->unset_edit();
}
if (!in_array('Delete', $permissionsArray['PDI'])) {
	$xcrud->unset_remove();
}

if (isset($_POST['Search'])) {
  $truckNo = $_POST['TruckNo'];
  $date = $_POST['Date'];
  //$truckID = $truckNo . $date;
  //$truckID = str_replace('-', '', $truckID);
  $date1=str_replace('-','',date('d-m-Y',strtotime($date)));
  $truckID = $truckNo ."-". $date1;
  
  $xcrud->where('TruckId=',$truckID);
}
				echo $xcrud->render();
			  ?>
			</div>
		</div>
    </section>
</div>

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

<div id="updateCompleteStatusModal" 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 complete</h4>
      </div>
      <div class="modal-body">
         <input type="hidden" id="hidden_truck_id">
		 <button type="button" class="btn btn-success" onclick="updateCompleteStatus()" 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 viewTruckDetail(id){
	dataString='action=viewTruckDetail&bill_no='+id;
	$.ajax({
		type:'POST',
		url:'ajax_data.php',
		data:dataString,
		success:function(data){
			console.log(data);
			$("#truck_detail_div").html(data)
		}
	});
}

function setCompleteDetail(id){
	$("#hidden_truck_id").val(id)
}

function updateCompleteStatus(){
	var truckId=$("#hidden_truck_id").val()
	dataString='action=updateCompleteStatus&truckId='+truckId;
	$.ajax({
		type:'POST',
		url:'ajax_data.php',
		data:dataString,
		success:function(data){
			location.reload();
		}
	});
}
</script>