/* ScanVault Label Designer — Product List JS */
(function ($) {
    'use strict';

    var params = svld_list_params;

    // Quick-print button in product column
    $(document).on('click', '.svld-quick-print', function () {
        var $btn = $(this);
        var id   = $btn.data('id');

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

        $.post(params.ajax_url, {
            action:     'svld_print_label',
            nonce:      $btn.data('nonce') || params.nonce,
            product_id: id,
            quantity:   1,
        }, function (res) {
            $btn.prop('disabled', false).find('.dashicons').removeClass('dashicons-update').addClass('dashicons-printer');
            if (res.success && res.data.pdf_url) {
                window.open(res.data.pdf_url, '_blank');
            } else {
                alert(params.error);
            }
        }).fail(function () {
            $btn.prop('disabled', false).find('.dashicons').removeClass('dashicons-update').addClass('dashicons-printer');
            alert(params.error);
        });
    });

    // Bulk print trigger button (in admin notice)
    $(document).on('click', '.svld-trigger-bulk-print', function () {
        var $btn    = $(this);
        var ids     = $btn.data('ids');
        var $spinner = $btn.closest('.svld-bulk-notice').find('.svld-bulk-spinner');

        $btn.hide();
        $spinner.show();

        $.post(params.ajax_url, {
            action:      'svld_bulk_print',
            nonce:       $btn.data('nonce') || params.nonce,
            product_ids: ids,
        }, function (res) {
            $spinner.hide();
            $btn.show();
            if (res.success && res.data.pdf_url) {
                window.open(res.data.pdf_url, '_blank');
            } else {
                alert(params.error);
            }
        }).fail(function () {
            $spinner.hide();
            $btn.show();
            alert(params.error);
        });
    });

}(jQuery));
