{% extends "baseAdmin.html" %}
{% load static %}
{% block titulo %}
  .:: Configuracion de Montos de Comision de Bancos
{% endblock titulo %}
{% block content1 %} 
<link href="{% static 'Handson1/css/pikaday.css' %}" rel="stylesheet">
  <script src="{% static 'Handson1/js/pikaday.js' %}"></script>
  <script  src="{% static 'Handson1/js/ZeroClipboard.js' %}"></script>
  <script  src="{% static 'Handson1/js/handsontable.js' %}"></script>
  <link  href="{% static 'Handson1/css/handsontable.css' %}" rel="stylesheet">
  <link href="{% static 'Bootstrap/css/sweet-alert.css' %}" rel="stylesheet">
  <script src="{% static 'Bootstrap/js/sweet-alert.js' %}"></script>
<script type="text/javascript">
  //$("#navCajaPrincipal").attr('class','dropdown active');
  //$("#navEstadoCajas").attr('class','active');
</script>
<div class="container">
  <div class="row">
    <div class="col-md-12 col-sm-12" >
        <div class="panel panel-default">
          <div class="panel-heading"> <strong>COMFIGURACION DE MONTOS PARA BANCOS</strong></div>
          <div class="panel-body">
            <div class="row">
              <div class="col-md-2">
                <button type="button" class="btn btn-block btn-info btn-sm" id="btnRefrescar"><span class="fa fa-refresh"></span> Actualizar</button>
              </div>
            </div>
            <div class="row">
              <div class="checkbox"  style="margin-left:15px;">
                  <label class="">
                    <input id="ts4" type="checkbox" value="success-inverse1"> Eliminar todas las comisiones antes de Añadir
                  </label>
              </div> 
            </div>
            <div class="row">
              <h4  style="margin-left:15px;">TABLA DE MONTOS DE BANCOS CON COMISIONES</h4>
            </div>

             <div class="row">
                <div class="col-md-2 col-md-offset-1">
                  <input type="search" class="form-control" id="buscar" placeholder="buscar en celda">
                </div>
                <div class="col-md-9">
                  <div id="example1" style="width: auto; height: 500px; overflow: hidden;"></div>
                </div>
             </div>
             <br>
             <div class="row">
                <div class="col-md-2 col-md-offset-5">
                  <input class="btn btn-danger btn-block" type="button"id="guardar" value="Guardar">
                </div>
             </div>
             <br>
          </div>
        </div>
    </div>
  </div>
</div>

<script  type="text/javascript" charset="utf-8">
    var ors=[];
    var dts=[];
    var Aux='';
    var fila=0;
    var auto=["123456","1111"];
    $(document).ready(function (){
      //var dniValidator = /^(([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]([0-9][0-9][0-9])?)?)$/;
      var $container = $("#example1");
      //var aux1='<button type="button"style="color: #fff;background-color: #337ab7;border-color: #2e6da4;"><span class="glyphicon glyphicon-search" aria-hidden="true"></span></button>';
      //var aux2='<button type="button"style="color: #fff;background-color: #5cb85c;border-color: #4cae4c;">Imprimir</button>';

      $container.handsontable({
        //data: data,
        startRows: 2,
        startCols: 7,
        rowHeaders: true,
        search: true,
        colHeaders: [ 'Monto Minimo','Monto Maxsimo','Banco Nacion','Continental','BCP','otros'],
        columns: [
                  {
                    type: 'numeric',
                    format: '0,0.00'
                  },
                  {
                    type: 'numeric',
                    format: '0,0.00'
                  },
                  {
                    
                  },
                  {
                    
                  },
                  {
                    
                  },
                  {
                    
                  }
                ],
        minSpareRows: 1,
        contextMenu: true
      });
      var hotInstance = $("#example1").handsontable('getInstance');

      function onlyExactMatch(queryStr, value) {
        return queryStr.toString() === value.toString();
      }
      document.querySelector('#guardar').onclick = function(){
        swal({
          title: "Guardar Configuracion",
          type: "warning",
          showCancelButton: true,
          confirmButtonClass: 'btn-danger',
          confirmButtonText: 'Guardar',
          closeOnConfirm: false,
          //closeOnCancel: false
        },
          function(){
            //EnviarCompra();
            /*
            console.log(hotInstance.getData());
            console.log(ors);
            console.log(dts);
            */
            var elimina="no"
            if($("#ts4").prop("checked")){
              elimina="si";
            }
            ajax_guardarComiciones(elimina);
            //alert(JSON.stringify(hotInstance.getData()));
        });
      };
      
      $("#buscar").keyup(function(){
        var queryResult = hotInstance.search.query(this.value);
        //console.log(queryResult);
        hotInstance.render();
      });
      hotInstance.updateSettings({
        afterSelection: function (e) {
          var pos = hotInstance.getSelected();
          if(pos[1]==2){
            console.log("hola");
          }
        }
      }
    );
  //------------------
  function preprocesarData(a){
    for (var i = 0; i < a.length-1; i++) {
      if(a[i][0]==null ||a[i][1]==null || a[i][2]==null){
        swal("","Revice la configuracion", "error");
      }
    }
    a.splice(a.length-1, 1);
    return a;
  }
  function ajax_guardarComiciones(el){
    //var dataTable=hotInstance.getData()
      //dataTable.pop();
      $.ajax({
        url:'/Guardar_Comisiones_Bancos_general_ajax/',
        data:{'data':JSON.stringify(preprocesarData(hotInstance.getData())),'eliminar':el},
        type:'GET',
        dataType:'text',
        success: function(data){
          console.log(data);
         if(data=='exito'){
          swal("Exito","las comiciones se guardaron", "success");
         }
         else{
          //bootbox.alert(data);
          swal("Error",data, "error");
         }
        }
      });
    };
  //------
  function ajustarTabla(n){
    var t=hotInstance.countRows();
      for(var i=0;i<t;i++){
        hotInstance.alter('remove_row',0);
      }  
      for(var j=0;j<n;j++){
        hotInstance.alter('insert_row',0);
      } 
  }
  //--------
  function ListarConfiguraciones(){
    $.ajax({
      url:'/listado_configuracionComicionesBancos_Admin_ajax/',
      type:'GET',
      dataType:'json',
      success: function(data){
        ajustarTabla(data.length);
        var html='';
        for(var i=0;i<data.length;i++){
          hotInstance.setDataAtCell(i,0,data[i].montomin);
          hotInstance.setDataAtCell(i,1,data[i].montomax);
          hotInstance.setDataAtCell(i,2,data[i].banco_nacion);
          hotInstance.setDataAtCell(i,3,data[i].continental);
          hotInstance.setDataAtCell(i,4,data[i].bcp);
          hotInstance.setDataAtCell(i,5,data[i].otros_bancos);
        }                        
      }
    });
  }
  //--------------------
  $("#btnRefrescar").click(function(){
      ListarConfiguraciones()
  });
  //--------------------
});
</script>

{% endblock content1 %}