<?php
session_start();
include '../conexion.php';


$reporte = $_POST['reporte'] ?? null;
$fechaini = $_POST['fechaini'] ?? null;
$fechafin = $_POST['fechafin'] ?? null;
$usados = $_POST['usados'] ?? '99';
$detallado = $_POST['detallado'] ?? '99';
$datox= $_POST['datox'] ?? null;
$pagina= $_POST['pagina'] ?? 1;
$response = array();
$valores=null;
if ($reporte === "empleado") {
    $valores .= '
    <div class="table-container" style="width:100%;heigth:100%">
                    <table border=\'0\' style=\'width:100%;font-size:8pt;\'>
                        <thead>
                            <tr>
                                <th style="width:5%;">id</th>
                                <th style="width:10%;">Fecha</th>
                                <th style="width:15%;">Cantidad</th>
                                <th style="width:25%;">Descripcion</th>
                                <th style="width:25;">Devuelto</th>
                                <th style="width:20%;">Fecha Devolución</th>
                            </tr>
                        </thead>
                        <tbody>
                        ';
                        $busq = filter_input(INPUT_GET, 'busqueda', FILTER_SANITIZE_STRING) ?: '';
                        $pagina = filter_input(INPUT_GET, 'pagina', FILTER_VALIDATE_INT) ?: 1;

                        $por_pagina = 15;
                        $desde = ($pagina - 1) * $por_pagina;

                        // Total registros
                        $query_total = "SELECT COUNT(*) as total_registros FROM datos_entregas where num_emp=? and uni_tipomov_idtipoMovUni='4' and fechaMov between ? and ?";
                        $stmt = $connection->prepare($query_total);
                        //$likeBusq = "%{$busq}%";
                        $stmt->bind_param("sss", $datox,$fechaini,$fechafin);
                        $stmt->execute();
                        $result = $stmt->get_result();
                        $total_registros = $result->fetch_assoc()['total_registros'];

                        $total_paginas = ceil($total_registros / $por_pagina);

                        // Consulta principal
                        //$query = "SELECT * FROM datos_traspasos WHERE medida like ? OR tipoUniforme LIKE ? ORDER BY orden ASC LIMIT ?, ?";
                        $query = "SELECT * FROM datos_entregas where num_emp=? and uni_tipomov_idtipoMovUni='4' and fechaMov between ? and ? ORDER BY fechaMov DESC,idmovimientosUni DESC LIMIT ?, ?";
                        $stmt = $connection->prepare($query);
                        $stmt->bind_param("sssii", $datox,$fechaini,$fechafin,$desde, $por_pagina);
                        $stmt->execute();
                        $result = $stmt->get_result();

                        if ($result->num_rows > 0) {
                            while ($data = $result->fetch_assoc()) {
                                $dev=($data['dev'] == 1) ? "SI" : "NO";
                                $valores .= "<tr>
                                        <td style='text-align:left;padding-left:20px'>{$data['idmovimientosUni']}</td>
                                        <td style='text-align:left;padding-left:20px'>{$data['fechaMov']}</td>
                                        <td style='text-align:left;padding-left:20px'>{$data['cant']}</td>
                                        <td style='text-align:left;padding-left:20px'>" . htmlspecialchars($data['descrip'], ENT_QUOTES, 'UTF-8') . "</td>
                                        <td style='text-align:left;padding-left:20px'>" . $dev. "</td>
                                        <td style='text-align:left;padding-left:20px'>{$data['fechaDev']}</td>   
                                        </td>
                                    </tr>";
                            }
                        }else{
                            $valores .= "<tr>
                                        <td colspan='6' style='text-align:center;padding-left:20px;color:red'>Sin datos que mostrar</td>
                                        </tr>";
                        }
                        $valores.='

                            <!-- Agrega más filas según sea necesario -->
                        </tbody>
                    </table>
                </div>
                <div class="paginador">
                    <ul>';
                        $numeroInicio=1;
                        if($pagina!=1){
                        
                        $valores .= '<li><a href="?pagina=<?php echo $numeroInicio?>&busqueda=<?php echo $busq?>"><i class="fa-solid fa-backward-fast"></i></a></li>';
                        
                        }
                            if(($pagina-3)>1){
                                $numeroInicio=$pagina-3;
                            }
                            $numeroFin=$numeroInicio+4;
                            
                            if($numeroFin>$total_paginas){
                                $numeroFin=$total_paginas;
                            }
                            
                            for($i=$numeroInicio;$i<=$numeroFin;$i++){
                                if($i==$pagina){
                                    $valores .= '<li class="pageSelected">'.$i.'</li>';
                                } else{
                                    $valores .= '<li><a href="?pagina='.$i.'">'.$i.'</a></li>';
                                }
                            }
                            
                            if($pagina!=$total_paginas){
                                if($pagina==0){
                                    $valores.="";
                                }else {
                                    $valores .= '<li><a href="?pagina='.$total_paginas.'"><i class="fa-solid fa-forward-fast"></i></a></li>';
                                }
                                
                            } 
                   $valores .='
                    </ul>
                </div>
                ';

   
        $response['success'] = true;
        $response['valores'] = $valores;

} else if ($reporte === "supervisor") {
    
    $valores .= '
    <div class="table-container" style="width:100%;heigth:100%">
                    <table border=\'0\' style=\'width:100%;font-size:8pt;\'>
                        <thead>
                            <tr>
                                <th style="width:5%;">id</th>
                                <th style="width:10%;">Fecha</th>
                                <th style="width:25%;">Empleado</th>
                                <th style="width:10%;">Cantidad</th>
                                <th style="width:25%;">Descripcion</th>
                                <th ">Devuelto</th>
                                <th style="width:20%;">Fecha Devolución</th>
                            </tr>
                        </thead>
                        <tbody>
                        ';
                        $busq = filter_input(INPUT_GET, 'busqueda', FILTER_SANITIZE_STRING) ?: '';
                        $pagina = filter_input(INPUT_GET, 'pagina', FILTER_VALIDATE_INT) ?: 1;

                        $por_pagina = 15;
                        $desde = ($pagina - 1) * $por_pagina;

                        // Total registros
                        $query_total = "SELECT COUNT(*) as total_registros FROM datos_entregas where supervisor=? and uni_tipomov_idtipoMovUni='4' and fechaMov between ? and ?";
                        $stmt = $connection->prepare($query_total);
                        //$likeBusq = "%{$busq}%";
                        $stmt->bind_param("sss", $datox,$fechaini,$fechafin);
                        $stmt->execute();
                        $result = $stmt->get_result();
                        $total_registros = $result->fetch_assoc()['total_registros'];

                        $total_paginas = ceil($total_registros / $por_pagina);

                        // Consulta principal
                        //$query = "SELECT * FROM datos_traspasos WHERE medida like ? OR tipoUniforme LIKE ? ORDER BY orden ASC LIMIT ?, ?";
                        $query = "SELECT * FROM datos_entregas where supervisor=? and uni_tipomov_idtipoMovUni='4' and fechaMov between ? and ? ORDER BY fechaMov DESC,idmovimientosUni DESC LIMIT ?, ?";
                        $stmt = $connection->prepare($query);
                        $stmt->bind_param("sssii", $datox,$fechaini,$fechafin,$desde, $por_pagina);
                        $stmt->execute();
                        $result = $stmt->get_result();

                        if ($result->num_rows > 0) {
                            while ($data = $result->fetch_assoc()) {
                                $dev=($data['dev'] == 1) ? "SI" : "NO";
                                $valores .= "<tr>
                                        <td style='text-align:left;padding-left:20px'>{$data['idmovimientosUni']}</td>
                                        <td style='text-align:left;padding-left:20px'>{$data['fechaMov']}</td>
                                        <td style='text-align:left;padding-left:20px'>{$data['dato1']}</td>
                                        <td style='text-align:left;padding-left:20px'>{$data['cant']}</td>
                                        <td style='text-align:left;padding-left:20px'>" . htmlspecialchars($data['descrip'], ENT_QUOTES, 'UTF-8') . "</td>
                                        <td style='text-align:left;padding-left:20px'>" . $dev. "</td>
                                        <td style='text-align:left;padding-left:20px'>{$data['fechaDev']}</td>   
                                        </td>
                                    </tr>";
                            }
                        }else{
                            $valores .= "<tr>
                                        <td colspan='6' style='text-align:center;padding-left:20px;color:red'>Sin datos que mostrar</td>
                                        </tr>";
                        }
                        $valores.='

                            <!-- Agrega más filas según sea necesario -->
                        </tbody>
                    </table>
                </div>
                <div class="paginador">
                    <ul>';
                        $numeroInicio=1;
                        if($pagina!=1){
                        
                        $valores .= '<li><a href="?pagina=<?php echo $numeroInicio?>&busqueda=<?php echo $busq?>"><i class="fa-solid fa-backward-fast"></i></a></li>';
                        
                        }
                            if(($pagina-3)>1){
                                $numeroInicio=$pagina-3;
                            }
                            $numeroFin=$numeroInicio+4;
                            
                            if($numeroFin>$total_paginas){
                                $numeroFin=$total_paginas;
                            }
                            
                            for($i=$numeroInicio;$i<=$numeroFin;$i++){
                                if($i==$pagina){
                                    $valores .= '<li class="pageSelected">'.$i.'</li>';
                                } else{
                                    $valores .= '<li><a href="?pagina='.$i.'">'.$i.'</a></li>';
                                }
                            }
                            
                            if($pagina!=$total_paginas){
                                if($pagina==0){
                                    $valores.="";
                                }else {
                                    $valores .= '<li><a href="?pagina='.$total_paginas.'"><i class="fa-solid fa-forward-fast"></i></a></li>';
                                }
                                
                            } 
                   $valores .='
                    </ul>
                </div>
                ';

   
        $response['success'] = true;
        $response['valores'] = $valores;
} else if ($reporte === "lugar") {
    $valores .= '
    <div class="table-container" style="width:100%;heigth:100%">
                    <table border=\'0\' style=\'width:100%;font-size:8pt;\'>
                        <thead>
                            <tr>
                                <th style="width:5%;">id</th>
                                <th style="width:10%;">Fecha</th>
                                <th style="width:25%;">Empleado</th>
                                <th style="width:10%;">Cantidad</th>
                                <th style="width:25%;">Descripcion</th>
                                <th ">Devuelto</th>
                                <th style="width:20%;">Fecha Devolución</th>
                            </tr>
                        </thead>
                        </table>
                        <div style="max-height: 600px; overflow-y: auto;">
                        <table border="0" style="width: 100%; font-size: 8pt; border-collapse: collapse;">
                        ';
                        $busq = filter_input(INPUT_GET, 'busqueda', FILTER_SANITIZE_STRING) ?: '';
                        $pagina = filter_input(INPUT_GET, 'pagina', FILTER_VALIDATE_INT) ?: 1;

                        $por_pagina = 15;
                        $desde = ($pagina - 1) * $por_pagina;

                        // Total registros
                        $query_total = "SELECT COUNT(*) as total_registros FROM datos_entregas where dato3=? and uni_tipomov_idtipoMovUni='4' and fechaMov between ? and ?";
                        $stmt = $connection->prepare($query_total);
                        //$likeBusq = "%{$busq}%";
                        $stmt->bind_param("sss", $datox,$fechaini,$fechafin);
                        $stmt->execute();
                        $result = $stmt->get_result();
                        $total_registros = $result->fetch_assoc()['total_registros'];

                        $total_paginas = ceil($total_registros / $por_pagina);

                        // Consulta principal
                        //$query = "SELECT * FROM datos_traspasos WHERE medida like ? OR tipoUniforme LIKE ? ORDER BY orden ASC LIMIT ?, ?";
                        $query = "SELECT * FROM datos_entregas where dato3=? and uni_tipomov_idtipoMovUni='4' and fechaMov between ? and ? ORDER BY fechaMov DESC,idmovimientosUni DESC LIMIT ?, ?";
                        $stmt = $connection->prepare($query);
                        $stmt->bind_param("sssii", $datox,$fechaini,$fechafin,$desde, $por_pagina);
                        $stmt->execute();
                        $result = $stmt->get_result();

                        if ($result->num_rows > 0) {
                            while ($data = $result->fetch_assoc()) {
                                $dev=($data['dev'] == 1) ? "SI" : "NO";
                                $valores .= "<tr>
                                        <td style='text-align:left;padding-left:20px'>{$data['idmovimientosUni']}</td>
                                        <td style='text-align:left;padding-left:20px'>{$data['fechaMov']}</td>
                                        <td style='text-align:left;padding-left:20px'>{$data['dato1']}</td>
                                        <td style='text-align:left;padding-left:20px'>{$data['cant']}</td>
                                        <td style='text-align:left;padding-left:20px'>" . htmlspecialchars($data['descrip'], ENT_QUOTES, 'UTF-8') . "</td>
                                        <td style='text-align:left;padding-left:20px'>" . $dev. "</td>
                                        <td style='text-align:left;padding-left:20px'>{$data['fechaDev']}</td>   
                                        </td>
                                    </tr>";
                            }
                        }else{
                            $valores .= "<tr>
                                        <td colspan='6' style='text-align:center;padding-left:20px;color:red'>Sin datos que mostrar</td>
                                        </tr>";
                        }
                        $valores.='

                            <!-- Agrega más filas según sea necesario -->
                        </tbody>
                    </table>
                </div>
           </div>
                ';

   
        $response['success'] = true;
        $response['valores'] = $valores;

} else if ($reporte === "cliente") {
            $query = "SELECT * FROM uni_clientes WHERE iduniCliente = ?";
            $stmt = $connection->prepare($query);

            // Vincula el parámetro correctamente
            $stmt->bind_param("s", $datox);

            // Ejecuta la consulta
            $stmt->execute();

            // Obtiene los resultados
            $result = $stmt->get_result();
            $datoxx=null;
            // Verifica si hay filas y procesa los datos
            if ($result->num_rows > 0) {
                while ($data = $result->fetch_assoc()) {
                    $datoxx = $data['cliente'];
                }
            }
    
    
                        $por_pagina = 24;
                        $desde = ($pagina - 1) * $por_pagina;

                        if($detallado=='99'){
                            $valores .= '
                            <div class="table-container" style="width:100%;heigth:100%">
                            <table border="0" style="width:100%;font-size:8pt;">
                            <thead>
                            <tr>
                                <th style="width:100px;">id</th>
                                <th style="width:100px;">Fecha</th>
                                <th style="width:270px;">Descripcion</th>
                                <th>Cantidad</th>
                                
                                
                            </tr>
                            </thead>
                        </table>
                        <div style="max-height: 600px; overflow-y: auto;">
                        <table border="0" style="width: 100%; font-size: 8pt; border-collapse: collapse;">
                            <tbody>
                        ';
                            // Total registros
                            $query_total = "SELECT COUNT(*) as total_registros FROM datos_entregas where cliente=? and uni_tipomov_idtipoMovUni='4' and fechaMov between ? and ? group by descrip";
                            $stmt = $connection->prepare($query_total);
                            //$likeBusq = "%{$busq}%";
                            $stmt->bind_param("sss", $datoxx,$fechaini,$fechafin);
                            $stmt->execute();
                            $result = $stmt->get_result();
                            $total_registros = $result->fetch_assoc()['total_registros'];

                            $total_paginas = ceil($total_registros / $por_pagina);

                            // Consulta principal
                            //$query = "SELECT * FROM datos_traspasos WHERE medida like ? OR tipoUniforme LIKE ? ORDER BY orden ASC LIMIT ?, ?";
                            $query = "SELECT idmovimientosUni,fechaMov,sum(cant) as cantx ,descrip,dev,fechaDev FROM datos_entregas where cliente=? and uni_tipomov_idtipoMovUni='4' and fechaMov between ? and ? group by descrip ORDER BY fechaMov DESC,idmovimientosUni DESC LIMIT ?, ? ";
                            $stmt = $connection->prepare($query);
                            $stmt->bind_param("sssii", $datoxx,$fechaini,$fechafin,$desde, $por_pagina);
                            $stmt->execute();
                            $result = $stmt->get_result();

                            if ($result->num_rows > 0) {
                                while ($data = $result->fetch_assoc()) {
                                    $dev=($data['dev'] == 1) ? "SI" : "NO";
                                    $valores .= "<tr>
                                            <td style='text-align:left;padding-left:20px;width:80px;'>{$data['idmovimientosUni']}</td>
                                            <td style='text-align:left;padding-left:20px;width:100px;'>{$data['fechaMov']}</td>
                                            <td style='text-align:left;width:300px;'>" . htmlspecialchars($data['descrip'], ENT_QUOTES, 'UTF-8') . "</td>
                                            <td style='text-align:left;padding-left:20px'>{$data['cantx']}</td>                                            
                                            
                                            </td>
                                        </tr>";
                                }
                            }else{
                                $valores .= "<tr>
                                            <td colspan='6' style='text-align:center;padding-left:20px;color:red'>Sin datos que mostrar</td>
                                            </tr>";
                            }
                            $valores.='

                                <!-- Agrega más filas según sea necesario -->
                            </tbody>
                            </table>
                            </div>
                            </div>
                            ';
                        }
                        else{
                            $valores .= '
                        <div class="table-container" style="width:100%;heigth:100%">
                    <table border=\'0\' style=\'width:100%;font-size:8pt;\' border-collapse: collapse;">
                        <thead>
                            <tr>
                                <th style="width:100px;">id</th>
                                <th style="width:150px;">Fecha</th>
                                <th style="width:450px;">Empleado</th>
                                <th style="width:450px;">Descripcion</th>
                                <th style="width:50px;"">Cantidad</th>                                
                                <th style="width:6px;">Devuelto</th>
                                <th >Fecha Devolución</th>
                            </tr>
                        </thead>
                        </table>
                        <div style="max-height: 600px; overflow-y: auto;">
                        <table border="0" style="width: 100%; font-size: 8pt; border-collapse: collapse;">
                        <tbody>
                        ';
                            // Total registros
                                $query_total = "SELECT COUNT(*) as total_registros FROM datos_entregas where cliente=? and uni_tipomov_idtipoMovUni='4' and fechaMov between ? and ?";
                                $stmt = $connection->prepare($query_total);
                                //$likeBusq = "%{$busq}%";
                                $stmt->bind_param("sss", $datoxx,$fechaini,$fechafin);
                                $stmt->execute();
                                $result = $stmt->get_result();
                                $total_registros = $result->fetch_assoc()['total_registros'];

                                $total_paginas = ceil($total_registros / $por_pagina);

                                // Consulta principal
                                //$query = "SELECT * FROM datos_traspasos WHERE medida like ? OR tipoUniforme LIKE ? ORDER BY orden ASC LIMIT ?, ?";
                                $query = "SELECT * FROM datos_entregas where cliente=? and uni_tipomov_idtipoMovUni='4' and fechaMov between ? and ? ORDER BY fechaMov DESC,idmovimientosUni DESC LIMIT ?, ?";
                                $stmt = $connection->prepare($query);
                                $stmt->bind_param("sssii", $datoxx,$fechaini,$fechafin,$desde, $por_pagina);
                                $stmt->execute();
                                $result = $stmt->get_result();

                                if ($result->num_rows > 0) {
                                    while ($data = $result->fetch_assoc()) {
                                        $dev=($data['dev'] == 1) ? "SI" : "NO";
                                        $valores .= "<tr>
                                                <td style='text-align:left;padding-left:20px;width:80px;'>{$data['idmovimientosUni']}</td>
                                                <td style='text-align:left;padding-left:20px';width:150px;>{$data['fechaMov']}</td>
                                                <td style='text-align:left;padding-left:20px';width:450px;>{$data['dato1']}</td>
                                                <td style='text-align:left;padding-left:20px';width:450px;>" . htmlspecialchars($data['descrip'], ENT_QUOTES, 'UTF-8') . "</td>
                                                <td style='text-align:left;padding-left:20px';width:50px;>{$data['cant']}</td>
                                                <td style='text-align:left;padding-left:20px';width:60px;>" . $dev. "</td>
                                                <td style='text-align:left;padding-left:20px'>{$data['fechaDev']}</td>   
                                                </td>
                                            </tr>";
                                    }
                                }else{
                                    $valores .= "<tr>
                                                <td colspan='6' style='text-align:center;padding-left:20px;color:red'>Sin datos que mostrar</td>
                                                </tr>";
                                }
                                $valores.='

                                    <!-- Agrega más filas según sea necesario -->
                                </tbody>
                            </table>
                        </div>
                        </div>
                        
                        ';

                        }
                        

   
        $response['success'] = true;
        $response['valores'] = $valores;


} else if ($reporte === "sucursal") {

                        if($detallado=='99'){
                            $valores .= '
                            <div class="table-container" style="width:100%;heigth:100%">
                            <table border="0" style="width:100%;font-size:8pt;">
                            <thead>
                            <tr>
                                <th style="width:100px;">id</th>
                                <th style="width:100px;">Fecha</th>
                                <th style="width:270px;">Descripcion</th>
                                <th>Cantidad</th>
                                
                                
                            </tr>
                            </thead>
                        </table>
                        <div style="max-height: 600px; overflow-y: auto;">
                        <table border="0" style="width: 100%; font-size: 8pt; border-collapse: collapse;">
                            <tbody>
                        ';
                            // Total registros
                            $query_total = "SELECT COUNT(*) as total_registros FROM datos_entregas where dato2 like ? and uni_tipomov_idtipoMovUni='4' and fechaMov between ? and ? group by descrip";
                            $stmt = $connection->prepare($query_total);
                            $likeBusq = "%{$datox}%";
                            $stmt->bind_param("sss", $likeBusq,$fechaini,$fechafin);
                            $stmt->execute();
                            $result = $stmt->get_result();
                           

                            // Consulta principal
                            //$query = "SELECT * FROM datos_traspasos WHERE medida like ? OR tipoUniforme LIKE ? ORDER BY orden ASC LIMIT ?, ?";
                            $query = "SELECT idmovimientosUni,fechaMov,sum(cant) as cantx ,descrip,dev,fechaDev FROM datos_entregas where dato2 like ? and uni_tipomov_idtipoMovUni='4' and fechaMov between ? and ? group by descrip ORDER BY fechaMov DESC,idmovimientosUni DESC ";
                            $x="SELECT idmovimientosUni,fechaMov,sum(cant) as cantx ,descrip,dev,fechaDev FROM datos_entregas where dato2 like $likeBusq and uni_tipomov_idtipoMovUni='4' and fechaMov between '$fechaini' and '$fechafin' group by descrip ORDER BY fechaMov DESC,idmovimientosUni DESC ";
                            $stmt = $connection->prepare($query);
                            $stmt->bind_param("sss", $likeBusq,$fechaini,$fechafin);
                            $stmt->execute();
                            $result = $stmt->get_result();

                            if ($result->num_rows > 0) {
                                while ($data = $result->fetch_assoc()) {
                                    $dev=($data['dev'] == 1) ? "SI" : "NO";
                                    $valores .= "<tr>
                                            <td style='text-align:left;padding-left:20px;width:80px;'>{$data['idmovimientosUni']}</td>
                                            <td style='text-align:left;padding-left:20px;width:100px;'>{$data['fechaMov']}</td>
                                            <td style='text-align:left;width:300px;'>" . htmlspecialchars($data['descrip'], ENT_QUOTES, 'UTF-8') . "</td>
                                            <td style='text-align:left;padding-left:20px'>{$data['cantx']}</td>                                            
                                            
                                            </td>
                                        </tr>";
                                }
                            }else{
                                $valores .= "<tr>
                                            <td colspan='6' style='text-align:center;padding-left:20px;color:red'>Sin datos que mostrar</td>
                                            </tr>";
                            }
                            $valores.='

                                <!-- Agrega más filas según sea necesario -->
                            </tbody>
                            </table>
                            </div>
                            </div>
                            ';
                        }
                        else{
                            $valores .= '
                                <div class="table-container" style="width:100%; height:100%;">
                                    <table border="0" style="width:100%; font-size:8pt; border-collapse:collapse;">
                                        <thead>
                                            <tr>
                                                <th style="width:100px;">ID</th>
                                                <th style="width:150px;">Fecha</th>
                                                <th style="width:350px;">Empleado</th>
                                                <th style="width:280px;">Descripción</th>
                                                <th style="width:50px;">Cantidad</th>
                                                <th style="width:50px;">Devuelto</th>
                                                <th>Fecha Devolución</th>
                                            </tr>
                                        </thead>
                                    </table>
                                    <div style="max-height:600px; overflow-y:auto;">
                                        <table border="0" style="width:100%; font-size:8pt; border-collapse:collapse;">
                                            <tbody>';

                                // Total registros
                                $query_total = "SELECT COUNT(*) as total_registros 
                                                FROM datos_entregas 
                                                WHERE dato2 LIKE ? AND uni_tipomov_idtipoMovUni='4' 
                                                AND fechaMov BETWEEN ? AND ?";
                                $stmt = $connection->prepare($query_total);
                                $likeBusq = "%{$datox}%";
                                $stmt->bind_param("sss", $likeBusq, $fechaini, $fechafin);
                                $stmt->execute();
                                $result = $stmt->get_result();

                                // Consulta principal
                                $query = "SELECT * FROM datos_entregas 
                                        WHERE dato2 LIKE ? AND uni_tipomov_idtipoMovUni='4' 
                                        AND fechaMov BETWEEN ? AND ?
                                        ORDER BY fechaMov DESC, idmovimientosUni DESC";
                                $stmt = $connection->prepare($query);
                                $stmt->bind_param("sss", $likeBusq, $fechaini, $fechafin);
                                $stmt->execute();
                                $result = $stmt->get_result();

                                if ($result->num_rows > 0) {
                                    while ($data = $result->fetch_assoc()) {
                                        $dev = ($data['dev'] == 1) ? "SI" : "NO";
                                        $valores .= "<tr>
                                                        <td style='text-align:left; padding-left:20px; width:80px;'>{$data['idmovimientosUni']}</td>
                                                        <td style='text-align:left; padding-left:20px; width:150px;'>{$data['fechaMov']}</td>
                                                        <td style='text-align:left; padding-left:20px; width:350px;'>". utf8_encode($data['dato1']) . 
                                                        "</td>
                                                        <td style='text-align:left; padding-left:20px; width:300px;'>"
                                                            . htmlspecialchars($data['descrip'], ENT_QUOTES, 'UTF-8') . 
                                                        "</td>
                                                        <td style='text-align:left; padding-left:20px; width:50px;'>{$data['cant']}</td>
                                                        <td style='text-align:left; padding-left:20px; width:60px;'>{$dev}</td>
                                                        <td style='text-align:left; padding-left:20px;'>{$data['fechaDev']}</td>
                                                    </tr>";
                                    }
                                } else {
                                    $valores .= "<tr>
                                                    <td colspan='7' style='text-align:center; padding:20px; color:red;'>Sin datos que mostrar</td>
                                                </tr>";
                                }

                                $valores .= '
                                            </tbody>
                                        </table>
                                    </div>
                                </div>';

                        }
                        

   
        $response['success'] = true;
        $response['valores'] = $valores;
        
} else if ($reporte === "articulo") {
    
    
    

    if($usados=='99'){
        $valores .= '
            <div class="table-container" style="width:100%; height:100%;">
                <table border="0" style="width:100%; font-size:8pt; border-collapse:collapse;">
                    <thead>
                        <tr>
                            <th style="width:50px;">ID</th>
                            <th style="width:350px;">Descripción</th>
                            <th style="width:50px;">Cantidad</th>
                            <th ></th>
                        </tr>
                    </thead>
                </table>
                <div style="max-height:600px; overflow-y:auto;">
                    <table border="0" style="width:100%; font-size:8pt; border-collapse:collapse;">
                        <tbody>';

          

            // Consulta principal
            if($datox=='99'){
                $query = "SELECT idmovimientosUni,descrip,sum(cant) as cantx FROM datos_entregas 
                WHERE uni_tipomov_idtipoMovUni='4' and estado='NUEVO'
                AND fechaMov BETWEEN ? AND ?
                group by descrip
                ORDER BY descrip ASC";
                $stmt = $connection->prepare($query);
                $stmt->bind_param("ss", $fechaini, $fechafin);
            }else{
                $query = "SELECT idmovimientosUni,descrip,sum(cant) as cantx FROM datos_entregas 
                WHERE u_descripcion= ? AND uni_tipomov_idtipoMovUni='4' and estado='NUEVO'
                AND fechaMov BETWEEN ? AND ?
                group by descrip
                ORDER BY descrip ASC";
                $stmt = $connection->prepare($query);
                $stmt->bind_param("sss", $datox, $fechaini, $fechafin);
            }
            
                    
            
            $stmt->execute();
            $result = $stmt->get_result();

            if ($result->num_rows > 0) {
                while ($data = $result->fetch_assoc()) {
                    
                    $valores .= "<tr>
                                    <td style='text-align:left; padding-left:20px; width:50px;'>{$data['idmovimientosUni']}</td>
                                    
                                    
                                    <td style='text-align:left; padding-left:20px; width:350px;'>"
                                        . utf8_encode($data['descrip']) . 
                                    "</td>
                                    <td style='text-align:left; padding-left:20px; width:50px;'>{$data['cantx']}</td>
                                    <td ></td>
                                </tr>";
                }
            } else {
                $valores .= "<tr>
                                <td colspan='7' style='text-align:center; padding:20px; color:red;'>Sin datos que mostrar</td>
                            </tr>";
            }

            $valores .= '
                        </tbody>
                    </table>
                </div>
            </div>';

    }
    else{
        $valores .= '
        <div class="table-container" style="width:100%;heigth:100%">
        <table border="0" style="width:100%;font-size:8pt;">
        <thead>
        <tr>
                            <th style="width:350px;">Descripción</th>
                            <th style="width:50px;">Cantidad</th>
                            <th ></th>
                        </tr>
        </thead>
    </table>
    <div style="max-height: 600px; overflow-y: auto;">
    <table border="0" style="width: 100%; font-size: 8pt; border-collapse: collapse;">
        <tbody>
    ';
        // Consulta principal
        if($datox=='99'){
            $query = "SELECT idmovimientosUni,descrip,sum(cant) as cantx FROM datos_entregas 
            WHERE uni_tipomov_idtipoMovUni='4'
            AND fechaMov BETWEEN ? AND ?
            group by descrip
            ORDER BY descrip ASC";
            $stmt = $connection->prepare($query);
            $stmt->bind_param("ss",$fechaini, $fechafin);
            $stmt->execute();
        }else{
            $query = "SELECT idmovimientosUni,descrip,sum(cant) as cantx FROM datos_entregas 
            WHERE u_descripcion= ? AND uni_tipomov_idtipoMovUni='4'
            AND fechaMov BETWEEN ? AND ?
            group by descrip
            ORDER BY descrip ASC";
            $stmt = $connection->prepare($query);
            $stmt->bind_param("sss", $datox, $fechaini, $fechafin);
            $stmt->execute();
        }
                    
                        $result = $stmt->get_result();

                        if ($result->num_rows > 0) {
                            while ($data = $result->fetch_assoc()) {
                                
                                $valores .= "<tr>
                                                            <td style='text-align:left; padding-left:20px; width:350px;'>"
                                                                . utf8_encode($data['descrip']) . 
                                                            "</td>
                                                            <td style='text-align:left; padding-left:20px; width:50px;'>{$data['cantx']}</td>
                                                            <td ></td>
                                                        </tr>";
                            }
                        } else {
                            $valores .= "<tr>
                                            <td colspan='7' style='text-align:center; padding:20px; color:red;'>Sin datos que mostrar</td>
                                        </tr>";
                        }

                        $valores .= '
                                    </tbody>
                                </table>
                            </div>
                        </div>';

    }
    
    


$response['success'] = true;
$response['valores'] = $valores;

} 



echo json_encode($response);
?>

