@extends('layouts.admin')

@section('title', $item ? "Modifier - {$item['title']}" : 'Ajouter une realisation')

@php
    $breadcrumbs = [['label' => 'Realisations / Galerie', 'route' => 'admin.gallery.index'], ['label' => $item ? $item['title'] : 'Ajouter']];
@endphp

@section('content')

    <form method="POST" action="{{ $item ? route('admin.gallery.update', $item) : route('admin.gallery.store') }}" data-dirty-form novalidate>
        @csrf
        @if ($item)
            @method('PATCH')
        @endif

        <div class="mb-5 flex flex-col gap-1">
            <h1 class="text-xl font-bold text-brand-ink">{{ $item ? 'Modifier la realisation' : 'Ajouter une realisation' }}</h1>
        </div>

        <div class="grid grid-cols-1 gap-6 lg:grid-cols-3">
            <div class="flex flex-col gap-4 lg:col-span-2">
                <div class="admin-card flex flex-col gap-4 p-5">
                    <x-admin.field type="text" name="title" label="Titre" required :value="old('title', $item['title'] ?? '')" :error="$errors->first('title')" />
                    <x-admin.media-picker name="image" label="Image principale" alt-name="image_alt" :value="old('image', $item['media_id'] ?? null)" :preview-url="$item['image'] ?? null" :alt="old('image_alt', $item['image_alt'] ?? '')" help="Ratio 4/3 recommande, coherent avec les autres realisations." />
                    <x-admin.field type="textarea" name="description" label="Description courte" :rows="3" :value="old('description', $item['description'] ?? '')" />
                    <div class="grid grid-cols-1 gap-4 sm:grid-cols-2">
                        <x-admin.field type="text" name="location" label="Localisation" :value="old('location', $item['location'] ?? '')" />
                        <x-admin.field type="text" name="completed_at" label="Date" :value="old('completed_at', $item['completed_at'] ?? '')" placeholder="ex. Juin 2026" />
                    </div>
                </div>
            </div>

            <div class="flex flex-col gap-4">
                <div class="admin-card flex flex-col gap-4 p-5">
                    <x-admin.field type="text" name="category" label="Categorie" :value="old('category', $item['category'] ?? '')" placeholder="ex. Transport" />
                    <x-admin.field type="number" name="sort_order" label="Ordre d'affichage" :value="old('sort_order', $item['sort_order'] ?? 0)" />
                    <x-admin.field type="select" name="status" label="Statut" :options="['published' => 'Publie', 'draft' => 'Brouillon', 'archived' => 'Archive']" :value="old('status', $item['status'] ?? 'draft')" />
                    <x-admin.field type="switch" name="is_active" label="Actif (visible publiquement)" :checked="old('is_active', $item['is_active'] ?? false)" />
                </div>
            </div>
        </div>

        <x-admin.action-bar :preview-href="route('gallery')" :status="$item['status'] ?? 'draft'" />
    </form>

@endsection
