@extends('admin.layouts.app')

@section('title', 'User Interface - All Sections')

@section('content')
<div class="max-w-6xl mx-auto space-y-5">
    <div>
        <h1 class="text-2xl font-bold text-gray-800">User Interface: All Sections</h1>
        <p class="text-sm text-gray-500 mt-1">Each section shows where it appears and where to edit/replace content from admin.</p>
    </div>

    <div class="bg-white rounded-xl border border-gray-200 p-4">
        <h2 class="text-sm font-semibold text-gray-700 mb-2">All Page List (Quick Open)</h2>
        <select id="page-jump" class="w-full md:w-[420px] rounded border-gray-300 text-sm">
            <option value="">Select a CMS page to open...</option>
            @foreach(($pages ?? []) as $page)
                <option value="{{ $page['url'] }}">{{ $page['label'] }}</option>
            @endforeach
        </select>
    </div>

    <div class="bg-white rounded-xl border border-gray-200 overflow-hidden">
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-100">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-5 py-3 text-left text-xs font-semibold text-gray-500 uppercase">Section</th>
                        <th class="px-5 py-3 text-left text-xs font-semibold text-gray-500 uppercase">Show On</th>
                        <th class="px-5 py-3 text-right text-xs font-semibold text-gray-500 uppercase">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @foreach($sections as $section)
                        <tr>
                            <td class="px-5 py-4 text-sm font-semibold text-gray-800">{{ $section['name'] }}</td>
                            <td class="px-5 py-4 text-sm text-gray-600">({{ $section['page'] }})</td>
                            <td class="px-5 py-4">
                                <div class="flex justify-end items-center gap-2">
                                    <button type="button"
                                            class="inline-flex items-center justify-center w-8 h-8 rounded-full border border-gray-300 text-gray-600 hover:bg-gray-50"
                                            data-help-title="{{ $section['name'] }}"
                                            data-help-text="{{ $section['help_text'] }}"
                                            data-help-image="{{ $section['sample_image'] }}"
                                            title="Section sample">
                                        ?
                                    </button>
                                    <a href="{{ $section['manage_url'] }}" class="inline-flex items-center px-3 py-1.5 text-xs font-semibold rounded bg-blue-600 text-white hover:bg-blue-700 transition">
                                        {{ $section['manage_label'] }}
                                    </a>
                                </div>
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
</div>

<div id="section-help-modal" class="fixed inset-0 bg-black/50 hidden items-center justify-center p-4 z-50">
    <div class="bg-white rounded-xl w-full max-w-3xl overflow-hidden shadow-2xl">
        <div class="px-5 py-4 border-b border-gray-200 flex items-center justify-between">
            <div>
                <h3 id="help-title" class="text-lg font-bold text-gray-800">Section Sample</h3>
                <p id="help-text" class="text-sm text-gray-500 mt-0.5"></p>
            </div>
            <button type="button" id="close-help" class="text-gray-500 hover:text-gray-700 text-xl leading-none">&times;</button>
        </div>
        <div class="p-4 bg-gray-50">
            <img id="help-image" src="" alt="Section sample" class="w-full h-[420px] object-cover rounded-lg border border-gray-200">
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
(function () {
    const modal = document.getElementById('section-help-modal');
    const title = document.getElementById('help-title');
    const text = document.getElementById('help-text');
    const image = document.getElementById('help-image');
    const closeBtn = document.getElementById('close-help');

    document.getElementById('page-jump')?.addEventListener('change', function () {
        if (this.value) window.location.href = this.value;
    });

    document.querySelectorAll('[data-help-title]').forEach((btn) => {
        btn.addEventListener('click', () => {
            title.textContent = btn.getAttribute('data-help-title') || 'Section Sample';
            text.textContent = btn.getAttribute('data-help-text') || '';
            image.src = btn.getAttribute('data-help-image') || '';
            modal.classList.remove('hidden');
            modal.classList.add('flex');
        });
    });

    const closeModal = () => {
        modal.classList.add('hidden');
        modal.classList.remove('flex');
    };

    closeBtn.addEventListener('click', closeModal);
    modal.addEventListener('click', (e) => {
        if (e.target === modal) closeModal();
    });
})();
</script>
@endpush