/* ScanVault Label Designer — Order List Bulk Print */
/* global svld_order_list_params */

(function ($) {
    'use strict';

    $(function () {
        // Inject "Print Order Labels" into the bulk actions dropdown if not already present.
        // This covers HPOS orders list where the PHP filter may not fire in time.
        $('select[name="action"], select[name="action2"]').each(function () {
            var $select = $(this);
            if ($select.find('option[value="svld_print_order_labels"]').length === 0) {
                $select.append(
                    $('<option>', {
                        value: 'svld_print_order_labels',
                        text: svld_order_list_params.bulk_action_label || 'Print Order Labels'
                    })
                );
            }
        });

        // ── Per-row print button (Actions column) ─────────────────────────────────
        $(document).on('click', '.svld-print-order-row', function () {
            var $btn     = $(this);
            var orderId  = $btn.data('order-id');
            var nonce    = $btn.data('nonce');
            var $spinner = $btn.find('.dashicons');

            $btn.prop('disabled', true);
            $spinner.removeClass('dashicons-printer').addClass('dashicons-update svld-spin');

            $.post(svld_order_list_params.ajax_url, {
                action:   'svld_print_order_labels',
                nonce:    nonce,
                order_id: orderId,
            }, function (res) {
                $btn.prop('disabled', false);
                $spinner.removeClass('dashicons-update svld-spin').addClass('dashicons-printer');

                if (res.success && res.data && res.data.pdf_url) {
                    window.open(res.data.pdf_url, '_blank');
                } else {
                    alert(svld_order_list_params.error);
                }
            }).fail(function () {
                $btn.prop('disabled', false);
                $spinner.removeClass('dashicons-update svld-spin').addClass('dashicons-printer');
                alert(svld_order_list_params.error);
            });
        });

        // ── Quick print button (custom Print column) ─────────────────────────────
        $(document).on('click', '.svld-quick-print-order', function () {
            var $btn     = $(this);
            var orderId  = $btn.data('id');
            var nonce    = $btn.data('nonce');
            var $spinner = $btn.find('.dashicons');

            $btn.prop('disabled', true);
            $spinner.removeClass('dashicons-printer').addClass('dashicons-update svld-spin');

            $.post(svld_order_list_params.ajax_url, {
                action:   'svld_print_order_labels',
                nonce:    nonce,
                order_id: orderId,
            }, function (res) {
                $btn.prop('disabled', false);
                $spinner.removeClass('dashicons-update svld-spin').addClass('dashicons-printer');

                if (res.success && res.data && res.data.pdf_url) {
                    window.open(res.data.pdf_url, '_blank');
                } else {
                    alert(svld_order_list_params.error);
                }
            }).fail(function () {
                $btn.prop('disabled', false);
                $spinner.removeClass('dashicons-update svld-spin').addClass('dashicons-printer');
                alert(svld_order_list_params.error);
            });
        });

        // ── Bulk print (after redirect back with transient key) ──────────────
        $(document).on('click', '.svld-trigger-order-bulk-print', function () {
            var $btn  = $(this);
            var ids   = $btn.data('ids');   // comma-separated order IDs
            var nonce = $btn.data('nonce');

            $btn.prop('disabled', true).hide();
            $('.svld-bulk-spinner').show();

            $.post(svld_order_list_params.ajax_url, {
                action:    'svld_bulk_print_order_labels',
                nonce:     nonce,
                order_ids: ids,
            }, function (res) {
                $('.svld-bulk-spinner').hide();
                $btn.remove();

                if (res.success && res.data && res.data.pdf_url) {
                    window.open(res.data.pdf_url, '_blank');
                } else {
                    alert(svld_order_list_params.error);
                    $btn.prop('disabled', false).show();
                }
            }).fail(function () {
                $('.svld-bulk-spinner').hide();
                alert(svld_order_list_params.error);
                $btn.prop('disabled', false).show();
            });
        });
    });

}(jQuery));
