<?php
  $ruta="../../../";
  include_once($ruta."class/tipobanca.php");
  $tipobanca=new tipobanca;
  include_once($ruta."class/fgarantia.php");
  $fgarantia=new fgarantia;


  include_once($ruta."funciones/funciones.php");
  session_start();

   extract($_GET);
  $valor=dcUrl($lblcode);
  $datobanca = $tipobanca->muestra($valor);
?>
<!DOCTYPE html>
<html lang="es">
<head>
  <?php
    $hd_titulo="Administrar Banca ";
    include_once($ruta."includes/head_basico.php");
    include_once($ruta."includes/head_tabla.php");
    include_once($ruta."includes/head_tablax.php");
  ?>
  <style type="text/css">
      .estIn input{
      border:solid 1px #4286f4;
      width: 138px;
      background-color: white;
    }
    </style>
</head>
<body>
    <?php
      include_once($ruta."head.php");
    ?>
    <div id="main">
      <div class="wrapper">
        <?php
          $idmenu=1038;
          include_once($ruta."aside.php");
        ?>
        <section id="content">
          <!--breadcrumbs start-->
          <div id="breadcrumbs-wrapper">
            <div class="container">
              <div class="row">
                <div class="col s12 m12 l12">
                  <h5 class="breadcrumbs-title"><i class="fa fa-tag"></i> <?php echo $hd_titulo; ?></h5>
                </div>
              </div>
            </div>
          </div>
          <div class="container">
            <div class="section">
            <a href="../listar/" class="btn waves-effect waves-light blue"><i class="fa fa-reply"></i> Volver</a>
               
              <div class="col s12 m12 l6">
                <h1><?php echo $datobanca['nombre'] ?></h1>
                    <form class="col s12" id="idform" action="return false" onsubmit="return false" method="POST">
                      <input type="hidden" name="idfgar" id="idfgar" value="<?php echo $valor ?>">
                      <fieldset class="informacion">
                        <legend>REGISTAR PARAMETRO</legend>
                        <div class="row">
                          <div class="input-field col s12 m12 l2">
                           <label>CICLO</label>
                            <select id="idciclo" name="idciclo">
                              <option value="1">CICLO 1</option>
                              <option value="2">CICLO 2 EN ADELANTE</option>
                            </select>
                          </div>
                          <div class="input-field col s12 m12 l2">
                            <input id="idmontoMin" name="idmontoMin" value="0"  type="text" class="validate">
                            <label for="idmontoMin">Monto Minimo(%)</label>
                          </div>
                          <div class="input-field col s12 m12 l2">
                            <input id="idmontoMax" name="idmontoMax" value="0"type="number" class="validate">
                            <label for="idmontoMax">Monto Maximo(%)</label>
                          </div>
                          <div class="input-field col s12 m12 l2">
                            <input id="idporcentaje" name="idporcentaje" value="0"type="number" class="validate">
                            <label for="idporcentaje">Porcentaje(%)</label>
                          </div>
                          <div class="input-field col s12 m12 l2">
                            <button id="btnSave" class="btn purple"><i class="fa fa-save"></i> AGREGAR</button>
                          </div> 
                        </div>
                      </fieldset>
                    </form>
                    <div class="col s12 m12 l6">
                    <table id="example" class="display table" cellspacing="0" width="100%">
                      <thead>
                        <tr>
                          <th>ID</th>
                          <th>CICLO</th>
                          <th>MONTO INICIAL (BS)</th>
                          <th>MONTO MAXIMO(BS)</th>
                          <th>PORCENTAJE (%)</th>
                          <th>Acciones</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php
                        $sw=true;
                        foreach($fgarantia->mostrarTodo("idtipobanca=$valor") as $f)
                        {
                          $lblcode=ecUrl($f['idtipobanca']);
                          ?>
                          <tr>
                            <td><?php echo $f['idfgarantia'] ?></td>
                            <td>
                              <div class="estIn">
                                <input type="number" name="c<?php echo $f['idfgarantia'] ?>" id="c<?php echo $f['idfgarantia'] ?>" value="<?php echo $f['ciclo'] ?>" >
                              </div>
                            </td>
                            <td>
                              <div class="estIn">
                                <input type="number" name="mI<?php echo $f['idfgarantia'] ?>" id="mI<?php echo $f['idfgarantia'] ?>" value="<?php echo $f['montoIn'] ?>" >
                              </div>
                            </td>
                            <td>
                              <div class="estIn">
                                <input type="number" name="mF<?php echo $f['idfgarantia'] ?>" id="mF<?php echo $f['idfgarantia'] ?>" value="<?php echo $f['montoFin'] ?>" >
                              </div>
                            </td>
                            <td>
                              <div class="estIn">
                                <input type="number" name="pr<?php echo $f['idfgarantia'] ?>" id="pr<?php echo $f['idfgarantia'] ?>" value="<?php echo $f['porcentaje'] ?>" >
                              </div>
                            </td>
                            <td> 
                              <button onclick="guardaAJUSTE('<?php echo $f['idfgarantia'] ?>');" class="btn purple"><i class="fa fa-save"></i></button>
                              <a href="../parametros/?lblcode=<?php echo $lblcode ?>" class="btn waves-effect darken-4 red"><i class="fa fa-trash"></i></a> 
                            </td>
                          </tr>
                          <?php
                          }
                        ?>
                      </tbody>
                    </table>
              </div>
              </div>
            </div>
          </div>
          <?php
            include_once("../../footer.php");
          ?>
        </section>
      </div>
    </div>
    <div id="idresultado"></div>
    <!-- end -->
    <!-- jQuery Library -->
    <?php
      include_once($ruta."includes/script_basico.php");
      include_once($ruta."includes/script_tabla.php");
      include_once($ruta."includes/script_tablax.php");
    ?>
    <script type="text/javascript">
      function guardaAJUSTE(id){
        var ciclo=$('#c'+id).val();
        var montoIn=$('#mI'+id).val();
        var montoFin=$('#mF'+id).val();
        var porcentaje=$('#pr'+id).val();
        var str="ciclo="+ciclo+"&montoIn="+montoIn+"&montoFin="+montoFin+"&porcentaje="+porcentaje+"&idtipo="+id;
        $.ajax({
          url: "actualizar.php",
          type: "POST",
          data: str,
          success: function(resp){
            console.log(resp);
            $('#idresultado').html(resp);
          }
        });
      }
    $(document).ready(function() {
      $('#example').DataTable({
        dom: 'Bfrtip',
        buttons: [
            'copy', 'csv', 'excel', 'pdf', 'print'
        ]
      });
    
      
      });
    $("#btnSave").click(function(){
        if (validar()) {    
         $('#btnSave').attr("disabled",true);      
          swal({
            title: "CONFIRMACION",
            text: "Se registrara configuracion",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#2c2a6c",
            confirmButtonText: "Registrar",
            closeOnConfirm: false
          }, function () {
            var str = $( "#idform" ).serialize();
            // alert(str);
            $.ajax({
              url: "guardar.php",
              type: "POST",
              data: str,
              success: function(resp){
                console.log(resp);
                $("#idresultado").html(resp);
              }
            }); 
          }); 
        }
        else{
          swal("DATOS INCORRECTOS");
        } 
      });
      function validar(){
        retorno=true;
        montoIn=$('#idmontoMin').val();
        montoMax=$('#idmontoMax').val();
        porcentaje=$('#idporcentaje').val();

        if(montoIn<=0){
          retorno=false;
        }
        if(montoMax<=0){
          retorno=false;
        }
        if(porcentaje<=0){
          retorno=false;
        }
        return retorno;
      }
     
    </script>
</body>

</html>