jQuery(document).ready(function () {
    let $ = jQuery;
    let form = $(".wpis-form");

    if( form.length > 1 ){
        alert('WP Immo Sync : Multiple forms have been found on the page; only one form could be present by page.');
    }

    form.submit(function (event) {

        event.preventDefault();

        let formType = jQuery('.wpis-form').attr('id');
        let formData = {
            url:       window.location.href,
            agreement: true,
        };

        switch(formType){
            case 'wpis-form-contact-default':
                formData.source    = 'contactForm';
                formData.gender    = form.find('[name=gender]').val();
                formData.firstname = form.find('[name=firstname]').val();
                formData.lastname  = form.find('[name=lastname]').val();
                formData.email     = form.find('[name=email]').val();
                formData.phone     = form.find('[name=phone]').val();
                formData.message   = form.find('[name=message]').val();
            break;
            case 'wpis-form-contact-extended':
                formData.source        = 'contactForm';
                formData.gender        = form.find('[name=gender]').val();
                formData.firstname     = form.find('[name=firstname]').val();
                formData.lastname      = form.find('[name=lastname]').val();
                formData.email         = form.find('[name=email]').val();
                formData.phone         = form.find('[name=phone]').val();
                formData.message       = form.find('[name=message]').val();
                formData.addressStreet = form.find('[name=addressStreet]').val();
                formData.addressNumber = form.find('[name=addressNumber]').val();
                formData.addressBox    = form.find('[name=addressBox]').val();
                formData.addressZip    = form.find('[name=addressZip]').val();
                formData.addressCity   = form.find('[name=addressCity]').val();
            break;
            case 'wpis-form-evaluation':
                formData.source        = 'evaluationForm';
                formData.gender        = form.find('[name=gender]').val();
                formData.firstname     = form.find('[name=firstname]').val();
                formData.lastname      = form.find('[name=lastname]').val();
                formData.email         = form.find('[name=email]').val();
                formData.phone         = form.find('[name=phone]').val();
                formData.message       = form.find('[name=message]').val();
                formData.addressStreet = form.find('[name=addressStreet]').val();
                formData.addressNumber = form.find('[name=addressNumber]').val();
                formData.addressBox    = form.find('[name=addressBox]').val();
                formData.addressZip    = form.find('[name=addressZip]').val();
                formData.addressCity   = form.find('[name=addressCity]').val();
            break;
            case 'wpis-form-estate':
                formData.source        = 'estateForm';
                formData.gender        = form.find('[name=gender]').val();
                formData.firstname     = form.find('[name=firstname]').val();
                formData.lastname      = form.find('[name=lastname]').val();
                formData.email         = form.find('[name=email]').val();
                formData.phone         = form.find('[name=phone]').val();
                formData.message       = form.find('[name=message]').val();
                formData.estateId      = form.data('eid');
                formData.postId        = form.data('pid');
            break;
        }

        form.find('button').html(literals['message.processing']).attr('disabled', true);

        $.ajax({
            type: "POST",
            url: "/wp-json/wpis/form-data",
            data: formData,
            dataType: "json",
            encode: true,
        })
        .done(function (data) {
            if( data.success ) {
                form.find('input, select, textarea, button').attr('disabled', true);
                form.find('button').hide();
                form.find('.form-group').css('opacity', '.65');
                form.append('<span class="wpis-form-success">' + literals['message.success'] + '</span>');

                $.ajax({
                    type: "POST",
                    url: "/wp-json/wpis/form-sync",
                    data: {},
                    dataType: "json"
                });
            }
            else {
                let errorMessage;
                errorMessage += '<div>';
                if( data.errors.length === 1 ){
                    errorMessage = '<span>' + literals['error.single'] + '</span>';
                }
                else {
                    errorMessage = '<span>' + literals['error.multiple'] + '</span>';
                }
                errorMessage += '<ul>';
                data.errors.forEach(element => {
                    errorMessage += '<li>' + element.message + '</li>';
                });
                errorMessage += '</ul>';
                errorMessage += '</div>';
                form.append($('.form-errors').html(errorMessage));
                form.find('button').html(literals['form.submit.value']).attr('disabled', true);
            }
        });
    });
});