$(document).ready(function() {
    var
        token = $('body').data('token'),
        info = $('[data-info]').data('info');

    $('.js-send-email').on('click', function (event) {
        event.preventDefault();
        var $this = $(this);
        $.ajax({
            url: 'index.php?route=fidelity/indication/sendEmailModal',
            type: 'GET',
            dataType: 'html',
            data: '',
            beforeSend: function() {
                $('.js-feedback-message').empty();
                $this.addClass("btn-loading");
            },
            complete: function() {
                $this.removeClass("btn-loading");
            },
            success: function(data) {
                $('.js-content-indication-email').append(data);

                $('.overlay, .js-indication-email-cancel').on('click', function(event) {
                    event.preventDefault();
                    $('.js-content-indication-email > *').remove();
                });

                // $('.js-indication-email-send').addClass("btn-loading");

                $('.js-indication-email-send').on('click', function(event) {
                    sendEmail(event);
                });
            }
        });
    });

    function sendEmail(event){
        event.preventDefault();
        var $this = $(this);
        $.ajax({
            url: 'index.php?route=fidelity/indication/sendEmail',
            type: 'POST',
            dataType: 'json',
            data: 'email_enderecos=' + $('#email_enderecos').val(),
            beforeSend: function() {
                $('.js-indication-email-send').addClass("btn-loading");
            },
            complete: function() {
                $('.js-indication-email-send').removeClass("btn-loading");
            },
            success: function(json) {
                $('.error, .success').remove();
                $('#email_enderecos').removeClass('border-error');
                $('.js-text-error').remove();

                if (json['error']) {
                    $.each(json['error'], function (key, value) {
                        $('#email_enderecos').addClass('border-error').after('<span class="text-x-small text-error js-text-error">' + value + '</span>');
                    });
                }
                if (json['success']) {
                    $('.js-feedback-message').append('<div class="success top-margin-medium">' + json['success'] + '</div>');
                    $('.js-content-indication-email > *').remove();
                }
            }
        });
    };

    // App

    $('.js-indication-email-send-app').on('click', function(event) {
        sendEmailApp(event);
    });

    function sendEmailApp(event){
        event.preventDefault();
        var $this = $(this);
        $.ajax({
            url: 'index.php?route=fidelity/indication/sendEmail',
            type: 'POST',
            dataType: 'json',
            data: 'email_enderecos=' + $('#email_enderecos').val(),
            beforeSend: function() {
                $('.js-indication-email-send-app').addClass("ftr-loading-bar");
            },
            complete: function() {
                $('.js-indication-email-send-app').removeClass("ftr-loading-bar");
            },
            success: function(json) {
                $('.error, .success').remove();
                $('.js-text-error').remove();

                if (json['error']) {
                    $.each(json['error'], function (key, value) {
                        $('#email_enderecos').after('<span class="ftr-text--red ftr-in-space--v-l js-text-error">' + value + '</span>');
                    });
                }
                if (json['success']) {
                    $('.js-feedback-message').append('<div class="success top-margin-medium">' + json['success'] + '</div>');
                    $('.js-content-indication-email > *').remove();
                    $('#email_enderecos').val('');
                    contentSwipeDisplayNone();
                }
            }
        });
    };
});
