@extends('layouts.admin')

@section('title', $slide->exists ? 'Edit Slide' : 'Add Slide')

@section('content')
<div class="container mx-auto max-w-3xl">
    <div class="flex items-center gap-4 mb-6">
        <a href="{{ route('admin.hero-slides.index') }}" class="w-10 h-10 rounded-full bg-white border border-slate-200 flex items-center justify-center text-slate-500 hover:text-brand-primary hover:border-brand-primary transition shadow-sm">
            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"></path></svg>
        </a>
        <h1 class="text-2xl font-bold text-slate-900">{{ $slide->exists ? 'Edit' : 'Add' }} Slide</h1>
    </div>

    <div class="bg-white shadow-sm rounded-xl border border-slate-100 p-8">
        <form action="{{ $slide->exists ? route('admin.hero-slides.update', $slide) : route('admin.hero-slides.store') }}" method="POST" enctype="multipart/form-data">
            @csrf
            @if($slide->exists)
                @method('PUT')
            @endif

            <div class="space-y-6">
                <!-- Caption -->
                <div>
                    <label class="block text-slate-700 text-sm font-bold mb-2">Caption (Optional)</label>
                    <input class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-lg focus:ring-2 focus:ring-brand-primary focus:border-transparent outline-none transition" 
                           name="caption" type="text" placeholder="Engaging main text..."
                           value="{{ old('caption', $slide->caption) }}">
                </div>

                <!-- Current Image preview -->
                @if($slide->exists && $slide->image_url)
                <div>
                    <label class="block text-slate-700 text-sm font-bold mb-2">Current Slide Image</label>
                    <img src="{{ asset($slide->image_url) }}" alt="" class="w-full h-48 object-cover rounded-xl border border-slate-200 shadow-sm" onerror="this.src='https://placehold.co/800x400/e2e8f0/94a3b8?text=Error'">
                </div>
                @endif

                <!-- Slide Image -->
                <x-media-picker 
                    name="image_url" 
                    label="Slide Image" 
                    :value="$slide->image_url" 
                    :preview="$slide->image_url ? asset($slide->image_url) : null" 
                />
                <div class="p-4 bg-slate-50 rounded-2xl border border-slate-100 mt-4 italic">
                    <p class="text-[11px] text-slate-500 leading-relaxed">
                        <strong>Direct Upload:</strong> You can also upload a slide image directly below. High resolution (1920x1080+) recommended.
                    </p>
                    <input class="mt-2 w-full text-xs text-slate-500 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-xs file:font-bold file:bg-brand-primary/10 file:text-brand-primary hover:file:bg-brand-primary/20 transition cursor-pointer" 
                           name="image" type="file" accept="image/*">
                </div>
                @error('image')
                    <p class="text-red-500 text-xs mt-1">{{ $message }}</p>
                @enderror

                <!-- Sort & Visibility -->
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label class="block text-slate-700 text-sm font-bold mb-2">Sort Order</label>
                        <input class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-lg focus:ring-2 focus:ring-brand-primary focus:border-transparent outline-none transition" 
                               name="sort_order" type="number" min="0" required
                               value="{{ old('sort_order', $slide->sort_order ?? 0) }}">
                    </div>
                    
                    <div class="flex flex-col justify-center pt-2">
                        <label class="flex items-center cursor-pointer group">
                            <div class="relative">
                                <input type="checkbox" name="is_active" class="sr-only" {{ old('is_active', $slide->exists ? $slide->is_active : true) ? 'checked' : '' }}>
                                <div class="block bg-slate-200 w-14 h-8 rounded-full transition-colors group-hover:bg-slate-300"></div>
                                <div class="dot absolute left-1 top-1 bg-white w-6 h-6 rounded-full transition-transform shadow-sm"></div>
                            </div>
                            <div class="ml-3 font-bold text-slate-700">Make Slide Active</div>
                        </label>
                        <style>
                            input:checked ~ .block { background-color: #38bdf8; }
                            input:checked ~ .dot { transform: translateX(100%); }
                        </style>
                    </div>
                </div>
            </div>

            <div class="mt-8 pt-6 border-t border-slate-100 flex items-center justify-end gap-3">
                <a href="{{ route('admin.hero-slides.index') }}" class="px-6 py-3 font-bold text-sm text-slate-500 hover:text-slate-800 hover:bg-slate-100 rounded-lg transition">Cancel</a>
                <button type="submit" class="bg-brand-primary hover:bg-brand-accent text-white font-bold py-3 px-8 rounded-lg shadow-lg shadow-brand-primary/30 transition flex items-center gap-2">
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg>
                    {{ $slide->exists ? 'Update Slide' : 'Save Slide' }}
                </button>
            </div>
        </form>
    </div>
</div>
@endsection
