{{--
    文件功能：我的履职记录页面
    展示当前登录者自己的全部权限操作记录（奖励、踢人、禁言、任命等）
    支持按操作类型和日期范围筛选，顶部显示各类操作汇总统计卡片

    @author   ChatRoom Laravel
    @version  1.0.0
--}}

@extends('admin.layouts.app')

@section('title', '我的履职记录')

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

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

    {{-- ── 页面标题 ── --}}
    <div class="{{ $adminListHeaderCardClass }}">
        <h2 class="{{ $adminListHeaderTitleClass }}">📝 我的履职记录</h2>
        <p class="{{ $adminListHeaderSubtitleClass }}">
            {{ $user->username }} 的全部职务操作历史，共
            <span class="font-semibold text-gray-700">{{ $logs->total() }}</span> 条记录
        </p>
    </div>

    {{-- ── 汇总统计卡片 ── --}}
    @php
        $statCards = [
            'reward' => ['label' => '奖励发放', 'icon' => '💰', 'color' => 'yellow'],
            'kick' => ['label' => '踢出操作', 'icon' => '🚫', 'color' => 'red'],
            'mute' => ['label' => '禁言操作', 'icon' => '🔇', 'color' => 'purple'],
            'warn' => ['label' => '警告操作', 'icon' => '⚠️', 'color' => 'orange'],
            'appoint' => ['label' => '任命操作', 'icon' => '🎖️', 'color' => 'green'],
            'revoke' => ['label' => '撤职操作', 'icon' => '❌', 'color' => 'gray'],
        ];
        $colorMap = [
            'yellow' => [
                'bg' => 'bg-yellow-50',
                'border' => 'border-yellow-200',
                'text' => 'text-yellow-700',
                'badge' => 'bg-yellow-100 text-yellow-800',
            ],
            'red' => [
                'bg' => 'bg-red-50',
                'border' => 'border-red-200',
                'text' => 'text-red-700',
                'badge' => 'bg-red-100 text-red-800',
            ],
            'purple' => [
                'bg' => 'bg-purple-50',
                'border' => 'border-purple-200',
                'text' => 'text-purple-700',
                'badge' => 'bg-purple-100 text-purple-800',
            ],
            'orange' => [
                'bg' => 'bg-orange-50',
                'border' => 'border-orange-200',
                'text' => 'text-orange-700',
                'badge' => 'bg-orange-100 text-orange-800',
            ],
            'green' => [
                'bg' => 'bg-green-50',
                'border' => 'border-green-200',
                'text' => 'text-green-700',
                'badge' => 'bg-green-100 text-green-800',
            ],
            'gray' => [
                'bg' => 'bg-gray-50',
                'border' => 'border-gray-200',
                'text' => 'text-gray-700',
                'badge' => 'bg-gray-100 text-gray-800',
            ],
        ];
    @endphp

    <div class="grid gap-3 md:grid-cols-3 xl:grid-cols-6">
        @foreach ($statCards as $type => $card)
            @php
                $stat = $summary->get($type);
                $c = $colorMap[$card['color']];
            @endphp
            <a href="{{ request()->fullUrlWithQuery(['type' => $type, 'page' => 1]) }}"
                class="rounded-xl border shadow-sm {{ $c['bg'] }} {{ $c['border'] }} px-3 py-3 text-center hover:shadow-md transition
                       {{ request('type') === $type ? 'ring-2 ring-offset-1 ring-indigo-400 shadow-md' : '' }}">
                <div class="text-xl mb-0.5">{{ $card['icon'] }}</div>
                <div class="text-xs text-gray-500 mb-0.5 whitespace-nowrap">{{ $card['label'] }}</div>
                <div class="text-xl font-black {{ $c['text'] }}">{{ $stat?->total ?? 0 }}</div>
                @if ($type === 'reward' && ($stat?->amount_sum ?? 0) > 0)
                    <div class="text-xs {{ $c['text'] }} mt-0.5">{{ number_format($stat->amount_sum) }} 金币</div>
                @endif
            </a>
        @endforeach
    </div>

    {{-- ── 筛选栏 ── --}}
    <div class="{{ $adminListFilterCardClass }}">
        <div class="{{ $adminListFilterInnerClass }}">
    <form method="GET" class="{{ $adminListFilterFormClass }}">
        <div>
            <label class="{{ $adminListFilterLabelClass }}">操作类型</label>
            <select name="type"
                class="{{ $adminListFilterInputClass }}">
                <option value="">全部类型</option>
                @foreach ($statCards as $type => $card)
                    <option value="{{ $type }}" {{ request('type') === $type ? 'selected' : '' }}>
                        {{ $card['icon'] }} {{ $card['label'] }}
                    </option>
                @endforeach
            </select>
        </div>
        <div>
            <label class="{{ $adminListFilterLabelClass }}">开始日期</label>
            <input type="date" name="date_from" value="{{ request('date_from') }}"
                class="{{ $adminListFilterInputClass }}">
        </div>
        <div>
            <label class="{{ $adminListFilterLabelClass }}">结束日期</label>
            <input type="date" name="date_to" value="{{ request('date_to') }}"
                class="{{ $adminListFilterInputClass }}">
        </div>
        <button type="submit"
            class="{{ $adminListPrimaryButtonClass }}">
            🔍 查询
        </button>
        @if (request()->hasAny(['type', 'date_from', 'date_to']))
            <a href="{{ route('admin.appointments.my-duty-logs') }}"
                class="{{ $adminListSecondaryButtonClass }}">
                ✖ 清除筛选
            </a>
        @endif
    </form>
        </div>
    </div>

    {{-- ── 记录表格 ── --}}
    @php
        $actionColors = [
            'appoint' => 'bg-green-100 text-green-700',
            'revoke' => 'bg-red-100 text-red-700',
            'reward' => 'bg-yellow-100 text-yellow-700',
            'warn' => 'bg-orange-100 text-orange-700',
            'kick' => 'bg-red-100 text-red-700',
            'mute' => 'bg-purple-100 text-purple-700',
            'banip' => 'bg-gray-200 text-gray-700',
            'other' => 'bg-gray-100 text-gray-600',
        ];
    @endphp

    <div class="{{ $adminListCardClass }}">
        <div class="{{ $adminListSectionHeadClass }}">
            <h3 class="{{ $adminListSectionTitleClass }}">履职操作记录</h3>
            <p class="{{ $adminListSectionDescClass }}">可按操作类型和日期范围筛选当前登录者的历史履职行为。</p>
        </div>
        <div class="{{ $adminListTableWrapClass }}">
            <table class="{{ $adminListTableClass }} whitespace-nowrap">
                <thead>
                    <tr class="{{ $adminListTableHeadRowClass }}">
                        <th class="{{ $adminListTableHeadCellClass }}">操作时间</th>
                        <th class="{{ $adminListTableHeadCellClass }} text-center">操作类型</th>
                        <th class="{{ $adminListTableHeadCellClass }}">操作对象</th>
                        <th class="{{ $adminListTableHeadCellClass }}">所属职务</th>
                        <th class="{{ $adminListTableHeadCellClass }} text-center">金币金额</th>
                        <th class="{{ $adminListTableHeadCellClass }}">备注说明</th>
                    </tr>
                </thead>
                <tbody class="{{ $adminListTableBodyClass }}">
                    @forelse ($logs as $log)
                        @php $colorClass = $actionColors[$log->action_type] ?? 'bg-gray-100 text-gray-600'; @endphp
                        <tr class="{{ $adminListTableRowClass }}">
                            <td class="px-4 py-3 whitespace-nowrap {{ $adminListSecondaryTextClass }}">
                                {{ $log->created_at->format('Y-m-d H:i') }}
                            </td>
                            <td class="px-4 py-3 text-center">
                                <span class="inline-flex items-center rounded-full px-2 py-0.5 text-xs font-semibold {{ $colorClass }}">
                                    {{ $log->action_label }}
                                </span>
                            </td>
                            <td class="px-4 py-3 {{ $adminListPrimaryTextClass }}">
                                {{ $log->targetUser?->username ?? '—' }}
                            </td>
                            <td class="px-4 py-3 {{ $adminListSecondaryTextClass }}">
                                @if ($log->userPosition?->position)
                                    <span>{{ $log->userPosition->position->department?->name }}</span>
                                    @if ($log->userPosition->position->department)
                                        <span class="mx-1 text-gray-300">·</span>
                                    @endif
                                    <span class="text-gray-600">{{ $log->userPosition->position->name }}</span>
                                @else
                                    <span class="text-gray-300">超级管理员</span>
                                @endif
                            </td>
                            <td class="px-4 py-3 text-center">
                                @if ($log->amount)
                                    <span class="text-sm font-semibold text-yellow-600">+{{ number_format($log->amount) }}</span>
                                    <span class="{{ $adminListSecondaryTextClass }}">金币</span>
                                @else
                                    <span class="text-gray-300">—</span>
                                @endif
                            </td>
                            <td class="px-4 py-3 {{ $adminListSecondaryTextClass }}">{{ $log->remark ?: '—' }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="{{ $adminListEmptyClass }}">
                                <div class="mb-3 text-4xl">📋</div>
                                <div>暂无履职操作记录</div>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if ($logs->hasPages())
            <div class="{{ $adminListPaginationClass }}">{{ $logs->links() }}</div>
        @endif
    </div>

    </div>
@endsection
