(function ($) {
    'use strict';
    $(function (e) {


        if (sessionStorage.getItem("formPadrao")) {
            // Restaura o conteúdo da caixa de texto
            const values = sessionStorage.getItem("formPadrao");
            const datas = JSON.parse(values);
            console.log(datas);
            datas.forEach(function (v, e) {
                if (v.name == 'nome') {
                    document.getElementsByName(v.name)[0].setAttribute("value", v.value);
                }
                if (v.name == 'email') {
                    document.getElementsByName(v.name)[0].setAttribute("value", v.value);
                }
                if (v.name == 'telefone') {
                    document.getElementsByName(v.name)[0].setAttribute("value", v.value);
                }
                if (v.name == 'mensagem') {
                    var d = document.getElementById('formMensagem');
                    d.value = v.value;
                }

                if (v.name == 'assunto') {
                    var d = document.getElementById('formAssunto');
                    d.value = v.value;
                }
            })
        }

        var btnSubmit = $('#btn-submit-form-alt');

        btnSubmit.on('click', function () {


            $('#btn-submit-form-alt').children().addClass("fa fa-spin fa-spinner done");
            let url = $('#alt_form_busca').attr('action');
            let data = $('#alt_form_busca').serializeArray()


            sessionStorage.setItem('formPadrao', JSON.stringify(data));


            console.log(data);
            $.ajax({
                method: "POST",
                url: url,
                data: data,
            }).done(function (data) {

                $('#btn-submit-form-alt').after("<div class='alert mt-5 alert-success'><button type='button' class='close' data-dismiss='alert' aria-label='Close'> <span aria-hidden='true'>&times;</span> </button>" + data.msg + "</div>");

            }).fail(function (data) {

                $('#btn-submit-form-alt').after("<div class='alert mt-5 alert-danger'><button type='button' class='close' data-dismiss='alert' aria-label='Close'> <span aria-hidden='true'>&times;</span> </button>" + data.responseJSON.msg + "</div>");

            }).always(function () {

                $('#btn-submit-form-alt').children().removeClass("fa fa-spin fa-spinner done");

            });


        })


        $("#formTelefoneAlt").mask("(99) 99999-9999").focusout(function (event) {
            var target, phone, element;
            target = (event.currentTarget) ? event.currentTarget : event.srcElement;
            phone = target.value.replace(/\D/g, '');
            element = $(target);
            element.unmask();
            if (phone.length > 10) {
                element.mask("(99) 99999-999?9");
            } else {
                element.mask("(99) 9999-9999?9");
            }
        });
    });


})(jQuery);


