/**
 * deactivate-survey.js
 *
 * @since      6.9.3
 * @package    WcElectronInvoice
 * @author     alfiopiccione <alfio.piccione@gmail.com>
 * @license    http://opensource.org/licenses/gpl-2.0.php GPL v2
 *
 * Copyright (C) 2025 POP
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

;(function ($, data) {
    'use strict';

    if (!data || !data.nonce || !data.plugin_file) {
        return;
    }

    $(document).ready(function () {

        // data-plugin contains the plugin file path (e.g. pop-wordpress/index.php),
        // which is stable regardless of the WordPress.org slug shown in data-slug.
        var row = document.querySelector('tr[data-plugin="' + data.plugin_file + '"]');
        if (!row) {
            return;
        }

        var deactivateLink = row.querySelector('.deactivate a');
        if (!deactivateLink) {
            return;
        }

        var deactivateUrl = deactivateLink.href;
        var reasons       = data.reasons || {};

        var overlay = document.createElement('div');
        overlay.id        = 'pop-deactivate-overlay';
        overlay.className = 'pop-deactivate-overlay pop-deactivate-overlay--hidden';
        overlay.innerHTML =
            '<div class="pop-deactivate-modal">' +
                '<button class="pop-deactivate-modal__close" id="pop-deactivate-close" aria-label="Close">&times;</button>' +
                '<div class="pop-deactivate-modal__logo">' +
                    '<img src="' + data.logo_url + '" alt="POP">' +
                '</div>' +
                '<h3 class="pop-deactivate-modal__title">' + data.title + '</h3>' +
                '<p class="pop-deactivate-modal__desc">' + data.desc + '</p>' +
                '<div class="pop-deactivate-modal__field">' +
                    '<label for="pop-deactivate-reason">' + data.reason_label + '</label>' +
                    '<select id="pop-deactivate-reason">' +
                        '<option value="">'                    + (reasons.placeholder     || '') + '</option>' +
                        '<option value="too_expensive">'  + (reasons.too_expensive   || '') + '</option>' +
                        '<option value="setup_issue">'    + (reasons.setup_issue     || '') + '</option>' +
                        '<option value="missing_feature">'+ (reasons.missing_feature || '') + '</option>' +
                        '<option value="not_working">'    + (reasons.not_working     || '') + '</option>' +
                        '<option value="not_needed">'     + (reasons.not_needed      || '') + '</option>' +
                        '<option value="switched_plugin">'+ (reasons.switched_plugin || '') + '</option>' +
                        '<option value="other">'          + (reasons.other           || '') + '</option>' +
                    '</select>' +
                '</div>' +
                '<div class="pop-deactivate-modal__field">' +
                    '<label for="pop-deactivate-message">' + data.message_label + '</label>' +
                    '<textarea id="pop-deactivate-message" placeholder="' + data.message_placeholder + '"></textarea>' +
                '</div>' +
                '<div class="pop-deactivate-modal__actions">' +
                    '<button class="pop-deactivate-modal__btn-submit" id="pop-deactivate-submit">' + data.deactivate + '</button>' +
                '</div>' +
            '</div>';

        document.body.appendChild(overlay);

        function showModal() {
            overlay.classList.remove('pop-deactivate-overlay--hidden');
        }

        function hideModal() {
            overlay.classList.add('pop-deactivate-overlay--hidden');
        }

        function doDeactivate() {
            window.location.href = deactivateUrl;
        }

        deactivateLink.addEventListener('click', function (e) {
            e.preventDefault();
            showModal();
        });

        document.getElementById('pop-deactivate-close').addEventListener('click', hideModal);

        overlay.addEventListener('click', function (e) {
            if (e.target === overlay) {
                hideModal();
            }
        });

        document.addEventListener('keydown', function (e) {
            if (e.key === 'Escape') {
                hideModal();
            }
        });

        document.getElementById('pop-deactivate-submit').addEventListener('click', function () {
            var reason    = document.getElementById('pop-deactivate-reason').value;
            var message   = document.getElementById('pop-deactivate-message').value;
            var submitBtn = document.getElementById('pop-deactivate-submit');

            submitBtn.disabled = true;

            $.ajax({
                url:    data.ajax_url,
                method: 'POST',
                data: {
                    action:  'pop_send_deactivation_feedback',
                    nonce:   data.nonce,
                    reason:  reason,
                    message: message,
                },
                complete: function () {
                    doDeactivate();
                },
            });
        });

    });

}(window.jQuery, window.wc_el_inv_deactivate));
