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

@extends('admin.layouts.app')

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

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

    <div class="{{ $adminListPageClass }}">
        <div class="{{ $adminListHeaderCardClass }}">
            <div class="flex flex-col gap-4 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="{{ $adminListHeaderTitleClass }}" style="color: {{ $vip->color }}">{{ $vip->name }} 会员列表</h2>
                            <p class="{{ $adminListHeaderSubtitleClass }}">查看该等级下的会员用户名、当前状态与会员到期日期</p>
                    </div>
                </div>
                <div class="mt-4 flex flex-wrap gap-3 text-sm">
                        <span class="{{ $adminListBadgeBaseClass }} border-indigo-200 bg-indigo-50 px-3 py-1 text-indigo-600">
                        总分配人数：{{ $totalAssignedCount }}
                    </span>
                        <span class="{{ $adminListBadgeBaseClass }} border-emerald-200 bg-emerald-50 px-3 py-1 text-emerald-600">
                        当前有效会员：{{ $activeCount }}
                    </span>
                        <span class="{{ $adminListBadgeBaseClass }} border-amber-200 bg-amber-50 px-3 py-1 text-amber-600">
                        等级时长：{{ $vip->duration_days ?: '永久' }}
                    </span>
                </div>
            </div>

                <div class="flex gap-3">
                <a href="{{ route('admin.vip.index') }}"
                        class="{{ $adminListSecondaryButtonClass }} inline-flex items-center justify-center">
                    返回等级列表
                </a>
                </div>
            </div>
        </div>

        <div class="{{ $adminListFilterCardClass }}">
            <div class="{{ $adminListFilterInnerClass }}">
                <form action="{{ route('admin.vip.members', $vip->id) }}" method="GET" class="{{ $adminListFilterFormClass }}">
                <div class="min-w-[180px] flex-1">
                        <label class="{{ $adminListFilterLabelClass }}">用户名</label>
                    <input type="text" name="keyword" value="{{ request('keyword') }}"
                        class="w-full {{ $adminListFilterInputClass }}"
                        placeholder="支持模糊搜索用户名">
                </div>
                <div class="w-44">
                        <label class="{{ $adminListFilterLabelClass }}">会员状态</label>
                        <select name="status" class="w-full {{ $adminListFilterInputClass }}">
                        <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="{{ $adminListPrimaryButtonClass }}">
                        查询
                    </button>
                    <a href="{{ route('admin.vip.members', $vip->id) }}"
                            class="{{ $adminListSecondaryButtonClass }}">
                        重置
                    </a>
                    </div>
                </div>
            </form>
        </div>
        </div>

        <div class="{{ $adminListCardClass }}">
            <div class="{{ $adminListTableWrapClass }}">
                <table class="{{ $adminListTableClass }} min-w-full text-left">
                    <thead class="{{ $adminListTableHeadRowClass }}">
                        <tr>
                            <th class="{{ $adminListTableHeadCellClass }}">用户ID</th>
                            <th class="{{ $adminListTableHeadCellClass }}">用户名</th>
                            <th class="{{ $adminListTableHeadCellClass }}">性别</th>
                            <th class="{{ $adminListTableHeadCellClass }}">会员状态</th>
                            <th class="{{ $adminListTableHeadCellClass }}">到期日期</th>
                            <th class="{{ $adminListTableHeadCellClass }}">注册时间</th>
                        </tr>
                    </thead>
                    <tbody class="{{ $adminListTableBodyClass }}">
                        @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' => 'border-purple-200 bg-purple-50 text-purple-600',
                                    'active' => 'border-emerald-200 bg-emerald-50 text-emerald-600',
                                    default => 'border-rose-200 bg-rose-50 text-rose-600',
                                };
                                $sexLabel = match ((int) $member->sex) {
                                    1 => '男',
                                    2 => '女',
                                    default => '未知',
                                };
                            @endphp
                            <tr class="{{ $adminListTableRowClass }}">
                                <td class="px-4 py-4 {{ $adminListNumericTextClass }} text-xs text-gray-500">#{{ $member->id }}</td>
                                <td class="px-4 py-4">
                                    <div class="{{ $adminListPrimaryTextClass }}">{{ $member->username }}</div>
                                </td>
                                <td class="px-4 py-4 {{ $adminListBodyTextClass }}">{{ $sexLabel }}</td>
                                <td class="px-4 py-4">
                                    <span class="{{ $adminListBadgeBaseClass }} {{ $statusClass }}">
                                        {{ $statusLabel }}
                                    </span>
                                </td>
                                <td class="px-4 py-4 {{ $adminListBodyTextClass }}">
                                    @if ($member->hy_time)
                                        {{ $member->hy_time->format('Y-m-d H:i') }}
                                    @else
                                        永久
                                    @endif
                                </td>
                                <td class="px-4 py-4 {{ $adminListBodyTextClass }}">{{ $member->created_at?->format('Y-m-d H:i') }}</td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="{{ $adminListEmptyClass }}">
                                    当前条件下暂无该等级会员记录
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            @if ($members->hasPages())
                <div class="{{ $adminListPaginationClass }}">
                    {{ $members->links() }}
                </div>
            @endif
        </div>
    </div>
@endsection
