<?php
 require_once('../../../includes/load.php');

 $convert_utf8 = function ($s) {
         if (!mb_check_encoding($s, 'UTF-8')) {
             $s = utf8_encode($s);
         }
         return $s;
     };
$Placa=$_REQUEST['Placa'];
$FechaInicio=$_REQUEST['FechaInicio'];
$FechaFin=$_REQUEST['FechaFin'];
$selectPlaca=$_REQUEST['selectPlaca'];

if ($Placa == 0 && $selectPlaca == 2) {

	$sql= $db->query("SELECT FECH_REG,ID_VEHICULO,ID_USER,MOTIVO_TRASLADO,AREA,DETALLE_CARGA,UBI_INICIO,UBI_LLEGADA,KM_INICIAL,KM_FINAL,KM_PROMEDIO,(KM_FINAL - KM_INICIAL)AS KM_TOTAL FROM det_veh WHERE FECH_REG BETWEEN '$FechaInicio' AND '$FechaFin' ORDER BY FECH_REG DESC"); 

} else{
    if ($selectPlaca == 1){
       $sql= $db->query("SELECT FECH_REG,ID_VEHICULO,ID_USER,MOTIVO_TRASLADO,AREA,DETALLE_CARGA,UBI_INICIO,UBI_LLEGADA,KM_INICIAL,KM_FINAL,KM_PROMEDIO,(KM_FINAL - KM_INICIAL)AS KM_TOTAL FROM det_veh WHERE FECH_REG BETWEEN '$FechaInicio' AND '$FechaFin' AND ID_VEHICULO = '$Placa' ORDER BY FECH_REG DESC"); 
    } else{
        return false;
    }

}


$data = array();
while( $rows = mysqli_fetch_assoc($sql) ) {
	$data[] = array_map($convert_utf8, $rows);
}
$results = array(
	"sEcho" => 1,
"iTotalRecords" => count($data),
"iTotalDisplayRecords" => count($data),
  "aaData"=>$data);
echo json_encode($results);
?>
