// Upsells admin picker — vanilla ES6 (no jQuery).
// Delegated off the .upsells form so it works for every picker row.
document.addEventListener('DOMContentLoaded', () => {
    const form = document.querySelector('.upsells');
    if (!form) {
        return;
    }

    form.addEventListener('click', (event) => {
        const browse = event.target.closest('.browseUpsell');
        if (browse) {
            const url = `/shop/admin/products/manage.products.mini.php?field=upsell_${browse.dataset.id}`;
            window.open(url, 'productselector', 'status=yes,scrollbars=yes,resizable=yes,width=500,height=600');
            return;
        }

        const cancel = event.target.closest('.cancelUpsell');
        if (cancel) {
            const id = cancel.dataset.id;
            const hidden = document.getElementById(`upsell_${id}`);
            const name = document.getElementById(`upsell_${id}_name`);
            if (hidden) {
                hidden.value = '';
            }
            if (name) {
                name.value = '';
            }
        }
    });
});
