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

    $(function () {
//ready
//ready
$('.select2').select2();
$("#saldo").maskMoney({allowNegative: true, thousands:'.', decimal:',', affixesStay: false});

const Toast = Swal.mixin({
  toast: true,
  position: 'top-end',
  showConfirmButton: false,
  timer: 3000
});
$(document).ready(function(e) {
});
var wp_table = $("#list_bank_accounts").DataTable({
  responsive: {
    details: {
      type: 'column'
    }
  },
  columnDefs: [ {
    className: 'control dt-center',
    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: 'banco' ,className: "tdcentrado"},
  { data: 'titular' ,className: "tdcentrado"},
  { data: 'num_cuenta' ,className: "tdcentrado"},
  { data: 'saldo' ,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/bank_accounts/query_bank_accounts')?>'};
  $.post('../core/process.php', data)
  .done(function(resp){
   wp_table = $("#list_bank_accounts").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> <button class="btn btn-info edit_balance" data-data1="' + wpid + '" ><i class="fas fa-hand-holding-usd"></i></button>';
  return actions_cell;
}
//Campo vacio para responsive//
function cel(wpid, wpactive) {
  actions_cell = '';
  return actions_cell;
}

dataTable_init();
//Eliminar item a plan//
function bancos(){
  data = {'option':'<?php echo $decryp->encryptroute('view/bank_accounts/query_banks')?>'};
  $.post('../core/process.php', data)
  .done(function(resp){
    $('#bancos').empty().append(new Option('Seleccione',''));
    $.each(resp.datos, function (k, v) {
      $('#bancos').append(new Option(v.name,v.id));
    });
  }).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);
 }
});
}
bancos();
//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 Cuenta de Banco <?php echo$_SESSION['lang']['REQUERIDOS']?> ');
    $('#edit').hide();
    $('#save').show();
    $('#bancos').val('');
    $('#bancos').select2().trigger('change');
    $( "#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 bancos = $("#bancos").val();
  var titular = $("#titular").val();
  var num_cuenta = $("#num_cuenta").val();

  if (typeof bancos == 'undefined' || bancos === null || bancos === '' || typeof titular == 'undefined' || titular === null || titular === '' || typeof num_cuenta == 'undefined' || num_cuenta === null || num_cuenta === '') {  
   toastr.error('<?php echo $_SESSION['lang']['REQUERIDOS1']?>');
   finish('#save');
 }else {
  data = {'option':'<?php echo $decryp->encryptroute('add/bank_accounts/register_bank_account')?>','bancos':bancos,'titular':titular,'num_cuenta':num_cuenta};
  $.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{
   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_/bank_accounts/view_bank_account')?>','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 Cuentas de Banco <?php echo $_SESSION['lang']['REQUERIDOS']?>');
      $.each(resp.datos, function (k, v) {
        $('#id_cuenta').val(v.id);
        $('#bancos').val(v.banco);
        $('#bancos').select2().trigger('change');
        $('#titular').val(v.titular);
        $('#num_cuenta').val(v.num_cuenta);
        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 editar propiedades//
$(document).on('click', '#edit', function(e){
  submit('#edit');
  var id = $("#id_cuenta").val();
  var status=$("#status").is(':checked') ? 1 : 2;
  var bancos = $("#bancos").val();
  var titular = $("#titular").val();
  var num_cuenta = $("#num_cuenta").val();

  if (id === null || id === '' || typeof status == 'undefined' || status === null || status === '' || typeof bancos == 'undefined' || bancos === null || bancos === '' || typeof titular == 'undefined' || titular === null || titular === '' || typeof num_cuenta == 'undefined' || num_cuenta === null || num_cuenta === '') {  
   toastr.error('<?php echo $_SESSION['lang']['REQUERIDOS1']?>');
   finish('#edit');
 }else {
   data = {'option':'<?php echo $decryp->encryptroute('add/bank_accounts/edit_bank_account')?>','bancos':bancos,'titular':titular,'num_cuenta':num_cuenta,'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{
     toastr.error(resp.msg);
   }
 }).always( function() {
  finish('#edit');
});
}
});
//Funcion abrir modal para eliminar cuenta// 
$(document).on('click', '.delete', function(e){
  var data1 = $(this).data('data1');
  $( '#delete_cat' ).data( 'data1', data1);
  $('#modal_eliminar').modal('show'); 
});
//Funcion eliminar cuenta// 
$(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/bank_accounts/delete_bank_account')?>','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();
});
}
});

//Detalles de propiedad//
$(document).on('click', '.edit_balance', 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/bank_accounts/view_balance')?>','id':id};
    $.post('../core/process.php', data)
    .done(function(resp){ 
      $('#balance_modal form').trigger('reset'); 
      $('#balance_modal').modal('show');
      $('#saldo').maskMoney('mask', 0.00);  
      $.each(resp.datos, function (k, v) {
        $('#detalle_titular').html('<span style="color: red">Titular: </span>'+v.titular);
        $('#detalle_num_cuenta').html('<span style="color: red">Num-Cuenta: </span> '+v.num_cuenta);
        $('#detalle_banco').html('<span style="color: red">Banco: </span> '+v.banco_n);

        $('#id_cuenta').val(id);
        $('#saldo').maskMoney('mask', parseFloat(v.saldo, 10));
      });
    }).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 editar propiedades//
$(document).on('click', '#edit_balance', function(e){
  submit('#edit_balance');
  var id = $("#id_cuenta").val();
  var saldo = $("#saldo").val();
  if (id === null || id === '' || typeof saldo == 'undefined' || saldo === null || saldo === '') {  
   toastr.error('<?php echo $_SESSION['lang']['REQUERIDOS1']?>');
   finish('#edit_balance');
 }else {
   data = {'option':'<?php echo $decryp->encryptroute('edit/bank_accounts/edit_balance')?>','saldo':saldo,'id':id};
   $.post('../core/process.php', data)
   .done(function(resp){
     $('#balance_modal').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{
     toastr.error(resp.msg);
     var codigo=resp.codigo;
     if (codigo == "2020") {
     $('#balance_modal').modal('hide'); 
     $('#modal_cierre').modal('show');
     }
   }
 }).always( function() {
  finish('#edit_balance');
});
}
});
//Funcion para editar propiedades//
$(document).on('click', '#cierre', function(e){
  submit('#cierre');
  var id = $("#id_cuenta").val();
  var saldo = $("#saldo").val();
  if (id === null || id === '' || typeof saldo == 'undefined' || saldo === null || saldo === '') {  
   toastr.error('<?php echo $_SESSION['lang']['REQUERIDOS1']?>');
   finish('#cierre');
 }else {
   data = {'option':'<?php echo $decryp->encryptroute('edit/bank_accounts/close_balance')?>','saldo':saldo,'id':id};
   $.post('../core/process.php', data)
   .done(function(resp){
     $('#modal_cierre').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{
     toastr.error(resp.msg);
   }
 }).always( function() {
  finish('#cierre');
});
}
});
//END//
});
</script>