function openDetailProductModal(name, reference, category, supplier, unit, stockQty, status, threshold) {
    const modal = document.getElementById('modal-detail-product');
    if (!modal) return;
    var setText = function(id, val) {
        var el = document.getElementById(id);
        if (el) el.textContent = val;
    };
    setText('detail_product_name', name || '-');
    setText('detail_product_reference', reference || '-');
    setText('detail_product_category', category || '-');
    setText('detail_product_supplier', supplier || '-');
    setText('detail_product_unit', unit || '-');
    setText('detail_product_stock', stockQty ?? '-');
    setText('detail_product_status', status === 'actif' ? 'Actif' : 'Archivé');
    setText('detail_product_threshold', 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();
    });
}