<?php
date_default_timezone_set('America/Bogota');
include('../config/database.php');
$metodo=$_SERVER['REQUEST_METHOD'];

if($metodo=='GET'){
    if(isset($_GET['inicio'])){
        //Guias Pendientes
        $query_ped = mysqli_query($mysqli, "SELECT COUNT(*) Total FROM guias WHERE estado_guia=200 or estado_guia=300");
        $rowped = mysqli_fetch_array($query_ped);
        $pend = $rowped['Total'];
        
        //Guias Entregados
        $query_ent = mysqli_query($mysqli, "SELECT COUNT(*) Total FROM guias WHERE estado_guia=400");
        $rowent = mysqli_fetch_array($query_ent);
        $ent = $rowent['Total'];
        
        //Guias Entregados Parcial
        $query_entp = mysqli_query($mysqli, "SELECT COUNT(*) Total FROM guias WHERE estado_guia=500");
        $rowentp = mysqli_fetch_array($query_entp);
        $entp = $rowentp['Total'];
        
        //Guias No Entregados
        $query_nent = mysqli_query($mysqli, "SELECT COUNT(*) Total FROM guias WHERE estado_guia=600");
        $rownent = mysqli_fetch_array($query_nent);
        $nent = $rownent['Total'];
        
        
        
        $json = array(); 
        $json[] = array(
            'pendientes' => $pend,
            'entregados' => $ent,
            'noentregados' => $nent,
            'parcial' => $entp,
            'total' => ($pend+$ent+$nent+$entp)
        );
        
        $jsonstring = json_encode($json[0]);
        echo $jsonstring; 
    }
    
    if(isset($_GET['page3'])){
        $fec_desde = $_GET['fd'].' 00:00:00';
        $fec_hasta = $_GET['fh'].' 23:59:59';
       
        $query = mysqli_query($mysqli, "SELECT * FROM guias g, estados e WHERE e.codigo_estado=g.estado_guia AND fecha_registro BETWEEN '$fec_desde' AND '$fec_hasta'");
        if(!$query){
            die('Error en la consulta'. mysqli_error($mysqli));
        }
        $json = array(); 
        while($row = mysqli_fetch_array($query)){
            $guia = "<a href='#' onclick='detalles($row[id_guia])' data-toggle='modal' data-target='#detalle_guia'>$row[codigo_guia]</a>";
            if($row['usuario_asignado']!=''){ $u=$row['usuario_asignado'];
                $query_v = mysqli_query($mysqli,"SELECT * FROM usuarios WHERE username='$u'");
                $d=mysqli_fetch_array($query_v); $vehiculo = $d['vehiculo'];
            }else{$vehiculo='';}
            
            $json[] = array(
                'guia' => $guia,
                'estado' => $row['label'],
                'vehiculo' => $vehiculo,
                'cliente' => $row['nombre_cliente'],
                'telefono' => $row['telefono_cliente'],
                'fecha' => $row['fecha_registro'],
                'id' => $row['id_guia']
            );
        }
        $jsonstring = json_encode($json);
        echo $jsonstring;
    }
}