<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>后台管理 - 流星</title>
    @vite(['resources/css/app.css', 'resources/js/app.js'])
    <script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
</head>

<body class="bg-gray-100 flex h-screen text-gray-800">
    @php
        $adminFlashToasts = collect([
            session('success') ? ['type' => 'success', 'message' => session('success')] : null,
            session('ops_success') ? ['type' => 'success', 'message' => session('ops_success')] : null,
            session('error') ? ['type' => 'error', 'message' => session('error')] : null,
        ])
            ->filter()
            ->values();
    @endphp

    <!-- 左侧侧边栏 -->
    <aside class="w-64 bg-slate-900 text-white flex flex-col">
        <div class="p-6 text-center border-b border-white/10">
            <h2 class="text-2xl font-extrabold tracking-widest uppercase">Admin</h2>
            <p class="text-xs text-slate-400 mt-2">控制台</p>
        </div>
        <nav class="flex-1 px-4 py-6 space-y-2 overflow-y-auto">

            {{-- ──────── 所有有职务的人都可见 ──────── --}}
            <a href="{{ route('admin.dashboard') }}"
                class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.dashboard') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                📊 仪表盘
            </a>
            <a href="{{ route('admin.currency-stats.index') }}"
                class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.currency-stats.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                📈 积分流水统计
            </a>
            <a href="{{ route('admin.users.index') }}"
                class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.users.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                👥 用户管理
            </a>
            {{-- ──────── 部门职务任命系统 ──────── --}}
            <div class="border-t border-white/10 my-2"></div>
            <a href="{{ route('admin.appointments.my-duty-logs') }}"
                class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.appointments.my-duty-logs') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                📝 我的履职记录
            </a>
            <a href="{{ route('admin.appointments.index') }}"
                class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.appointments.*') && !request()->routeIs('admin.appointments.my-duty-logs') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                🎖️ 任命管理
            </a>

            {{-- superlevel 及以上：可查看（只读标注）以下模块；id=1 可编辑 --}}
            @php $superLvl = (int) \App\Models\Sysparam::getValue('superlevel', '100'); @endphp
            @if (Auth::user()->user_level >= $superLvl)
                <div class="border-t border-white/10 my-2"></div>
                <p class="px-4 text-xs text-slate-500 uppercase tracking-widest mb-1">
                    {{ Auth::id() === 1 ? '站长功能' : '查看' }}</p>

                <a href="{{ route('admin.system.edit') }}"
                    class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.system.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                    {!! '⚙️ 聊天室参数' !!}
                </a>
                <a href="{{ route('admin.wechat_bot.edit') }}"
                    class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.wechat_bot.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                    {!! '🤖 微信机器人' !!}
                </a>
                <a href="{{ route('admin.currency-logs.index') }}"
                    class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.currency-logs.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                    {!! '💴 用户流水' !!}
                </a>
                <a href="{{ route('admin.rooms.index') }}"
                    class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.rooms.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                    {!! '🏠 房间管理' !!}
                </a>
                <a href="{{ route('admin.autoact.index') }}"
                    class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.autoact.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                    {!! '🎲 随机事件' !!}
                </a>
                <a href="{{ route('admin.sign-in-rules.index') }}"
                    class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.sign-in-rules.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                    {!! '✅ 签到奖励' !!}
                </a>
                <a href="{{ route('admin.vip.index') }}"
                    class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.vip.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                    {!! '👑 VIP 会员等级' !!}
                </a>
                <a href="{{ route('admin.vip-payment-logs.index') }}"
                    class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.vip-payment-logs.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                    {!! '🧾 会员购买日志' !!}
                </a>
                <a href="{{ route('admin.shop.index') }}"
                    class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.shop.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                    {!! '🛒 商店管理' !!}
                </a>
                <a href="{{ route('admin.marriages.index') }}"
                    class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.marriages.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                    {!! '💒 婚姻管理' !!}
                </a>
                <a href="{{ route('admin.holiday-events.index') }}"
                    class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.holiday-events.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                    {!! '🎊 节日福利' !!}
                </a>
                <a href="{{ route('admin.game-configs.index') }}"
                    class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.game-configs.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                    {!! '🎮 游戏管理' !!}
                </a>
                <a href="{{ route('admin.fishing.index') }}"
                    class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.fishing.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                    {!! '🎣 钓鱼事件' !!}
                </a>
                <a href="{{ route('admin.departments.index') }}"
                    class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.departments.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                    {!! '🏛️ 部门管理' !!}
                </a>
                <a href="{{ route('admin.positions.index') }}"
                    class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.positions.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                    {!! '📋 职务管理' !!}
                </a>

                {{-- 以下纯写操作：仅 id=1 可见 --}}
                @if (Auth::id() === 1)
                    <div class="border-t border-white/10 my-2"></div>
                    <p class="px-4 text-xs text-slate-500 uppercase tracking-widest mb-1">系统配置</p>
                    <a href="{{ route('admin.smtp.edit') }}"
                        class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.smtp.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                        📧 邮件 SMTP 配置
                    </a>
                    <a href="{{ route('admin.vip-payment.edit') }}"
                        class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.vip-payment.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                        💳 VIP 支付配置
                    </a>
                    <a href="{{ route('admin.ai-providers.index') }}"
                        class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.ai-providers.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                        🤖 AI 厂商配置
                    </a>
                    <a href="{{ route('admin.ops.index') }}"
                        class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.ops.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                        🛠️ 运维工具
                    </a>
                    <a href="{{ route('admin.changelogs.index') }}"
                        class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.changelogs.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                        📋 开发日志
                    </a>
                    <a href="{{ route('admin.feedback.index') }}"
                        class="flex items-center justify-between px-4 py-3 rounded-md transition {{ request()->routeIs('admin.feedback.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                        <span>💬 用户反馈</span>
                        @php $pendingFeedback = \App\Models\FeedbackItem::pending()->count(); @endphp
                        @if ($pendingFeedback > 0)
                            <span
                                class="bg-orange-500 text-white text-xs px-1.5 py-0.5 rounded-full font-bold">{{ $pendingFeedback }}</span>
                        @endif
                    </a>
                    <a href="{{ route('admin.forbidden-usernames.index') }}"
                        class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.forbidden-usernames.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
                        🚫 禁用用户名
                    </a>
                @endif
            @endif
        </nav>
        <div class="p-4 border-t border-white/10">
            <a href="{{ route('rooms.index') }}"
                class="block w-full text-center px-4 py-2 bg-slate-800 hover:bg-slate-700 rounded transition text-sm">
                返回前台大厅
            </a>
        </div>
    </aside>

    <!-- 右侧主体内容 -->
    <main class="flex-1 flex flex-col h-full overflow-hidden relative">
        <!-- 顶栏 -->
        <header class="bg-white shadow relative z-20 flex items-center justify-between px-6 py-4">
            <h1 class="text-xl font-bold text-gray-700">@yield('title', '控制台')</h1>
            <div class="flex items-center space-x-4">
                <span class="text-sm font-medium">当前操作人: <span
                        class="text-indigo-600">{{ Auth::user()->username }}</span></span>
            </div>
        </header>

        <!-- 内容滚动区 -->
        <div class="flex-1 overflow-y-auto p-6 relative">
            @if ($errors->any())
                <div class="mb-6 bg-red-100 border-l-4 border-red-500 text-red-700 p-4 rounded shadow-sm">
                    <ul class="list-disc list-inside text-sm">
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif

            @yield('content')
        </div>
    </main>

    <div x-data="createAdminToastStore(@js($adminFlashToasts))" x-init="boot()"
        class="pointer-events-none fixed right-6 top-6 z-[100] flex w-full max-w-sm flex-col gap-3">
        <template x-for="toast in toasts" :key="toast.id">
            <div x-cloak x-show="visibleIds.includes(toast.id)"
                x-transition:enter="transform transition duration-300 ease-out"
                x-transition:enter-start="translate-y-2 opacity-0 sm:translate-x-6 sm:translate-y-0"
                x-transition:enter-end="translate-y-0 opacity-100 sm:translate-x-0"
                x-transition:leave="transform transition duration-200 ease-in"
                x-transition:leave-start="translate-y-0 opacity-100"
                x-transition:leave-end="translate-y-2 opacity-0 sm:translate-x-6 sm:translate-y-0"
                class="pointer-events-auto overflow-hidden rounded-2xl border border-slate-200/80 bg-white/95 shadow-2xl ring-1 ring-slate-200/60 backdrop-blur">
                <div class="flex items-start gap-3 p-4">
                    <div class="flex h-10 w-10 shrink-0 items-center justify-center rounded-2xl text-lg font-bold"
                        :class="toast.type === 'error'
                            ? 'bg-red-50 text-red-500 ring-1 ring-red-100'
                            : 'bg-emerald-50 text-emerald-500 ring-1 ring-emerald-100'">
                        <span x-text="toast.type === 'error' ? '!' : '✓'"></span>
                    </div>
                    <div class="min-w-0 flex-1">
                        <div class="flex items-start justify-between gap-3">
                            <div>
                                <p class="text-sm font-bold text-slate-800"
                                    x-text="toast.title || (toast.type === 'error' ? '操作失败' : '操作成功')"></p>
                                <p class="mt-1 break-words text-sm leading-6 text-slate-600" x-text="toast.message"></p>
                            </div>
                            <button type="button" @click="remove(toast.id)"
                                class="rounded-full p-1 text-slate-400 transition hover:bg-slate-100 hover:text-slate-600"
                                aria-label="关闭提示">
                                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor"
                                    class="h-4 w-4">
                                    <path
                                        d="M6.28 5.22a.75.75 0 0 1 1.06 0L10 7.94l2.66-2.72a.75.75 0 0 1 1.08 1.04L11.06 9l2.68 2.74a.75.75 0 1 1-1.08 1.04L10 10.06l-2.66 2.72a.75.75 0 1 1-1.08-1.04L8.94 9 6.28 6.26a.75.75 0 0 1 0-1.04Z" />
                                </svg>
                            </button>
                        </div>
                        <div class="mt-3 h-1 overflow-hidden rounded-full bg-slate-100">
                            <div class="h-full rounded-full"
                                :class="toast.type === 'error' ? 'bg-red-400' : 'bg-emerald-400'"
                                :style="`animation: admin-toast-progress ${toast.duration || 4200}ms linear forwards;`">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </template>
    </div>

    {{-- ══════════════════════════════════════════════════════════
         全局弹窗组件：window.adminDialog.alert / window.adminDialog.confirm
         用法：
           window.adminDialog.alert('操作成功！', '✅ 提示');
           window.adminDialog.confirm('确定要删除？', '⚠️ 确认', () => { ... });
    ══════════════════════════════════════════════════════════ --}}
    <div id="admin-dialog-overlay"
        style="display:none; position:fixed; inset:0; background:rgba(15,23,42,.55);
               backdrop-filter:blur(3px); z-index:99999; align-items:center; justify-content:center;">
        <div id="admin-dialog-box"
            style="background:#fff; border-radius:16px; box-shadow:0 24px 64px rgba(0,0,0,.22);
                   min-width:320px; max-width:480px; width:90%; padding:32px 32px 24px; text-align:center;
                   animation:admin-dialog-pop .25s cubic-bezier(.175,.885,.32,1.275);">
            <div id="admin-dialog-icon" style="font-size:36px; margin-bottom:10px;"></div>
            <div id="admin-dialog-title" style="font-size:16px; font-weight:800; color:#1e293b; margin-bottom:8px;">
            </div>
            <div id="admin-dialog-msg" style="font-size:14px; color:#475569; line-height:1.6; margin-bottom:20px;">
            </div>
            <div id="admin-dialog-btns" style="display:flex; gap:10px; justify-content:center;"></div>
        </div>
    </div>
    <style>
        [x-cloak] {
            display: none !important;
        }

        @keyframes admin-dialog-pop {
            0% {
                opacity: 0;
                transform: scale(.8);
            }

            70% {
                transform: scale(1.03);
            }

            100% {
                opacity: 1;
                transform: scale(1);
            }
        }

        @keyframes admin-toast-progress {
            from {
                transform: translateX(0);
            }

            to {
                transform: translateX(-100%);
            }
        }
    </style>
    <script>
        /**
         * 后台全局 Toast 工厂。
         *
         * 负责统一渲染右上角成功/失败提示，并提供全局调用入口。
         *
         * @param initialToasts 初始提示列表
         * @returns {object}
         */
        function createAdminToastStore(initialToasts = []) {
            return {
                toasts: [],
                visibleIds: [],
                nextToastId: Date.now(),

                /**
                 * 初始化 Toast 列表，并挂载全局调用方法。
                 */
                boot() {
                    initialToasts.forEach((toast) => this.push(toast));

                    window.adminToast = {
                        show: (message, type = 'success', title = null, duration = 4200) => this.push({
                            message,
                            type,
                            title,
                            duration
                        }),
                        success: (message, title = '操作成功', duration = 4200) => this.push({
                            message,
                            type: 'success',
                            title,
                            duration
                        }),
                        error: (message, title = '操作失败', duration = 4200) => this.push({
                            message,
                            type: 'error',
                            title,
                            duration
                        }),
                    };
                },

                /**
                 * 追加一条 Toast，并在指定时长后自动关闭。
                 *
                 * @param toast 待展示的提示对象
                 */
                push(toast) {
                    if (!toast?.message) {
                        return;
                    }

                    const normalizedToast = {
                        id: this.nextToastId++,
                        type: toast.type === 'error' ? 'error' : 'success',
                        title: toast.title,
                        message: toast.message,
                        duration: Number(toast.duration) > 0 ? Number(toast.duration) : 4200,
                    };

                    this.toasts.push(normalizedToast);
                    this.visibleIds.push(normalizedToast.id);

                    window.setTimeout(() => {
                        this.remove(normalizedToast.id);
                    }, normalizedToast.duration);
                },

                /**
                 * 关闭指定 Toast，并在退场动画后清理节点。
                 *
                 * @param {number} toastId
                 */
                remove(toastId) {
                    this.visibleIds = this.visibleIds.filter((visibleId) => visibleId !== toastId);

                    window.setTimeout(() => {
                        this.toasts = this.toasts.filter((toast) => toast.id !== toastId);
                    }, 220);
                },
            };
        }

        /**
         * 后台全局弹窗组件。
         *
         * 提供 alert / confirm 两种模式，替换原生 alert/confirm。
         */
        window.adminDialog = (function() {
            const overlay = document.getElementById('admin-dialog-overlay');
            const box = document.getElementById('admin-dialog-box');
            const elIcon = document.getElementById('admin-dialog-icon');
            const elTitle = document.getElementById('admin-dialog-title');
            const elMsg = document.getElementById('admin-dialog-msg');
            const elBtns = document.getElementById('admin-dialog-btns');

            /** 关闭弹窗 */
            function close() {
                overlay.style.display = 'none';
            }

            /** 点击遮罩层关闭 */
            overlay.addEventListener('click', function(e) {
                if (e.target === overlay) close();
            });

            /**
             * 创建按钮元素
             *
             * @param {string}   label   按钮文字
             * @param {string}   color   按钮背景色
             * @param {Function} onClick 点击回调
             */
            function makeBtn(label, color, onClick) {
                const btn = document.createElement('button');
                btn.textContent = label;
                btn.style.cssText = `padding:9px 24px; border-radius:8px; border:none; cursor:pointer;
                    font-size:14px; font-weight:700; color:#fff; background:${color};
                    transition:opacity .15s; box-shadow:0 3px 10px rgba(0,0,0,.12);`;
                btn.onmouseover = () => btn.style.opacity = '.82';
                btn.onmouseout = () => btn.style.opacity = '1';
                btn.addEventListener('click', () => {
                    close();
                    if (onClick) onClick();
                });
                return btn;
            }

            /**
             * 弹出提示框（仅「确定」按钮）
             *
             * @param {string}   message  消息内容（支持 HTML）
             * @param {string}   title    标题
             * @param {string}   icon     图标 Emoji
             * @param {Function} onOk     确定回调
             */
            function alert(message, title = '提示', icon = 'ℹ️', onOk = null) {
                elIcon.textContent = icon;
                elTitle.textContent = title;
                elMsg.innerHTML = message;
                elBtns.innerHTML = '';
                elBtns.appendChild(makeBtn('确定', '#4f46e5', onOk));
                overlay.style.display = 'flex';
            }

            /**
             * 弹出确认框（「确定」+「取消」按钮）
             *
             * @param {string}   message    消息内容
             * @param {string}   title      标题
             * @param {Function} onConfirm  确认回调
             * @param {string}   icon       图标 Emoji
             */
            function confirm(message, title = '确认操作', onConfirm = null, icon = '⚠️') {
                elIcon.textContent = icon;
                elTitle.textContent = title;
                elMsg.innerHTML = message;
                elBtns.innerHTML = '';

                const confirmBtn = makeBtn('确定', '#4f46e5', onConfirm);
                const cancelBtn = makeBtn('取消', '#94a3b8', null);
                elBtns.appendChild(confirmBtn);
                elBtns.appendChild(cancelBtn);
                overlay.style.display = 'flex';
            }

            return {
                alert,
                confirm,
                close
            };
        })();
    </script>
</body>

</html>
