@extends('admin.layouts.app')

@section('title', '随机事件管理')

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

    <div class="{{ $adminListPageClass }}">
        <div class="{{ $adminListCardClass }}">
            <div class="{{ $adminListSectionHeadClass }} flex justify-between items-center">
            <div>
                    <h2 class="{{ $adminListSectionTitleClass }}">随机事件管理 (autoact)</h2>
                    <p class="{{ $adminListSectionDescClass }}">管理聊天室中随机触发的好运、坏运和中性事件，保留原有奖励与惩罚语义。</p>
                </div>
            </div>

        {{-- 新增事件表单 --}}
            <div class="p-6 border-b border-gray-100 bg-indigo-50/50">
                <h3 class="{{ $adminListSectionTitleClass }} mb-3">➕ 添加新事件</h3>
            <form action="{{ route('admin.autoact.store') }}" method="POST">
                @csrf
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4 max-w-4xl">
                    <div class="md:col-span-2">
                            <label class="{{ $adminListFilterLabelClass }}">事件文本
                            <span class="text-gray-400 font-normal">（{username} 将被替换为触发者用户名）</span></label>
                        <input type="text" name="text_body" required placeholder="例：🎉 恭喜【{username}】获得 100 经验値！"
                                class="w-full {{ $adminListFilterInputClass }} bg-white">
                    </div>
                    <div>
                            <label class="{{ $adminListFilterLabelClass }}">事件类型</label>
                        <select name="event_type"
                                class="w-full {{ $adminListFilterInputClass }} bg-white">
                            <option value="good">🟢 好运（奖励）</option>
                            <option value="bad">🔴 坏运（惩罚）</option>
                            <option value="neutral">🟣 中性（纯文字）</option>
                        </select>
                    </div>
                    <div class="flex gap-3">
                        <div class="flex-1">
                                <label class="{{ $adminListFilterLabelClass }}">经验变化</label>
                            <input type="number" name="exp_change" value="0"
                                    class="w-full {{ $adminListFilterInputClass }} bg-white">
                        </div>
                        <div class="flex-1">
                                <label class="{{ $adminListFilterLabelClass }}">金币变化</label>
                            <input type="number" name="jjb_change" value="0"
                                    class="w-full {{ $adminListFilterInputClass }} bg-white">
                        </div>
                    </div>
                </div>
                <div class="mt-4">
                        <button type="submit" class="{{ $adminListPrimaryButtonClass }}">
                        添加事件
                    </button>
                </div>
            </form>
        </div>

        {{-- 事件列表 --}}
            <div class="p-6">
                <h3 class="{{ $adminListSectionTitleClass }} mb-3">📋 现有事件列表（共 {{ $events->count() }} 条）</h3>
                <div class="{{ $adminListTableWrapClass }}">
                    <table class="{{ $adminListTableClass }}">
                        <thead class="{{ $adminListTableHeadRowClass }}">
                            <tr>
                                <th class="{{ $adminListTableHeadCellClass }} w-10">ID</th>
                                <th class="{{ $adminListTableHeadCellClass }}">事件文本</th>
                                <th class="{{ $adminListTableHeadCellClass }} w-20">类型</th>
                                <th class="{{ $adminListTableHeadCellClass }} w-16">经验</th>
                                <th class="{{ $adminListTableHeadCellClass }} w-16">金币</th>
                                <th class="{{ $adminListTableHeadCellClass }} w-16">状态</th>
                                <th class="{{ $adminListTableHeadCellClass }} w-32">操作</th>
                            </tr>
                        </thead>
                        <tbody class="{{ $adminListTableBodyClass }}">
                        @forelse ($events as $event)
                            <tr class="{{ $adminListTableRowClass }} {{ !$event->enabled ? 'opacity-40' : '' }}">
                                <td class="p-3 {{ $adminListSecondaryTextClass }}">{{ $event->id }}</td>
                                <td class="p-3">
                                    <span class="{{ $adminListBodyTextClass }}">{{ Str::limit($event->text_body, 60) }}</span>
                                </td>
                                <td class="p-3">
                                    @if ($event->event_type === 'good')
                                            <span class="{{ $adminListBadgeBaseClass }} border-green-200 bg-green-100 text-green-700">好运</span>
                                    @elseif($event->event_type === 'bad')
                                            <span class="{{ $adminListBadgeBaseClass }} border-red-200 bg-red-100 text-red-700">坏运</span>
                                    @else
                                            <span class="{{ $adminListBadgeBaseClass }} border-purple-200 bg-purple-100 text-purple-700">中性</span>
                                    @endif
                                </td>
                                <td
                                    class="p-3 {{ $adminListNumericTextClass }} {{ $event->exp_change > 0 ? 'text-green-600' : ($event->exp_change < 0 ? 'text-red-600' : 'text-gray-400') }} font-bold">
                                    {{ $event->exp_change > 0 ? '+' : '' }}{{ $event->exp_change }}
                                </td>
                                <td
                                    class="p-3 {{ $adminListNumericTextClass }} {{ $event->jjb_change > 0 ? 'text-green-600' : ($event->jjb_change < 0 ? 'text-red-600' : 'text-gray-400') }} font-bold">
                                    {{ $event->jjb_change > 0 ? '+' : '' }}{{ $event->jjb_change }}
                                </td>
                                <td class="p-3">
                                    <button type="button"
                                        data-autoact-toggle-url="{{ route('admin.autoact.toggle', $event->id) }}"
                                            class="{{ $adminListBadgeBaseClass }} px-2 py-1 {{ $event->enabled ? 'border-green-200 bg-green-100 text-green-700' : 'border-gray-200 bg-gray-200 text-gray-500' }} cursor-pointer">
                                        {{ $event->enabled ? '启用' : '禁用' }}
                                    </button>
                                </td>
                                <td class="p-3">
                                    <form action="{{ route('admin.autoact.destroy', $event->id) }}" method="POST"
                                        class="inline" data-autoact-delete-confirm="确定要删除此事件吗？">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit"
                                                class="{{ $adminListActionButtonClass }} bg-red-50 text-red-600 hover:bg-red-100">删除</button>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" class="{{ $adminListEmptyClass }}">暂无事件，请添加。</td>
                            </tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
@endsection
