@extends('admin.layout')

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

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

@section('content')
<nav class="mb-6">
    <a href="{{ route('admin.reservation.services') }}" class="text-slate-500 hover:text-slate-900 text-sm">← Retour aux services</a>
</nav>

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

    @if($errors->any())
    <div class="p-4 bg-red-50 border border-red-200 text-red-800 rounded-xl">
        <p class="font-medium">Veuillez corriger les erreurs suivantes :</p>
        <ul class="list-disc list-inside mt-1 text-sm">
            @foreach($errors->all() as $e)<li>{{ $e }}</li>@endforeach
        </ul>
    </div>
    @endif

    <div class="bg-white rounded-2xl border border-slate-200 p-6 space-y-6">
        <div>
            <label class="block text-sm font-medium text-slate-700 mb-1">Nom du service *</label>
            <input type="text" name="name" value="{{ old('name', $service?->name) }}" required placeholder="Ex: Atelier Bijoux, Consultation..." class="w-full px-4 py-2.5 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>
            <input type="hidden" name="description" id="description" value="{{ old('description', $service?->description) }}">
            <trix-editor input="description" placeholder="Décrivez le service proposé (gras, italique, listes...)" 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>
            <label class="block text-sm font-medium text-slate-700 mb-1">Image</label>
            @if($service?->image_url)
            <div class="mb-3 flex items-center gap-4">
                <img src="{{ $service->image_url }}" alt="" class="w-24 h-24 object-cover rounded-xl border border-slate-200">
                <p class="text-sm text-slate-500">Cliquez sur l'icône pour remplacer</p>
            </div>
            @endif
            <x-admin.file-upload-button name="image" accept="image/*" />
            @error('image')<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">Durée (minutes) *</label>
                <input type="number" name="duration_minutes" value="{{ old('duration_minutes', $service?->duration_minutes ?? 60) }}" min="15" max="480" required class="w-full px-4 py-2.5 border border-slate-200 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                <p class="mt-1 text-xs text-slate-500">Entre 15 et 480 min (8h)</p>
                @error('duration_minutes')<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">Prix (€)</label>
                <input type="number" name="price" step="0.01" min="0" value="{{ old('price', $service?->price) }}" placeholder="Gratuit si vide" class="w-full px-4 py-2.5 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>

        <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', $service?->order ?? 0) }}" class="w-24 px-4 py-2.5 border border-slate-200 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
            <p class="mt-1 text-xs text-slate-500">0 = premier, 1 = deuxième...</p>
        </div>

        @if(!empty($multilang['locales']) && $service)
        <div class="pt-4 border-t border-slate-200">
            <h3 class="text-sm font-medium text-slate-700 mb-3">Traductions</h3>
            <p class="text-xs text-slate-500 mb-3">Le nom et la description ci-dessus sont en langue par défaut.</p>
            <div class="space-y-3">
                @foreach($multilang['locales'] as $loc)
                <details class="border border-slate-200 rounded-xl">
                    <summary class="cursor-pointer px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50 rounded-xl">{{ $multilang['labels'][$loc] ?? strtoupper($loc) }}</summary>
                    <div class="p-4 space-y-3 border-t border-slate-100">
                        <div>
                            <label class="block text-xs text-slate-600 mb-1">Nom</label>
                            <input type="text" name="translations[{{ $loc }}][name]" value="{{ $service->translations[$loc]['name'] ?? '' }}" placeholder="Nom en {{ $multilang['labels'][$loc] ?? $loc }}" class="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm">
                        </div>
                        <div>
                            <label class="block text-xs text-slate-600 mb-1">Description</label>
                            <textarea name="translations[{{ $loc }}][description]" rows="3" placeholder="Description en {{ $multilang['labels'][$loc] ?? $loc }}" class="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm">{{ $service->translations[$loc]['description'] ?? '' }}</textarea>
                        </div>
                    </div>
                </details>
                @endforeach
            </div>
        </div>
        @endif
    </div>

    <div class="flex gap-3 items-center">
        <button type="submit" class="px-6 py-2.5 bg-primary-500 text-white rounded-xl font-medium hover:bg-primary-600 transition">{{ $service ? 'Enregistrer' : 'Créer le service' }}</button>
        <a href="{{ route('admin.reservation.services') }}" 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>

@if($service)
<div class="mt-8 pt-6 border-t border-slate-200">
    <p class="text-sm text-slate-500 mb-2">Zone dangereuse</p>
    <form method="POST" action="{{ route('admin.reservation.services.destroy', $service) }}" class="inline" onsubmit="return confirm('Supprimer ce service ? Les créneaux et réservations associés seront perdus.')">
        @csrf
        @method('DELETE')
        <button type="submit" class="px-6 py-2.5 bg-red-50 text-red-600 rounded-xl font-medium hover:bg-red-100 transition">Supprimer le service</button>
    </form>
</div>
@endif

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