@extends('layouts.admin')
@section('title', $gallery->exists ? 'Edit Foto' : 'Upload Foto Galeri')
@section('page-title', $gallery->exists ? 'Edit Foto Galeri' : 'Upload Foto Galeri')

@section('content')
<div class="max-w-lg">
    <form method="POST" action="{{ $gallery->exists ? route('admin.galeri.update', $gallery->id) : route('admin.galeri.store') }}" enctype="multipart/form-data" class="card-admin space-y-4">
        @csrf
        @if($gallery->exists) @method('PUT') @endif

        <div>
            <label class="form-label">Kategori *</label>
            <select name="kategori" class="form-select" required>
                <option value="">Pilih Kategori</option>
                @foreach(['kegiatan_desa' => 'Kegiatan Desa', 'masyarakat' => 'Masyarakat', 'alam_lingkungan' => 'Alam & Lingkungan', 'potensi_desa' => 'Potensi Desa', 'pemerintahan' => 'Pemerintahan', 'wisata' => 'Wisata'] as $v => $l)
                <option value="{{ $v }}" {{ old('kategori', $gallery->kategori) === $v ? 'selected' : '' }}>{{ $l }}</option>
                @endforeach
            </select>
        </div>

        <div><label class="form-label">Judul / Keterangan Foto</label><input name="judul" value="{{ old('judul', $gallery->judul) }}" class="form-input" placeholder="Opsional"></div>

        <div>
            <label class="form-label">{{ $gallery->exists ? 'Ganti Foto' : 'Foto *' }} <span class="text-gray-400 text-xs">(bisa pilih banyak saat upload pertama kali)</span></label>
            @if($gallery->exists && $gallery->foto_url)
                <img src="{{ $gallery->foto_url }}" class="w-full h-32 object-cover rounded-lg mb-2">
            @endif
            <input type="file" name="{{ $gallery->exists ? 'foto' : 'foto[]' }}" accept="image/*" class="form-input" {{ $gallery->exists ? '' : 'multiple required' }}>
            <p class="text-xs text-gray-400 mt-1">JPG/PNG/WebP, maks 2MB per foto</p>
        </div>

        @if($errors->any())
            <div class="bg-red-50 border border-red-200 rounded-lg p-3">
                @foreach($errors->all() as $e)<p class="text-red-600 text-sm">{{ $e }}</p>@endforeach
            </div>
        @endif

        <div class="flex gap-3 pt-2">
            <button type="submit" class="btn-primary">💾 {{ $gallery->exists ? 'Perbarui' : 'Upload' }}</button>
            <a href="{{ route('admin.galeri.index') }}" class="btn-secondary">Batal</a>
        </div>
    </form>
</div>
@endsection
