"use strict";
$(document).ready(function () {
    $("#sendForm").submit(function (e) {
        $( "#buttonSend" ).prop( "disabled", true );
        e.preventDefault();
        var data = $(this).serializeArray();
        $.ajax({
            type: "post",
            url: "./php/envio.php",
            dataType: "json",
            data: data,
            beforeSend: function () {
                Swal.fire({
                    icon: 'info',
                    title: "<strong>Procesando su mensaje</strong>",
                    html: "Estamos procesando tu mensaje.<br>Espere un momento por favor...",
                    showConfirmButton: !1,
                    allowOutsideClick: false,
                  });
            },
        })
        .done(function (response) {
            const Toast = Swal.mixin({
                toast: true,
                position: 'top-end',
                showConfirmButton: false,
                timer: 3000,
                timerProgressBar: true,
                onOpen: (toast) => {
                toast.addEventListener('mouseenter', Swal.stopTimer)
                toast.addEventListener('mouseleave', Swal.resumeTimer)
                }
            });
            if (response.success){
                Toast.fire({
                    icon: 'success',
                    title: 'Su mensaje se envio correctamente.'
                  });
            }else{
                Toast.fire({
                    icon: 'error',
                    title: 'Se presento un error inesperado.'
                  });
            }  
        })
        .fail(function (response) {
            const Toast = Swal.mixin({
                toast: true,
                position: 'top-end',
                showConfirmButton: false,
                timer: 3000,
                timerProgressBar: true,
                onOpen: (toast) => {
                toast.addEventListener('mouseenter', Swal.stopTimer)
                toast.addEventListener('mouseleave', Swal.resumeTimer)
                }
            });
            Toast.fire({
                icon: 'error',
                title: 'Se presento un error inesperado, intentalo más tarde.'
              });
        });
    });
});
