$(document).on('click', '#btnPrueba', function(event) {
  event.preventDefault();
  /* Act on the event */
  var namePrint = $('#impresora').val();
  var driverName = $('#impresora').find(':selected').data('drivername');
  var local = $('#impresora').find(':selected').data('local');
  var network = $('#impresora').find(':selected').data('network');
  var servername = $('#impresora').find(':selected').data('servername');
  pruebaImpresion(namePrint, driverName, local, network, servername);
});

var pruebaImpresion = function(namePrint, driverName, local, network, servername){
  var options = {
    type: 'POST',
    url:'index.php?page=panel&action=pruebaimpresion',
    data:  {
      'namePrint' : namePrint,
      'driverName' : driverName,
      'local' : local,
      'network' : network,
      'servername' : servername,
    },
    dataType: 'json',
    success: function(response){
      
    }
  };
  $.ajax(options);
};

$(document).on('click', '#btnRecargar', function(event) {
  event.preventDefault();
  /* Act on the event */

  recargarImpresoras();

});

var recargarImpresoras = function(){
  var options = {
    type: 'POST',
    url:'index.php?page=panel&action=recargarImpresoras',
    dataType: 'json',
    success: function(response){
      $('#impresora').html(response.options);
      callValidarTicketera();
    }
  };
  $.ajax(options);
};

$(document).on('click', '#btnGuardarTicketera', function(event) {
  event.preventDefault();
  /* Act on the event */

  var namePrint = $('#impresora').val();
  var driverName = $('#impresora').find(':selected').data('drivername');
  var local = $('#impresora').find(':selected').data('local');
  var network = $('#impresora').find(':selected').data('network');
  var servername = $('#impresora').find(':selected').data('servername');

  var formData = new FormData();

  formData.append('namePrint', namePrint);
  formData.append('driverName', driverName);
  formData.append('local', local);
  formData.append('network', network);
  formData.append('servername', servername);
  
  guardarTicketera(formData);
});

var guardarTicketera = function(formData){
  var options = {
    type: 'POST',
    url: 'index.php?page=panel&action=guardarTicketera',
    data:formData,
    processData: false,
    contentType: false,
    dataType: 'json',
    success: function(response){
      $('#msj_ticketera').html(response.msj);
    }
  };
  $.ajax(options);
};

$(document).ready(function () {
  recargarImpresoras();
});

$(document).on('change', '#impresora', function(event) {
  event.preventDefault();
  /* Act on the event */
  callValidarTicketera();
});


var callValidarTicketera = function () {

  var namePrint = $('#impresora').val();
  var driverName = $('#impresora').find(':selected').data('drivername');
  var local = $('#impresora').find(':selected').data('local');
  var network = $('#impresora').find(':selected').data('network');
  var servername = $('#impresora').find(':selected').data('servername');
  console.log('hola validacion 🎅');
  if (local == 'FALSE') {
    $('#span_tipo_conexion').html('Impresora de Red');
    $('#span_direccion_ip').html(servername);
    $('#div_msj_network').html(`
      <div class="col-sm-offset-2 col-sm-10">
        <code><b>RECOMENDACION:</b> Guardar las credenciales permanentemente y  la IP de Pc de la ticketera remota debe ser estatica</code>
      </div>
    `);
    console.log('impresora de red 😎');
  }
  if (local == 'TRUE') {
    $('#span_tipo_conexion').html('Impresora Local');
    $('#span_direccion_ip').html('--');
    $('#div_msj_network').empty();
    console.log('Impresora local 🤗');
  }
}