Aquarius.component.define('Frontend/Profile', function ($element, options) {
    var defaults = {};

    function Profile() {
    }

    Profile.prototype = {
        init: function () {
            var self = this;

            this.$element = $element;
            this.options = Aquarius.extend(defaults, options);

            this.modal = $('#event-modal');

            this.events();
        },

        events: function() {
            var self = this;

            self.$element.on('click', '[data-action-btn]', function() {
                const $button = $(this);
                const $eventItem = $button.closest('[data-event-id]');
                const eventId = $eventItem.attr('data-event-id');
                const websiteId = $eventItem.attr('data-website-id');
                const applicationId = $eventItem.attr('data-application-id');

                self.showModal(websiteId, eventId, applicationId);
            });

            this.modal.on('click', '[data-action]', function() {
                const $button = $(this);
                const action = $button.data('action');
                const websiteId = $button.attr('data-website');
                const applicationId = $button.attr('data-application');

                if (action === 'resign') {
                    Aquarius.ajax({
                            method: 'POST',
                            url: `/api/v1/companies/1053/me/applications/${websiteId}/resignation/${applicationId}`,
                            success: function(res) {
                                if (res.isValid) {
                                    self.closeModal();
                                    location.reload();
                                } else {
                                    self.$element.find('.errors').append('Hiba történt');
                                }
                            },
                            fail: function(err) {
                                self.$element.find('.errors').append('Hiba történt');
                            }
                        })
                }
            })

            this.modal.find('.modal-backdrop, [data-modal-close]').on('click', () => {
                self.closeModal();
            });

            $('.modal-close').on('click', () => {
                self.closeModal();
            });
        },

        closeModal: function() {
            this.modal.removeClass('active');
            $('body, html').removeClass('modal-is-active');
        },

        showModal: function(websiteId, eventId, applicationId) {
            this.modal.addClass('active');
            $('body, html').addClass('modal-is-active');

            this.modal.find('h2').text('Biztos, hogy lemondod az edzést?');
            this.modal
                .find('[data-action-btn]')
                .attr('data-action', 'resign')
                .attr('data-event', eventId)
                .attr('data-website', websiteId)
                .attr('data-application', applicationId)
                .text('Lemondom');
        }
    }

    return Profile
})