$(document).on('click', '.btn[data-item-company-state]', function (e) {
    e.preventDefault();

    const btn = $(this);

    const companyUuid = btn.data('item-uuid');
    const currentState = btn.data('item-state');         // "active" or "imported"
    const newState = (currentState === "active") ? "imported" : "active";

    const url = "/api/v1/customers/companies/activate/";
    const params = {
        company_uuid: companyUuid,
        company_state: newState,
        "X-HTTP-Method-Override": "PUT"
    };

    const rootStyles = getComputedStyle(document.documentElement);
    const background = rootStyles.getPropertyValue('--swal-bg')?.trim();
    const color = rootStyles.getPropertyValue('--swal-text-color')?.trim();

    $.post(url, params)
        .done(function (response, status, jqXHR) {

            if (jqXHR.status === 200) {


                btn.data('item-state', newState);
                btn.removeClass('btn-success btn-danger');
                btn.addClass(newState === "active" ? "btn-danger" : "btn-success");
                btn.find('i')
                    .removeClass('fa-plus fa-xmark')
                    .addClass(newState === "active" ? "fa-xmark" : "fa-plus");

                const row = btn.closest("tr");
                const stateCell = row.find("td").eq(3);
                stateCell.text(newState);
            }
        })
        .fail(function (jqXHR) {
            let response = jqXHR.responseJSON || {};

            Swal.fire({
                background: background,
                color: color,
                icon: "error",
                title: __('connection_error_title'),
                text: response.message || __('connection_error_text') + jqXHR.status,
                confirmButtonText: __('close'),
                customClass: {confirmButton: "btn btn-danger"},
                buttonsStyling: false
            });
        });
});