/**
 * SiteStaffr Banned Users — Ban modal flow.
 *
 * The banned table itself is server-rendered (SiteStaffr_Banned_List_Table)
 * and Unban is a nonce link, so this file only drives the Ban User modal.
 * A successful ban reloads the page so the table and tab count refresh.
 *
 * @package    SiteStaffr
 * @subpackage SiteStaffr/admin/js
 * @since      1.2.3
 */

(function($) {
    'use strict';

    var data = window.sitestaffrBannedData;
    if (!data) {
        return;
    }

    $(document).ready(function() {
        // Ban User modal — chrome (Esc, backdrop, close X, focus trap,
        // focus-return) comes from the shared component.
        var banModalEl = document.getElementById('sitestaffr-ban-modal');
        var banController = banModalEl && window.SiteStaffrModal
            ? window.SiteStaffrModal.enhance(banModalEl, {
                initialFocus: '#sitestaffr-ban-phone',
                onOpen: function() {
                    $('#sitestaffr-ban-phone').val('').removeClass('error');
                    $('#sitestaffr-ban-reason').val('');
                    $('#sitestaffr-ban-phone-error').hide();
                }
            })
            : null;

        $('#sitestaffr-open-ban-modal, #sitestaffr-tab-ban-btn').on('click', function() {
            if (banController) {
                banController.open();
            }
        });

        $('#sitestaffr-cancel-ban').on('click', function() {
            if (banController) {
                banController.close();
            }
        });

        // Ban User form submission
        $('#sitestaffr-submit-ban').on('click', function() {
            var identifier = $('#sitestaffr-ban-phone').val().trim();
            var reason = $('#sitestaffr-ban-reason').val().trim();

            // Clear previous errors
            $('#sitestaffr-ban-phone').removeClass('error');
            $('#sitestaffr-ban-phone-error').hide();

            if (!identifier) {
                $('#sitestaffr-ban-phone').addClass('error');
                $('#sitestaffr-ban-phone-error')
                    .text(data.i18n.identifierRequired)
                    .show();
                return;
            }

            var $submitBtn = $('#sitestaffr-submit-ban');
            $submitBtn.prop('disabled', true).html('<span class="spinner is-active" style="float: none; margin: 0 8px 0 0;"></span>' + data.i18n.banning);

            $.ajax({
                url: ajaxurl,
                type: 'POST',
                data: {
                    action: 'sitestaffr_ban_visitor',
                    identifier: identifier,
                    reason: reason,
                    nonce: data.nonce
                },
                success: function(response) {
                    if (response.success) {
                        // Reload so the server-rendered table + tab count refresh.
                        window.location.reload();
                        return;
                    }
                    var errorMsg = response.data && response.data.message ? response.data.message : data.i18n.banFailed;
                    $('#sitestaffr-ban-phone').addClass('error');
                    $('#sitestaffr-ban-phone-error').text(errorMsg).show();
                    $submitBtn.prop('disabled', false).html('<span class="dashicons dashicons-shield" aria-hidden="true"></span>' + data.i18n.banUser);
                },
                error: function(xhr, status, error) {
                    console.error('SiteStaffr: Ban failed:', error);
                    $('#sitestaffr-ban-phone').addClass('error');
                    $('#sitestaffr-ban-phone-error').text(data.i18n.banFailed).show();
                    $submitBtn.prop('disabled', false).html('<span class="dashicons dashicons-shield" aria-hidden="true"></span>' + data.i18n.banUser);
                }
            });
        });
    });

})(jQuery);
