// Example starter JavaScript for disabling form submissions if there are invalid fields
(function() {
    'use strict'

    window.addEventListener('load', function() {
        // Fetch all the forms we want to apply custom Bootstrap validation styles to
        var forms = document.getElementsByClassName('needs-validation')

        // Loop over them and prevent submission
        Array.prototype.filter.call(forms, function(form) {
            form.addEventListener('submit', function(event) {
                if (form.checkValidity() === false) {
                    event.preventDefault()
                    event.stopPropagation()
                }
                form.classList.add('was-validated')
            }, false)
        })
    }, false)
}())

$('#myform').submit(function(e) {

    if ($("#myform")[0].checkValidity()) {
        //your form execution code
        e.preventDefault();
        // check if the input is valid
        $("#sbmtBtn").attr("disabled", true);
        
        $.ajax({
            url: 'https://leads.getmarket.mx/api/em6622/',
           // url: 'http://localhost:8000/api/em6622/',
            type: 'post',
            data: $('#myform').serialize(),
            success: function() {
                // Redirect to another success page
                // window.location = "thanks.html";
                

                  Swal.fire({
                    icon: 'success',
                    title: 'Mensaje enviado, en breve nos pondremos en contacto contigo',
                    confirmButtonText: `Ok`,
                    customClass: {
                        id: 'ConfirmButton'
                    }
                  }).then((result) => {
                    window.location.reload();
                  })
                
            }
        });
    } else console.log("invalid form");

    // if ($("#myform")[0].checkValidity()) {
    //     //your form execution code
    //     e.preventDefault();
    //     // check if the input is valid
    //     $("#sbmtBtn").attr("disabled", true);
    //     var paramss = 'Hola, soy ' + $("#first_name").val() + ' ' + $("#last_name").val() + ' mi número es ' + $("#phone").val() + ' y mi correo es ' + $("#email").val() + ' quiero agendar una cita.';
    //     console.log(paramss);
    //     window.open('https://wa.me/522288368566?text=' + paramss);
    //} else console.log("invalid form");
});