
$(function() {

    var selectFormato, selectCategoria, pesquisa = '';

    var page = $('#page').val();
    var totalPages = $('#totalPages').val();

    $('.c-select select').on('change', function (){

        selectFormato = $('#c-select__formato').val();
        selectCategoria = $('#c-select__categoria').val();
        pesquisa = $('#c-search__pesquisa').val();

        $('#page').val('1');
        page = 1;

        $('.c-button_loadmore').css({'display':''});
        $('.c-articles-list__body').html('');

        sendSearch()

    })

    function sendSearch(){

        var url = $('.c-filters__body').data('action');
        var tipo = $('.c-filters__body').data('tipo');
        totalPages = $('#totalPages').val();

        $.ajax({
            method: 'POST',
            url: url,
            headers: {'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')},
            data: {formato: selectFormato, categoria: selectCategoria, pesquisa : pesquisa, page : page, tipo:tipo},
            dataType: 'json',
            success: function(response) {
                var totalPages = response.totalPages;

                if (response.html) {
                    $('.c-articles-list__body').append(response.html);
                    window.checkLikeList();
                    $('#totalPages').val(totalPages);
                    if (page >= parseInt(totalPages)) {
                        $('.c-button_loadmore').css({'display':'none'});
                    } else {
                        page = page++;
                        $('#page').val(page.toString());
                    }
                }
            }
        })
    }


    $('#c-search__pesquisa').on('search', function (){
        selectFormato = $('#c-select__formato').val();
        selectCategoria = $('#c-select__categoria').val();
        pesquisa = $('#c-search__pesquisa').val();

        $('#page').val('1');
        page = 1;

        $('.c-button_loadmore').css({'display':''});
        $('.c-articles-list__body').html('');

        sendSearch()
    })



    $('.c-filters__footer__clear').on('click', function (){

        var selectFormato = $('#c-select__formato').val('');
        var selectCategoria = $('#c-select__categoria').val('');
        var pesquisa = $('#c-search__pesquisa').val('');

        $('#page').val('1');
        page = 1;

        $('.c-button_loadmore').css({'display':''});
        $('.c-articles-list__body').html('');

        sendSearch()
    })


    $('.c-button_loadmore').on('click', function(e){

        selectFormato = $('#c-select__formato').val();
        selectCategoria = $('#c-select__categoria').val();
        pesquisa = $('#c-search__pesquisa').val();
        page = $('#page').val();
        page++;

        sendSearch()
        return false;

    })

    $('.js-form').on('submit', function (e){
        e.preventDefault();
        console.log('clicou')

        grecaptcha.execute();

    });

    $('.contato-modal').on('submit', function (event){
        event.preventDefault();

        $('.js-form-send').prop("disabled", true);
        $('.js-form-send').text("Enviando...");

        var url = $(this).attr('action');
        var dataForm = $(this).serialize();



        $.ajax({
            method: 'POST',
            url: url,
            headers: {'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')},
            data: dataForm,
            dataType: 'json',
            success: function(response) {

                if (response.status) {
                    $('.js-form-alert').html(response.txt);
                    $('.js-form-alert').removeClass('d-none');
                    $('.js-form-send').prop("disabled", false);
                    $('.js-form-send').text("Enviar");

                    setTimeout(function(){
                        $('.js-form-alert').html('');
                        $('.js-form-alert').addClass('d-none');

                    }, 5000);
                } else {
                    console.log('entrou');
                    $('.js-form-alert').html(response.txt);
                    $('.js-form-alert').removeClass('d-none');

                }
            }
        })

        return false;

    })

    // Exibir formulário de área do cliente
    var btPularEtapa = document.getElementById("js-exibir-form");
    var formAreaCliente = document.getElementById("formAreaCliente");

    if (btPularEtapa) {
        btPularEtapa.addEventListener("click", function(e) {
            e.preventDefault();
            formAreaCliente.classList.remove("d-none");
            btPularEtapa.classList.add("d-none");
        });
    }

})



function sendForm(token){

    $('.js-form-send').prop("disabled", true);
    $('.js-form-send').text("Enviando...");

    var url = $('.js-form').data('action');
    var dataForm = new FormData(document.getElementById("form-contato"));
    console.log(url);

    var modal =  document.getElementById("modalCta");

    $.ajax({
        method: 'POST',
        url: url,
        headers: {'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')},
        data: dataForm,
        processData: false,
        contentType: false,
        success: function(response) {

            if (response.status) {

                $('.js-form-alert').html(response.txt);
                $('.js-form-alert').removeClass('d-none');
                $('.js-form-send').prop("disabled", false);
                $('.js-form-send').text("Enviar");

                // $('#modalCta .btn-close').click();

                document.getElementById("form-contato").reset();

                setTimeout(function(){
                    $('.js-form-alert').html('');
                    $('.js-form-alert').addClass('d-none');
                }, 5000);

            } else {
                console.log('entrou');
                $('.js-form-alert').html(response.txt);
                $('.js-form-alert').removeClass('d-none');

                setTimeout(function(){
                    $('.js-form-alert').html('');
                    $('.js-form-alert').addClass('d-none');
                }, 5000);

            }
        }
    });

    $('.js-form-send').prop("disabled", false);
    $('.js-form-send').text("Enviar");

    return false;
}

function sendFormTrabalhe(token){

    $('.js-form-send').prop("disabled", true);
    $('.js-form-send').text("Enviando...");

    var url = $('.js-form').data('action');
    var dataForm = new FormData(document.getElementById("form-contato-trabalhe"));
    console.log(url);

    var modal =  document.getElementById("modalCta");

    $.ajax({
        method: 'POST',
        url: url,
        headers: {'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')},
        data: dataForm,
        processData: false,
        contentType: false,
        success: function(response) {

            if (response.status) {

                $('.js-form-alert').html(response.txt);
                $('.js-form-alert').removeClass('d-none');
                $('.js-form-send').prop("disabled", false);
                $('.js-form-send').text("Enviar");

                $('#modalCta .btn-close').click();

                document.getElementById("form-contato-trabalhe").reset();

                setTimeout(function(){
                    $('.js-form-alert').html('');
                    $('.js-form-alert').addClass('d-none');
                }, 5000);

            } else {
                console.log("entrou");
                $(".js-form-alert").html(response.txt);
                $(".js-form-alert").removeClass("d-none");

                setTimeout(function() {
                    $(".js-form-alert").html("");
                    $(".js-form-alert").addClass("d-none");
                }, 5000);
            }
        }
    });

    $('.js-form-send').prop("disabled", false);
    $('.js-form-send').text("Enviar");

    return false;
}
