@extends('layouts.admin')

@section('content')
<div class="mb-8 flex items-center">
    <a href="{{ route('admin.gallery.index') }}" class="text-teal-600 hover:text-teal-900 mr-4">
        &larr; Back to Gallery
    </a>
    <h1 class="text-2xl font-bold text-gray-900">Add New Gallery Item</h1>
</div>

<div class="bg-white shadow px-4 py-5 sm:rounded-lg sm:p-6">
    <form action="{{ route('admin.gallery.store') }}" method="POST" enctype="multipart/form-data" x-data="{ type: 'image' }">
        @csrf
        <div class="grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-6">
            
            <div class="sm:col-span-4">
                <label for="title" class="block text-sm font-medium text-gray-700">Title / Caption</label>
                <div class="mt-1">
                    <input type="text" name="title" id="title" class="shadow-sm focus:ring-teal-500 focus:border-teal-500 block w-full sm:text-sm border-gray-300 rounded-md py-2 px-3 border" value="{{ old('title') }}">
                </div>
            </div>

            <div class="sm:col-span-4">
                <label for="type" class="block text-sm font-medium text-gray-700">Media Type</label>
                <div class="mt-1">
                    <select id="type" name="type" x-model="type" class="shadow-sm focus:ring-teal-500 focus:border-teal-500 block w-full sm:text-sm border-gray-300 rounded-md py-2 px-3 border">
                        <option value="image">Image Upload</option>
                        <option value="youtube">YouTube Embed URL</option>
                        <option value="tiktok">TikTok Embed URL</option>
                    </select>
                </div>
            </div>

            <div class="sm:col-span-6" x-show="type === 'image'">
                <label for="image_file" class="block text-sm font-medium text-gray-700">Image File</label>
                <div class="mt-1">
                    <input type="file" name="image_file" id="image_file" class="shadow-sm focus:ring-teal-500 focus:border-teal-500 block w-full sm:text-sm border-gray-300 rounded-md py-2 px-3 border" accept="image/*">
                </div>
                <p class="mt-2 text-sm text-gray-500">Max size 5MB.</p>
            </div>

            <div class="sm:col-span-6" x-show="type !== 'image'" style="display: none;">
                <label for="media_url" class="block text-sm font-medium text-gray-700">Embed URL</label>
                <div class="mt-1">
                    <input type="text" name="media_url" id="media_url" class="shadow-sm focus:ring-teal-500 focus:border-teal-500 block w-full sm:text-sm border-gray-300 rounded-md py-2 px-3 border" value="{{ old('media_url') }}">
                </div>
                <p class="mt-2 text-sm text-gray-500">For YouTube, provide the full URL (e.g. https://www.youtube.com/watch?v=...). For TikTok, provide the video URL.</p>
            </div>

            <div class="sm:col-span-6">
                <div class="flex items-start">
                    <div class="flex items-center h-5">
                        <input id="is_active" name="is_active" type="checkbox" checked value="1" class="focus:ring-teal-500 h-4 w-4 text-teal-600 border-gray-300 rounded">
                    </div>
                    <div class="ml-3 text-sm">
                        <label for="is_active" class="font-medium text-gray-700">Active (Visible to public)</label>
                    </div>
                </div>
                <div class="flex items-start mt-4">
                    <div class="flex items-center h-5">
                        <input id="show_on_homepage" name="show_on_homepage" type="checkbox" value="1" class="focus:ring-teal-500 h-4 w-4 text-teal-600 border-gray-300 rounded">
                    </div>
                    <div class="ml-3 text-sm">
                        <label for="show_on_homepage" class="font-medium text-gray-700">Show on Homepage Gallery (Only checked items appear on the homepage grid)</label>
                    </div>
                </div>
            </div>

        </div>

        <div class="pt-5 mt-6 border-t border-gray-200">
            <div class="flex justify-end">
                <a href="{{ route('admin.gallery.index') }}" class="bg-white py-2 px-4 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-teal-500">
                    Cancel
                </a>
                <button type="submit" class="ml-3 inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-teal-600 hover:bg-teal-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-teal-500">
                    Save Item
                </button>
            </div>
        </div>
    </form>
</div>
<!-- Alpine.js for dynamic form fields -->
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js" defer></script>
@endsection
