@extends('layouts.admin', ['heading' => 'Homepage CMS - Hero Slider'])

@section('content')
    @include('admin.cms._tabs')

    @if(session('status'))
        <div class="mb-4 rounded bg-emerald-100 p-3 text-emerald-800">{{ session('status') }}</div>
    @endif
    @if($errors->any())
        <div class="mb-4 rounded bg-rose-100 p-3 text-rose-800">
            <ul class="list-disc pl-5 text-sm">
                @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    <div class="mb-6 rounded bg-white p-4 shadow">
        <h3 class="mb-3 font-semibold">Add Hero Slide</h3>
        <form method="POST" action="{{ route('admin.cms.hero-slides.store') }}" enctype="multipart/form-data" class="grid grid-cols-1 gap-3 md:grid-cols-2">
            @csrf
            <input name="title" placeholder="Title" class="rounded border p-2 text-sm" required>
            <input name="subtitle" placeholder="Subtitle" class="rounded border p-2 text-sm">
            <input name="button_text" placeholder="Button Text" class="rounded border p-2 text-sm">
            <input name="button_link" placeholder="Button Link" class="rounded border p-2 text-sm">
            <input name="animation_type" value="fade_up" placeholder="Animation Type" class="rounded border p-2 text-sm">
            <input name="sort_order" type="number" min="0" placeholder="Sort Order" class="rounded border p-2 text-sm">
            <input name="background_image" type="file" class="rounded border p-2 text-sm md:col-span-2">
            <label class="flex items-center gap-2 text-sm md:col-span-2">
                <input type="checkbox" name="is_active" value="1" checked> Active
            </label>
            <button class="rounded bg-emerald-600 px-4 py-2 text-sm text-white md:col-span-2">Add Slide</button>
        </form>
    </div>

    <div class="overflow-x-auto rounded bg-white shadow">
        <table class="min-w-full text-sm">
            <thead class="bg-slate-100 text-left">
                <tr>
                    <th class="p-3">Slide</th>
                    <th class="p-3">Animation</th>
                    <th class="p-3">Status</th>
                    <th class="p-3">Actions</th>
                </tr>
            </thead>
            <tbody>
                @forelse($slides as $slide)
                    <tr class="border-t align-top">
                        <td class="p-3">
                            <div class="font-semibold">{{ $slide->title }}</div>
                            <div class="text-xs text-slate-500">{{ $slide->subtitle }}</div>
                            @if($slide->background_image_path)
                                <img src="{{ asset('storage/'.$slide->background_image_path) }}" alt="{{ $slide->title }}" class="mt-2 h-20 w-36 rounded object-cover">
                            @endif
                        </td>
                        <td class="p-3 text-xs">{{ $slide->animation_type }}</td>
                        <td class="p-3 text-xs">
                            Active: {{ $slide->is_active ? 'Yes' : 'No' }}<br>
                            Sort: {{ $slide->sort_order }}
                        </td>
                        <td class="p-3">
                            <form method="POST" action="{{ route('admin.cms.hero-slides.update', $slide->id) }}" enctype="multipart/form-data" class="grid grid-cols-1 gap-2">
                                @csrf
                                <input name="title" value="{{ $slide->title }}" class="rounded border p-1 text-xs" required>
                                <input name="subtitle" value="{{ $slide->subtitle }}" class="rounded border p-1 text-xs">
                                <input name="button_text" value="{{ $slide->button_text }}" class="rounded border p-1 text-xs">
                                <input name="button_link" value="{{ $slide->button_link }}" class="rounded border p-1 text-xs">
                                <input name="animation_type" value="{{ $slide->animation_type }}" class="rounded border p-1 text-xs">
                                <input name="sort_order" type="number" min="0" value="{{ $slide->sort_order }}" class="rounded border p-1 text-xs">
                                <input name="background_image" type="file" class="rounded border p-1 text-xs">
                                <label class="text-xs"><input type="checkbox" name="is_active" value="1" {{ $slide->is_active ? 'checked' : '' }}> Active</label>
                                <div class="flex gap-2">
                                    <button class="rounded bg-slate-900 px-2 py-1 text-xs text-white">Update</button>
                                    <button formaction="{{ route('admin.cms.hero-slides.delete', $slide->id) }}" class="rounded bg-rose-600 px-2 py-1 text-xs text-white">Delete</button>
                                </div>
                            </form>
                        </td>
                    </tr>
                @empty
                    <tr><td colspan="4" class="p-4 text-center text-slate-500">No hero slides found.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
@endsection
