/**
 * CloudXM NFe Legacy Order Meta Box JavaScript
 */

(function($) {
    'use strict';

    $(document).ready(function() {
        $(document).on('click', '.cloudxm-nfe-issue', function(e) {
            e.preventDefault();
            var $button = $(this);
            var orderId = $button.data('order-id');
            var nonce = $button.data('nonce');

            $button.prop('disabled', true).text(cloudxm_nfe_legacy_meta.i18n.issuing);

            $.ajax({
                url: cloudxm_nfe_legacy_meta.ajax_url,
                type: 'POST',
                data: {
                    action: 'cloudxm_nfe_issue',
                    order_id: orderId,
                    _wpnonce: nonce
                },
                success: function(response) {
                    if (response.success) {
                        alert(response.data.message);
                        location.reload();
                    } else {
                        alert(response.data.message);
                        $button.prop('disabled', false).text(cloudxm_nfe_legacy_meta.i18n.issue_button);
                    }
                },
                error: function() {
                    alert(cloudxm_nfe_legacy_meta.i18n.error_occurred);
                    $button.prop('disabled', false).text(cloudxm_nfe_legacy_meta.i18n.issue_button);
                }
            });
        });

        $(document).on('click', '.cloudxm-nfe-query-status', function(e) {
            e.preventDefault();
            var $button = $(this);
            var orderId = $button.data('order-id');
            var nonce = $button.data('nonce');

            $button.prop('disabled', true).text(cloudxm_nfe_legacy_meta.i18n.querying);

            $.ajax({
                url: cloudxm_nfe_legacy_meta.ajax_url,
                type: 'POST',
                data: {
                    action: 'cloudxm_nfe_query_status',
                    order_id: orderId,
                    _wpnonce: nonce
                },
                success: function(response) {
                    if (response.success) {
                        alert(response.data.message);
                        location.reload();
                    } else {
                        alert(response.data.message);
                        $button.prop('disabled', false).text(cloudxm_nfe_legacy_meta.i18n.query_button);
                    }
                },
                error: function() {
                    alert(cloudxm_nfe_legacy_meta.i18n.error_occurred);
                    $button.prop('disabled', false).text(cloudxm_nfe_legacy_meta.i18n.query_button);
                }
            });
        });

        $(document).on('click', '.cloudxm-nfe-cancel', function(e) {
            e.preventDefault();
            var $button = $(this);
            var orderId = $button.data('order-id');
            var nonce = $button.data('nonce');

            if (!confirm(cloudxm_nfe_legacy_meta.i18n.confirm_cancel)) {
                return;
            }

            $button.prop('disabled', true).text(cloudxm_nfe_legacy_meta.i18n.cancelling);

            $.ajax({
                url: cloudxm_nfe_legacy_meta.ajax_url,
                type: 'POST',
                data: {
                    action: 'cloudxm_nfe_cancel',
                    order_id: orderId,
                    _wpnonce: nonce
                },
                success: function(response) {
                    if (response.success) {
                        alert(response.data.message);
                        location.reload();
                    } else {
                        alert(response.data.message);
                        $button.prop('disabled', false).text(cloudxm_nfe_legacy_meta.i18n.cancel_button);
                    }
                },
                error: function() {
                    alert(cloudxm_nfe_legacy_meta.i18n.error_occurred);
                    $button.prop('disabled', false).text(cloudxm_nfe_legacy_meta.i18n.cancel_button);
                }
            });
        });
    });

})(jQuery);
