<?php
ob_start();
ini_set('session.gc_maxlifetime', 14400);
session_set_cookie_params(14400);
session_start();

if (!isset($_SESSION["nombre"]))
{
  header("Location: index.php");
}
else
{
    require '../header.php';

if($_SESSION['ventas']==1){

      $sql="SELECT count(idventa)+1 as contador FROM venta ORDER BY idventa desc";
      $respuesta= ejecutarConsulta($sql);

      $fila = mysqli_fetch_assoc($respuesta);
      $con= '000'.$fila['contador'];
      
      $estado_pedido_activo=$_SESSION["estado_pedido"];
     
?>

<style>
    .nuevo-cliente {
        color: #6c757d; /* Gris plomo */
        text-decoration: none; /* Quita el subrayado */
    }
    .nuevo-cliente:hover {
        color: #5a6268; /* Gris más oscuro al pasar el mouse */
    }
</style>
        
            <div class="content-wrapper">
                <!-- Breadcrumb Section -->
                <section class="content-header">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-sm-12">
                                <nav aria-label="breadcrumb">
                                    <ol class="breadcrumb">
                                        <li class="breadcrumb-item">
                                            <a href="#"><i class="fas fa-home"></i> Panel</a>
                                        </li>
                                        <li class="breadcrumb-item active" aria-current="page">
                                            <a href="list.php">
                                                <i class="module-icon"></i> Venta 
                                            </a>
                                        </li>
                                    </ol>
                                </nav>
                            </div>
                          
                            <div class="col-sm-12" style="margin-top: 20px;">
                                <form class="form" method="post"  id="frmForm" autocomplete="off">
                                    <div class="card">
                                        <div class="card-header">
                                            <h3 class="card-title" id="idtitle" >
                                                <i class="fas fa-plus"></i> <!-- Icono para agregar -->
                                                Registro Venta Productos<!-- Título del formulario -->
                                            </h3>
                                        </div>
                                        <div class="card-body" style="display: block;">
                                            <div class="container-fluid">
                                                 <div class="form-group col-lg-4 col-md-4 col-sm-4 col-xs-12">
                                                    <strong>NRO BOLETA: </strong><strong id="nrocorrelativo_texto" name="nrocorrelativo_texto" style="background-color: green; color: white; padding: 5px; border-radius: 3px;"><?php echo $con; ?> </strong>
                                                    <input type="hidden" name="nrocorrelativo" id="nrocorrelativo" value="<?php echo $con; ?>">
                                                </div>
                                                <input type="hidden" name="idventa" id="idventa">
                                                <div class="row">
                                                    <!-- Primera columna -->
                                                    <div class="col-12 col-sm-6 col-md-4">
                                                        <div class="form-group">
                                                            <label for="idpersona">Cliente:</label>
                                                            <a href="#" class="text-secondary" data-toggle="modal" data-target="#nuevoClienteModal">[+ Nuevo]</a>
                                                            <select id="idpersona" name="idpersona" class="form-control select2" data-live-search="true" required="" >
                                                            </select>
                                                        </div>
                                                    </div>
                                                    <div class="col-12 col-sm-6 col-md-4">
                                                        <div class="form-group">
                                                            <label for="nroguia">Numero - Guia:</label>
                                                            <input type="text" class="form-control" name="nroguia" id="nroguia" required="" placeholder="Numero - Guia">
                                                        </div>
                                                    </div>
                                                    <div class="col-12 col-sm-6 col-md-4">
                                                        <div class="form-group">
                                                            <label>Fecha Venta(*):</label>
                                                            <?php
                                                            $fechaActual = date("Y-m-d"); // Obtiene la fecha actual en formato yyyy-mm-dd
                                                            if ($_SESSION["estado_fecha"] == 1) {
                                                            ?>
                                                                <input type="date" class="form-control" name="fecha_hora" id="fecha_hora" required>
                                                            <?php
                                                            } else {
                                                            ?>
                                                                <input type="date" class="form-control" name="fecha_hora" id="fecha_hora" value="<?= $fechaActual ?>" readonly>
                                                            <?php
                                                            }
                                                            ?>
                                                        </div>
                                                    </div>
                                                    <div class="col-12 col-sm-12 col-md-4">
                                                        <div class="form-group">
                                                            <label >Tipo Comprobante:</label>
                                                            <select  name="tipo_comprobante" id="tipo_comprobante" class="form-control select2"   data-live-search="true" data-style="btn-default" >
                                                            <option value="1">Boleta</option>
                                                            <option value="2">Factura</option>
                                                            <option value="3">Ticket</option>
                                                            </select>
                                                        </div>
                                                    </div>
                                                  
                                                    <div class="col-12 col-sm-12 col-md-3">
                                                        <div class="form-group">
                                                            <label >Tipo Venta:</label>
                                                                <select  class="form-control" name="tipoventa" id="tipoventa" data-live-search="true" data-style="btn-default" required >
                                                                    <option value="">Selecciones Tipo Venta</option>
                                                                    <option value="1">Contado</option>
                                                                    <option value="2">Acuenta</option>
                                                                </select>
                                                        </div>
                                                    </div>
                                                    <div class="col-12 col-sm-12 col-md-2">
                                                        <div class="form-group">
                                                            <br>
                                                            <button id="btnAgregarArt" type="button" class="btn btn-warning btn-xs btn-flat" data-toggle="modal" data-target="#myModalStock">
                                                                <span class="fa fa-plus"></span>&nbsp;&nbsp;Agregar productos
                                                            </button>
                                                        </div>
                                                    </div>
                                                    
                                                    <div id="tblcorte" class="form-group col-lg-12 col-md-12 col-sm-12 col-xs-12 table-responsive">
                                                        <table id="detalles" class="table table-striped table-bordered table-condensed table-hover">
                                                            <thead>
                                                            <th style="background-color:#000000;color: white;">Opciones</th>
                                                                <th style="background-color:#000000;color: white;">Producto</th>
                                                                <th style="background-color:#000000;color: white;">Color</th>
                                                                <th style="background-color:#000000;color: white;">Combinacion</th>
                                                                <th style="background-color:#000000;color: white;">Talla</th>
                                                                <th style="background-color:#000000;color: white;">Cantidad</th>
                                                                <th style="background-color:#000000;color: white;">Precio venta</th>
                                                                <th style="background-color:#000000;color: white;">Tipo</th>
                                                                <th style="background-color:#000000;color: white;">Subtotal</th>  
                                                            </thead>
                                                            <tbody>
                                                            </tbody>
                                                            <tfoot>
                                                            <tr>
                                                                <tr>
                                                                <th style="height:2px; text-align: right; font-size: 15px;" colspan="8">TIPO SERVICIO</th>
                                                                <th style="height:2px; text-align: right;" colspan="1">
                                                                     <select class="form-control" name="tipo_servicio" id="tipo_servicio" data-live-search="true" data-style="btn-default">
                                                                        <option value="0">Selecciones Tipo Servicio</option>
                                                                        <option value="1">Estampado</option>
                                                                        <option value="2">Bordado</option>
                                                                    </select>
                                                                    <br>
                                                                     <input type="number" step="0.01" class="form-control" name="total_servicio" id="total_servicio" placeholder="0.00" value="0.00" onkeyup="modificarSubtotales()" >
                                                                </th>
                                                                </tr>
                                                                <th style="height:2px; text-align: right; font-size: 15px;" colspan="8">COSTO ENVIO</th>
                                                                <th style="height:2px; text-align: right;" colspan="1">
                                                                    <input type="number" step="0.01" class="form-control" name="total_envio" id="total_envio" placeholder="0.00" value="0.00" onkeyup="modificarSubtotales()" >
                                                                </th>
                                                                </tr>
                                                                <th style="height:2px; text-align: right; font-size: 15px;" colspan="8">VENTA GRAVADO</th>
                                                                <th style="height:2px; text-align: right;" colspan="1">
                                                                    <input type="text" class="form-control" name="subtotal_total" id="subtotal_total" placeholder="0.00" readonly>
                                                                </th>
                                                                </tr>
                                                                <tr>
                                                                <th style="height:2px; text-align: right; font-size: 15px;" colspan="8">IGV 18%</th>
                                                                <th style="height:2px; text-align: right;" colspan="1">
                                                                    <input type="text" class="form-control" name="total_igv" id="total_igv" placeholder="0.00" readonly>
                                                                </th>
                                                                </tr>
                                                                <tr>
                                                                <th style="height:2px; text-align: right; font-size: 15px;" colspan="8">TOTAL VENTA</th>
                                                                <th style="height:2px; text-align: right;" colspan="1">
                                                                    <input type="text" class="form-control" name="total_venta" id="total_venta" placeholder="0.00" readonly>
                                                                </th>
                                                                </tr>
                                                                <th id="tipo-pago-th" style="height:2px; text-align: right; font-size: 15px;" colspan="8">TIPO PAGO</th>
                                                                    <th style="height:2px; text-align: right;" colspan="1">
                                                                        <select class="form-control" name="tipopago" id="tipopago" data-live-search="true" data-style="btn-default">
                                                                            <option value="">Selecciones Tipo Pago</option>
                                                                            <option value="1">Efectivo</option>
                                                                            <option value="2">Transferencia</option>
                                                                        </select>
                                                                        <br>
                                                                        <div id="titular-fields" style="display: none; text-align: left;">
                                                                            <div class="col-12 col-sm-12 col-md-12">
                                                                                <label style="text-align: left; display: block;">Titular:</label>
                                                                                <select class="form-control" type="text" name="idtitular" id="idtitular"></select>
                                                                            </div>
                                                                        </div>
                                                                        <div id="deposito-fields" style="display: none; text-align: left;">
                                                                            <div class="col-12 col-sm-12 col-md-12">
                                                                                <label style="text-align: left; display: block;">Banco:</label>
                                                                                <select class="form-control" type="text" name="idbanco" id="idbanco"></select>
                                                                            </div>
                                                                        </div>
                                                                    </th>
                                                            </tfoot>
                                                        </table>
                                                     </div>
                                                </div>  
                                            </div>
                                        </div>
                                        <div class="card-footer">
                                            <!-- Botones de acción -->
                                            <button type="submit" id="btnGuardar" class="btn btn-primary btn-flat">
                                                <i class="fas fa-save"></i> Guardar registro
                                            </button>
                                            <a href="list.php" class="btn btn-danger btn-flat">
                                                <i class="fas fa-times"></i> Cancelar
                                            </a>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </section>                
            </div>

            <div class="modal fade" id="myModalStock" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true">
                <div class="modal-dialog modal-lg" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="modalLabel"> Agregar Productos</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Cerrar">
                                <span>&times;</span>
                            </button>
                        </div>
                            <div class="modal-body">
                            <div class="row">
                                <?php if($_SESSION["estado_pedido"]==1){
                               ?>
                                <div class="col-12 col-sm-6 col-md-4">
                                    <div class="form-group">
                                        <label >Tipo Salida (Venta-Manual) :</label>
                                        <select id="tipo_salida" name="tipo_salida" class="form-control" data-live-search="true" required="" >
                                        <option value="1" selected>Venta</option>
                                        <option value="2">Manual</option>
                                        </select>
                                    </div>
                                </div>
                                <?php
                                 }
                                 ?>
                                <div class="col-12 col-sm-6 col-md-8">
                                    <div class="form-group">
                                        <label for="idproducto">Nombre Producto:</label>
                                        <select id="idproducto" name="idproducto" class="form-control select2" data-live-search="true" required="" >
                                        </select>
                                        <input type="hidden" id="precio_venta" name="precio_venta" class="form-control" >
                                    </div>
                                </div>
                                <div class="col-12 col-sm-6 col-md-4">
                                    <div class="form-group">
                                        <label >Nombre Color:</label>
                                        <select id="idcolor" name="idcolor" class="form-control select2" data-live-search="true" required="" >
                                        <option value="">Seleccione un Color</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-12 col-sm-6 col-md-4">
                                    <div class="form-group">
                                        <label >Nombre Combinacion:</label>
                                        <select id="idcombinacion" name="idcombinacion" class="form-control select2" data-live-search="true" required="" >
                                        <option value="">Seleccione una Combinacion</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-12 col-sm-6 col-md-4">
                                    <div class="form-group">
                                        <label >Tallas Disponibles:</label>
                                        <select id="idtalla" name="idtalla" class="form-control select2" data-live-search="true" required="" >
                                        <option value="">Seleccione una Talla</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-12 col-sm-6 col-md-4">
                                    <div class="form-group">
                                        <label >Cantidad Disponibles:</label>
                                        <input id="cantidad" name="cantidad" class="form-control" data-live-search="true" placeholder="Cantidad Stock" readonly="">
                                        <input type="hidden" id="idalmacen_central" name="idalmacen_central" class="form-control" >
                                       
                                    </div>
                                </div>
                                <div class="col-12 col-sm-12 col-md-12 text-right">
                                    <div class="form-group">
                                        <br>
                                        <button id="adicionar" type="button" class="btn btn-danger btn-xs btn-flat">
                                            <span class="fa fa-plus"></span>&nbsp;&nbsp;Adicionar
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div> 
                    </div>
                </div>
            </div>


            <div class="modal fade" id="nuevoClienteModal" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true">
                <div class="modal-dialog modal-lg" role="document">
                    <div class="modal-content">
                        <div class="modal-header bg-primary">
                            <h5 class="modal-title" id="modalLabel">Registro Cliente</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Cerrar">
                                <span>&times;</span>
                            </button>
                        </div>
                        <form id="clienteForm">
                            <div class="modal-body">
                                <div class="row">
                                <div class="col-12 col-sm-12 col-md-12">
                                        <div class="form-group">
                                            <label >Nombre Cliente:</label>
                                            <input type="text" class="form-control" name="nombre" id="nombre"  placeholder="Nombre Cliente" required>                                       
                                        </div>
                                    </div>
                                    <div class="col-12 col-sm-6 col-md-6">
                                        <div class="form-group">
                                            <label >Tipo Documento:</label>
                                            <select  name="idtipo_documento" id="idtipo_documento" class="form-control select2"  data-live-search="true" data-style="btn-default" >
                                            </select>                                      
                                        </div>
                                    </div>
                                    <div class="col-12 col-sm-6 col-md-6">
                                        <div class="form-group">
                                            <label >Numero Documento:</label>
                                            <input type="number" class="form-control" name="num_documento" id="num_documento"  placeholder="Numero Documento" >
                                            <small id="num_documento_help" class="form-text text-muted"></small>                                      
                                        </div>
                                    </div>
                                    <div class="col-12 col-sm-6 col-md-6">
                                        <div class="form-group">
                                            <label >Departamento:</label>
                                            <select  name="iddepartamento" id="iddepartamento" class="form-control select2"  data-live-search="true" data-style="btn-default" >
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-12 col-sm-6 col-md-6">
                                        <div class="form-group">
                                            <label for="numero_celular" >Numero Celular:</label>
                                            <input type="number" class="form-control" name="numero_celular" id="numero_celular"  placeholder="Numero Celular" >                                       
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="modal-footer">
                                    <button type="button" class="btn btn-secondary  btn-flat" data-dismiss="modal">Cerrar</button>
                                    <button type="submit" id="btnCliente" class="btn btn-primary btn-flat"> <i class="fas fa-save"></i> Registrar Cliente</button>
                                </div>
                               
                            </div>
                        </form>
                    </div>
                </div>
            </div>


               
<?php
}
require '../footer.php';
?>
<script type="text/javascript" src="../../scripts/venta.js"></script>

<?php 
}
ob_end_flush();
?>