@extends('layouts.app')

@section('title', 'System Settings')

@section('content')
<div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8">
    <div class="mb-6 flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4">
        <h1 class="text-3xl font-bold text-gray-800">System Settings</h1>
        <a href="{{ route('admin.dashboard') }}" class="bg-gray-600 hover:bg-gray-700 text-white px-4 py-2 rounded">
            <i class="fas fa-arrow-left mr-2"></i>Back to Dashboard
        </a>
    </div>

    <form method="POST" action="{{ route('admin.settings.update') }}" class="bg-white rounded-lg shadow p-6">
        @csrf
        @method('PUT')

        <div class="mb-6">
            <h2 class="text-xl font-bold mb-4">Patient Features</h2>
            
            <div class="flex items-center justify-between p-4 border rounded">
                <div>
                    <label class="block text-gray-700 text-sm font-bold mb-2">Patient Registration</label>
                    <p class="text-sm text-gray-600">Allow patients to register and create accounts</p>
                </div>
                <label class="relative inline-flex items-center cursor-pointer">
                    <input type="checkbox" name="patient_registration_enabled" value="1" 
                           {{ $settings['patient_registration_enabled'] ? 'checked' : '' }}
                           class="sr-only peer">
                    <div class="w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-blue-300 rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-blue-600"></div>
                </label>
            </div>
        </div>

        <div class="flex justify-end">
            <button type="submit" class="bg-[#ec4899] hover:bg-[#db2777] text-white px-6 py-2 rounded">
                Save Settings
            </button>
        </div>
    </form>

    <!-- Carousel Images Management -->
    <div class="bg-white rounded-lg shadow p-6 mt-6">
        <h2 class="text-xl font-bold mb-4">Carousel Images</h2>
        <p class="text-sm text-gray-600 mb-4">Upload images to display in the top banner carousel. Images will rotate automatically.</p>
        
        <!-- Upload Form -->
        <form method="POST" action="{{ route('admin.settings.carousel.upload') }}" enctype="multipart/form-data" class="mb-6">
            @csrf
            <div class="flex flex-col sm:flex-row gap-4 items-end">
                <div class="flex-1">
                    <label class="block text-gray-700 text-sm font-bold mb-2">Upload New Image</label>
                    <input type="file" name="carousel_image" accept="image/jpeg,image/jpg,image/png,image/gif" required
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-[#ec4899]">
                    <p class="text-xs text-gray-500 mt-1">Max size: 5MB. Supported formats: JPEG, PNG, GIF</p>
                </div>
                <button type="submit" class="bg-[#ec4899] hover:bg-[#db2777] text-white px-6 py-2 rounded whitespace-nowrap">
                    <i class="fas fa-upload mr-2"></i>Upload Image
                </button>
            </div>
        </form>

        <!-- Current Carousel Images -->
        @if(count($carouselImages) > 0)
        <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
            @foreach($carouselImages as $index => $imagePath)
            <div class="border rounded-lg overflow-hidden relative group">
                <div class="aspect-video bg-gray-100 flex items-center justify-center overflow-hidden">
                    @php
                        // Check if it's a storage path (carousel/filename.jpg)
                        if (strpos($imagePath, 'carousel/') !== false) {
                            $imageUrl = asset('storage/' . $imagePath);
                        } else {
                            // It's a public path (banner 1.jpeg, etc.)
                            $imageUrl = asset($imagePath);
                        }
                    @endphp
                    <img src="{{ $imageUrl }}" alt="Carousel Image {{ $index + 1 }}" 
                         class="w-full h-full object-cover">
                </div>
                <div class="p-3 bg-gray-50">
                    <p class="text-xs text-gray-600 truncate mb-2">{{ basename($imagePath) }}</p>
                    <form method="POST" action="{{ route('admin.settings.carousel.delete', $index) }}" 
                          onsubmit="return confirm('Are you sure you want to delete this carousel image?');">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="w-full bg-red-600 hover:bg-red-700 text-white px-3 py-1 rounded text-sm">
                            <i class="fas fa-trash mr-1"></i>Delete
                        </button>
                    </form>
                </div>
            </div>
            @endforeach
        </div>
        @else
        <div class="text-center py-8 text-gray-500">
            <i class="fas fa-images text-4xl mb-2"></i>
            <p>No carousel images uploaded yet.</p>
            <p class="text-sm mt-1">Upload your first image above to get started.</p>
        </div>
        @endif
    </div>
</div>
@endsection

