jQuery(document).ready(function () {
   $("#form_add_tasa").submit(function (e) {
      e.preventDefault();
      var f = $(this);
      var formData = new FormData(document.getElementById("form_add_tasa"));
      $.ajax({
         url: base_url + "configs/add_tasa",
         type: "POST",
         dataType: "JSON",
         data: formData,
         cache: false,
         contentType: false,
         processData: false,
         beforeSend: function () {
            $("#loader").removeClass("display-none");
         },
         success: function (r) {
      //var r = JSON.parse(respuesta);
            epsToast(
               "light",
               "white",
               '<i class="bi bi-success"></i>',
               "" + r.success,
               "" ,
               ""+ r.data.tasa,
               "" + r.mensaje
               );
         },
         error: function (jqXHR, textStatus, error) {
            epsToast(
               "danger",
               '<i class="bi bi-x-octagon"></i>',
               jqXHR,
               jqXHR.status,
               jqXHR.responseText,
               ""
               );
            clearTimeout();
               //setTimeout(function(){ location.reload(); }, 10000);
         },
         complete: function () {
            $("#loader").addClass("display-none");
            location.reload();
               //setTimeout(function(){ $('#loader').addClass('display-none'); }, 4000);
         },
      });
   });
   $("#form_add_permisos_usuario").submit(function (e) {
      e.preventDefault();
      var f = $(this);
      var formData = new FormData(document.getElementById("form_add_permisos_usuario"));
      $.ajax({
         url: base_url + "config/add_permisos_usuario",
         type: "POST",
         dataType: "JSON",
         data: formData,
         cache: false,
         contentType: false,
         processData: false,
         beforeSend: function () {
            $("#loader").removeClass("display-none");
         },
         success: function (r) {
         //var r = JSON.parse(respuesta);
            epsToast(
               "danger",
               "white",
               '<i class="bi bi-error"></i>',
               "" + r.success,
               "" + r.data.id_cliente,
               ""+ r.data.nombre_modulo,
               "" + r.mensaje
               );
         },
         error: function (jqXHR, textStatus, error) {
            epsToast(
               "danger",
               '<i class="bi bi-x-octagon"></i>',
               jqXHR,
               jqXHR.status,
               jqXHR.responseText,
               ""
               );
            clearTimeout();
            //setTimeout(function(){ location.reload(); }, 10000);
         },
         complete: function () {
            $("#loader").addClass("display-none");
            location.reload();
         },
      });
   });
   $("#form_add_permisos_modulos").submit(function (e) {
      e.preventDefault();
      var f = $(this);
      var formData = new FormData(document.getElementById("form_add_permisos_modulos"));
      $.ajax({
         url: base_url + "config/add_permisos_modulos",
         type: "POST",
         dataType: "JSON",
         data: formData,
         cache: false,
         contentType: false,
         processData: false,
         beforeSend: function () {
            $("#loader").removeClass("display-none");
         },
         success: function (r) {
         //var r = JSON.parse(respuesta);
            epsToast(
               "danger",
               "white",
               '<i class="bi bi-error"></i>',
               "" + r.success,
               "" + r.data.id_cliente,
               ""+ r.data.nombre_modulo,
               "" + r.mensaje
               );
         },
         error: function (jqXHR, textStatus, error) {
            epsToast(
               "danger",
               '<i class="bi bi-x-octagon"></i>',
               jqXHR,
               jqXHR.status,
               jqXHR.responseText,
               ""
               );
            clearTimeout();
            //setTimeout(function(){ location.reload(); }, 10000);
         },
         complete: function () {
            $("#loader").addClass("display-none");
            location.reload();
         },
      });
   });

   //EMPRESAS
   $("#form_add_empresas").submit(function (e) {
      e.preventDefault();
      var f = $(this);
      var formData = new FormData(document.getElementById("form_add_empresas"));
      $.ajax({
         url: base_url + "administrador/add_empresas",
         type: "POST",
         dataType: "JSON",
         data: formData,
         cache: false,
         contentType: false,
         processData: false,
         beforeSend: function () {
            $("#loader").removeClass("display-none");
         },
         success: function (r) {
            epsToast(
               "light", "dark", '<i class="bi bi-check"></i>',
               "" + r.success, "" + r.data.id, "" + r.data.razonsocial, "" + r.mensaje
               );

            $("#tbl_empresas").bootstrapTable("refresh", {
               url: base_url + "administrador/listaempresas",
            });

            //window.location.href = base_url + "administrador/empresas_list/" + r.data.id;
            window.location.href = base_url + "administrador/empresas_list/";
            
         },
         error: function (jqXHR, textStatus, error) {
            epsToast(
               "danger", "light", '<i class="bi bi-x-octagon"></i>',
               jqXHR, jqXHR.status, jqXHR.responseText, ""
               );
            clearTimeout();
         },
         complete: function () {
            $("#loader").addClass("display-none");
            //setTimeout(function(){ $('#loader').addClass('display-none'); }, 4000);
         },
      });
   });
   $("#form_edit_empresas").submit(function (e) {
      e.preventDefault();
      var f = $(this);
      var formData = new FormData(document.getElementById("form_edit_empresas"));
      $.ajax({
         url: base_url + "administrador/edit_empresas",
         type: "POST",
         dataType: "JSON",
         data: formData,
         cache: false,
         contentType: false,
         processData: false,
         beforeSend: function () {
            $("#loader").removeClass("display-none");
         },
         success: function (r) {
            epsToast(
               "light", "dark", '<i class="bi bi-check"></i>',
               "" + r.success, "" + r.data.id, "" + r.data.razonsocial, "" + r.mensaje
               );

            $("#tbl_empresas").bootstrapTable("refresh", {
               url: base_url + "administrador/listaempresas",
            });

            //window.location.href = base_url + "administrador/empresas_list/" + r.data.id;
            window.location.href = base_url + "administrador/empresas_list/";
            
         },
         error: function (jqXHR, textStatus, error) {
            epsToast(
               "danger", "light", '<i class="bi bi-x-octagon"></i>',
               jqXHR, jqXHR.status, jqXHR.responseText, ""
               );
            clearTimeout();
         },
         complete: function () {
            $("#loader").addClass("display-none");
            //setTimeout(function(){ $('#loader').addClass('display-none'); }, 4000);
         },
      });
   });

   $("#btn_tbl_empresas_edit").click(function () {
      var resultJSON = $("#tbl_empresas").bootstrapTable("getSelections");
      var seleccion = $('input[name="rtbl_empresas"]:checked').length;
      if (seleccion == 0) {
         epsToast(
            "danger", "white", '<i class="bi bi-hdd"></i>', "Seleccion",
            "Error", "Seleccione un item de la tabla.", "bottom"
            );
         return false;
      } else {
         var ref = resultJSON[0]["id_empresas"];
         fill_input_empr(resultJSON[0]);
         $("#modal_empresas_edit").modal("show");
      }
   });

   // CARGAR IMAGEN EN CUADRO AL CAMBIAR INPUT
   $("#load_img").change(function () {
      readURL(this);
   });
   function readURL(input) {
      if (input.files && input.files[0]) {
         var reader = new FileReader();
         reader.onload = function (e) {
            $("#out_img").attr("src", e.target.result);
         };
         reader.readAsDataURL(input.files[0]); // convert to base64 string
      }
   }

   // CARGAR IMAGEN EN CUADRO AL CAMBIAR INPUT EDIT
   $("#load_img_productos_edit").change(function () {
      readURL_productos_edit(this);
   });
   function readURL_productos_edit(input) {
      if (input.files && input.files[0]) {
      var reader = new FileReader();
      reader.onload = function (e) {
         $("#out_img_productos_edit").attr("src", e.target.result);
         };
         reader.readAsDataURL(input.files[0]); // convert to base64 string
      }
   }
});

   //FUNCION PARA FORMATEAR FECHA DD/MM/YYY
   function dateFormat(value, row, index) {
      var d = new Date(value);
      var day = d.getDate();
      var month = d.getMonth() + 1;
      var year = d.getFullYear();
      if (day < 10) {
         day = "0" + day;
      }
      if (month < 10) {
         month = "0" + month;
      }
      var date = day + "/" + month + "/" + year;
      return date;
   }
   //FUNCION LOADING
   function loading() {
      $("#loader").removeClass("display-none");
      //$("#loader").fadeOut("slow");
   }
   //TOAST EPSCOLOR 
   function epsToast(color, text, icon, head, headsmall, mensaje, boton) {
      $("#epsAlert").append(
         '<div id="alertToast" class="pe-2" style="z-index: 11"> ' +
         '<div class="toast bg-' +
         color +
         " text-" +
         text +
         ' hide" role="alert" aria-live="assertive" aria-atomic="true">' +
         '<div class="toast-header">' +
         icon +
         " &nbsp " +
         '<strong class="me-auto">' +
         head +
         "</strong>" +
         "<small>" +
         headsmall +
         "</small>" +
         '<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>' +
         "</div>" +
         '<div class="toast-body">' +
         mensaje +
         '<div class="mt-2 pt-2 border-top">' +
         boton +
         "</div>" +
         "</div>" +
         "</div>" +
         "</div>"
      );
      $(".toast").toast({ delay: 9000 });
      $(".toast").toast("show");
      setTimeout(function () { $("#alertToast").remove(); }, 9000);
            /*let myAlert = document.querySelector('.toast');
            let bsAlert = new  bootstrap.Toast(myAlert);
            bsAlert.show();*/
            /*var myAlert = document.getElementById('liveToast');//select id of toast
            var bsAlert = new bootstrap.Toast(myAlert);//inizialize it
            bsAlert.show(); */
   }
   function epsAlert(positiony, positionx, icon, titulo, topsmall, mensaje, boton, time) {
     $("#epsAlert").append(
       '<div class=" ' +
       positiony +
       " " +
       positionx +
       ' p-3" style="z-index: 11"> ' +
       '<div id="alertToast" class="toast bg-secondary text-white" role="alert" aria-live="assertive" aria-atomic="true"> ' +
       '<div class="toast-header">' +
       +icon +
       '<strong class="mr-auto"> &nbsp' +
       titulo +
       "</strong>" +
       '<small class="text-muted">' +
       topsmall +
       "</small>" +
       '<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">' +
       '<span aria-hidden="true">' +
       "&times;</span>" +
       "</button>" +
       "</div>" +
       '<div class="toast-body">' +
       mensaje +
       "<br><br>" +
       boton +
       "</div>" +
       "</div>" +
       "</div>"
       );
     $(".toast").toast({ delay: 20000 });
     $(".toast").toast("show");
     setTimeout(function () {
       $("#alertToast").remove();
     }, 20000);
   }

   //FILL INPUT EMPRESAS FORM 
   function fill_input_empr(data) {
   $('input[name="id_empresa"]').val(data.id);
   $('input[name="rif"]').val(data.rif);
   $('input[name="razonsocial"]').val(data.razonsocial);
   $('input[name="telefono1"]').val(data.telefono1);
   $('input[name="contacto"]').val(data.contacto);
   $('input[name="telefono2"]').val(data.telefono2);
   $('input[name="email"]').val(data.email);
   $('input[name="nombrecorto"]').val(data.nombrecorto);
   $('#direccion').val(data.direccion);
   var url = base_url + 'assets/epscolor/img/empresas/' + data.logo;
   $('#img_file_input_logo').prop("src", url);

   var url = base_url + 'assets/epscolor/img/empresas/' + data.logo2;
   $('#img_file_input_logo2').prop("src", url);

   var url = base_url + 'assets/epscolor/img/empresas/' + data.logowhite;
   $('#img_file_input_logow').prop("src", url);

   //$("#img_file_input_logo").show();
   //$('input[name="logo"]').val(data.logo);
   //$('input[name="logo2"]').val(data.logo2);
   //$('input[name="logow"]').val(data.logow);
   if (data.activo === "1") { 
      document.getElementById("check_activo").checked = true;} 
   else { document.getElementById("check_activo").checked = false;}

   //$("#categoria_edit_art").val(data.categoria);
   //$("#condicion_edit_art").val(data.condicion);

//if (data.excento === "1") { $("#excento_art").attr("checked", true);}
//$('input[name="excento_art"]').val(data.excento);
   }

   
   //Input File Logo on Change cargar imagen en card
   $("#file_input_logo").change(function () {
      readURL1(this);
   });
   function readURL1(input) {
      if (input.files && input.files[0]) {
         var reader = new FileReader();
         reader.onload = function (e) {
            $("#img_file_input_logo").attr("src", e.target.result);
         };
         reader.readAsDataURL(input.files[0]); // convert to base64 string
      }
   }

   //Input File Logo2 on Change cargar imagen en card
   $("#file_input_logo2").change(function () {
      readURL2(this);
   });
   function readURL2(input) {
      if (input.files && input.files[0]) {
         var reader = new FileReader();
         reader.onload = function (e) {
            $("#img_file_input_logo2").attr("src", e.target.result);
         };
         reader.readAsDataURL(input.files[0]); // convert to base64 string
      }
   }

   //Input File Logo on Change cargar imagen en card
   $("#file_input_logow").change(function () {
      readURL3(this);
   });
   function readURL3(input) {
      if (input.files && input.files[0]) {
         var reader = new FileReader();
         reader.onload = function (e) {
            $("#img_file_input_logow").attr("src", e.target.result);
         };
         reader.readAsDataURL(input.files[0]); // convert to base64 string
      }
   }