{{--
    文件功能：星光留言板页面（含公共留言、收件箱、发件箱三个分类）
    支持公开留言和悄悄话私信功能

    @extends layouts.app
--}}
@extends('layouts.app')

@section('title', '星光留言板 - 飘落流星')

@section('nav-icon', '✉️')
@section('nav-title', '星光留言板')


@section('content')
    <div x-data="{ showWriteForm: false, towho: '{{ $defaultTo }}' }" class="w-full">

        {{-- 验证错误信息 --}}
        @if (isset($errors) && $errors->any())
            <div class="bg-red-100 border-l-4 border-red-500 text-red-700 p-4 mb-4 mx-4 mt-4 shadow-sm">
                <ul class="list-disc pl-5">
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif

        {{-- 弹窗遮罩层 --}}
        <div x-show="showWriteForm" x-transition:enter="transition ease-out duration-200" x-transition:enter-start="opacity-0"
            x-transition:enter-end="opacity-100" x-transition:leave="transition ease-in duration-150"
            x-transition:leave-start="opacity-100" x-transition:leave-end="opacity-0"
            class="fixed inset-0 bg-black/50 backdrop-blur-sm z-50 flex items-center justify-center p-4"
            @click.self="showWriteForm = false" style="display: none;">

            {{-- 对话框 --}}
            <div x-show="showWriteForm" x-transition:enter="transition ease-out duration-200"
                x-transition:enter-start="opacity-0 scale-95" x-transition:enter-end="opacity-100 scale-100"
                x-transition:leave="transition ease-in duration-150" x-transition:leave-start="opacity-100 scale-100"
                x-transition:leave-end="opacity-0 scale-95"
                class="bg-white rounded-2xl shadow-2xl w-full max-w-xl overflow-hidden">

                {{-- 弹窗头部 --}}
                <div class="bg-gradient-to-r from-indigo-600 to-indigo-500 px-6 py-4 flex justify-between items-center">
                    <div class="flex items-center space-x-2">
                        <svg class="w-5 h-5 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z">
                            </path>
                        </svg>
                        <h3 class="text-white font-bold text-lg">撰写留言</h3>
                    </div>
                    <button @click="showWriteForm = false" class="text-white/70 hover:text-white transition">
                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12">
                            </path>
                        </svg>
                    </button>
                </div>

                {{-- 表单内容 --}}
                <form action="{{ route('guestbook.store') }}" method="POST" class="p-6 space-y-5">
                    @csrf

                    {{-- 收件人选择 --}}
                    <div>
                        <label class="block text-sm font-semibold text-gray-700 mb-2">
                            📬 收件人
                            <span class="font-normal text-gray-400 ml-1">(留空则为公共留言)</span>
                        </label>
                        <div class="relative">
                            <select name="towho" x-model="towho"
                                class="w-full border-gray-200 rounded-xl py-2.5 px-4 focus:ring-indigo-500 focus:border-indigo-500 text-sm appearance-none bg-gray-50 pr-10">
                                <option value="">🌍 公共留言（所有人可见）</option>
                                @foreach ($users as $uname)
                                    <option value="{{ $uname }}" :selected="towho === '{{ $uname }}'">👤
                                        {{ $uname }}</option>
                                @endforeach
                            </select>
                            <div class="pointer-events-none absolute inset-y-0 right-3 flex items-center">
                                <svg class="w-4 h-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                        d="M19 9l-7 7-7-7"></path>
                                </svg>
                            </div>
                        </div>
                    </div>

                    {{-- 您您话开关 --}}
                    <label class="flex items-center space-x-3 cursor-pointer group">
                        <div class="relative">
                            <input type="checkbox" name="secret" value="1" class="sr-only peer">
                            <div class="w-10 h-6 bg-gray-200 rounded-full peer-checked:bg-pink-500 transition-colors"></div>
                            <div
                                class="absolute top-0.5 left-0.5 w-5 h-5 bg-white rounded-full shadow transition-transform peer-checked:translate-x-4">
                            </div>
                        </div>
                        <span class="text-sm font-medium text-gray-700 group-hover:text-pink-600 transition-colors">🔒
                            您您话（仅双方可见）</span>
                    </label>

                    {{-- 正文 --}}
                    <div>
                        <label class="block text-sm font-semibold text-gray-700 mb-2">
                            📝 留言内容 <span class="text-red-500">*</span>
                        </label>
                        <textarea name="text_body" x-ref="textBody" rows="5" required
                            class="w-full border-gray-200 rounded-xl p-4 focus:ring-indigo-500 focus:border-indigo-500 text-sm bg-gray-50 resize-none"
                            placeholder="相逢何必曾相识，留下您的足迹吧..."></textarea>
                    </div>

                    {{-- 操作按钮 --}}
                    <div class="flex justify-end space-x-3 pt-2">
                        <button type="button" @click="showWriteForm = false"
                            class="px-5 py-2.5 text-sm font-medium text-gray-600 bg-gray-100 hover:bg-gray-200 rounded-xl transition">
                            取消
                        </button>
                        <button type="submit"
                            class="px-6 py-2.5 text-sm font-bold text-white bg-indigo-600 hover:bg-indigo-700 rounded-xl shadow-md hover:shadow-lg transition-all transform hover:-translate-y-0.5 flex items-center">
                            <svg class="w-4 h-4 mr-1.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                    d="M12 19l9 2-9-18-9 18 9-2zm0 0v-8"></path>
                            </svg>
                            发送飞鸽
                        </button>
                    </div>
                </form>
            </div>
        </div>

        {{-- 主体内容区 --}}
        <div class="flex max-w-7xl mx-auto mt-4 px-4 sm:px-6 lg:px-8 gap-6">


            {{-- 左侧：分类导航 --}}
            <div
                class="w-72 bg-white border border-gray-100 rounded-2xl shadow-sm shrink-0 hidden md:flex flex-col mb-10 self-start sticky top-20">
                <div class="p-5 flex-1">
                    {{-- 新建留言按钮 --}}
                    <button
                        @click="showWriteForm = !showWriteForm; if(showWriteForm) setTimeout(() => $refs.textBody.focus(), 100)"
                        class="w-full bg-indigo-600 hover:bg-indigo-700 text-white px-4 py-3 rounded-xl font-bold mb-6 shadow-md transition flex justify-center items-center">
                        <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z">
                            </path>
                        </svg>
                        <span x-text="showWriteForm ? '取消撰写' : '写新留言'"></span>
                    </button>

                    <nav class="space-y-2">
                        <a href="{{ route('guestbook.index', ['tab' => 'public']) }}"
                            class="flex items-center px-4 py-3 rounded-lg font-medium transition-colors {{ $tab === 'public' ? 'bg-indigo-50 text-indigo-700' : 'text-gray-600 hover:bg-gray-50' }}">
                            <span class="mr-3 text-lg">🌍</span> 公共留言墙
                        </a>
                        <a href="{{ route('guestbook.index', ['tab' => 'inbox']) }}"
                            class="flex items-center px-4 py-3 rounded-lg font-medium transition-colors {{ $tab === 'inbox' ? 'bg-indigo-50 text-indigo-700' : 'text-gray-600 hover:bg-gray-50' }}">
                            <span class="mr-3 text-lg">📥</span> 我收件的
                        </a>
                        <a href="{{ route('guestbook.index', ['tab' => 'outbox']) }}"
                            class="flex items-center px-4 py-3 rounded-lg font-medium transition-colors {{ $tab === 'outbox' ? 'bg-indigo-50 text-indigo-700' : 'text-gray-600 hover:bg-gray-50' }}">
                            <span class="mr-3 text-lg">📤</span> 我发出的
                        </a>
                    </nav>
                </div>
            </div>

            {{-- 右侧：留言流列表 --}}
            <main class="flex-1 pb-20 min-w-0">
                <div class="space-y-4">

                    @forelse($messages as $msg)
                        @php
                            $isSecret = $msg->secret == 1;
                            $isToMe = Auth::check() && $msg->towho === Auth::user()->username;
                            $isFromMe = Auth::check() && $msg->who === Auth::user()->username;
                        @endphp

                        {{-- 消息卡片 --}}
                        <div
                            class="bg-white rounded-2xl border {{ $isSecret ? 'border-pink-200 bg-pink-50/20' : 'border-gray-100' }} shadow-sm hover:shadow-md transition-shadow duration-200 overflow-hidden">

                            {{-- 卡片头部：发件人 / 收件人 / 时间 --}}
                            <div
                                class="flex items-center justify-between px-5 py-2.5 border-b {{ $isSecret ? 'border-pink-100 bg-pink-50/50' : 'border-gray-50 bg-gray-50/50' }}">
                                <div class="flex items-center gap-2 text-sm">
                                    {{-- 发件人头像占位 --}}
                                    <div
                                        class="w-7 h-7 rounded-full {{ $isSecret ? 'bg-pink-200' : 'bg-indigo-100' }} flex items-center justify-center text-xs font-bold {{ $isSecret ? 'text-pink-700' : 'text-indigo-700' }} shrink-0">
                                        {{ mb_substr($msg->who, 0, 1) }}
                                    </div>
                                    <span class="font-bold text-gray-800">{{ $msg->who }}</span>
                                    <span class="text-gray-400 text-xs">向</span>
                                    <span class="font-semibold {{ $msg->towho ? 'text-indigo-600' : 'text-gray-500' }}">
                                        {{ $msg->towho ?: '大家' }}
                                    </span>
                                    @if ($isSecret)
                                        <span
                                            class="bg-pink-100 text-pink-600 text-[10px] font-bold px-1.5 py-0.5 rounded-md">🔒
                                            您您话</span>
                                    @endif
                                </div>
                                <div class="flex items-center gap-3 text-xs text-gray-400 shrink-0">
                                    <span>{{ \Carbon\Carbon::parse($msg->post_time)->diffForHumans() }}</span>
                                    @if ($isFromMe || $isToMe || (Auth::check() && Auth::user()->user_level >= 15))
                                        <form action="{{ route('guestbook.destroy', $msg->id) }}" method="POST"
                                            onsubmit="return confirm('确定要抓除这条留言吗？');" class="inline">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit"
                                                class="text-red-400 hover:text-red-600 transition opacity-0 group-hover:opacity-100">
                                                删除
                                            </button>
                                        </form>
                                    @endif
                                </div>
                            </div>

                            {{-- 正文内容 --}}
                            <div class="px-5 pt-2.5 pb-2">
                                <p class="text-left text-gray-700 text-[15px] leading-relaxed">{!! nl2br(e($msg->text_body)) !!}</p>
                            </div>

                            {{-- 回复TA按钒 --}}
                            @if (!Auth::check() || $msg->who !== Auth::user()->username)
                                <div class="px-5 pb-3 flex justify-end">
                                    <button
                                        @click="showWriteForm = true; towho = '{{ $msg->who }}'; setTimeout(() => $refs.textBody.focus(), 100)"
                                        class="text-xs text-indigo-500 hover:text-indigo-700 font-medium flex items-center gap-1 transition">
                                        <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                                d="M3 10h10a8 8 0 018 8v2M3 10l6 6m-6-6l6-6"></path>
                                        </svg>
                                        回复TA
                                    </button>
                                </div>
                            @endif
                        </div>
                    @empty
                        <div
                            class="bg-white rounded-2xl shadow-[0_8px_30px_rgb(0,0,0,0.04)] border border-gray-100/50 p-16 text-center flex flex-col items-center justify-center min-h-[400px]">
                            <span class="text-6xl drop-shadow-sm mb-6">📭</span>
                            <h3 class="text-xl font-bold text-gray-800 tracking-wide">暂无信件</h3>
                            <p class="mt-3 text-gray-400">这里是空空如也的荒原。</p>
                            <button
                                @click="showWriteForm = true; towho = ''; setTimeout(() => $refs.textBody.focus(), 100)"
                                class="mt-8 bg-indigo-50 hover:bg-indigo-100 text-indigo-600 font-bold py-2.5 px-6 rounded-xl transition-colors border border-indigo-100/50">
                                来抢沙发留言吧！
                            </button>
                        </div>
                    @endforelse

                    {{-- 分页 --}}
                    <div class="mt-6">
                        {{ $messages->links() }}
                    </div>

                </div>
            </main>
        </div>

        {{-- 移动端悬浮写留言按钮 --}}
        <button @click="showWriteForm = !showWriteForm; if(showWriteForm) setTimeout(() => $refs.textBody.focus(), 100)"
            class="md:hidden fixed bottom-20 right-4 w-14 h-14 bg-indigo-600 text-white rounded-full shadow-lg flex justify-center items-center hover:bg-indigo-700 transition z-50">
            <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" x-show="!showWriteForm">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                    d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z">
                </path>
            </svg>
            <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" x-show="showWriteForm"
                style="display: none;">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
            </svg>
        </button>

        {{-- 移动端底部分类栏 --}}
        <div class="md:hidden bg-white border-t border-gray-200 flex justify-around p-2 shrink-0 relative z-20">
            <a href="{{ route('guestbook.index', ['tab' => 'public']) }}"
                class="flex flex-col items-center {{ $tab === 'public' ? 'text-indigo-600' : 'text-gray-500' }}">
                <span class="text-xl">🌍</span>
                <span class="text-xs mt-1">公共墙</span>
            </a>
            <a href="{{ route('guestbook.index', ['tab' => 'inbox']) }}"
                class="flex flex-col items-center {{ $tab === 'inbox' ? 'text-indigo-600' : 'text-gray-500' }}">
                <span class="text-xl">📥</span>
                <span class="text-xs mt-1">收件箱</span>
            </a>
            <a href="{{ route('guestbook.index', ['tab' => 'outbox']) }}"
                class="flex flex-col items-center {{ $tab === 'outbox' ? 'text-indigo-600' : 'text-gray-500' }}">
                <span class="text-xl">📤</span>
                <span class="text-xs mt-1">发件箱</span>
            </a>
        </div>
    </div>
@endsection
