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

$nombre_tercero = $_GET['nombre_tercero'];
$fecha = $_GET['fecha'];
?>
<!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">
                                <a href="cierre.php"> 
                                    Cierre
                                </a>
                            </li>
                            <li class="breadcrumb-item">
                                <a href="cierre-tercero.php?nombre_tercero=<?php echo $nombre_tercero; ?>"> 
                                    <?php echo $nombre_tercero ?>
                                </a>
                            </li>
                            <li class="breadcrumb-item active" aria-current="page"><?php echo $fecha; ?></li>
                        </ol>
                    </nav>


                    <div class="card">
                        <div class="card-body text-center">
                            <h3><b>CIERRE <br> <?php echo $nombre_tercero ?></b></h3>
                        </div>
                    </div>

                    <div class="card ">
                        <div class="card-body">


                            <style>
                                th, td { white-space: nowrap; }

                                div.dataTables_wrapper {
                                    width: 100%;
                                    margin: 0 auto;
                                }


                            </style>



                            <style>
                                th, td { white-space: nowrap; }

                                div.dataTables_wrapper {
                                    width: 100%;
                                    margin: 0 auto;
                                }


                            </style>


                            <div class=" mt-3">
                                <table id="example" class="table table-striped table-bordered order-column" style="width:100%">
                                    <thead class="bg-gradient-branding text-white">
                                        <tr class=" ">
                                            <th class="bg-gradient-branding" >REFERENCIAS</th>
                                            <th class="bg-gradient-branding" >COLOR</th>
                                            <?php
                                            $sql = "SELECT talla FROM alm_cierre_detalle WHERE nombre_tercero='$nombre_tercero' AND fecha='$fecha' GROUP BY talla ORDER BY talla asc";
                                            $query = $conn->query($sql);
                                            if ($query->num_rows > 0) {
                                                while ($row = $query->fetch_assoc()) {
                                                    ?>
                                                    <th class="text-center" ><?php echo $row['talla']; ?></th>
                                                    <?php
                                                }
                                            }
                                            ?>
                                            <th class="text-center">TOTAL</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $sql1 = "SELECT referencia_producto FROM alm_cierre_detalle WHERE nombre_tercero='$nombre_tercero' AND fecha='$fecha' GROUP BY referencia_producto ORDER BY referencia_producto asc";
                                        $query1 = $conn->query($sql1);
                                        if ($query1->num_rows > 0) {
                                            while ($row1 = $query1->fetch_assoc()) {

                                                $sql1p = "SELECT * FROM producto WHERE referencia='" . $row1['referencia_producto'] . "'";
                                                $query1p = $conn->query($sql1p);
                                                $row1p = $query1p->fetch_assoc();
                                                ?>
                                                <tr class="" >
                                                    <td class="">
                                                        <b class="text-primary">
                                                            <a href="../producto/kardex.php?referencia=<?php echo $row1['referencia_producto']; ?>" target="_blank">
                                                                <?php echo $row1['referencia_producto']; ?>
                                                            </a>
                                                        </b>
                                                    </td>
                                                    <td class="">
                                                        <b class="text-primary"><?php echo $row1p['color']; ?></b>
                                                    </td>

                                                    <?php
                                                    $total_talla = 0;
                                                    $sql2 = "SELECT talla FROM alm_cierre_detalle WHERE nombre_tercero='$nombre_tercero' AND fecha='$fecha'  GROUP BY talla ORDER BY talla asc";
                                                    $query2 = $conn->query($sql2);
                                                    if ($query2->num_rows > 0) {
                                                        while ($row2 = $query2->fetch_assoc()) {


                                                            $sql3 = "SELECT SUM(cantidad) as cantidad FROM alm_cierre_detalle WHERE referencia_producto='" . $row1['referencia_producto'] . "' AND talla='" . $row2['talla'] . "' AND nombre_tercero='$nombre_tercero' AND fecha='$fecha'  ";
                                                            $query3 = $conn->query($sql3);
                                                            $row3 = $query3->fetch_assoc();

                                                            if(isset($row3['cantidad'])){
                                                                $row3['cantidad'] = $row3['cantidad'];
                                                            }else{
                                                                $row3['cantidad'] = 0;
                                                            }
                                                            
                                                            
                                                            $talla_pedido = $row3['cantidad'];

                                                            if ($talla_pedido > 0) {
                                                                $bg_color_talla = 'bg-warning';
                                                                $text_color = 'text-dark';
                                                            } else {
                                                                $bg_color_talla = '';
                                                                $text_color = 'text-secondary';
                                                            }
                                                            ?>
                                                            <td class="text-center <?php echo $bg_color_talla; ?> <?php echo $text_color; ?>" >
                                                                <?php echo number_format($talla_pedido, 0); ?>
                                                            </td>


                                                            <?php
                                                            $total_talla += $row3['cantidad'];
                                                        }
                                                    }

                                                    if ($total_talla > 0) {
                                                        $text_total_color = 'text-warning';
                                                    } else {
                                                        $text_total_color = '';
                                                    }
                                                    ?>
                                                    <td class="text-center <?php echo $text_total_color; ?> h6"  style="font-weight: bold; ">

                                                        <?php echo $total_talla; ?>
                                                    </td>

                                                </tr>
                                                <?php
                                            }
                                        }
                                        ?>
                                    </tbody>
                                    <tfoot align="right">
                                        <tr class="bg-light-dark">
                                            <th class="bg-light-dark" colspan="2">TOTAL</th>
                                            <?php
                                            $item_footer = 1;
                                            $sql2 = "SELECT talla FROM alm_cierre_detalle WHERE nombre_tercero='$nombre_tercero' AND fecha='$fecha'  GROUP BY talla ORDER BY talla asc";
                                            $query2 = $conn->query($sql2);
                                            if ($query2->num_rows > 0) {
                                                while ($row2 = $query2->fetch_assoc()) {
                                                    ?>
                                                    <th class="text-center"></th>
                                                    <?php
                                                    $item_footer++;
                                                }
                                            }
                                            ?>
                                            <th class="text-center"></th>
                                        </tr>
                                    </tfoot>
                                </table>
                            </div>



                            <div class="text-end">
                                <form class="" method="POST" action="script/cierre-eliminar.php">
                                    <input type="text" name="nombre_tercero" value="<?php echo $nombre_tercero; ?>" hidden>
                                    <input type="text" name="fecha" value="<?php echo $fecha; ?>" hidden>
                                    <button type="submit" class="btn btn-danger radius-10 px-5 " id="btn"
                                            onclick="if (!confirm('Seguro deseas eliminar este registro?')) {
                                                        return false

                                                    }">ELIMINAR CIERRE</button>
                                </form>
                            </div>


                        </div>
                    </div>


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




       
    </body>
</html>

