/* Funcion global para instalar nuestra aplicacion*/
function updateDB(i){
  $.ajax({
    url: $(i).attr('action'),
    type: 'POST',
    data:$(i).serialize(),
      beforeSend: function( xhr ) {
      var $target = $(i),
            block = $(i).parent('.contentform');
        
        // Block card
        $(block).block({ 
            message: '<i class="icon-spinner2 spinner"></i>',
            overlayCSS: {
                backgroundColor: '#fff',
                opacity: 0.8,
                cursor: 'wait',
                'box-shadow': '0 0 0 1px #ddd'
            },
            css: {
                border: 0,
                padding: 0,
                backgroundColor: 'none'
            }
        });    
    }
  })
  .done(function(data) {
    var block = $(i).parent('.contentform');
    $(block).unblock();

    if(jQuery.isEmptyObject(data.error)){
      if (!jQuery.isEmptyObject(data.connect)) {
        connect();
      }else{
        if (!jQuery.isEmptyObject(data.response)) {
          $('.response-'+data.response).removeClass('alert alert-danger').addClass('alert alert-success').html(data.success).show(500);
        }else{
          $('.response-data').removeClass('alert alert-danger').addClass('alert alert-success').html(data.success).show(500);
        }

        setTimeout(function() {
          if (!jQuery.isEmptyObject(data.url)) {
            location.href=data.url;
          }
        },4000);
      }
    }
  })
  .fail(function() {
    console.log("error");
  })
  .always(function(data) {

  });
  
}
function connect(){
  $.ajax({
    url: 'install',
    type: 'POST',
    data:{'action':'connect'},
    beforeSend: function( xhr ) {
      $('.response-data').removeClass('alert alert-success').addClass('alert alert-info').html('Verificando Conexion').show(500);
    }
  })
  .done(function(data) {
      if(jQuery.isEmptyObject(data.error)){
        $('.response-data').removeClass('alert alert-info').addClass('alert alert-success').html(data.success).show(500);
        $('.form-installbd').fadeOut(100);
        $('.button-next').fadeIn(100);
      }else{
        $('.response-data').removeClass('alert alert-info').addClass('alert alert-danger').html(data.error).show(500);
      }
  })
  .fail(function() {
    console.log("error");
  })
  .always(function(data) {

  });
}
function installTables(i){
  $.ajax({
    url: 'install',
    type: 'POST',
    data:{'action':'installtables'},
    beforeSend: function( xhr ) {
      $('.btn-install').addClass('disabled');
      $('.response-data').removeClass('alert alert-success').addClass('alert alert-info').html('Instalando tablas.......').show(500);

    }
  })
  .done(function(data) {
    if(jQuery.isEmptyObject(data.error)){
      $('.btn-install').removeAttr('onclick').attr('href',$(i).attr('data-url')).removeClass('disabled').html('Ir al login');
      $('.response-data').removeClass('alert alert-info').addClass('alert alert-success').html(data.success).show(500);
    }
  })
  .fail(function() {
    console.log("error");
  })
  .always(function(data) {

  });
}

function installModule(i){
  let carpeta = $(i).data('carpeta');
  $.ajax({
    url: 'moduleinstall',
    type: 'POST',
    dataType:'json',
    data:$(i).serialize(),
    beforeSend: function( xhr ) {
      $(i).block({ 
        message: '<i class="icon-spinner2 spinner"></i>',
        overlayCSS: {
            backgroundColor: '#fff',
            opacity: 0.8,
            cursor: 'wait',
            'box-shadow': '0 0 0 1px #ddd'
        },
        css: {
            border: 0,
            padding: 0,
            backgroundColor: 'none'
        }
      }); 
    }
  })
  .done(function(data) {
    $(i).unblock();
    if(jQuery.isEmptyObject(data.error)){
      if (!jQuery.isEmptyObject(data.url)) {
        location.href=data.url;
      }
      $('.'+carpeta).fadeOut(100)
    }else{
      $('.response-data').html('');
      $.each( data.error, function( key, value ) {
        $('.response-data').removeClass('alert alert-success').addClass('alert alert-danger').append('<p>* '+value+'</p>').show(500);
      });
    }
  })
  .fail(function() {
    console.log("error");
  })
  .always(function(data) {

  });
}