// Máscara de CPF automática
document.getElementById(‘cpf_aluno’).addEventListener(‘input’, function (e) {
let value = e.target.value.replace(/\D/g, ”);
value = value.replace(/(\d{3})(\d)/, ‘$1.$2’);
value = value.replace(/(\d{3})(\d)/, ‘$1.$2’);
value = value.replace(/(\d{3})(\d{1,2})$/, ‘$1-$2’);
e.target.value = value;
});

async function gerarCertificado() {
const nome = document.getElementById(‘nome_aluno’).value;
const cpf = document.getElementById(‘cpf_aluno’).value;
const btn = document.getElementById(‘btn-gerar’);

if(nome.length < 3 || cpf.length < 14) {
alert("Por favor, preencha os dados corretamente.");
return;
}

btn.innerText = "Processando…";
btn.disabled = true;

// URL do seu Web App (publicado no Google Apps Script)
const URL_SCRIPT = "SUA_URL_DO_APPS_SCRIPT_AQUI";

try {
const response = await fetch(URL_SCRIPT, {
method: 'POST',
mode: 'no-cors', // Importante para evitar erros de CORS no Apps Script
body: JSON.stringify({ nome: nome, cpf: cpf })
});

document.getElementById('mensagem').innerText = "Sucesso! Seu certificado está sendo gerado e será enviado para o seu e-mail.";
} catch (error) {
document.getElementById('mensagem').innerText = "Erro ao processar. Tente novamente.";
btn.disabled = false;
}
}