$(document).ready(function () { const ambiente = 'producao'; const urlAmbiente = getUrl(ambiente); const reCaptcha = getUrlReCaptcha(ambiente); let isSubmitting = false; function apiEmitirCertidao(cpf, token) { if (isSubmitting) return; isSubmitting = true; cpf = cpf.replace(/\D/g, ''); $.ajax({ url: urlAmbiente + 'emitir_certidao_irregular', type: 'POST', data: { cpf: cpf, 'g-recaptcha-response': token }, success: function(response) { gerarPDF(response.data.idCas); enviraMensagem('Sucesso', response.message); }, error: function(response) { $("#cpf").val(''); enviraMensagem('Erro', response.responseJSON?.message || 'Erro desconhecido'); }, complete: function() { isSubmitting = false; $("#cpf").val(''); esconderLoading(); } }); } function apiValidarCertidao(codigoValidacao, token) { $.ajax({ url: urlAmbiente + 'validar_certidao_irregular', type: 'POST', data: { codigoValidacao: codigoValidacao, 'g-recaptcha-response': token }, success: function(response) { gerarPDF(response.data.idCas); enviraMensagem('Sucesso', response.message); }, error: function(response) { enviraMensagem('Erro', response.responseJSON.message); }, complete: function() { $("#codigo").val(''); esconderLoading(); } }); } // Remove a mensagem ao clicar no icon x antes dos 10 segundos $(document).on("click", ".alert-dismissible .close", function() { $(this).closest(".alert").fadeOut(); }); function exibirLoading() { $("#minhaDiv").css("display", "block"); } function esconderLoading() { $("#minhaDiv").css("display", "none"); } function gerarPDF(base64) { $.ajax({ url: urlAmbiente + 'info_certidao_irregular/' + base64, type: 'GET', success: function(response) { const pdfData = response.anexo_base64; // Cria um link temporário const link = document.createElement('a'); link.href = 'data:application/pdf;base64,' + pdfData; link.download = response.info.nome_original + '.pdf'; // Simula o clique no link document.body.appendChild(link); link.click(); document.body.removeChild(link); // Remove o link do DOM }, error: function(response) { enviraMensagem('Erro', response.responseJSON.message); } }); } function gerarDownload(blob, nomeCertidao) { const url = URL.createObjectURL(blob); $('', { href: url, download: nomeCertidao // Nome do arquivo PDF })[0].click(); // Limpe o URL temporário após o download URL.revokeObjectURL(url); } function enviraMensagem(status, message) { $("#alertaPrefixo").text(status+': '); $("#alertaMensagem").text(message); if (status === 'Sucesso') { $("#alerta").removeClass("alert-danger").addClass("alert-success").fadeIn(); } else { $("#alerta").removeClass("alert-success").addClass("alert-danger").fadeIn(); } // Fechar a mensagem após 10 segundos setTimeout(function() { $("#alerta").fadeOut(); }, 10000); } // Formulário Emitir certidão $("#cpf").mask('000.000.000-00'); $("#emitir-form").submit(function (e) { e.preventDefault(); const cpf = $("#cpf").val(); if (cpf === "") { $("#cpf").css("border-color", "red"); $("#cpf-error").show(); return; } $("#cpf").css("border-color", ""); $("#cpf-error").hide(); // Sempre gerar um novo token e só então enviar grecaptcha.ready(function () { grecaptcha.execute(reCaptcha, { action: 'emitir_certidao' }).then(function (token) { exibirLoading(); apiEmitirCertidao(cpf, token); }); }); }); $("#validar-form").submit(function (e) { e.preventDefault(); const codigo = $("#codigo").val(); if (codigo === "") { $("#codigo").css("border-color", "red"); $("#codigo-error").show(); return; } $("#codigo").css("border-color", ""); $("#codigo-error").hide(); grecaptcha.ready(function () { grecaptcha.execute(reCaptcha, { action: 'validar_certidao' }).then(function (token) { exibirLoading(); apiValidarCertidao(codigo, token); }); }); }); });