<?php 
include("../config.php");
$current_date = date('Y-m-d');
if(isset($_GET['emp_id']) && !empty($_GET['emp_id'])) {
	$emp_id = strtoupper($_GET['emp_id']);
}

if(isset($_GET['type']) && !empty($_GET['type'])) {
	$type = strtoupper($_GET['type']);
}

if(isset($_GET['vendor_id']) && !empty($_GET['vendor_id'])) {
	$vendor_id = strtoupper($_GET['vendor_id']);
}

$sd = mysqli_real_escape_string($conn, date('Y-m-d', strtotime($_GET['t_date'])));
if(!empty($vendor_id) && !empty($type)){
	if($type == 'CONSIGNEE'){
		$query = mysqli_query($conn, "SELECT * FROM `lr_mst` WHERE l_date <= '".$sd."' AND consignee_name != '' AND consignee_name = '".$vendor_id."' AND `tracking_status` != 'C' AND branch_id = '".$_GET['branch_id']."' AND company_id = '".$_GET['company_id']."'");
	}else{
		$query = mysqli_query($conn, "SELECT * FROM `lr_mst` WHERE l_date <= '".$sd."' AND consignor_name != '' AND consignor_name = '".$vendor_id."' AND `tracking_status` != 'C' AND branch_id = '".$_GET['branch_id']."' AND company_id = '".$_GET['company_id']."'");
	}

}else if(!empty($vendor_id) && !empty($type) && !empty($emp_id)){
	if($type == 'CONSIGNEE'){
		$query = mysqli_query($conn, "SELECT * FROM `lr_mst` WHERE l_date <= '".$sd."' AND consignee_name != '' AND consignee_name = '".$vendor_id."' AND `tracking_status` != 'C' AND branch_id = '".$_GET['branch_id']."' AND company_id = '".$_GET['company_id']."'");
	}else{
		$query = mysqli_query($conn, "SELECT * FROM `lr_mst` WHERE l_date <= '".$sd."' AND consignor_name != '' AND consignor_name = '".$vendor_id."' AND `tracking_status` != 'C' AND branch_id = '".$_GET['branch_id']."' AND company_id = '".$_GET['company_id']."'");
	}

}else if(!empty($emp_id)){
	$query = mysqli_query($conn, "SELECT * FROM `lr_mst` WHERE l_date <= '".$sd."' AND consignor_name != '' AND consignee_name != '' AND emp_id = '".$emp_id."' AND `tracking_status` != 'C' AND branch_id = '".$_GET['branch_id']."' AND company_id = '".$_GET['company_id']."'");

}else{
	$query = mysqli_query($conn, "SELECT * FROM `lr_mst` WHERE l_date <= '".$sd."' AND consignor_name != '' AND consignee_name != '' AND `tracking_status` != 'C' AND branch_id = '".$_GET['branch_id']."' AND company_id = '".$_GET['company_id']."'");
}

$total = mysqli_num_rows($query);

$master_vendor  = combine(getAllRecords('vendor_master'), 'id', 'name');

$tracking = "SELECT lr_no FROM tracking WHERE t_date = '".$sd."' AND branch_id = '".$_GET['branch_id']."' AND company_id = '".$_GET['company_id']."'";
$res_tracking = mysqli_query($conn, $tracking);
$today_lr_no = array();
while ($row_tracking = mysqli_fetch_array($res_tracking)) {
	$today_lr_no[] = $row_tracking['lr_no'];
}

$table       	= '';
$total_count    = 0;
if(count($total) > 0) {
	$total_fright = 0;
	$i = 0;
	while ($row = mysqli_fetch_array($query)) {
		/*if(in_array($row['lr_no'], $today_lr_no)) {
			continue;
		}*/

		$table .= '<tr class="text-center">';
		$table .= '<td>'.$row["lr_no"].'
		<input type="hidden" name="lrdata['.$i.'][lr_no]" value="'.$row['lr_no'].'">
		</td>';

		$table .= '<td>'.$row["truck_no"].'
		<input type="hidden" name="lrdata['.$i.'][truck_no]" value="'.$row['truck_no'].'">
		</td>';

		$table .= '<td>'.@$master_vendor[$row['consignee_name']].'</td>';
		$table .= '<td>'.@$master_vendor[$row['consignor_name']].'</td>';

		$table .= '<td>
		<input type="time" name="lrdata['.$i.'][time]" class="form-control add-more-table-tracking" autocomplete="off">
		</td>';

		$table .= '<td>'.$row['from_place'].'</td>';
		$table .= '<td>'.$row['to_place'].'</td>';
		$table .= '<td>'.$row['truck_driver_no'].'</td>';
		
		$table .= '<td>
		<input type="text" name="lrdata['.$i.'][location]" class="form-control add-more-table-tracking" autocomplete="off">
		</td>';
		$table .= '<td>
		<select class="form-control select2 add-more-table-tracking chosen-select" name="lrdata['.$i.'][tracking_status]">
          <option value></option>
          <option value="P">PENDING</option>
          <option value="C">CLOSING</option>
        </select>
		</td>';
		
		
		$table .= '</tr>';
		$i++;
	}
	
}
$data = array('status' => 'success', 'total_count' => $total, 'tbody' => $table);
	echo json_encode($data);
    die();
?>