jQuery(document).ready(function ($) {
    var $modal = $('.sifency-feedback-modal');
    var $overlay = $('.sifency-feedback-modal-overlay');
    var actionUrl = '';
    var actionType = '';

    var deactivateLinkSelector = '[data-slug="' + sifencyDeactivation.plugin_basename.split('/')[0] + '"] .deactivate a';
    var deleteLinkSelector = '[data-slug="' + sifencyDeactivation.plugin_basename.split('/')[0] + '"] .delete a';

    $('#the-list').on('click', deactivateLinkSelector + ', ' + deleteLinkSelector, function (e) {
        e.preventDefault();
        
        actionUrl = this.href;
        actionType = $(this).closest('.delete').length > 0 ? 'delete' : 'deactivate';

        if (actionType === 'delete') {
            $modal.find('.sifency-feedback-submit').text(sifencyDeactivation.i18n.submit_delete);
            $modal.find('.sifency-feedback-skip').text(sifencyDeactivation.i18n.skip_delete);
            $modal.find('.sifency-data-choice-warning').show();
        } else {
            $modal.find('.sifency-feedback-submit').text(sifencyDeactivation.i18n.submit_deactivate);
            $modal.find('.sifency-feedback-skip').text(sifencyDeactivation.i18n.skip_deactivate);
            $modal.find('.sifency-data-choice-warning').hide();
        }

        $overlay.fadeIn(200);
        $modal.fadeIn(200);
    });

    // Handle form actions (Submit or Skip)
    $modal.on('click', '.sifency-feedback-submit, .sifency-feedback-skip', function (e) {
        e.preventDefault();
        var $button = $(this);
        $button.prop('disabled', true).text('Processing...');

        var promises = [];
        var dataChoice = $modal.find('input[name="sifency_data_choice"]:checked').val();
        var isSubmittingFeedback = $button.hasClass('sifency-feedback-submit');

        if (dataChoice === 'delete') {
            promises.push($.post(sifencyDeactivation.ajax_url, {
                action: 'sifency_set_uninstall_flag',
                nonce: sifencyDeactivation.nonce
            }));
        }

        if (isSubmittingFeedback) {
            promises.push($.post(sifencyDeactivation.ajax_url, {
                action: 'sifency_submit_deactivation_feedback',
                nonce: sifencyDeactivation.nonce,
                reason: $modal.find('input[name="sifency_feedback_reason"]:checked').val(),
                other_info: $modal.find('.sifency-feedback-other').val()
            }));
        }

        $.when.apply(null, promises).always(function () {
            window.location.href = actionUrl;
        });
    });

    $modal.on('change', 'input[name="sifency_feedback_reason"]', function () {
        var $otherTextarea = $modal.find('.sifency-feedback-other');
        if ($(this).val() === 'other') {
            $otherTextarea.slideDown();
        } else {
            $otherTextarea.slideUp();
        }
    });

    // Handle cancel
    $modal.on('click', '.sifency-feedback-cancel', function (e) {
        e.preventDefault();
        $modal.fadeOut(200);
        $overlay.fadeOut(200);
    });
});