  <?php
  session_start();
  ?>
  <script>

    $(function () {
//ready
//ready

const Toast = Swal.mixin({
  toast: true,
  position: 'top-end',
  showConfirmButton: false,
  timer: 3000
});
$(document).ready(function(e) {
});
var wp_table = $("#list_banks").DataTable({
  responsive: {
    details: {
      type: 'column'
    }
  },
  columnDefs: [ {
    className: 'control',
    orderable: false,
    targets:   0
  } ],
  order: [ 1, 'asc' ],
  autoWidth: true,
  bProcessing: false,
  bServerSide: false,
  bInfo: false,
  columns: [
  { data: 'id' , render: function(data, type, row){
    return cel(data, row.id);
  }},
  { data: 'name',className: "tdcentrado" },
  { data: 'cantidad',className: "tdcentrado" },
  { data: 'status' , render: function(data, type, row){
    return status(data, row.color);
  },className: "tdcentrado"},
  { data: 'id', render: function(data, type, row){
    return actions(data, row.name,row.level);
  },className: "tdcentrado"},
  ],
});
//Funcion para consultar datos de usarios y llenar tabla//
function dataTable_init() {
  data = {'option':'<?php echo $decryp->encryptroute('view/banks/query_banks')?>'};
  $.post('../core/process.php', data)
  .done(function(resp){
     wp_table = $("#list_banks").DataTable();
     wp_table.clear();
     $.each(resp.datos, function (k, v) {
      wp_table.row.add(v);
    });
     wp_table.draw();
     wp_table.columns.adjust().responsive.recalc();
 }).fail( function(jqXHR, textStatus, errorThrown) {
   var resp=jqXHR.responseJSON;
   var status=jqXHR.status;
   if(status == 401){
    toastr.error(resp.msg);
    logout();
  }else{
   toastr.error(resp.msg);
 }
});
}
//Funcion status//
function status(wpid, wpactive) {
  var status='<span class="badge badge-'+wpactive+'">'+wpid+'</span>'; 
  actions_cell = status;
  return actions_cell;
}
//Funcion status//
function name(wpid, wpactive) {
  var actions_cell='<span class="badge" style="background-color:'+wpid+';color:#fff;">'+wpactive+'</span> ';
  return actions_cell;
}
//Funcion para botones de accion//
function actions(wpid, wpactive,level) {
  actions_cell = '<button class="btn btn-primary edit_cat" data-data1="' + wpid + '" ><i class="fas fa-pencil-alt"></i></button> <button class="btn btn-danger delete" data-data1="' + wpid + '" ><i class="fa fa fa-trash"></i></button>';
  return actions_cell;
}
//Campo vacio para responsive//
function cel(wpid, wpactive) {
  actions_cell = '';
  return actions_cell;
}

dataTable_init();
//Abrir modal de propiedades para registrar//
$(document).on('click', '#add_modal', function(e){
  data = {'option':'<?php echo $decryp->encryptroute('add/N')?>'};
  $.post('../core/process.php', data)
  .done(function(resp){
    $('#addcat').modal('show'); 
    $('#addcat form').trigger('reset'); 
    $('#title_modal').html('Agregar Banco <?php echo$_SESSION['lang']['REQUERIDOS']?> ');
    $('#edit').hide();
    $('#save').show();
    $( "#status" ).prop( "checked", true );
    $('#sts').hide();
  }).fail( function(jqXHR, textStatus, errorThrown) {
   var resp=jqXHR.responseJSON;
   var status=jqXHR.status;
   if(status == 401){
    toastr.error(resp.msg);
    logout();
  }else{
   $('#addcat').modal('hide'); 
   toastr.error(resp.msg);
 }
});
});
//Funcion para registrar propiedades//
$(document).on('click', '#save', function(e){
  submit('#save');
  var name = $("#name").val();
  if (typeof name == 'undefined' || name === null || name === '') {  
   toastr.error('<?php echo $_SESSION['lang']['REQUERIDOS1']?>');
   finish('#save');
 }else {
  data = {'option':'<?php echo $decryp->encryptroute('add/banks/register_bank')?>','name':name};
  $.post('../core/process.php', data)
  .done(function(resp){
   $('#addcat').modal('hide'); 
   toastr.success(resp.msg);
   dataTable_init();
 }).fail( function(jqXHR, textStatus, errorThrown) {
   var resp=jqXHR.responseJSON;
   var status=jqXHR.status;
   if(status == 401){
    toastr.error(resp.msg);
    logout();
  }else{
   //$('#addcat').modal('hide'); 
   toastr.error(resp.msg);
 }
}).always( function() {
  finish('#save');
});
}
});

//Detalles de propiedad//
$(document).on('click', '.edit_cat', function(e){
  var id = $(this).data('data1');
  if (typeof id == 'undefined' || id === null || id === '') {  
    toastr.error('<?php echo $_SESSION['lang']['REQUERIDOS1']?>');
  } else {
    data = {'option':'<?php echo $decryp->encryptroute('view_/banks/view_bank')?>','id':id};
    $.post('../core/process.php', data)
    .done(function(resp){ 
      $('#addcat form').trigger('reset'); 
      $('#addcat').modal('show'); 
      $('#edit').show();
      $('#save').hide();
      $('#sts').show();
      $('#title_modal').html('Editar Banco <?php echo $_SESSION['lang']['REQUERIDOS']?>');
      $.each(resp.datos, function (k, v) {
        $('#id_rol').val(v.id);
        $('#name').val(v.name);
        if (v.status == '1') {
          $( "#status" ).prop( "checked", true );
        }else{
          $( "#status" ).prop( "checked", false );
        }
      });
    }).fail( function(jqXHR, textStatus, errorThrown) {
     var resp=jqXHR.responseJSON;
     var status=jqXHR.status;
     if(status == 401){
      toastr.error(resp.msg);
      logout();
    }else{
     toastr.error(resp.msg);
   }
 });
  }
});
//Funcion para registrar propiedades//
$(document).on('click', '#edit', function(e){
  submit('#edit');
  var id = $("#id_rol").val();
  var status=$("#status").is(':checked') ? 1 : 2;
  var name = $("#name").val();

  if (id === null || id === '' || typeof status == 'undefined' || status === null || status === '' || typeof name == 'undefined' || name === null || name === '' ) {  
   toastr.error('<?php echo $_SESSION['lang']['REQUERIDOS1']?>');
   finish('#edit');
 }else {
   data = {'option':'<?php echo $decryp->encryptroute('add/banks/edit_bank')?>','name':name,'status':status,'id':id};
  $.post('../core/process.php', data)
  .done(function(resp){
   $('#addcat').modal('hide'); 
   toastr.success(resp.msg);
   dataTable_init();
 }).fail( function(jqXHR, textStatus, errorThrown) {
   var resp=jqXHR.responseJSON;
   var status=jqXHR.status;
   if(status == 401){
    toastr.error(resp.msg);
    logout();
  }else{
   //$('#addcat').modal('hide'); 
   toastr.error(resp.msg);
 }
}).always( function() {
  finish('#edit');
});
}
});
//Funcion abrir modal para eliminar Producto// 
$(document).on('click', '.delete', function(e){
  var data1 = $(this).data('data1');
  $( '#delete_cat' ).data( 'data1', data1);
  $('#modal_eliminar').modal('show'); 
});
//Funcion eliminar Producto// 
$(document).on('click', '#delete_cat', function(e){
  submit('#delete_cat');
  var data1 = $(this).data('data1');
  if (typeof data1 == 'undefined' || data1 === null || data1 === '') {  
   toastr.error('<?php echo $_SESSION['lang']['REQUERIDOS1']?>');
   finish('#delete_cat');
   $('#modal_eliminar').modal('hide'); 
 } else {
  data = {'option':'<?php echo $decryp->encryptroute('delete/banks/delete_bank')?>','id':data1};
  $.post('../core/process.php', data)
  .done(function(resp){
   toastr.success(resp.msg);
 }).fail( function(jqXHR, textStatus, errorThrown) {
   var resp=jqXHR.responseJSON;
   var status=jqXHR.status;
   if(status == 401){
    toastr.error(resp.msg);
    logout();
  }else{
   toastr.error(resp.msg); 
 }
}).always( function() {
  $('#modal_eliminar').modal('hide');
  finish('#delete_cat');
  dataTable_init();
});
}
});


//END//
});
</script>