// ============================================ // components/admin/ProductCreateModal.tsx - VERSION AVEC BOUTONS // ============================================ // ✅ Boutons stylisés au lieu de select natif (comme AdminUsers) // ✅ Pas de hover gris natif ! import React, { useState } from 'react'; import { X, Plus, Trash2, Upload, DollarSign } from 'lucide-react'; import { createProductAdmin } from '../api/api_admin'; import type { CreateProductData, ProductPrice} from '../api/api_admin_types'; import './ProductModal.css'; interface ProductCreateModalProps { onClose: () => void; onSuccess: () => void; } const ProductCreateModal: React.FC = ({ onClose, onSuccess }) => { // ============================================ // 📝 STATE // ============================================ const [name, setName] = useState(''); const [category, setCategory] = useState('weed&hash'); const [description, setDescription] = useState(''); const [stock, setStock] = useState(0); const [prices, setPrices] = useState([ { quantity: 1, price: 0 } ]); const [mediaFiles, setMediaFiles] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); // ✅ Options de catégories const categoryOptions = [ { value: 'weed&hash', label: 'Weed & Hash' }, { value: 'zipette&co', label: 'Zipette & Co' }, { value: 'gros&semi', label: 'Gros & Semi' } ]; // ============================================ // 💰 GESTION DES PRIX // ============================================ const addPriceRow = () => { setPrices([...prices, { quantity: 1, price: 0 }]); }; const removePriceRow = (index: number) => { if (prices.length > 1) { setPrices(prices.filter((_, i) => i !== index)); } }; const updatePrice = (index: number, field: 'quantity' | 'price', value: number) => { const newPrices = [...prices]; newPrices[index][field] = value; setPrices(newPrices); }; // ============================================ // 📁 GESTION DES FICHIERS // ============================================ const handleFileSelect = (e: React.ChangeEvent) => { if (e.target.files) { const newFiles = Array.from(e.target.files); setMediaFiles([...mediaFiles, ...newFiles]); console.log(`📁 ${newFiles.length} fichier(s) ajouté(s)`); } }; const removeFile = (index: number) => { setMediaFiles(mediaFiles.filter((_, i) => i !== index)); }; // ============================================ // ✅ VALIDATION // ============================================ const validateForm = (): string | null => { if (!name.trim()) return 'Le nom est requis'; if (!description.trim()) return 'La description est requise'; if (stock < 0) return 'Le stock ne peut pas être négatif'; if (prices.length === 0) return 'Au moins un prix est requis'; for (const price of prices) { if (price.quantity <= 0) return 'Toutes les quantités doivent être positives'; if (price.price <= 0) return 'Tous les prix doivent être positifs'; } return null; }; // ============================================ // 📤 SOUMISSION // ============================================ const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); const validationError = validateForm(); if (validationError) { setError(validationError); return; } try { setLoading(true); setError(null); console.log('📤 [CREATE_MODAL] Envoi du formulaire...'); const productData: CreateProductData = { name: name.trim(), category, description: description.trim(), stock, prices, }; const response = await createProductAdmin(productData, mediaFiles); if (response.success) { console.log('✅ [CREATE_MODAL] Produit créé avec succès'); onSuccess(); } else { setError(response.error || 'Erreur lors de la création du produit'); } } catch (error) { console.error('❌ [CREATE_MODAL] Erreur:', error); setError(error instanceof Error ? error.message : 'Erreur inconnue'); } finally { setLoading(false); } }; // ============================================ // 🎨 RENDER // ============================================ return ( <>
{/* Header */}

Créer un Nouveau Produit

{/* Content */}
{error && (

{error}

)} {/* Informations de base */}

Informations de Base

setName(e.target.value)} placeholder="Ex: OG Kush Premium" required />
{/* ✅ CATÉGORIE AVEC BOUTONS AU LIEU DE SELECT */}
{categoryOptions.map(option => ( ))}