@extends('admin.layouts.app')

@section('title', '签到奖励管理')

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

    @php
        $signInRulePayload = $rules->mapWithKeys(
            fn($rule) => [
                (string) $rule->id => [
                    'id' => $rule->id,
                    'streak_days' => $rule->streak_days,
                    'gold_reward' => $rule->gold_reward,
                    'exp_reward' => $rule->exp_reward,
                    'charm_reward' => $rule->charm_reward,
                    'identity_badge_code' => $rule->identity_badge_code,
                    'identity_badge_name' => $rule->identity_badge_name,
                    'identity_badge_icon' => $rule->identity_badge_icon,
                    'identity_badge_color' => $rule->identity_badge_color,
                    'identity_duration_days' => $rule->identity_duration_days,
                    'sort_order' => $rule->sort_order,
                    'is_enabled' => (bool) $rule->is_enabled,
                    'update_url' => route('admin.sign-in-rules.update', $rule),
                ],
            ],
        );
    @endphp

    <script type="application/json" id="admin-sign-in-rules-data">@json($signInRulePayload)</script>

    <div class="{{ $adminListPageClass }}">
        <div class="{{ $adminListHeaderCardClass }}">
            <div class="flex items-center justify-between gap-4">
            <div>
                        <h2 class="{{ $adminListHeaderTitleClass }}">✅ 签到奖励管理</h2>
                        <p class="{{ $adminListHeaderSubtitleClass }}">按连续签到天数配置金币、经验、魅力和专属身份。</p>
                    </div>
            </div>
        </div>

        <div class="{{ $adminListCardClass }}">
            <div class="{{ $adminListTableWrapClass }}">
                <table class="{{ $adminListTableClass }}">
                    <thead class="{{ $adminListTableHeadRowClass }}">
                    <tr>
                        <th class="{{ $adminListTableHeadCellClass }}">连续天数</th>
                        <th class="{{ $adminListTableHeadCellClass }} text-center">金币</th>
                        <th class="{{ $adminListTableHeadCellClass }} text-center">经验</th>
                        <th class="{{ $adminListTableHeadCellClass }} text-center">魅力</th>
                        <th class="{{ $adminListTableHeadCellClass }}">身份</th>
                        <th class="{{ $adminListTableHeadCellClass }} text-center">有效期</th>
                        <th class="{{ $adminListTableHeadCellClass }} text-center">状态</th>
                        <th class="{{ $adminListTableHeadCellClass }} text-right">操作</th>
                    </tr>
                </thead>
                    <tbody class="{{ $adminListTableBodyClass }}">
                    @forelse ($rules as $rule)
                        <tr id="rule-row-{{ $rule->id }}" class="{{ $adminListTableRowClass }} {{ $rule->is_enabled ? '' : 'opacity-50' }}">
                            <td class="px-4 py-3 {{ $adminListPrimaryTextClass }}">第 {{ $rule->streak_days }} 天</td>
                            <td class="px-4 py-3 text-center text-amber-600 {{ $adminListNumericTextClass }}">+{{ $rule->gold_reward }}</td>
                            <td class="px-4 py-3 text-center text-emerald-600 {{ $adminListNumericTextClass }}">+{{ $rule->exp_reward }}</td>
                            <td class="px-4 py-3 text-center text-pink-600 {{ $adminListNumericTextClass }}">+{{ $rule->charm_reward }}</td>
                            <td class="px-4 py-3">
                                @if ($rule->identity_badge_name)
                                    <span class="{{ $adminListBadgeBaseClass }} px-2 py-1"
                                        style="color: {{ $rule->identity_badge_color ?: '#0f766e' }}; border-color: #99f6e4; background: #f0fdfa;">
                                        <span>{{ $rule->identity_badge_icon ?: '✅' }}</span>
                                        <span>{{ $rule->identity_badge_name }}</span>
                                    </span>
                                    <div class="mt-1 {{ $adminListSecondaryTextClass }}">{{ $rule->identity_badge_code }}</div>
                                @else
                                    <span class="{{ $adminListSecondaryTextClass }}">未配置身份</span>
                                @endif
                            </td>
                            <td class="px-4 py-3 text-center {{ $adminListBodyTextClass }}">
                                {{ $rule->identity_duration_days > 0 ? $rule->identity_duration_days . ' 天' : '永久' }}
                            </td>
                            <td class="px-4 py-3 text-center">
                                <button type="button" data-sign-in-rule-toggle-url="{{ route('admin.sign-in-rules.toggle', $rule) }}"
                                    id="toggle-rule-{{ $rule->id }}"
                                    class="{{ $adminListBadgeBaseClass }} px-2 py-1 transition {{ $rule->is_enabled ? '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' }}">
                                    {{ $rule->is_enabled ? '启用' : '停用' }}
                                </button>
                            </td>
                            <td class="px-4 py-3 text-right">
                                <button type="button" data-sign-in-rule-edit-id="{{ $rule->id }}"
                                    class="{{ $adminListActionButtonClass }} bg-indigo-50 text-indigo-700 hover:bg-indigo-100 mr-1">
                                    编辑
                                </button>
                                <form action="{{ route('admin.sign-in-rules.destroy', $rule) }}" method="POST"
                                    class="inline" data-sign-in-rule-delete-confirm="确定删除第 {{ $rule->streak_days }} 天签到规则？">
                                    @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="8" class="{{ $adminListEmptyClass }}">暂无签到奖励规则。</td>
                        </tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        <div class="{{ $adminListCardClass }}">
            <div class="{{ $adminListSectionHeadClass }}">
                <h3 class="{{ $adminListSectionTitleClass }}">➕ 新增签到档位</h3>
            </div>
            <form action="{{ route('admin.sign-in-rules.store') }}" method="POST" class="p-5">
                @csrf
                @include('admin.sign-in-rules.partials.form-fields', ['rule' => null])
                <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_enabled" value="1" checked class="rounded">
                        立即启用
                    </label>
                </div>
            </form>
        </div>
    </div>

        <div id="edit-rule-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-3xl 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-sign-in-rule-close class="text-gray-400 hover:text-gray-600 text-xl">✕</button>
            </div>
            <form id="edit-rule-form" method="POST" class="p-5">
                @csrf
                @method('PUT')
                @include('admin.sign-in-rules.partials.form-fields', ['rule' => null, 'prefix' => 'edit-'])
                <div class="mt-5 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_enabled" id="edit-is-enabled" value="1" class="rounded">
                        启用此规则
                    </label>
                    <button type="button" data-sign-in-rule-close
                        class="{{ $adminListSecondaryButtonClass }}">
                        取消
                    </button>
                </div>
            </form>
        </div>
    </div>
@endsection
