$("body").append("<div class=\"fixed z-50 bottom-0 right-4 w-80 h-[400px] bg-white border shadow-lg rounded-lg\" id=\"chat-container\"><div class=\"p-4 bg-white text-center text-[#03487C] font-bold rounded-t-lg\"><img alt=\"Simbolo FIT\" class=\"absolute top-0 left-1 mt-1\" src=\"/assets/simbolo_fit-8e63a556e8d8e1db55d2bc04ef1d3da1796e337fe611306dfa0cd6010e73fc19.svg\" loading=\"lazy\" />Precisa de ajuda?<button class=\"float-right text-gray-500\" id=\"close-chat\">&times;<\/button><\/div><div class=\"bg-[#F5F5F5] h-full py-16 text-center text-[14px]\"><p class=\"font-sans font-bold text-[#5B5B5B]\">Antes de iniciar o chat, identifique-se<\/p><div class=\"mt-4 flex flex-col items-center gap-3 px-4\"><form class=\"simple_form w-full z-30 lg:max-w-[484px] flex justify-center lg:justify-self-auto\" id=\"cadastro-form-cadastro\" novalidate=\"novalidate\" action=\"/cadastro_form_whatsapp\" accept-charset=\"UTF-8\" data-remote=\"true\" method=\"post\"><div class=\"w-full flex flex-col justify-center gap-[7px] leading-[19.6px] font-normal\"><div class=\"sant-input-card bg-white\"><div class=\"sant-input-img\"><img alt=\"Icone avatar\" class=\"w-[16px] h-[16px]\" src=\"/assets/avatar-line-green-ffe665f59d0099338b20742779891b9a65f895ce6cd5658e0376fc80b67be4eb.svg\" loading=\"lazy\" /><\/div><div class=\"w-full px-2\"><div class=\"input string required lead_nome\"><div class=\"form-group mb-2\"><input class=\"string required form-control admin-form-input input-call-bg sant-input-field\" placeholder=\"Digite seu nome*\" name=\"firstname\" id=\"whatsapp_lead_nome\" type=\"text\" /><\/div><\/div><\/div><\/div><div class=\"sant-input-card bg-white\"><div class=\"sant-input-img\"><img alt=\"Icone celular\" class=\"w-[20px] h-[20px]\" src=\"/assets/jam_phone_green-1092a94eefd531ee4c550c33e6dd491762b4e7f29a671506a1a809e228c21473.svg\" loading=\"lazy\" /><\/div><div class=\"w-full px-2\"><div class=\"input string required lead_telefone\"><div class=\"form-group mb-2\"><input class=\"string required form-control admin-form-input input-call-bg sant-input-field phone-mask\" placeholder=\"Digite seu celular*\" name=\"phone\" type=\"text\" id=\"lead_telefone\" /><\/div><\/div><\/div><\/div><input type=\"hidden\" name=\"url\" id=\"lp_url\" value=\"\" autocomplete=\"off\" /><input type=\"submit\" name=\"commit\" value=\"INICIAR O CHAT\" id=\"envio_button\" class=\"mt-4 w-full h-[45px] items-center justify-center text-white bg-[#00BA00] text-center text-[14px] font-extrabold cursor-pointer leading-[17.07px] rounded-lg tracking-[0.0825em] border-2 border-[#00BA00]\" data-disable-with=\"INICIAR O CHAT\" /><\/div><\/form><\/div><\/div><\/div><link rel=\"stylesheet\" href=\"/assets/views/shared/_whatsapp_form_chat-3ee70bcd35d94ef797f2a82525ae177f7ccefb5978f308705c72519fcd3e3829.css\" media=\"all\" /><script>\$(\"#envio_button\").click(function() {\n  \$(this)\n    .addClass(\'bg-gray-500 border-gray-500 cursor-not-allowed\')\n    .attr(\'disabled\', true);\n  if (\$(this).find(\'.fa-spinner\').length === 0) {\n    \$(this).prepend(\'<i class=\"fa fa-spinner fa-spin fa-fw primary-font mr-2\"><\/i>\');\n  }\n  \$(\"#cadastro-form-cadastro\").submit();\n\n});<\/script>");

$("#close-chat").click(function() {
  $("#chat-container").remove();
});

$(document).ready(function() {
  $("#lp_url").val(window.location.href);
});

setTimeout(function () {
  $(".phone-mask").on("input", function (e) {
    let raw = e.target.value.replace(/\D/g, ""); // Remove tudo que não for número

    if (/^0(800|300|500|900)/.test(raw)) {

      raw = raw.slice(0, 11);
      e.target.value = raw.replace(/^(\d{4})(\d{3})(\d{4})$/, "$1 $2 $3");
      return;
    }

    if (raw.startsWith("0") && raw.length > 10) {
      raw = raw.substring(1);
    }

    raw = raw.slice(0, 11);
    let formatted = "";

    if (raw.length === 11) {
      formatted = raw.replace(/^(\d{2})(\d{5})(\d{4})$/, "($1) $2-$3");
    } else if (raw.length >= 10) {
      formatted = raw.replace(/^(\d{2})(\d{4})(\d{4})$/, "($1) $2-$3");
    } else {
      formatted = raw;
    }

    e.target.value = formatted;
  });
}, 300);
