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

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


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



                    <div class="row">
                        <div class="col-sm-8">

                            <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-warning text-dark">
                                                <tr class=" ">
                                                    <th class="bg-gradient-warning" >REFERENCIAS</th>
                                                    <th class="bg-gradient-warning" >COLOR</th>
                                                    <?php
                                                    $sql = "SELECT talla FROM alm_cierre_pedido_detalle WHERE nombre_tercero='$nombre_tercero' 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_pedido_detalle WHERE nombre_tercero='$nombre_tercero' 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_pedido_detalle WHERE nombre_tercero='$nombre_tercero'  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_pedido_detalle WHERE referencia_producto='" . $row1['referencia_producto'] . "' AND talla='" . $row2['talla'] . "' AND nombre_tercero='$nombre_tercero'  ";
                                                                    $query3 = $conn->query($sql3);
                                                                    $row3 = $query3->fetch_assoc();

                                                                    $talla_pedido = $row3['cantidad'];

                                                                    if ($talla_pedido < 0) {
                                                                        $bg_color_talla = 'bg-danger';
                                                                        $text_color = 'text-white';
                                                                    } 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'];
                                                                }
                                                            }
                                                            ?> 






                                                            <?php
                                                            if ($total_talla < 0) {
                                                                $text_total_color = 'text-danger';
                                                            } elseif ($total_talla > 0) {
                                                                $text_total_color = 'text-purple';
                                                            } 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_pedido_detalle WHERE nombre_tercero='$nombre_tercero' GROUP BY talla ORDER BY talla asc";
                                                    $query2 = $conn->query($sql2);
                                                    if ($query2->num_rows > 0) {
                                                        while ($row2 = $query2->fetch_assoc()) {
                                                            ?>
                                                            <th></th>
                                                            <?php
                                                            $item_footer++;
                                                        }
                                                    }
                                                    ?>
                                                    <th></th>
                                                </tr>
                                            </tfoot>
                                        </table>
                                    </div>

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


                        <div class="col-sm-4">
                            <div class="card ">
                                <div class="card-body">




                                    <div class=" mt-2" >
                                        <h5 class="mt-3 text-decoration-underline"><b>LISTA DE CIERRE PEDIDO</b></h5>

                                        <table class="table align-middle table-hover table-striped mb-0 table-bordered" id="example1" style="width: 100%; font-size: 14px;">
                                            <thead>
                                                <tr class="bg-gradient-warning text-dark">
                                                    <th class="text-center">FECHA</th>
                                                    <th class="text-center">CANT.</th>
                                                    <th class="text-center">DETALLE</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                $sql3 = "SELECT * FROM alm_cierre_pedido_detalle WHERE nombre_tercero='$nombre_tercero' GROUP BY fecha ORDER BY fecha desc";
                                                $query3 = $conn->query($sql3);
                                                if ($query3->num_rows > 0) {
                                                    while ($row3 = $query3->fetch_assoc()) {

                                                        $sql4 = "SELECT SUM(cantidad) as cantidad FROM alm_cierre_pedido_detalle WHERE nombre_tercero='$nombre_tercero' AND fecha='" . $row3['fecha'] . "'";
                                                        $query4 = $conn->query($sql4);
                                                        $row4 = $query4->fetch_assoc();
                                                        ?>
                                                        <tr>
                                                            <td class="text-center"><strong><?php echo date('d-m-Y', strtotime($row3['fecha'])); ?></strong></td>
                                                            <td class="text-center">
                                                                <?php echo number_format($row4['cantidad'], 0); ?>
                                                            </td>
                                                            <td class="text-center">
                                                                <a href="cierre-tercero-detalle.php?nombre_tercero=<?php echo $nombre_tercero; ?>&&fecha=<?php echo $row3['fecha']; ?>">
                                                                    <i class="fa-solid fa-arrow-right-to-bracket fa-2x"></i>
                                                                </a>
                                                            </td>
                                                        </tr>
                                                        <?php
                                                    }
                                                }
                                                ?>
                                            </tbody>
                                        </table>
                                    </div>

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

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




    </body>
</html>