function openDetailProductModal(name, reference, category, supplier, unit, stockQty, status, threshold) {
    const modal = document.getElementById('modal-detail-product');
    if (!modal) return;
    document.getElementById('detail_product_name').textContent = name || '-';
    document.getElementById('detail_product_reference').textContent = reference || '-';
    document.getElementById('detail_product_category').textContent = category || '-';
    document.getElementById('detail_product_supplier').textContent = supplier || '-';
    document.getElementById('detail_product_unit').textContent = unit || '-';
    document.getElementById('detail_product_stock').textContent = stockQty ?? '-';
    document.getElementById('detail_product_status').textContent = status === 'actif' ? 'Actif' : 'Archivé';
    document.getElementById('detail_product_threshold').textContent = threshold ?? '-';
    modal.classList.remove('hidden');
    modal.classList.add('flex', 'items-center', 'justify-center');
    document.body.style.overflow = 'hidden';
}

function openEditProductModal(id, name, categoryId, supplierId, unitId, status, stockQty, threshold) {
    const modal = document.getElementById('modal-edit-product');
    const form = document.getElementById('edit-product-form');
    if (!modal || !form) return;
    form.action = '{{ url("/product-stocks") }}/' + id;
    document.getElementById('edit_product_name').value = name || '';
    document.getElementById('edit_product_category').value = categoryId || '';
    document.getElementById('edit_product_supplier').value = supplierId || '';
    document.getElementById('edit_product_unit').value = unitId || '';
    document.getElementById('edit_product_status').value = status || 'actif';
    var stockEl = document.getElementById('edit_product_stock');
    var thresholdEl = document.getElementById('edit_product_threshold');
    if (stockEl) stockEl.value = stockQty ? ? 0;
    if (thresholdEl) thresholdEl.value = threshold ? ? 10;
    modal.classList.remove('hidden');
    modal.classList.add('flex', 'items-center', 'justify-center');
    document.body.style.overflow = 'hidden';
}

function openDeleteProductModal(productId) {
    openDeleteConfirmModal('delete-product-modal', function() {
        const form = document.getElementById('delete-form-product-' + productId);
        if (form) form.submit();
    });
}