<?php
session_start();
include '../../script/conexion.php';
require '../general/validacion.php';
?>
<!doctype html>
<html lang="en" class="light-theme">
    <head>
        <?php include '../general/libreria-css.php'; ?>
    </head>
    <body>
        <!--start wrapper-->
        <div class="wrapper">
            <?php echo include '../general/menu.php'; ?>

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

                    <nav aria-label="breadcrumb">
                        <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 active" aria-current="page">Inventario</li>
                        </ol>
                    </nav>






                    <div class="card card-table mb-4">

                        <div class="card-body">



                            <div class="table-responsive">
                                <table class="table  table-striped table-hover table-bordered " id="example2" >
                                    <thead>
                                        <tr class="bg-tianzi">
                                            <th class="text-center" >FOTO</th>
                                            <th class="text-center" >REFERENCIA</th>
                                            <th class="text-center" >CATEGORIA</th>
                                            <?php
                                            $sql1 = "SELECT talla FROM prueba_inventario GROUP BY talla ORDER BY talla asc";
                                            $query1 = $conn->query($sql1);
                                            if ($query1->num_rows > 0) {
                                                while ($row1 = $query1->fetch_assoc()) {
                                                    ?>
                                                    <th class="text-center p-2" ><?php echo $row1['talla']; ?></th>
                                                    <?php
                                                }
                                            }
                                            ?>
                                            <th class="text-center" >TOTAL</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $sql = "SELECT referencia_producto FROM prueba_inventario GROUP BY referencia_producto ORDER BY referencia_producto asc";
                                        $query = $conn->query($sql);
                                        if ($query->num_rows > 0) {
                                            while ($row = $query->fetch_assoc()) {

                                                $sql2 = "SELECT * FROM producto WHERE referencia='" . $row['referencia_producto'] . "'";
                                                $query2 = $conn->query($sql2);
                                                $row2 = $query2->fetch_assoc();

                                                $referencia = $row['referencia_producto'];
                                                ?>
                                                <tr class="font-11 align-middle">
                                                    <td class="p-0 text-center">
                                                        <div class="inbox-item-img "  id="hover_enlace">
                                                            <?php
                                                            if ($row2['foto1'] == 'NO APLICA') {
                                                                ?>
                                                                <img src="../../../innovasuelas/img/camara.jpg"  class="img-thumbnail" alt="" width="50">

                                                                <?php
                                                            } else {
                                                                ?>
                                                                <a href="javascript:window.open('../../../innovasuelas/img-producto/<?php echo $row2['foto1']; ?>','','width=1200,height=700,left=100,top=10,toolbar=yes');void 0" >
                                                                    <img src=".../../../innovasuelas/img-producto/<?php echo $row2['foto1']; ?>" class="img-thumbnail" alt="" width="50">
                                                                </a>
                                                                <?php
                                                            }
                                                            ?>
                                                        </div>

                                                    </td>
                                                    <td class="">
                                                        <a href="../producto/kardex.php?referencia=<?php echo $row['referencia_producto']; ?>" target="_blank">

                                                            <div class="pt-1">
                                                                <strong class="text-dark"><?php echo $row['referencia_producto']; ?></strong><br>
                                                            </div>
                                                        </a>
                                                    </td>
                                                    <td class="text-center"><?php echo $row2['categoria']; ?></td>



                                                    <?php
                                                    $total_tallas1 = 0;
                                                    $query4 = $conn->query($sql1);
                                                    if ($query4->num_rows > 0) {
                                                        while ($row4 = $query4->fetch_assoc()) {



                                                            $sql1_1i = "SELECT SUM(cantidad) as cantidad FROM prueba_inventario WHERE referencia_producto='$referencia' AND talla='" . $row4['talla'] . "'";
                                                            $query1_1i = $conn->query($sql1_1i);
                                                            $row1_1i = $query1_1i->fetch_assoc();

                                                             $disponibilidad1 = $row1_1i['cantidad'];
                                                            
                                                            

                                                            if ($disponibilidad1 > 0) {
                                                                $color_campo = 'text-success';
                                                                $texto = 'background-color: #bceed5';
                                                            } elseif ($disponibilidad1 < 0) {
                                                                $color_campo = 'text-danger';
                                                                $texto = 'background-color: #ffc5e6';
                                                            } else {
                                                                $color_campo = '';
                                                                $texto = '';
                                                            }
                                                            ?>

                                                            <td class="text-center font-12 <?php echo $color_campo ?>" style="<?php echo $texto; ?>">
                                                                <b><?php echo number_format($disponibilidad1, 0); ?></b>
                                                            </td>
                                                            <?php
                                                            $total_tallas1 += $disponibilidad1;
                                                        }
                                                    }
                                                    ?>
                                                    <td class="text-center font-14"> <strong><?php echo $total_tallas1; ?></strong></td>

                                                </tr>
                                                <?php
                                            }
                                        }
                                        ?>
                                    </tbody>
                                </table>
                            </div>




                        </div> 


                    </div>




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

            <?php echo include '../general/footer.php'; ?>

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


        <script>
            //Tabla producto
            $(document).ready(function () {
                var table = $('#example2').DataTable({
                    lengthChange: false,
                    buttons: [
                        {
                            extend: 'excel',
                            text: 'EXCEL',
                            filename: 'INVENTARIO',
                            title: null,
                        },
                        {
                            extend: 'pdf',
                            text: 'PDF',
                            filename: 'INVENTARIO',
                            title: null,
                        }
                    ],
                });

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

        </script>
    </body>
</html>