@extends('layouts.app')

@section('title', 'Create Gallery Type')

@section('content')
<x-app-layout>
    <x-module-hero
        title="Create Gallery Type"
        description="Add a new gallery category"
        icon="fa-layer-group">

        <x-slot name="backButton">
            <a href="{{ route('media.gallery-types.index') }}"
               class="inline-flex items-center gap-2 text-[#475569] hover:text-[#1e293b] text-sm transition">
                <i class="fas fa-arrow-left text-xs"></i> Back to Gallery Types
            </a>
        </x-slot>
    </x-module-hero>

    <div class="p-6 max-w-2xl">
        {{-- Error Summary --}}
        @if ($errors->any())
        <div class="mb-6 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg">
            <p class="font-semibold flex items-center gap-2">
                <i class="fas fa-exclamation-circle"></i> Please fix the errors below:
            </p>
            <ul class="list-disc list-inside text-sm mt-2">
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
        @endif

        {{-- Create Form --}}
        <form action="{{ route('media.gallery-types.store') }}" method="POST"
              class="bg-white rounded-lg shadow-sm border border-gray-200 p-8">
            @csrf

            {{-- Name --}}
            <div class="mb-6">
                <label class="block text-sm font-medium text-gray-700 mb-2">
                    Gallery Type Name <span class="text-red-500">*</span>
                </label>
                <input type="text" name="name"
                       value="{{ old('name') }}"
                       placeholder="e.g., Events, Team, Achievements"
                       class="w-full px-4 py-2 border {{ $errors->has('name') ? 'border-red-500' : 'border-gray-300' }} rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                       required>
                @error('name')
                <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                @enderror
            </div>

            {{-- Description --}}
            <div class="mb-6">
                <label class="block text-sm font-medium text-gray-700 mb-2">
                    Description <span class="text-gray-400 text-xs">(optional)</span>
                </label>
                <textarea name="description" rows="4"
                          placeholder="Describe the purpose of this gallery type..."
                          class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">{{ old('description') }}</textarea>
                @error('description')
                <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                @enderror
            </div>

            {{-- Color Picker --}}
            <div class="mb-6">
                <label class="block text-sm font-medium text-gray-700 mb-2">
                    Color <span class="text-gray-400 text-xs">(optional)</span>
                </label>
                <div class="flex gap-4 items-center">
                    <input type="color" name="color"
                           value="{{ old('color', '#3b82f6') }}"
                           class="h-12 w-20 border border-gray-300 rounded-lg cursor-pointer">
                    <input type="text" name="color_text" placeholder="#3b82f6"
                           value="{{ old('color', '#3b82f6') }}"
                           class="flex-1 px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm"
                           id="colorText">
                </div>
                @error('color')
                <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                @enderror
            </div>

            {{-- Icon --}}
            <div class="mb-6">
                <label class="block text-sm font-medium text-gray-700 mb-2">
                    Icon <span class="text-gray-400 text-xs">(Font Awesome class, optional)</span>
                </label>
                <div class="flex gap-2">
                    <input type="text" name="icon"
                           value="{{ old('icon', 'fa-images') }}"
                           placeholder="e.g., fa-images, fa-users, fa-trophy"
                           class="flex-1 px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                           id="iconInput">
                    <div class="px-4 py-2 border border-gray-300 rounded-lg bg-gray-50 flex items-center">
                        <i class="fas {{ old('icon', 'fa-images') }} text-xl text-gray-600" id="iconPreview"></i>
                    </div>
                </div>
                <p class="mt-2 text-xs text-gray-500">
                    <a href="https://fontawesome.com/icons" target="_blank" class="text-blue-600 hover:text-blue-700">
                        Browse Font Awesome icons →
                    </a>
                </p>
                @error('icon')
                <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                @enderror
            </div>

            {{-- Active Status --}}
            <div class="mb-6">
                <label class="flex items-center gap-3">
                    <input type="checkbox" name="is_active" value="1"
                           {{ old('is_active', true) ? 'checked' : '' }}
                           class="w-5 h-5 text-blue-600 rounded focus:ring-2 focus:ring-blue-500 cursor-pointer">
                    <span class="text-sm font-medium text-gray-700">Activate this gallery type</span>
                </label>
            </div>

            {{-- Form Actions --}}
            <div class="flex gap-3 pt-6 border-t border-gray-200">
                <button type="submit"
                    class="px-6 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition font-medium">
                    <i class="fas fa-save"></i> Create Gallery Type
                </button>
                <a href="{{ route('media.gallery-types.index') }}"
                   class="px-6 py-2 bg-gray-300 text-gray-700 rounded-lg hover:bg-gray-400 transition font-medium">
                    <i class="fas fa-times"></i> Cancel
                </a>
            </div>
        </form>
    </div>
</x-app-layout>

<script>
    // Update color text input when color picker changes
    document.querySelector('input[type="color"]').addEventListener('change', function(e) {
        document.getElementById('colorText').value = e.target.value;
    });

    // Update icon preview when input changes
    document.getElementById('iconInput').addEventListener('input', function(e) {
        const preview = document.getElementById('iconPreview');
        preview.className = 'fas ' + e.target.value + ' text-xl text-gray-600';
    });
</script>
@endsection
