@extends('admin.layouts.app')

@section('title', '钓鱼事件管理')

@section('content')
    @php require resource_path('views/admin/partials/list-theme.php'); @endphp

    @php
        $fishingEventPayload = $events->mapWithKeys(
            fn($event) => [
                (string) $event->id => [
                    'id' => $event->id,
                    'emoji' => $event->emoji,
                    'name' => $event->name,
                    'message' => $event->message,
                    'exp' => $event->exp,
                    'jjb' => $event->jjb,
                    'weight' => $event->weight,
                    'sort' => $event->sort,
                    'is_active' => (bool) $event->is_active,
                    'update_url' => route('admin.fishing.update', $event->id),
                    'toggle_url' => route('admin.fishing.toggle', $event->id),
                ],
            ],
        );
    @endphp

    <script type="application/json" id="admin-fishing-events-data">@json($fishingEventPayload)</script>

    <div class="{{ $adminListPageClass }}">

        {{-- 页头 --}}
        <div class="{{ $adminListHeaderCardClass }}">
            <div class="flex flex-col gap-4 lg:flex-row lg:items-center lg:justify-between">
                <div>
                    <h2 class="{{ $adminListHeaderTitleClass }}">🎣 钓鱼事件管理</h2>
                    <p class="{{ $adminListHeaderSubtitleClass }}">
                        管理钓鱼随机事件。权重越大被触发概率越高。
                        当前激活事件总权重：<strong class="text-indigo-600">{{ $totalWeight }}</strong>
                    </p>
                </div>
                <a href="{{ route('admin.game-configs.index') }}"
                    class="{{ $adminListSecondaryButtonClass }} inline-flex items-center justify-center">
                    ⚙️ 钓鱼参数设置
                </a>
            </div>
        </div>

        {{-- 钓鱼事件列表 --}}
        <div class="{{ $adminListCardClass }}">
            <div class="{{ $adminListTableWrapClass }}">
                <table class="{{ $adminListTableClass }}">
                    <thead class="{{ $adminListTableHeadRowClass }}">
                    <tr>
                        <th class="{{ $adminListTableHeadCellClass }}">排序</th>
                        <th class="{{ $adminListTableHeadCellClass }}">符号</th>
                        <th class="{{ $adminListTableHeadCellClass }}">名称</th>
                        <th class="{{ $adminListTableHeadCellClass }} w-1/3">播报内容</th>
                        <th class="{{ $adminListTableHeadCellClass }} text-center">经验</th>
                        <th class="{{ $adminListTableHeadCellClass }} text-center">金币</th>
                        <th class="{{ $adminListTableHeadCellClass }} text-center">权重</th>
                        <th class="{{ $adminListTableHeadCellClass }} text-center">概率</th>
                        <th class="{{ $adminListTableHeadCellClass }} text-center">状态</th>
                        <th class="{{ $adminListTableHeadCellClass }} text-right">操作</th>
                    </tr>
                </thead>
                    <tbody class="{{ $adminListTableBodyClass }}">
                    @foreach ($events as $event)
                        <tr id="row-{{ $event->id }}" class="{{ $adminListTableRowClass }} {{ $event->is_active ? '' : 'opacity-50' }}">
                            <td class="px-4 py-3 {{ $adminListSecondaryTextClass }}">{{ $event->sort }}</td>
                            <td class="px-4 py-3 text-xl">{{ $event->emoji }}</td>
                            <td class="px-4 py-3 {{ $adminListPrimaryTextClass }}">{{ $event->name }}</td>
                            <td class="px-4 py-3 {{ $adminListBodyTextClass }}">{{ $event->message }}</td>
                            <td
                                class="px-4 py-3 text-center {{ $adminListNumericTextClass }}
                                {{ $event->exp > 0 ? 'text-green-600' : ($event->exp < 0 ? 'text-red-500' : 'text-gray-400') }}">
                                {{ $event->exp > 0 ? '+' : '' }}{{ $event->exp }}
                            </td>
                            <td
                                class="px-4 py-3 text-center {{ $adminListNumericTextClass }}
                                {{ $event->jjb > 0 ? 'text-amber-600' : ($event->jjb < 0 ? 'text-red-500' : 'text-gray-400') }}">
                                {{ $event->jjb > 0 ? '+' : '' }}{{ $event->jjb }}
                            </td>
                            <td class="px-4 py-3 text-center text-indigo-600 {{ $adminListNumericTextClass }} font-bold">{{ $event->weight }}</td>
                            <td class="px-4 py-3 text-center {{ $adminListBodyTextClass }}">
                                @if ($totalWeight > 0 && $event->is_active)
                                    {{ number_format(($event->weight / $totalWeight) * 100, 1) }}%
                                @else
                                    —
                                @endif
                            </td>
                            <td class="px-4 py-3 text-center">
                                <button type="button" data-fishing-toggle-id="{{ $event->id }}"
                                    id="toggle-{{ $event->id }}"
                                    class="{{ $adminListBadgeBaseClass }} px-2 py-1 transition
                                        {{ $event->is_active ? 'border-emerald-200 bg-emerald-100 text-emerald-700 hover:bg-emerald-200' : 'border-gray-200 bg-gray-100 text-gray-500 hover:bg-gray-200' }}">
                                    {{ $event->is_active ? '启用' : '禁用' }}
                                </button>
                            </td>
                            <td class="px-4 py-3 text-right">
                                <button type="button" data-fishing-edit-id="{{ $event->id }}"
                                    class="{{ $adminListActionButtonClass }} bg-indigo-50 text-indigo-700 hover:bg-indigo-100 mr-1">
                                    编辑
                                </button>
                                <form action="{{ route('admin.fishing.destroy', $event->id) }}" method="POST"
                                    class="inline" data-fishing-delete-confirm="确定删除事件「{{ $event->name }}」？">
                                    @csrf @method('DELETE')
                                    <button type="submit"
                                        class="{{ $adminListActionButtonClass }} bg-red-50 text-red-600 hover:bg-red-100">
                                        删除
                                    </button>
                                </form>
                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
        </div>

        {{-- 新增事件卡片 --}}
        <div class="{{ $adminListCardClass }}">
            <div class="{{ $adminListSectionHeadClass }}">
                <h3 class="{{ $adminListSectionTitleClass }}">➕ 新增钓鱼事件</h3>
            </div>
            <form action="{{ route('admin.fishing.store') }}" method="POST" class="p-5">
                @csrf
                <div class="grid grid-cols-2 md:grid-cols-4 gap-4">
                    <div>
                        <label class="{{ $adminListFilterLabelClass }}">Emoji 符号</label>
                        <input type="text" name="emoji" value="{{ old('emoji') }}" placeholder="🐟" required
                            class="w-full {{ $adminListFilterInputClass }}">
                    </div>
                    <div>
                        <label class="{{ $adminListFilterLabelClass }}">名称</label>
                        <input type="text" name="name" value="{{ old('name') }}" placeholder="小鲤鱼" required
                            class="w-full {{ $adminListFilterInputClass }}">
                    </div>
                    <div>
                        <label class="{{ $adminListFilterLabelClass }}">经验变化</label>
                        <input type="number" name="exp" value="{{ old('exp', 0) }}"
                            class="w-full {{ $adminListFilterInputClass }}">
                    </div>
                    <div>
                        <label class="{{ $adminListFilterLabelClass }}">金币变化</label>
                        <input type="number" name="jjb" value="{{ old('jjb', 0) }}"
                            class="w-full {{ $adminListFilterInputClass }}">
                    </div>
                    <div class="md:col-span-2">
                        <label class="{{ $adminListFilterLabelClass }}">播报内容</label>
                        <input type="text" name="message" value="{{ old('message') }}" placeholder="钓到一条小鲤鱼..." required
                            class="w-full {{ $adminListFilterInputClass }}">
                    </div>
                    <div>
                        <label class="{{ $adminListFilterLabelClass }}">权重（概率）</label>
                        <input type="number" name="weight" value="{{ old('weight', 10) }}" min="1"
                            class="w-full {{ $adminListFilterInputClass }}">
                    </div>
                    <div>
                        <label class="{{ $adminListFilterLabelClass }}">排序</label>
                        <input type="number" name="sort" value="{{ old('sort', 0) }}" min="0"
                            class="w-full {{ $adminListFilterInputClass }}">
                    </div>
                </div>
                <div class="mt-4 flex items-center gap-4">
                    <button type="submit" class="{{ $adminListPrimaryButtonClass }}">
                        💾 添加事件
                    </button>
                    <label class="flex items-center gap-2 text-sm text-gray-600 cursor-pointer">
                        <input type="checkbox" name="is_active" value="1" checked class="rounded">
                        立即启用
                    </label>
                </div>
            </form>
        </div>

    </div>

    {{-- 编辑弹窗 --}}
    <div id="edit-modal" class="hidden fixed inset-0 bg-black/40 z-50 flex items-center justify-center p-4">
        <div class="bg-white rounded-xl w-full max-w-lg shadow-2xl">
            <div class="p-5 border-b border-gray-100 flex justify-between items-center">
                <h3 class="font-bold text-gray-800">✏️ 编辑钓鱼事件</h3>
                <button type="button" data-fishing-edit-close class="text-gray-400 hover:text-gray-600 text-xl">✕</button>
            </div>
            <form id="edit-form" method="POST" class="p-5">
                @csrf @method('PUT')
                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <label class="block text-xs font-bold text-gray-600 mb-1">Emoji</label>
                        <input type="text" name="emoji" id="edit-emoji" required
                            class="w-full border border-gray-300 rounded-lg p-2 text-sm">
                    </div>
                    <div>
                        <label class="block text-xs font-bold text-gray-600 mb-1">名称</label>
                        <input type="text" name="name" id="edit-name" required
                            class="w-full border border-gray-300 rounded-lg p-2 text-sm">
                    </div>
                    <div class="col-span-2">
                        <label class="block text-xs font-bold text-gray-600 mb-1">播报内容</label>
                        <input type="text" name="message" id="edit-message" required
                            class="w-full border border-gray-300 rounded-lg p-2 text-sm">
                    </div>
                    <div>
                        <label class="block text-xs font-bold text-gray-600 mb-1">经验变化</label>
                        <input type="number" name="exp" id="edit-exp"
                            class="w-full border border-gray-300 rounded-lg p-2 text-sm">
                    </div>
                    <div>
                        <label class="block text-xs font-bold text-gray-600 mb-1">金币变化</label>
                        <input type="number" name="jjb" id="edit-jjb"
                            class="w-full border border-gray-300 rounded-lg p-2 text-sm">
                    </div>
                    <div>
                        <label class="block text-xs font-bold text-gray-600 mb-1">权重</label>
                        <input type="number" name="weight" id="edit-weight" min="1"
                            class="w-full border border-gray-300 rounded-lg p-2 text-sm">
                    </div>
                    <div>
                        <label class="block text-xs font-bold text-gray-600 mb-1">排序</label>
                        <input type="number" name="sort" id="edit-sort" min="0"
                            class="w-full border border-gray-300 rounded-lg p-2 text-sm">
                    </div>
                    <div class="col-span-2">
                        <label class="flex items-center gap-2 text-sm cursor-pointer">
                            <input type="checkbox" name="is_active" id="edit-is-active" value="1" class="rounded">
                            启用此事件
                        </label>
                    </div>
                </div>
                <div class="mt-5 flex gap-3">
                    <button type="submit" class="{{ $adminListPrimaryButtonClass }}">
                        💾 保存修改
                    </button>
                    <button type="button" data-fishing-edit-close
                        class="{{ $adminListSecondaryButtonClass }}">
                        取消
                    </button>
                </div>
            </form>
        </div>
    </div>
@endsection
