@extends('admin.layout')

@push('head')
<link rel="stylesheet" href="https://unpkg.com/trix@2.0.8/dist/trix.css">
@endpush

@section('title', $product ? 'Modifier le produit' : 'Nouveau produit')
@section('header', $product ? 'Modifier le produit' : 'Nouveau produit')

@section('content')
<form method="POST" action="{{ $product ? route('admin.ecommerce.products.update', $product) : route('admin.ecommerce.products.store') }}" enctype="multipart/form-data" class="max-w-2xl space-y-6">
    @csrf
    @if($product) @method('PATCH') @endif

    <div>
        <label class="block text-sm font-medium text-slate-700 mb-1">Nom *</label>
        <input type="text" name="name" value="{{ old('name', $product?->name) }}" required class="w-full px-4 py-2 border border-slate-200 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
        @error('name')<p class="mt-1 text-sm text-red-500">{{ $message }}</p>@enderror
    </div>

    <div>
        <label class="block text-sm font-medium text-slate-700 mb-1">Description</label>
        @php $descId = 'product-description-input'; @endphp
        <input type="hidden" name="description" id="{{ $descId }}" value="{{ old('description', $product?->description) }}">
        <trix-editor input="{{ $descId }}" placeholder="Détail du produit (gras, listes, liens…)" class="trix-content w-full min-h-[200px] px-4 py-2.5 border border-slate-200 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-primary-500 prose prose-sm max-w-none"></trix-editor>
        @error('description')<p class="mt-1 text-sm text-red-500">{{ $message }}</p>@enderror
    </div>

    <div class="grid md:grid-cols-2 gap-6">
        <div>
            <label class="block text-sm font-medium text-slate-700 mb-1">Prix (€) *</label>
            <input type="number" name="price" step="0.01" min="0" value="{{ old('price', $product?->price) }}" required class="w-full px-4 py-2 border border-slate-200 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
            @error('price')<p class="mt-1 text-sm text-red-500">{{ $message }}</p>@enderror
        </div>
        <div>
            <label class="block text-sm font-medium text-slate-700 mb-1">Catégorie</label>
            <select name="product_category_id" class="w-full px-4 py-2 border border-slate-200 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                <option value="">— Aucune —</option>
                @foreach($categories as $c)
                    <option value="{{ $c->id }}" {{ old('product_category_id', $product?->product_category_id) == $c->id ? 'selected' : '' }}>{{ $c->name }}</option>
                    @foreach($c->children as $sub)
                    <option value="{{ $sub->id }}" {{ old('product_category_id', $product?->product_category_id) == $sub->id ? 'selected' : '' }}>↳ {{ $sub->name }}</option>
                    @endforeach
                @endforeach
            </select>
        </div>
    </div>

    <div class="grid md:grid-cols-2 gap-6">
        <div>
            <label class="block text-sm font-medium text-slate-700 mb-1">Stock (vide = illimité)</label>
            <input type="number" name="stock" min="0" value="{{ old('stock', $product?->stock) }}" placeholder="Illimité" class="w-full px-4 py-2 border border-slate-200 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
        </div>
        <div>
            <label class="block text-sm font-medium text-slate-700 mb-1">Poids (kg) - pour livraison</label>
            <input type="number" name="weight_kg" step="0.001" min="0" value="{{ old('weight_kg', $product?->weight_kg) }}" placeholder="0.5" class="w-full px-4 py-2 border border-slate-200 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
        </div>
        <div class="md:col-span-2">
            <label class="block text-sm font-medium text-slate-700 mb-1">Image principale</label>
            @if($product?->image)
            <div class="mb-2 flex items-center gap-3">
                <img src="{{ asset('storage/'.$product->image) }}" alt="" class="h-20 w-20 object-cover rounded-lg border border-slate-200">
                <span class="text-sm text-slate-500">Image actuelle</span>
            </div>
            @endif
            <x-admin.file-upload-button name="image_file" accept="image/*" />
            <p class="text-xs text-slate-500 mt-1">JPG, PNG ou WebP. Réduction automatique avant envoi puis optimisation sur le serveur.</p>
        </div>
        <div class="md:col-span-2">
            <label class="block text-sm font-medium text-slate-700 mb-2">Photos supplémentaires (galerie)</label>
            @if($product)
                @foreach($product->galleryPaths() as $gp)
                <div class="flex items-center gap-3 mb-2 p-2 rounded-xl border border-slate-100 bg-slate-50">
                    <img src="{{ asset('storage/'.$gp) }}" alt="" class="h-16 w-16 object-cover rounded-lg border border-slate-200 shrink-0">
                    <label class="flex items-center gap-2 text-sm text-slate-600 cursor-pointer">
                        <input type="checkbox" name="remove_gallery[]" value="{{ $gp }}" class="rounded border-slate-300 text-primary-500">
                        Retirer cette photo
                    </label>
                </div>
                @endforeach
            @endif
            <input type="file" name="gallery_files[]" accept="image/*" multiple class="block w-full text-sm text-slate-600 file:mr-4 file:py-2 file:px-4 file:rounded-xl file:border-0 file:text-sm file:font-medium file:bg-primary-50 file:text-primary-700 hover:file:bg-primary-100" data-admin-compress-image>
            <p class="text-xs text-slate-500 mt-1">Plusieurs fichiers possibles. Les images lourdes sont réduites dans le navigateur avant envoi (évite « Request entity too large »).</p>
            @error('gallery_files.*')<p class="mt-1 text-sm text-red-500">{{ $message }}</p>@enderror
        </div>
    </div>

    <div class="flex items-center gap-4">
        <label class="flex items-center gap-2 cursor-pointer">
            <input type="hidden" name="is_active" value="0">
            <input type="checkbox" name="is_active" value="1" {{ old('is_active', $product?->is_active ?? true) ? 'checked' : '' }} class="rounded border-slate-300 text-primary-500 focus:ring-primary-500">
            <span class="text-sm font-medium text-slate-700">Produit actif (visible en boutique)</span>
        </label>
    </div>

    <div>
        <label class="block text-sm font-medium text-slate-700 mb-1">Ordre d'affichage</label>
        <input type="number" name="order" min="0" value="{{ old('order', $product?->order ?? 0) }}" class="w-24 px-4 py-2 border border-slate-200 rounded-xl">
    </div>

    <div class="flex gap-3">
        <button type="submit" class="px-6 py-2.5 bg-primary-500 text-white rounded-xl font-medium hover:bg-primary-600 transition">{{ $product ? 'Enregistrer' : 'Créer' }}</button>
        <a href="{{ route('admin.ecommerce.products') }}" class="px-6 py-2.5 border border-slate-200 rounded-xl font-medium hover:bg-slate-50 transition text-slate-700">Annuler</a>
    </div>
</form>
@endsection

@push('scripts')
<script src="https://unpkg.com/trix@2.0.8/dist/trix.umd.min.js"></script>
@endpush
