<?php
	session_start();
	include('../../includes/config.php');
	require_once("../../includes/dbcontroller.php");
	require_once("../../includes/perpage.php");
	if(!isset($_SESSION['uid']) && empty($_SESSION['uid'])){
		header("location:$website_url/");
		exit();
	}
	$role= $_SESSION['role'];
	
	$db_handle = new DBController();
	//SELECT CAST(DeliveryDate AS DATE) FROM `deliveries`;
	//$trucks_query="SELECT * FROM deliveries";
	
	
	//print_r($deliveries_data);
	$truck_fields='';
	$delievery_date=date("Y-m-d");
	$err_msg='';
	$search_data='';
	$search_truckid='';
	if((!empty($_GET["deldate"]))&&(!empty($_GET["deltruckid"]))){
		$_POST["search"]["deliverydate"]= $_GET["deldate"];
		$_POST["search"]["truckName"]= $_GET["deltruckid"];
	}
	if(!empty($_POST["search"])) {
		$search_date=$_POST["search"]["deliverydate"];
		$delievery_date=$search_date;
		$search_truckid=$_POST["search"]["truckName"];
		if((isset($search_date) && !empty($search_date)) && (isset($search_truckid) && !empty($search_truckid))){
			$search_query="SELECT * FROM deliveries WHERE  DATE(DeliveryDate)='".$search_date."' and TruckID='".$search_truckid."'  ORDER BY LastUpdated DESC";
			$search_data = $db_handle->runQuery($search_query);
		}
		else{
			$err_msg="Please select Delivery Date and Truck / Route";
		}
	}
	if(!empty($_POST["search"]["deliverydate"])) {
		$deliveries_query="SELECT * FROM deliveries WHERE  DATE(DeliveryDate)='".$search_date."'";
	}
	else{
		$deliveries_query="SELECT TruckID FROM deliveries WHERE DATE(DeliveryDate) = CURDATE()";
	}
	$deliveries_data = $db_handle->runQuery($deliveries_query);
	if(!empty($deliveries_data)) {
		$deliveries_data = array_column($deliveries_data, 'TruckID');
		$truck_ids=implode(",",$deliveries_data);
		$get_relative_truck_query='select Truck_id,TruckName from trucks where Truck_id in ('.$truck_ids.')';
		$get_relative_truck_data = $db_handle->runQuery($get_relative_truck_query);
		$truck_fields=array_column($get_relative_truck_data, 'Truck_id', 'TruckName');
	}
	
	//print_r($truck_fields);
	include('../../includes/header.php');
	include('../../includes/menu.php');
?>

<div id="form-grid">      
	<form name="frmSearch" method="post" action="<?php echo $website_url;?><?php echo $deliveries_url ?>">
		<span style="color:red;" >
		<?php
		echo $err_msg;
		?>
		</span>
		<div class="search-box">
			<div class="col-lg-6 col-xs-12 col-sm-6  col-md-6 outer-cell">
				<div class="inner-cell">
					<div class="frm-item">
						<label for="deliverydate">Delivery Date</label> 
						<input type="text" name="search[deliverydate]" id="sdeliverydate" class="demoInputBox datepicker" value="<?php echo $delievery_date; ?>"/>
					</div>
				</div>
			</div>
			<div class="clearfix"></div>
			<div class="col-lg-6 col-xs-12 col-sm-6  col-md-6 outer-cell">
				<div class="inner-cell">
					<div class="frm-item">
						<label for="truckName">Truck / Route</label>  
						<select name="search[truckName]" id="struckName" class="demoInputBox">
							<option value="">Please Select Truck</option>
						  <?php foreach($truck_fields as $truck_fields_key=>$truck_fields_value) { ?>
					<option value="<?php echo $truck_fields_value; ?>" <?php echo (($search_truckid == $truck_fields_value) ? 'selected' : '') ?>><?php echo $truck_fields_key; ?></option>
					<?php } ?>
						</select>
					</div>
				</div>
			</div>
			<input type="submit" name="go" class="btnSearch" value="Get Deliveries">
		</div>
		
		<?php if(!empty($search_data)) { ?>
			<table class="table table-striped test-data-del">
				<thead>
					<tr>
						<th width="10%" class="text-center"><strong>DeliveryID</strong></th>
						<th width="40%" class="text-center"><strong>Address</strong></th>          
						<th width="20%" class="text-center"><strong>DeliveryDate</strong></th>
						<th width="25%" class="text-center"><strong>Actions</strong></th>
					</tr>
				</thead>
				<tbody>
				<?php
					foreach($search_data as $k=>$v) {
					?>
					<tr>
						<td class="text-center"><?php echo $search_data[$k]["DeliveryID"]; ?></td>
						<td class="text-center"><?php echo $search_data[$k]["Address"]; ?></td>
						<td class="text-center"><?php echo $search_data[$k]["DeliveryDate"]; ?></td>
						<td class="text-center">
							<a href="<?php echo $website_url.$edit_delievery_url; ?>?id=<?php echo $search_data[$k]["DeliveryID"]; ?>&deldate=<?php echo $delievery_date; ?>&deltruckid=<?php echo $search_truckid; ?>" class="link delete-link"><i class="fa fa-trash" aria-hidden="true"></i>Edit</a>
							
						</td> 
					</tr>
					<?php
					   }
					if(isset($result["perpage"])) {
					?>
					<tr>
					<td colspan="12" align=center> <?php echo $result["perpage"]; ?></td>
					</tr>
					<?php } ?>
				</tbody>
			</table>
		<?php } ?>
	</form>	
	
</div>
<?php
	include('../../includes/webfooter.php');
	include('../../includes/footer.php');
?>