{{--
    文件功能：后台会员购买日志页面
    供后台管理员查询全站 VIP 购买订单，查看支付状态与会员开通情况
--}}
@extends('admin.layouts.app')

@section('title', '会员购买日志')

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

    <div class="{{ $adminListPageClass }}">
        <div class="{{ $adminListFilterCardClass }}">
            <div class="{{ $adminListFilterInnerClass }}">
                <form action="{{ route('admin.vip-payment-logs.index') }}" method="GET"
                    class="{{ $adminListFilterFormClass }}">
                    <div class="flex-1 min-w-[140px]">
                        <label class="{{ $adminListFilterLabelClass }}">用户名</label>
                <input type="text" name="username" value="{{ request('username') }}"
                    class="w-full {{ $adminListFilterInputClass }}"
                    placeholder="支持模糊搜索">
                    </div>

                    <div class="flex-1 min-w-[140px]">
                        <label class="{{ $adminListFilterLabelClass }}">订单号/流水号</label>
                <input type="text" name="order_no" value="{{ request('order_no') }}"
                    class="w-full {{ $adminListFilterInputClass }}"
                    placeholder="本地单号 / 平台单号 / 第三方单号">
                    </div>

                    <div class="flex-1 min-w-[130px]">
                        <label class="{{ $adminListFilterLabelClass }}">订单状态</label>
                        <select name="status" class="w-full {{ $adminListFilterInputClass }}">
                            <option value="">全部状态</option>
                            @foreach ($statusOptions as $value => $label)
                                <option value="{{ $value }}" {{ request('status') === $value ? 'selected' : '' }}>{{ $label }}</option>
                            @endforeach
                        </select>
                    </div>

                    <div class="flex-none w-36">
                        <label class="{{ $adminListFilterLabelClass }}">开始日期</label>
                <input type="date" name="date_start" value="{{ request('date_start') }}"
                    class="w-full {{ $adminListFilterInputClass }}">
                    </div>

                    <div class="flex-none w-36">
                        <label class="{{ $adminListFilterLabelClass }}">结束日期</label>
                <input type="date" name="date_end" value="{{ request('date_end') }}"
                    class="w-full {{ $adminListFilterInputClass }}">
                    </div>

                    <div class="flex-none flex items-center gap-2">
                        <button type="submit" class="{{ $adminListPrimaryButtonClass }}">查询</button>
                        <a href="{{ route('admin.vip-payment-logs.index') }}"
                            class="{{ $adminListSecondaryButtonClass }}">重置</a>
                    </div>
                </form>
            </div>
        </div>

        <div class="{{ $adminListCardClass }}">
            <div class="{{ $adminListTableWrapClass }}">
                <table class="{{ $adminListTableClass }} min-w-[1200px] text-left">
                    <thead class="{{ $adminListTableHeadRowClass }}">
                        <tr>
                            <th class="{{ $adminListTableHeadCellClass }} whitespace-nowrap">订单ID</th>
                            <th class="{{ $adminListTableHeadCellClass }} whitespace-nowrap">用户</th>
                            <th class="{{ $adminListTableHeadCellClass }} whitespace-nowrap">会员等级</th>
                            <th class="{{ $adminListTableHeadCellClass }} whitespace-nowrap">本地订单号</th>
                            <th class="{{ $adminListTableHeadCellClass }} whitespace-nowrap">平台支付单号</th>
                            <th class="{{ $adminListTableHeadCellClass }} whitespace-nowrap">支付金额</th>
                            <th class="{{ $adminListTableHeadCellClass }} whitespace-nowrap">状态</th>
                            <th class="{{ $adminListTableHeadCellClass }} whitespace-nowrap">支付时间</th>
                            <th class="{{ $adminListTableHeadCellClass }} whitespace-nowrap">开通时间</th>
                            <th class="{{ $adminListTableHeadCellClass }} whitespace-nowrap text-right">创建时间</th>
                        </tr>
                    </thead>
                    <tbody class="{{ $adminListTableBodyClass }}">
                    @forelse ($logs as $log)
                        @php
                            $statusClass = match ($log->status) {
                                'paid' => 'border-emerald-200 bg-emerald-100 text-emerald-700',
                                'pending' => 'border-amber-200 bg-amber-100 text-amber-700',
                                'closed' => 'border-gray-200 bg-gray-100 text-gray-500',
                                'failed' => 'border-rose-200 bg-rose-100 text-rose-700',
                                default => 'border-slate-200 bg-slate-100 text-slate-700',
                            };
                        @endphp
                        <tr class="{{ $adminListTableRowClass }} cursor-default">
                            <td class="px-4 py-4 {{ $adminListSecondaryTextClass }}">#{{ $log->id }}</td>
                            <td class="px-4 py-4 whitespace-nowrap">
                                <div class="{{ $adminListPrimaryTextClass }} whitespace-nowrap">{{ $log->user?->username ?? '未知用户' }}</div>
                            </td>
                            <td class="px-4 py-4 whitespace-nowrap">
                                <span style="color: {{ $log->vipLevel?->color ?: '#111827' }}">{{ $log->vipLevel?->icon ?: '👑' }}</span>
                                <span class="{{ $adminListPrimaryTextClass }} ml-1">{{ $log->vip_name }}</span>
                            </td>
                            <td class="px-4 py-4 {{ $adminListNumericTextClass }} text-xs text-gray-600 whitespace-nowrap">{{ $log->order_no }}</td>
                            <td class="px-4 py-4 {{ $adminListNumericTextClass }} text-xs text-gray-600 whitespace-nowrap">{{ $log->payment_order_no ?: '-' }}</td>
                            <td class="px-4 py-4 whitespace-nowrap font-bold text-rose-600">¥{{ number_format((float) $log->amount, 2) }}</td>
                            <td class="px-4 py-4 whitespace-nowrap">
                                <span class="{{ $adminListBadgeBaseClass }} {{ $statusClass }}">{{ $statusOptions[$log->status] ?? $log->status }}</span>
                            </td>
                            <td class="px-4 py-4 {{ $adminListBodyTextClass }} whitespace-nowrap">{{ $log->paid_at?->format('Y-m-d H:i:s') ?? '未支付' }}</td>
                            <td class="px-4 py-4 {{ $adminListBodyTextClass }} whitespace-nowrap">{{ $log->opened_vip_at?->format('Y-m-d H:i:s') ?? '未开通' }}</td>
                            <td class="px-4 py-4 {{ $adminListBodyTextClass }} text-right whitespace-nowrap">{{ $log->created_at->format('Y-m-d H:i:s') }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="10" class="{{ $adminListEmptyClass }}">📭 暂无会员购买记录</td>
                        </tr>
                    @endforelse
                    </tbody>
                </table>
            </div>

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