<?php
session_start();
include '../../script/conexion.php';
require '../general/validacion.php';

if (isset($_GET['filtro'])) {
    $fecha_desde = $_GET['fecha_desde'];
    $fecha_hasta = $_GET['fecha_hasta'];
    $nombre_tercero = $_GET['nombre_tercero'];

    if ($nombre_tercero == 'Todos') {
        $sqltercero = '';
    } else {
        $sqltercero = " AND nombre_tercero='" . $nombre_tercero . "' ";
    }


    $sqlfecha = " WHERE fecha BETWEEN '" . $fecha_desde . "' AND '" . $fecha_hasta . "' ";

    $sql_consult = $sqlfecha . $sqltercero;
} else {
    $fecha_desde = '';
    $fecha_hasta = '';
}
?>
<!doctype html>
<html lang="en" class="light-theme">
    <head>
        <?php include '../general/libreria-css.php'; ?>
                        <title> Compras <?php echo $nombre_tercero;?></title>

    </head>
    <body>

        <!-- start page content wrapper-->
        <div class="page-content-wrapper">
            <!-- start page content-->
            <div class="page-content">


                <nav aria-label="breadcrumb" class="d-print-none">

                    <ol class="breadcrumb">
                        <li class="breadcrumb-item">
                            <a href="../inicio/inicio.php"> 
                                <ion-icon name="home-outline"></ion-icon>
                            </a>
                        </li>
                        <li class="breadcrumb-item">Reporte de compras</li>
                    </ol>


                </nav>


                <div class="row d-print-none">
                    <div class="col-sm-12">
                        <div class="card">
                            <div class="card-body">
                                <div class="p-2 border rounded">

                                    <form method="GET" action="rpt-compra.php" class=" d-print-none">
                                        <div class="row">
                                            <div class="col-6 col-sm-2">
                                                <div class="form-group">
                                                    <label>Desde</label>
                                                    <input type="date" class="form-control" name="fecha_desde" value="<?php echo $fecha_desde; ?>" required>  
                                                </div>
                                            </div>
                                            <div class="col-6 col-sm-2">
                                                <div class="form-group">
                                                    <label>Hasta</label>
                                                    <input type="date" class="form-control" name="fecha_hasta" value="<?php echo $fecha_hasta; ?>" required>  
                                                </div>
                                            </div>

                                            <div class="col-6 col-sm-2">
                                                <div class="form-group">
                                                    <label>Tercero</label>
                                                    <select class="form-select single-select"  required="" name="nombre_tercero">                                           
                                                        <option selected="" value="Todos">Todos</option>
                                                        <?php
                                                        $sqlt = "SELECT * FROM tercero";
                                                        $queryt = $conn->query($sqlt);
                                                        if ($queryt->num_rows > 0) {
                                                            while ($rowt = $queryt->fetch_assoc()) {
                                                                ?>
                                                                <option class="text-sm" value="<?php echo $rowt['nombre']; ?>"><?php echo $rowt['nombre']; ?></option>
                                                                <?php
                                                            }
                                                        }
                                                        ?>
                                                    </select>         
                                                </div>
                                            </div>

                                            <div class="col-5 col-sm-1 pt-3">
                                                <div class="form-group">
                                                    <button class="btn btn-primary px-2 radius-30" type="submit" name="filtro" style="width: 100%;">
                                                        Consultar
                                                    </button>  
                                                </div>
                                            </div>
                                        </div>
                                    </form>

                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="card d-print-none">
                    <div class="card-body">
                        <div class="row">
                            <div class="col-sm-4">


                            </div>
                            <div class="col-sm-4">
                                <h6 class=" mb-3 text-center"><b>Compras <br><?php echo strftime("%d - %B - %Y", strtotime(date($fecha_desde))); ?> <strong class="text-info">al</strong> <?php echo strftime("%d - %B - %Y", strtotime(date($fecha_hasta))); ?></b></h6>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- end page title -->
                <style>
                    #hover_enlace:hover{
                        color: orange;
                        border-radius: 10px; border: #3fc3ee 1px solid;
                        box-shadow: 0px 0px 5px 0px #3fc3ee;
                    }

                </style>


                <?php
                if (isset($_GET['filtro'])) {
                    ?>



                    <div class="card">
                        <div class="card-body">
                            
                              <div class="d-print-none">
                            <div class="float-end">
                                <a href="javascript:window.print()" class="btn btn-info waves-effect waves-light">
                                    <i class="fas fa-print"> Imprimir</i>
                                </a>
                            </div>
                            <div class="clearfix"></div>
                        </div>
                            <h3 class=""><b><?php echo $nombre_tercero; ?></b></h3>
                            <hr/>

                            <style>
                                #hover_enlace:hover{
                                    color: purple;
                                    border-radius: 10px; 
                                    box-shadow: 0px 0px 10px 0px purple;
                                }

                            </style>

                            <div class="table-responsive mt-2">
                                <table class="table align-middle mb-0 table-striped" id="example2" style="width: 100%;">
                                    <thead class="table-light">
                                        <tr class="bg-light-primary text-primary">
                                            <th class="text-center ">Id</th>
                                            <th class="text-center ">Fecha</th>
                                            <th class=" ">Nombre tercero</th>
                                            <th class=" ">N° Factura</th>
                                            <th class="text-center ">Descripcion</th>
                                            <th class="text-center ">Cant.</th>
                                            <th class="text-center ">Total valor</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $total_compra = 0;
                                        $sql4 = "SELECT * FROM compra $sql_consult ORDER BY fecha desc";
                                        $query4 = $conn->query($sql4);
                                        if ($query4->num_rows > 0) {
                                            while ($row4 = $query4->fetch_assoc()) {
                                                ?>
                                                <tr>

                                                    <td>
                                                        <strong class="text-dark">C <?php echo $row4['id']; ?></strong><br>
                                                    </td>
                                                    <td class="text-center">
                                                        <strong class="text-dark"><?php echo $row4['fecha']; ?></strong><br>
                                                    </td>
                                                    <td class="">
                                                        <?php echo $row4['nombre_tercero']; ?>
                                                    </td>         
                                                    <td class="">
                                                        <?php echo $row4['numero_factura']; ?>
                                                    </td>  
                                                    <td class="" style="width: 20%;">
                                                        <?php echo $row4['descripcion']; ?>
                                                    </td>
                                                    <td class="text-center">
                                                        <?php
                                                        $sql5 = "SELECT SUM(cantidad) as cantidad FROM compra_detalle WHERE id_compra='" . $row4['id'] . "'";
                                                        $query5 = $conn->query($sql5);
                                                        $row5 = $query5->fetch_assoc();
                                                        ?>

                                                        <?php echo $row5['cantidad']; ?>
                                                    </td>
                                                    <td class="text-end">
                                                        <?php echo number_format($row4['total'], 0); ?>
                                                    </td>                                                    

                                                </tr>
                                                <?php
                                                $total_compra += $row4['total'];
                                            }
                                        }
                                        ?>
                                    </tbody>
                                    <tfoot>
                                        <tr class="h4">
                                            <th class="text-end" colspan="6">TOTAL</th>
                                            <th class="text-end">$ <?php echo number_format($total_compra); ?></th>
                                        </tr>
                                    </tfoot>
                                </table>
                            </div>
                        </div>
                    </div>
                    <?php
                } else {
                    
                }
                ?>




            </div><!-- end col -->
            <!-- end page content-->
        </div>
        <!--end page content wrapper-->

        <!--end wrapper-->
        <?php echo include '../general/libreria-js.php'; ?>

   <script>
            //Tabla producto
            $(document).ready(function () {
                var table = $('#example2').DataTable({
                    lengthChange: false,
                    buttons: ['copy', 'excel', 'pdf', 'print'],
                    "order": false,
                    'pageLength': 50,
                });

                table.buttons().container()
                        .appendTo('#example2_wrapper .col-md-6:eq(0)');
            });

        </script>

    </body>
</html>