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

    @author   ChatRoom Laravel
    @version  1.0.0
--}}

@extends('admin.layouts.app')

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

@section('content')

    {{-- ── 页面标题 ── --}}
    <div class="mb-6">
        <h2 class="text-xl font-bold text-gray-800">📝 我的履职记录</h2>
        <p class="text-sm text-gray-500 mt-1">
            {{ $user->username }} 的全部职务操作历史，共
            <span class="font-bold 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 style="display:grid; grid-template-columns:repeat(6,1fr); gap:12px; margin-bottom:24px;">
        @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 {{ $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>

    {{-- ── 筛选栏 ── --}}
    <form method="GET" class="bg-white rounded-xl shadow-sm border p-4 mb-4 flex flex-wrap gap-3 items-end">
        <div>
            <label class="block text-xs text-gray-500 mb-1">操作类型</label>
            <select name="type"
                class="border rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-indigo-300 outline-none">
                <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="block text-xs text-gray-500 mb-1">开始日期</label>
            <input type="date" name="date_from" value="{{ request('date_from') }}"
                class="border rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-indigo-300 outline-none">
        </div>
        <div>
            <label class="block text-xs text-gray-500 mb-1">结束日期</label>
            <input type="date" name="date_to" value="{{ request('date_to') }}"
                class="border rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-indigo-300 outline-none">
        </div>
        <button type="submit"
            class="px-5 py-2 bg-indigo-600 text-white text-sm font-bold rounded-lg hover:bg-indigo-700 transition">
            🔍 查询
        </button>
        @if (request()->hasAny(['type', 'date_from', 'date_to']))
            <a href="{{ route('admin.appointments.my-duty-logs') }}"
                class="px-5 py-2 bg-gray-100 text-gray-600 text-sm rounded-lg hover:bg-gray-200 transition">
                ✖ 清除筛选
            </a>
        @endif
    </form>

    {{-- ── 记录表格 ── --}}
    @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="bg-white rounded-xl shadow-sm border overflow-hidden">
        <table class="w-full text-sm">
            <thead class="bg-gray-50 text-gray-600 text-xs">
                <tr>
                    <th class="px-4 py-3 text-left">操作时间</th>
                    <th class="px-4 py-3 text-center">操作类型</th>
                    <th class="px-4 py-3 text-left">操作对象</th>
                    <th class="px-4 py-3 text-left">所属职务</th>
                    <th class="px-4 py-3 text-center">金币金额</th>
                    <th class="px-4 py-3 text-left">备注说明</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-100">
                @forelse ($logs as $log)
                    @php $colorClass = $actionColors[$log->action_type] ?? 'bg-gray-100 text-gray-600'; @endphp
                    <tr class="hover:bg-gray-50 transition">
                        <td class="px-4 py-3 text-gray-400 text-xs whitespace-nowrap">
                            {{ $log->created_at->format('Y-m-d H:i') }}
                        </td>
                        <td class="px-4 py-3 text-center">
                            <span class="text-xs px-2 py-0.5 rounded-full font-bold {{ $colorClass }}">
                                {{ $log->action_label }}
                            </span>
                        </td>
                        <td class="px-4 py-3 font-bold text-gray-700">
                            {{ $log->targetUser?->username ?? '—' }}
                        </td>
                        <td class="px-4 py-3 text-gray-500 text-xs">
                            @if ($log->userPosition?->position)
                                <span class="text-gray-400">{{ $log->userPosition->position->department?->name }}</span>
                                @if ($log->userPosition->position->department)
                                    <span class="text-gray-300 mx-1">·</span>
                                @endif
                                {{ $log->userPosition->position->name }}
                            @else
                                <span class="text-gray-300">超级管理员</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center">
                            @if ($log->amount)
                                <span class="text-yellow-600 font-bold">+{{ number_format($log->amount) }}</span>
                                <span class="text-gray-400 text-xs">金币</span>
                            @else
                                <span class="text-gray-300">—</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-gray-400 text-xs">{{ $log->remark ?: '—' }}</td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="6" class="px-4 py-16 text-center text-gray-400">
                            <div class="text-4xl mb-3">📋</div>
                            <div>暂无履职操作记录</div>
                        </td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <div class="mt-4">{{ $logs->links() }}</div>

@endsection
