/**
 * CloudXM NFe Legacy Services Table JavaScript
 */

(function($) {
    'use strict';

    $(document).ready(function() {
        $('#cloudxm-nfe-sync').on('click', function() {
            alert(cloudxm_nfe_services.i18n.sync_coming_soon);
        });

        $('.cloudxm-nfe-download-pdf').on('click', function() {
            var invoiceId = $(this).data('invoice-id');
            downloadInvoiceFile(invoiceId, 'pdf');
        });

        $('.cloudxm-nfe-download-xml').on('click', function() {
            var invoiceId = $(this).data('invoice-id');
            downloadInvoiceFile(invoiceId, 'xml');
        });

        $('.cloudxm-nfe-cancel').on('click', function() {
            var invoiceId = $(this).data('invoice-id');
            var reason = prompt(cloudxm_nfe_services.i18n.prompt_cancel);

            if (reason) {
                $.ajax({
                    url: cloudxm_nfe_services.rest_url + invoiceId + '/cancel',
                    method: 'POST',
                    data: { reason: reason },
                    beforeSend: function(xhr) {
                        xhr.setRequestHeader('X-WP-Nonce', cloudxm_nfe_services.nonce);
                    },
                    success: function(response) {
                        if (response.success) {
                            alert(cloudxm_nfe_services.i18n.cancel_success);
                            location.reload();
                        } else {
                            alert(response.message || cloudxm_nfe_services.i18n.cancel_error);
                        }
                    },
                    error: function() {
                        alert(cloudxm_nfe_services.i18n.cancel_error);
                    }
                });
            }
        });

        $('.cloudxm-nfe-issue').off('click').on('click', function() {
            var orderId = $(this).data('order-id');

            if (confirm(cloudxm_nfe_services.i18n.confirm_issue)) {
                $.ajax({
                    url: cloudxm_nfe_services.rest_url,
                    method: 'POST',
                    data: { order_id: orderId },
                    beforeSend: function(xhr) {
                        xhr.setRequestHeader('X-WP-Nonce', cloudxm_nfe_services.nonce);
                    },
                    success: function(response) {
                        if (response.success) {
                            alert(cloudxm_nfe_services.i18n.issue_success);
                            location.reload();
                        } else {
                            alert(response.message || cloudxm_nfe_services.i18n.issue_error);
                        }
                    },
                    error: function() {
                        alert(cloudxm_nfe_services.i18n.issue_error);
                    }
                });
            }
        });
    });

    /**
     * Download invoice file (XML or PDF) using AJAX
     */
    function downloadInvoiceFile(invoiceId, type) {
        var form = $('<form>', {
            'method': 'POST',
            'action': cloudxm_nfe_services.ajax_url
        });

        form.append($('<input>', { 'type': 'hidden', 'name': 'action', 'value': 'nfe_download_file' }));
        form.append($('<input>', { 'type': 'hidden', 'name': 'nonce', 'value': cloudxm_nfe_services.nonce }));
        form.append($('<input>', { 'type': 'hidden', 'name': 'invoice_id', 'value': invoiceId }));
        form.append($('<input>', { 'type': 'hidden', 'name': 'type', 'value': type }));

        form.appendTo('body').submit().remove();
    }

})(jQuery);
