<?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>Completed Trucks</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} AND select_status IS NULL');
$xcrud->columns('TruckNo,bill_no,Date,Qty,Status,completed_date,show_status',false);
$xcrud->fields('TruckNo,bill_no,Date',false);
$xcrud->change_type('Date','date');
$xcrud->order_by('Date','desc');
$xcrud->change_type('completed_date','date');
$xcrud->label('show_status','Approved');
$xcrud->label('TruckNo','Truck No');
$xcrud->where('Status=','completed');
$xcrud->unset_title();

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

if (in_array('Truck-Data', $permissionsArray['Completed'])) {
	$xcrud->button('TruckData.php?bill_no={bill_no}&type=complete','Truck Data','glyphicon glyphicon-eye-open','',array('target'=>'_self'));
}
if (in_array('Approved', $permissionsArray['Completed'])) {
	//$xcrud->button('TruckData.php?truckID={truckID}','Approved','glyphicon glyphicon-check','',array('target'=>'_self'));
	$xcrud->button('#','Approved','glyphicon glyphicon-check','',array('data-toggle'=>'modal','data-target'=>'#approvedModal','onclick'=>'setTruckId("{bill_no}")'),array('show_status','!=','ok'));
}

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="approvedModal" 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">Are your sure?</h4>
      </div>
      <div class="modal-body">
         <form method="post">
			<input class="form-control" type="text" id="sub_bill_no" name="sub_bill_no" readonly>
			<button type="submit" name="updateChassisStatus" class="btn btn-info" style="margin-top:5px">Submit</button>
		</form> 
      </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();
if (isset($_POST['updateChassisStatus'])) {
	$bill_no = $_POST['sub_bill_no'];

	if (!empty($bill_no)) {
		//$updateQuery = "UPDATE truckno SET show_status = 'ok',Status='approved' WHERE TruckId = '$editTruckId'";
		$updateQuery = "UPDATE truckno SET show_status = 'ok',Status='approved' WHERE bill_no = '$bill_no'";
		if (mysqli_query($conn, $updateQuery)) {
			$updateQueryStatus = "UPDATE truckdetails SET select_status='approved' WHERE bill_no = '$bill_no' AND select_status IS NULL";
			mysqli_query($conn, $updateQueryStatus);
			echo "<script>alert('Truck chassis status has been updated.')</script>";
			//echo "<script>window.location.href = 'Completed.php';</script>";
			//exit();
		}else{
		  echo "Error: " . mysqli_error($conn);
		}
	}
}
?>
<script>
function setTruckId(id){
	$('#sub_bill_no').val(id);
}
</script>