<?php $r="../";
require_once($r."inc/funciones.php");
$titlepage='Lista de Presentaciones';
validNotSession($r);
$spe=$_SESSION['perfil'];
$mysqli = connectDB($r);
roleaccess(2601);
$sqlKg = $mysqli->query("SELECT * FROM z_presentaciones_productos WHERE unidad = 'Kilo' AND oculto = 'no' ORDER BY `z_presentaciones_productos`.`cantidad` ASC");
$sqlLt = $mysqli->query("SELECT * FROM z_presentaciones_productos WHERE unidad = 'Litro' AND oculto = 'no' ORDER BY `z_presentaciones_productos`.`cantidad` ASC");
$sqlPz = $mysqli->query("SELECT * FROM z_presentaciones_productos WHERE unidad = 'Pieza' AND oculto = 'no' ORDER BY `z_presentaciones_productos`.`cantidad` ASC");



?>
  <?php include $r."inc/head.php"; ?>
  <style>
      .container-fluid{padding-right:5px;padding-left:5px;}
  </style>
<body class="fixed-nav sticky-footer sidenav-toggled bg-dark" id="page-top">
  <?php include $r."inc/nav.php"; ?>
  <div class="content-wrapper">
    <div class="container-fluid">
        <h3 class="mb-2">Porcentaje de Ganacia por tipo de Cliente <i class="btn btn-primary fa fa-plus float-right" onclick="nuevo()"></i></h3>
        
        <ul class="nav nav-tabs" id="tabPresentaciones" role="tablist">
          <li class="nav-item">
            <a class="nav-link active" id="kg-tab" data-toggle="tab" href="#kg" role="tab">Solidos</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" id="lt-tab" data-toggle="tab" href="#lt" role="tab">Liquidos</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" id="lt-tab" data-toggle="tab" href="#pz" role="tab">Piezas</a>
          </li>
        </ul>
        <div class="tab-content mt-3" id="tabContent">
        
          <!-- TAB KG -->
          <div class="tab-pane fade show active" id="kg" role="tabpanel">
        
            <div class="table-responsive">
              <table class="table table-bordered" id="dataTableKg" width="100%">
                <thead>
                  <tr>
                    <th>Presentacion</th>
                    <th>Masa</th>
                    <th>% Ganancia</th>
                    <th>Predeterminado</th>
                    <th><i class="fa fa-cog"></i></th>
                  </tr>
                </thead>
                <tbody>
                  <? while($row1 = $sqlKg->fetch_assoc()) { ?>
                  <tr>
                    <td><?= $row1['presentacion']; ?></td>
                    <td><?= $row1['cantidad']; ?></td>
                    <td><?= $row1['ganancia']; ?></td>
                    <td class="text-uppercase"><?= $row1['predeterminado']; ?></td>
                    <td width="20px">
                      <a class="btn text-primary fa fa-pencil"
                         onclick="editar(<?= $row1['id']; ?>,'<?= $row1['presentacion']; ?>',<?= $row1['cantidad']; ?>,<?= $row1['ganancia']; ?>,'<?= $row1['unidad']; ?>','<?= $row1['predeterminado']; ?>','<?= $row1['actualizar']; ?>')"></a>
                    </td>
                  </tr>
                  <? } ?>
                </tbody>
              </table>
            </div>
        
          </div>
        
          <!-- TAB LT -->
          <div class="tab-pane fade" id="lt" role="tabpanel">
        
            <div class="table-responsive">
              <table class="table table-bordered" id="dataTableLt" width="100%">
                <thead>
                  <tr>
                    <th>Presentacion</th>
                    <th>Volumen</th>
                    <th>% Ganancia</th>
                    <th>Predeterminado</th>
                    <th><i class="fa fa-cog"></i></th>
                  </tr>
                </thead>
                <tbody>
                  <? while($row2 = $sqlLt->fetch_assoc()) { ?>
                  <tr>
                    <td><?= $row2['presentacion']; ?></td>
                    <td><?= $row2['cantidad']; ?></td>
                    <td><?= $row2['ganancia']; ?></td>
                    <td class="text-uppercase"><?= $row2['predeterminado']; ?></td>
                    <td width="20px">
                      <a class="btn text-primary fa fa-pencil"
                         onclick="editar(<?= $row2['id']; ?>,'<?= $row2['presentacion']; ?>',<?= $row2['cantidad']; ?>,<?= $row2['ganancia']; ?>,'<?= $row2['unidad']; ?>','<?= $row2['predeterminado']; ?>','<?= $row2['actualizar']; ?>')"></a>
                    </td>
                  </tr>
                  <? } ?>
                </tbody>
              </table>
            </div>
        
          </div>
          
          <!-- TAB LT -->
          <div class="tab-pane fade" id="pz" role="tabpanel">
        
            <div class="table-responsive">
              <table class="table table-bordered" id="dataTablePz" width="100%">
                <thead>
                  <tr>
                    <th>Presentacion</th>
                    <th>Volumen</th>
                    <th>% Ganancia</th>
                    <th>Predeterminado</th>
                    <th><i class="fa fa-cog"></i></th>
                  </tr>
                </thead>
                <tbody>
                  <? while($row3 = $sqlPz->fetch_assoc()) { ?>
                  <tr>
                    <td><?= $row3['presentacion']; ?></td>
                    <td><?= $row3['cantidad']; ?></td>
                    <td><?= $row3['ganancia']; ?></td>
                    <td class="text-uppercase"><?= $row3['predeterminado']; ?></td>
                    <td width="20px">
                      <a class="btn text-primary fa fa-pencil"
                         onclick="editar(<?= $row3['id']; ?>,'<?= $row3['presentacion']; ?>',<?= $row3['cantidad']; ?>,<?= $row3['ganancia']; ?>,'<?= $row3['unidad']; ?>','<?= $row3['predeterminado']; ?>','<?= $row3['actualizar']; ?>')"></a>
                    </td>
                  </tr>
                  <? } ?>
                </tbody>
              </table>
            </div>
        
          </div>
        
        </div>

    </div>
  </div>
  <!-- Modal Presentacion-->
<div class="modal fade" id="modalpresent" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Presentacion</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form id="formulario" action="acciones.php" method="POST">
          <input type="hidden" name="actionpresent">

          <div class="form-group">
            <label>Presentacion</label>
            <input type="text" class="form-control" name="presentacion">
          </div>

          <div class="form-group">
            <label>Unidades</label>
            <input type="number" class="form-control" step="0.001" name="cantidad">
            <small>cantidad en kilogramos de la presentacion</small>
          </div>

          <div class="form-group">
            <label>Ganancia </label>
            <input type="number" name="ganancia" class="form-control" step="0.01">
            <small>A partir del Kg</small>
          </div>

          <div class="form-group">
            <label>Unidad principal </label>
            <select class="form-control" name="unidad">
              <option selected disabled>Seleccione la unidad principal</option>
              <option value="Kilo">KILO</option>
              <option value="Litro">LITRO</option>
              <option value="Pieza">PIEZA</option>
            </select>
          </div>

          <!-- NUEVO: Predeterminado -->
          <div class="form-group">
            <label>¿Es presentación predeterminada?</label>
            <select class="form-control" name="predeterminado">
              <option value="no" selected>NO</option>
              <option value="si">SI</option>
            </select>
            <small>Si es SI, todos los productos usarán esta presentación por defecto.</small>
          </div>


          <div class="form-group form-check">
            <input type="checkbox" class="form-check-input" id="actualizar_todos" name="actualizar_todos" value="1">
            <label class="form-check-label" for="actualizar_todos">
              Actualizar ganancia en todos los productos con esta presentación
            </label>
          </div>

        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-dismiss="modal">
          <i class="fa fa-close"></i> CERRAR
        </button>
        <button form="formulario" type="submit" class="btn btn-primary">
          <i class="fa fa-save"></i> GUARDAR
        </button>
      </div>
    </div>
  </div>
</div>

  <?php include $r."inc/foot.php"; ?>
  
  <script>
        $(document).ready(function () {
            $('#dataTableKg').DataTable({
                stateSave: true,
                stateDuration: -1, // guarda para siempre
                "lengthMenu": [[25, 50, 100, 500, -1], [25, 50, 100, 500, "Todos"]],
                "order": [[1, "asc"]],
                <?php if($actionexcel){ ?>
                "dom": '<"top"lfB>rt<"bottom"ip><"clear">',
                "buttons": ['copy','csv','excel','pdf','print'],
                <?php } ?>
            });
            
            $('#dataTableLt').DataTable({
                stateSave: true,
                stateDuration: -1,
                "lengthMenu": [[25, 50, 100, 500, -1], [25, 50, 100, 500, "Todos"]],
                "order": [[1, "asc"]],
                <?php if($actionexcel){ ?>
                "dom": '<"top"lfB>rt<"bottom"ip><"clear">',
                "buttons": ['copy','csv','excel','pdf','print'],
                <?php } ?>
            });
            
            
            $('#dataTablePz').DataTable({
                stateSave: true,
                stateDuration: -1,
                "lengthMenu": [[25, 50, 100, 500, -1], [25, 50, 100, 500, "Todos"]],
                "order": [[1, "asc"]],
                <?php if($actionexcel){ ?>
                "dom": '<"top"lfB>rt<"bottom"ip><"clear">',
                "buttons": ['copy','csv','excel','pdf','print'],
                <?php } ?>
            });
    
        });


        function nuevo(){
            $('#modalpresent').modal('show');
            $('input[name=actionpresent]').removeAttr('value');
            $('#formulario')[0].reset();
        }
        function editar(id,presentacion,cantidad,ganancia,unidad,predeterminado,actualizar){
            $('#modalpresent').modal('show');
            $('input[name=actionpresent]').val(id);
            $('input[name=presentacion]').val(presentacion);
            $('input[name=cantidad]').val(cantidad);
            $('select[name=unidad]').val(unidad);
            $('input[name=ganancia]').val(ganancia);
            $('select[name=predeterminado]').val(predeterminado);
            if (actualizar == 1) {
              $('#actualizar_todos').prop('checked', true);
            } else {
              $('#actualizar_todos').prop('checked', false);
            }

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