<?php
session_start();
include '../../script/conexion.php';
require '../general/validacion.php';
$referencia = $_GET['referencia'];

$sqlpro = "SELECT * FROM producto WHERE referencia='$referencia'";
$querypro = $conn->query($sqlpro);
$rowpro = $querypro->fetch_assoc();
?> 
<!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="#"> 
                                    <ion-icon name="home-outline"></ion-icon>
                                </a>
                            </li>
                            <li class="breadcrumb-item">
                                <a href="producto.php"> 
                                    Productos             
                                </a>
                            </li>
                            <li class="breadcrumb-item active" aria-current="page"><?php echo $referencia; ?></li>
                            <li class="breadcrumb-item active" aria-current="page">Novedad</li>
                        </ol>
                    </nav>


                    <div class="card card-table mb-3">
                        <div class="card-header">
                            <h4 >REFERENCIA: <strong class="text-titulo"><?php echo $referencia; ?></strong></h4>
                        </div>
                    </div>

                    <div class="card">
                        <div class="card-body">
                            <div class="row mb-3"> 
                                <div class="col-12 col-md-12">
                                    <div class="row"> 
                                        <div class="col   col mt-2 ">
                                            <a class="btn btn-outline-dark px-3 radius-30 border-info w-100" href="kardex.php?referencia=<?php echo $referencia; ?>">
                                                <i class="far fa-chart-bar opacity-5 me-2 text-titulo" ></i> Kardex        
                                            </a>
                                        </div>

                                        <div class="col   mt-2">
                                            <a class="btn btn-outline-dark px-2 border-info radius-30 w-100" href="movimiento.php?referencia=<?php echo $referencia; ?>">
                                                <i class="fas fa-file-signature opacity-5 me-2 text-titulo" ></i> Movimientos        
                                            </a>
                                        </div>

                                        <div class="col mt-2">
                                            <a class="btn btn-tianzi  px-2 border-info radius-30 w-100" href="devolucion.php?referencia=<?php echo $referencia; ?>">
                                                <i class="fa-solid fa-rotate-left opacity-5 me-2 text-titulo" ></i> Devoluciones        
                                            </a>
                                        </div>

                                    </div>
                                </div>

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

                    <div class="card">
                        <div class="card-body">
                            <div class="row"> 
                                <div class="col-12 col-md-3">
                                    <div class="p-4 border rounded">
                                        <form method="POST" action="script/devolucion-guardar.php" class="row g-3 needs-validation" >
                                            <input type="text" required="" name="nombre_usuario" value="<?php echo $rowusu['nombre']; ?>" hidden="">
                                            <input type="text" required="" name="referencia_producto" value="<?php echo $referencia; ?>" hidden="">

                                            <div class="col-md-12">
                                                <label class="form-label" for="validationCustom01">Fecha</label>
                                                <input class="form-control" id="validationCustom01" type="date" required="" name="fecha" value="<?php echo date('Y-m-d'); ?>">
                                                <div class="valid-feedback">Se ve bien!</div>
                                                <div class="invalid-feedback">Por favor, seleccione una fecha.</div>
                                            </div>
                                            <div class="col-md-12 mt-3">
                                                <label class="form-label" for="validationCustom02">Tercero</label>
                                                <select class="form-select custom-select"  required="" name="nombre_tercero" autofocus="">
                                                    <option selected="" disabled="" value="">Seleccione</option>
                                                    <?php
                                                    $sql6m = "SELECT * FROM alm_tercero";
                                                    $query6m = $conn->query($sql6m);
                                                    if ($query6m->num_rows > 0) {
                                                        while ($row6m = $query6m->fetch_assoc()) {
                                                            ?>

                                                            <option value="<?php echo $row6m['nombre'] ?>"><?php echo $row6m['nombre'] ?></option>
                                                            <?php
                                                        }
                                                    }
                                                    ?>
                                                </select>
                                                <div class="valid-feedback">Se ve bien!</div>
                                                <div class="invalid-feedback">Por favor, Seleccione una talla.</div>
                                            </div>
                                            <div class="col-md-6 mt-3">
                                                <label class="form-label" for="validationCustom02">Talla</label>
                                                <select class="form-select custom-select" id="validationCustom04" required="" name="talla" autofocus="">
                                                    <option selected="" disabled="" value="">Seleccione</option>
                                                    <?php
                                                    for ($i = 10; $i < 50; $i++) {
                                                        ?>
                                                        <option value="<?php echo $i ?>">Talla <?php echo $i ?></option>
                                                        <?php
                                                    }
                                                    ?>
                                                </select>
                                                <div class="valid-feedback">Se ve bien!</div>
                                                <div class="invalid-feedback">Por favor, Seleccione una talla.</div>
                                            </div>
                                            <div class="col-md-6 mt-3">
                                                <label class="form-label" for="validationCustomUsername">Cantidad</label>
                                                <input class="form-control" id="validationCustom02" type="text" required="" name="cantidad">
                                                <div class="valid-feedback">Se ve bien!</div>
                                                <div class="invalid-feedback">Por favor, Digite la cantidad de prendas.</div>
                                            </div>
                                            <div class="col-md-12 mt-3">
                                                <label class="form-label" for="validationCustomUsername">Precio unidad</label>
                                                <input class="form-control numero-input" id="" type="text" required="" name="precio_unidad">
                                                <div class="valid-feedback">Se ve bien!</div>
                                                <div class="invalid-feedback">Por favor, Digite la cantidad de prendas.</div>
                                            </div>

                                            <div class="col-md-12 mt-3">
                                                <label class="form-label" for="validationCustom02">Observacion</label>
                                                <textarea class="form-control" id="validationCustom02" type="text" required=""  name="observacion">No aplica</textarea>
                                                <div class="valid-feedback">Se ve bien!</div>
                                                <div class="invalid-feedback">Por favor, Digite una descripcion.</div>
                                            </div>


                                            <div class="col-12 text-end">
                                                <button class="btn btn-success" type="submit">Guardar</button>
                                            </div>

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


                                <div class="col-12 col-sm-9">


                                    <h6><b>DEVOLUCION</b></h6>
                                    <div class="table-responsive">
                                        <table class="table table-striped  mb-0 table-hover table-bordered" id="">
                                            <thead>
                                                <tr class="bg-tianzi">
                                                    <th class="text-center">FECHA</th>
                                                    <th class="">TERCERO</th>
                                                    <th class="text-center">TALLA</th>
                                                    <th class="text-center">CANTIDAD</th>
                                                    <th class="text-center">PRECIO UNIDAD</th>
                                                    <th class="text-center">TOTAL</th>
                                                    <th class="">OBSERVACION</th>
                                                    <th class="">MOVIMIENTO</th>
                                                    <th class="text-center" colspan="2">ACCIONES</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                $sql3 = "SELECT * FROM alm_novedad WHERE  referencia_producto='$referencia'  AND tipo_novedad='Devolucion' ";
                                                $query3 = $conn->query($sql3);
                                                if ($query3->num_rows > 0) {
                                                    while ($row3 = $query3->fetch_assoc()) {
                                                        ?>
                                                        <tr class="align-middle">
                                                            <td class="text-center"><?php echo strftime('%d - %B - %Y', strtotime($row3['fecha'])); ?></td>
                                                            <td class=""><?php echo $row3['nombre_tercero']; ?></td>
                                                            <td class="text-center">
                                                                <strong>T - <?php echo $row3['talla']; ?></strong>
                                                            </td>
                                                            <td class="text-center"><?php echo $row3['cantidad']; ?></td>
                                                            <td class="text-end"><b class="float-start">$</b> <?php echo number_format($row3['precio_unidad'], 0); ?></td>
                                                            <td class="text-end">
                                                                <b class="float-start">$</b> 
                                                                <b><?php echo number_format($row3['precio_unidad'] * $row3['cantidad'], 0); ?></b>
                                                            </td>
                                                            <td class=""><?php echo $row3['observacion']; ?></td>
                                                            <td class="text-center">
                                                                <?php if ($row3['movimiento'] == 'Salida') { ?>
                                                                    <span class="badge alert-danger"><?php echo $row3['movimiento']; ?></span>
                                                                    <?php
                                                                } else {
                                                                    ?>
                                                                    <span class="badge alert-success"><?php echo $row3['movimiento']; ?></span>

                                                                    <?php
                                                                }
                                                                ?>
                                                            </td>

                                                            <td class="text-center">
                                                                <a class="" href="#!" data-bs-toggle="modal" data-bs-target="#editar<?php echo $row3['id'] ?>">
                                                                    <i class="far fa-edit opacity-5  text-warning"></i>
                                                                </a>

                                                                <div class="modal fade text-start" id="editar<?php echo $row3['id']; ?>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                                                    <div class="modal-dialog modal-sm">
                                                                        <div class="modal-content">
                                                                            <div class="modal-header bg-warning">
                                                                                <h5 class="modal-title card-heading" id="exampleModalLabel">Editar movimiento</h5>
                                                                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                                                            </div>
                                                                            <form class=" g-3 needs-validation" novalidate="" method="POST" action="script/devolucion-editar.php">
                                                                                <input type="text" required="" name="id" value="<?php echo $row3['id']; ?>"  hidden>
                                                                                <input type="text" required="" name="referencia_producto" value="<?php echo $referencia; ?>" hidden>
                                                                                <input type="text" required="" name="nombre_usuario" value="<?php echo $rowusu['nombre']; ?>" hidden>

                                                                                <div class="modal-body ">
                                                                                    <div class="row ">
                                                                                        <div class="col-md-12">
                                                                                            <label class="form-label" for="validationCustom01">Fecha</label>
                                                                                            <input class="form-control" id="validationCustom01" type="date" required="" name="fecha" value="<?php echo $row3['fecha']; ?>">
                                                                                            <div class="valid-feedback">Se ve bien!</div>
                                                                                            <div class="invalid-feedback">Por favor, seleccione una fecha.</div>
                                                                                        </div>
                                                                                        <div class="col-md-12 mt-3">
                                                                                            <label class="form-label" for="validationCustom02">Tercero</label>
                                                                                            <select class="form-select"  required="" name="nombre_tercero" autofocus="">
                                                                                                <option value="<?php echo $row3['nombre_tercero']; ?>"><?php echo $row3['nombre_tercero']; ?></option>
                                                                                                <option disabled="" value="">Seleccione</option>
                                                                                                <?php
                                                                                                $sql6m = "SELECT * FROM alm_tercero ";
                                                                                                $query6m = $conn->query($sql6m);
                                                                                                if ($query6m->num_rows > 0) {
                                                                                                    while ($row6m = $query6m->fetch_assoc()) {
                                                                                                        ?>

                                                                                                        <option value="<?php echo $row6m['nombre'] ?>"><?php echo $row6m['nombre'] ?></option>
                                                                                                        <?php
                                                                                                    }
                                                                                                }
                                                                                                ?>
                                                                                            </select>
                                                                                            <div class="valid-feedback">Se ve bien!</div>
                                                                                            <div class="invalid-feedback">Por favor, Seleccione una talla.</div>
                                                                                        </div>
                                                                                        <div class="col-md-6 mt-3">
                                                                                            <label class="form-label" for="validationCustom02">Talla</label>
                                                                                            <select class="form-select custom-select" id="validationCustom04" required="" name="talla" autofocus="">
                                                                                                <option value="<?php echo $row3['talla']; ?>"><?php echo $row3['talla']; ?></option>
                                                                                                <option disabled="" value="">Seleccione</option>
                                                                                                <?php
                                                                                                for ($i = 4; $i < 23; $i++) {
                                                                                                    ?>
                                                                                                    <option value="<?php echo $i ?>">Talla <?php echo $i ?></option>
                                                                                                    <?php
                                                                                                    $i = $i + 1;
                                                                                                }
                                                                                                ?>
                                                                                            </select>
                                                                                            <div class="valid-feedback">Se ve bien!</div>
                                                                                            <div class="invalid-feedback">Por favor, Seleccione una talla.</div>
                                                                                        </div>
                                                                                        <div class="col-md-6 mt-3">
                                                                                            <label class="form-label" for="validationCustomUsername">Cantidad</label>
                                                                                            <input class="form-control" id="validationCustom02" type="text" required="" name="cantidad" value="<?php echo $row3['cantidad']; ?>">
                                                                                            <div class="valid-feedback">Se ve bien!</div>
                                                                                            <div class="invalid-feedback">Por favor, Digite la cantidad de prendas.</div>
                                                                                        </div>
                                                                                        <div class="col-md-12 mt-3">
                                                                                            <label class="form-label" for="validationCustomUsername">Precio unidad</label>
                                                                                            <input class="form-control numero-input" id="" type="text" required="" name="precio_unidad" value="<?php echo $row3['precio_unidad']; ?>">
                                                                                            <div class="valid-feedback">Se ve bien!</div>
                                                                                            <div class="invalid-feedback">Por favor, Digite la cantidad de prendas.</div>
                                                                                        </div>

                                                                                        <div class="col-md-12 mt-3">
                                                                                            <label class="form-label" for="validationCustom02">Observacion</label>
                                                                                            <textarea class="form-control" id="validationCustom02" type="text" required=""  name="observacion"><?php echo $row3['observacion']; ?></textarea>
                                                                                            <div class="valid-feedback">Se ve bien!</div>
                                                                                            <div class="invalid-feedback">Por favor, Digite una descripcion.</div>
                                                                                        </div>
                                                                                    </div>

                                                                                </div>
                                                                                <div class="modal-footer">
                                                                                    <button type="button" class="btn btn-secondary  radius-30" data-bs-dismiss="modal">Cancelar</button>
                                                                                    <button type="submit" class="btn btn-warning  radius-30" >Guardar cambios</button>
                                                                                </div>
                                                                            </form>
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                            </td>
                                                            <td class="text-center">
                                                                <a class="" href="#!" data-bs-toggle="modal" data-bs-target="#eliminar<?php echo $row3['id'] ?>">
                                                                    <i class="far fa-trash-alt opacity-5 text-danger"></i>        
                                                                </a>

                                                                <div class="modal fade" id="eliminar<?php echo $row3['id'] ?>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                                                    <div class="modal-dialog modal-sm">
                                                                        <div class="modal-content">
                                                                            <div class="modal-header bg-danger">
                                                                                <h5 class="modal-title card-heading text-white" id="exampleModalLabel">Eliminar</h5>
                                                                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                                                            </div>
                                                                            <form class=" g-3 needs-validation" novalidate="" method="POST" action="script/devolucion-eliminar.php">
                                                                                <input class="form-control" type="text" required=""  name="referencia_producto" value="<?php echo $referencia; ?>" hidden >

                                                                                <input class="form-control" type="text" required="" name="id"  value="<?php echo $row3['id'] ?>" hidden>
                                                                                <div class="modal-body ">
                                                                                    ¿Seguro deseas eliminar este registro?

                                                                                </div>
                                                                                <div class="modal-footer">
                                                                                    <button type="button" class="btn btn-secondary  radius-30" data-bs-dismiss="modal">Cancelar</button>
                                                                                    <button type="submit" class="btn btn-danger  radius-30" >Eliminar</button>
                                                                                </div>
                                                                            </form>
                                                                        </div>
                                                                    </div>
                                                                </div>


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



                </div>

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



    </body>
</html>