/* Captain Widgets Kit — Elementor Notice JS */
(function ($) {
    'use strict';

    var CaptWikiNotice = {

        init: function () {
            $(document).on('click', '.captwiki-el-action-btn', this.handleAction.bind(this));
            $(document).on('click', '.captwiki-el-dismiss-btn', this.handleDismiss.bind(this));
        },

        handleAction: function (e) {
            e.preventDefault();

            var $btn    = $(e.currentTarget);
            var action  = $btn.data('action'); // 'install' or 'activate'
            var $notice = $btn.closest('.captwiki-el-notice');

            if ($btn.hasClass('is-loading')) {
                return;
            }

            this.setLoading($btn, true);

            $.ajax({
                url:    captwiki_notice_data.ajax_url,
                type:   'POST',
                data: {
                    action:   'captwiki_elementor_' + action,
                    nonce:    captwiki_notice_data.nonce,
                },
                success: function (response) {
                    if (response.success) {
                        CaptWikiNotice.showSuccess($notice, response.data.message);

                        // Reload page after 1.8s so Elementor initialises
                        setTimeout(function () {
                            window.location.reload();
                        }, 1800);
                    } else {
                        CaptWikiNotice.showError($btn, response.data.message || captwiki_notice_data.error_msg);
                    }
                },
                error: function () {
                    CaptWikiNotice.showError($btn, captwiki_notice_data.error_msg);
                },
            });
        },

        handleDismiss: function (e) {
            e.preventDefault();
            var $notice = $(e.currentTarget).closest('.captwiki-el-notice-wrap');

            $.ajax({
                url:  captwiki_notice_data.ajax_url,
                type: 'POST',
                data: {
                    action: 'captwiki_dismiss_elementor_notice',
                    nonce:  captwiki_notice_data.nonce,
                },
            });

            $notice.fadeOut(250, function () {
                $(this).remove();
            });
        },

        setLoading: function ($btn, state) {
            if (state) {
                $btn.addClass('is-loading');
                $btn.find('.captwiki-el-btn-text').hide();
                $btn.find('.captwiki-el-btn-spinner').show();
            } else {
                $btn.removeClass('is-loading');
                $btn.find('.captwiki-el-btn-text').show();
                $btn.find('.captwiki-el-btn-spinner').hide();
            }
        },

        showSuccess: function ($notice, message) {
            $notice.addClass('captwiki-el-notice--success');
            $notice.find('.captwiki-el-notice-actions').html(
                '<div class="captwiki-el-notice-success-msg">' +
                    '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg>' +
                    '<span>' + message + '</span>' +
                '</div>'
            );
        },

        showError: function ($btn, message) {
            this.setLoading($btn, false);
            $btn.after('<span style="color:#dc2626;font-size:12px;margin-left:8px;">' + message + '</span>');
        },
    };

    $(document).ready(function () {
        CaptWikiNotice.init();
    });

}(jQuery));
