{{--
    文件功能：后台 VIP 等级会员列表页面
    用于查看某个会员等级下有哪些用户，以及对应会员到期日期和当前状态
--}}

@extends('admin.layouts.app')

@section('title', $vip->name . ' 会员列表')

@section('content')
    <div class="space-y-6">
        <div class="flex flex-col gap-4 rounded-2xl border border-gray-200 bg-white p-6 shadow-sm lg:flex-row lg:items-center lg:justify-between">
            <div>
                <div class="flex items-center gap-3">
                    <span class="text-3xl">{{ $vip->icon }}</span>
                    <div>
                        <h2 class="text-xl font-bold" style="color: {{ $vip->color }}">{{ $vip->name }} 会员列表</h2>
                        <p class="mt-1 text-sm text-gray-500">查看该等级下的会员用户名、当前状态与会员到期日期</p>
                    </div>
                </div>
                <div class="mt-4 flex flex-wrap gap-3 text-sm">
                    <span class="inline-flex items-center rounded-full bg-indigo-50 px-3 py-1 font-semibold text-indigo-600">
                        总分配人数：{{ $totalAssignedCount }}
                    </span>
                    <span class="inline-flex items-center rounded-full bg-emerald-50 px-3 py-1 font-semibold text-emerald-600">
                        当前有效会员：{{ $activeCount }}
                    </span>
                    <span class="inline-flex items-center rounded-full bg-amber-50 px-3 py-1 font-semibold text-amber-600">
                        等级时长：{{ $vip->duration_days ?: '永久' }}
                    </span>
                </div>
            </div>

            <div class="flex gap-3">
                <a href="{{ route('admin.vip.index') }}"
                    class="inline-flex items-center justify-center rounded-lg border border-gray-300 px-4 py-2 text-sm font-semibold text-gray-600 transition hover:bg-gray-50">
                    返回等级列表
                </a>
            </div>
        </div>

        <div class="rounded-2xl border border-gray-200 bg-white p-5 shadow-sm">
            <form action="{{ route('admin.vip.members', $vip->id) }}" method="GET" class="flex flex-wrap items-end gap-4">
                <div class="min-w-[180px] flex-1">
                    <label class="mb-1.5 block text-xs font-semibold uppercase tracking-wider text-gray-500">用户名</label>
                    <input type="text" name="keyword" value="{{ request('keyword') }}"
                        class="w-full rounded-lg border-gray-300 px-3 py-2 text-sm shadow-sm focus:border-indigo-500 focus:ring-indigo-500"
                        placeholder="支持模糊搜索用户名">
                </div>
                <div class="w-44">
                    <label class="mb-1.5 block text-xs font-semibold uppercase tracking-wider text-gray-500">会员状态</label>
                    <select name="status"
                        class="w-full rounded-lg border-gray-300 px-3 py-2 text-sm shadow-sm focus:border-indigo-500 focus:ring-indigo-500">
                        <option value="">全部状态</option>
                        <option value="active" @selected(request('status') === 'active')>当前有效</option>
                        <option value="expired" @selected(request('status') === 'expired')>已过期</option>
                    </select>
                </div>
                <div class="flex gap-2">
                    <button type="submit"
                        class="rounded-lg bg-indigo-600 px-5 py-2 text-sm font-semibold text-white transition hover:bg-indigo-700">
                        查询
                    </button>
                    <a href="{{ route('admin.vip.members', $vip->id) }}"
                        class="rounded-lg border border-gray-300 px-4 py-2 text-sm font-semibold text-gray-600 transition hover:bg-gray-50">
                        重置
                    </a>
                </div>
            </form>
        </div>

        <div class="overflow-hidden rounded-2xl border border-gray-200 bg-white shadow-sm">
            <div class="overflow-x-auto">
                <table class="min-w-full text-left">
                    <thead class="bg-gray-50 text-xs font-semibold uppercase tracking-wider text-gray-500">
                        <tr>
                            <th class="px-6 py-4">用户ID</th>
                            <th class="px-6 py-4">用户名</th>
                            <th class="px-6 py-4">性别</th>
                            <th class="px-6 py-4">会员状态</th>
                            <th class="px-6 py-4">到期日期</th>
                            <th class="px-6 py-4">注册时间</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100 text-sm text-gray-700">
                        @forelse ($members as $member)
                            @php
                                $status = $member->hy_time === null ? 'permanent' : ($member->hy_time->isFuture() ? 'active' : 'expired');
                                $statusLabel = match ($status) {
                                    'permanent' => '永久会员',
                                    'active' => '当前有效',
                                    default => '已过期',
                                };
                                $statusClass = match ($status) {
                                    'permanent' => 'bg-purple-50 text-purple-600',
                                    'active' => 'bg-emerald-50 text-emerald-600',
                                    default => 'bg-rose-50 text-rose-600',
                                };
                                $sexLabel = match ((int) $member->sex) {
                                    1 => '男',
                                    2 => '女',
                                    default => '未知',
                                };
                            @endphp
                            <tr class="transition hover:bg-gray-50">
                                <td class="px-6 py-4 font-mono text-xs text-gray-500">#{{ $member->id }}</td>
                                <td class="px-6 py-4">
                                    <div class="font-semibold text-gray-800">{{ $member->username }}</div>
                                </td>
                                <td class="px-6 py-4 text-gray-500">{{ $sexLabel }}</td>
                                <td class="px-6 py-4">
                                    <span class="inline-flex rounded-full px-2.5 py-1 text-xs font-semibold {{ $statusClass }}">
                                        {{ $statusLabel }}
                                    </span>
                                </td>
                                <td class="px-6 py-4 text-gray-600">
                                    @if ($member->hy_time)
                                        {{ $member->hy_time->format('Y-m-d H:i') }}
                                    @else
                                        永久
                                    @endif
                                </td>
                                <td class="px-6 py-4 text-gray-500">{{ $member->created_at?->format('Y-m-d H:i') }}</td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="px-6 py-12 text-center text-sm text-gray-400">
                                    当前条件下暂无该等级会员记录
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            @if ($members->hasPages())
                <div class="border-t border-gray-200 px-6 py-4">
                    {{ $members->links() }}
                </div>
            @endif
        </div>
    </div>
@endsection
