@login_required
def edit_product(request, pk):
    product = get_object_or_404(Product, pk=pk)
    if request.method == 'POST':
        product.name_en = request.POST.get('name_en')
        product.name_ar = request.POST.get('name_ar')
        product.sku = request.POST.get('sku')
        product.category = get_object_or_404(Category, id=request.POST.get('category'))
        
        unit_id = request.POST.get('unit')
        product.unit = get_object_or_404(Unit, id=unit_id) if unit_id else None
        
        supplier_id = request.POST.get('supplier')
        product.supplier = get_object_or_404(Supplier, id=supplier_id) if supplier_id else None
        
        product.cost_price = request.POST.get('cost_price', 0)
        product.price = request.POST.get('price', 0)
        product.vat = request.POST.get('vat', 0)
        product.description = request.POST.get('description', '')
        product.opening_stock = request.POST.get('opening_stock', 0)
        product.stock_quantity = request.POST.get('stock_quantity', 0)
        product.min_stock_level = request.POST.get('min_stock_level', 0)
        product.is_active = request.POST.get('is_active') == 'on'
        product.has_expiry = request.POST.get('has_expiry') == 'on'
        product.expiry_date = request.POST.get('expiry_date')
        if not product.has_expiry:
            product.expiry_date = None
        
        if 'image' in request.FILES:
            product.image = request.FILES['image']
            
        product.save()
        messages.success(request, _("Product updated successfully!"))
        return redirect(reverse('inventory') + '#items')
    return redirect(reverse('inventory') + '#items')
