@extends('layouts.admin.app')

@section('content')
<div class="py-12">
    <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
        @if(session('success'))
        <div id="alert-success" class="flex items-center p-4 mb-4 text-green-800 rounded-lg bg-green-50" role="alert">
            <i class="fas fa-check-circle flex-shrink-0 w-4 h-4"></i>
            <div class="ms-3 text-sm font-medium">
                {{ session('success') }}
            </div>
            <button type="button" class="ms-auto -mx-1.5 -my-1.5 bg-green-50 text-green-500 rounded-lg focus:ring-2 focus:ring-green-400 p-1.5 hover:bg-green-200 inline-flex items-center justify-center h-8 w-8" data-dismiss-target="#alert-success" aria-label="Close">
                <span class="sr-only">Close</span>
                <i class="fas fa-times w-3 h-3"></i>
            </button>
        </div>
        @endif

        <!-- Add New Image Form -->
        <div class="bg-white dark:bg-gray-800 shadow-md rounded-lg mb-6 overflow-hidden">
            <div class="p-6">
                <h3 class="text-xl font-semibold text-gray-900 dark:text-white mb-4">
                    <i class="fas fa-plus-circle mr-2"></i>Add New Carousel Image
                </h3>
                <form action="{{ route('admin.carousel.store') }}" method="POST" enctype="multipart/form-data" class="space-y-4">
                    @csrf
                    <div>
                        <label class="block mb-2 text-sm font-medium text-gray-900 dark:text-white" for="image">
                            Upload Image
                        </label>
                        <input type="file" name="image" id="image" accept="image/*" required
                            class="block w-full text-sm text-gray-900 border border-gray-300 rounded-lg cursor-pointer bg-gray-50 dark:text-gray-400 focus:outline-none dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400">
                        <p class="mt-1 text-sm text-gray-500 dark:text-gray-300">PNG, JPG or JPEG (MAX. 2MB)</p>
                    </div>
                    <button type="submit" class="text-red bg-maroon hover:bg-red-800 focus:ring-4 focus:ring-red-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center">
                        <i class="fas fa-plus mr-2"></i>Add Image
                    </button>
                </form>
            </div>
        </div>

        <!-- Existing Images -->
        <div class="bg-white dark:bg-gray-800 shadow-md rounded-lg">
            <div class="p-6">
                <h3 class="text-xl font-semibold text-gray-900 dark:text-white mb-4">
                    <i class="fas fa-images mr-2"></i>Manage Carousel Images
                </h3>
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                    @foreach($images as $image)
                    <div class="bg-white dark:bg-gray-700 rounded-lg shadow-md">
                        <img src="{{ asset('storage/' . $image->image) }}" alt="Carousel Image" class="w-full h-48 object-cover rounded-t-lg">
                        <div class="p-4">
                            <div class="flex items-center justify-between mb-4">
                                <form action="{{ route('admin.carousel.update', $image) }}" method="POST" class="inline">
                                    @csrf
                                    @method('PUT')
                                    <input type="hidden" name="is_active" value="{{ $image->is_active ? '0' : '1' }}">
                                    <button type="submit" class="px-3 py-1 text-sm font-medium rounded-full {{ $image->is_active ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-700' }}">
                                        <i class="fas {{ $image->is_active ? 'fa-eye' : 'fa-eye-slash' }} mr-1"></i>
                                        {{ $image->is_active ? 'Active' : 'Inactive' }}
                                    </button>
                                </form>
                            </div>
                            <div class="flex items-center justify-between">
                                <!-- Edit Button -->
                                <button data-modal-target="editModal-{{ $image->id }}" data-modal-toggle="editModal-{{ $image->id }}" class="text-blue-700 hover:text-white border border-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-3 py-2 text-center">
                                    <i class="fas fa-edit mr-1"></i>Edit
                                </button>
                                <!-- Delete Button -->
                                <form action="{{ route('admin.carousel.destroy', $image) }}" method="POST" class="inline" onsubmit="return confirm('Are you sure you want to delete this image?');">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="text-red-700 hover:text-white border border-red-700 hover:bg-red-800 focus:ring-4 focus:outline-none focus:ring-red-300 font-medium rounded-lg text-sm px-3 py-2 text-center">
                                        <i class="fas fa-trash-alt mr-1"></i>Delete
                                    </button>
                                </form>
                            </div>
                        </div>
                    </div>

                    <!-- Edit Modal -->
                    <div id="editModal-{{ $image->id }}" tabindex="-1" aria-hidden="true" class="hidden overflow-y-auto overflow-x-hidden fixed top-0 right-0 left-0 z-50 justify-center items-center w-full md:inset-0 h-[calc(100%-1rem)] max-h-full">
                        <div class="relative p-4 w-full max-w-md max-h-full">
                            <!-- Modal content -->
                            <div class="relative bg-white rounded-lg shadow dark:bg-gray-700">
                                <!-- Modal header -->
                                <div class="flex items-center justify-between p-4 md:p-5 border-b rounded-t dark:border-gray-600">
                                    <h3 class="text-lg font-semibold text-gray-900 dark:text-white">
                                        Edit Carousel Image
                                    </h3>
                                    <button type="button" class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm w-8 h-8 ms-auto inline-flex justify-center items-center dark:hover:bg-gray-600 dark:hover:text-white" data-modal-toggle="editModal-{{ $image->id }}">
                                        <i class="fas fa-times"></i>
                                        <span class="sr-only">Close modal</span>
                                    </button>
                                </div>
                                <!-- Modal body -->
                                <form action="{{ route('admin.carousel.update', $image) }}" method="POST" enctype="multipart/form-data" class="p-4 md:p-5">
                                    @csrf
                                    @method('PUT')
                                    <div class="grid gap-4 mb-4">
                                        <div>
                                            <label class="block mb-2 text-sm font-medium text-gray-900 dark:text-white" for="edit-image-{{ $image->id }}">
                                                Upload New Image (Optional)
                                            </label>
                                            <input type="file" name="image" id="edit-image-{{ $image->id }}" accept="image/*"
                                                class="block w-full text-sm text-gray-900 border border-gray-300 rounded-lg cursor-pointer bg-gray-50 dark:text-gray-400 focus:outline-none dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400">
                                        </div>
                                    </div>
                                    <button type="submit" class="text-white bg-maroon hover:bg-red-800 focus:ring-4 focus:outline-none focus:ring-red-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center">
                                        Save Changes
                                    </button>
                                </form>
                            </div>
                        </div>
                    </div>
                    @endforeach
                </div>
            </div>
        </div>
    </div>
</div>

@push('scripts')
<script>
    // Auto-hide alerts after 3 seconds
    document.addEventListener('DOMContentLoaded', function() {
        setTimeout(function() {
            const alerts = document.querySelectorAll('[role="alert"]');
            alerts.forEach(alert => {
                alert.style.display = 'none';
            });
        }, 3000);
    });
</script>
@endpush
@endsection 