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

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

@section('content')
    <div class="bg-white p-5 rounded-xl border border-gray-100 shadow-sm mb-6">
        <form action="{{ route('admin.vip-payment-logs.index') }}" method="GET" class="flex flex-wrap items-end gap-4">
            <div class="flex-1 min-w-[140px]">
                <label class="block text-xs font-semibold text-gray-500 uppercase tracking-wider mb-1.5">用户名</label>
                <input type="text" name="username" value="{{ request('username') }}"
                    class="w-full border-gray-300 rounded-lg shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm px-3 py-2"
                    placeholder="支持模糊搜索">
            </div>

            <div class="flex-1 min-w-[140px]">
                <label class="block text-xs font-semibold text-gray-500 uppercase tracking-wider mb-1.5">订单号/流水号</label>
                <input type="text" name="order_no" value="{{ request('order_no') }}"
                    class="w-full border-gray-300 rounded-lg shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm px-3 py-2"
                    placeholder="本地单号 / 平台单号 / 第三方单号">
            </div>

            <div class="flex-1 min-w-[130px]">
                <label class="block text-xs font-semibold text-gray-500 uppercase tracking-wider mb-1.5">订单状态</label>
                <select name="status"
                    class="w-full border-gray-300 rounded-lg shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm px-3 py-2">
                    <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="block text-xs font-semibold text-gray-500 uppercase tracking-wider mb-1.5">开始日期</label>
                <input type="date" name="date_start" value="{{ request('date_start') }}"
                    class="w-full border-gray-300 rounded-lg shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm px-3 py-2">
            </div>

            <div class="flex-none w-36">
                <label class="block text-xs font-semibold text-gray-500 uppercase tracking-wider mb-1.5">结束日期</label>
                <input type="date" name="date_end" value="{{ request('date_end') }}"
                    class="w-full border-gray-300 rounded-lg shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm px-3 py-2">
            </div>

            <div class="flex-none flex items-center space-x-2">
                <button type="submit"
                    class="bg-indigo-600 hover:bg-indigo-700 text-white font-medium py-2 px-5 rounded-lg shadow-sm transition text-sm">查询</button>
                <a href="{{ route('admin.vip-payment-logs.index') }}"
                    class="bg-white border border-gray-300 hover:bg-gray-50 text-gray-700 font-medium py-2 px-4 rounded-lg shadow-sm transition text-sm">重置</a>
            </div>
        </form>
    </div>

    <div class="bg-white rounded shadow overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full text-left border-collapse min-w-[1200px]">
                <thead>
                    <tr class="bg-gray-50 text-gray-600 text-sm uppercase tracking-wider border-b border-gray-200">
                        <th class="px-6 py-4 font-semibold whitespace-nowrap">订单ID</th>
                        <th class="px-6 py-4 font-semibold whitespace-nowrap">用户</th>
                        <th class="px-6 py-4 font-semibold whitespace-nowrap">会员等级</th>
                        <th class="px-6 py-4 font-semibold whitespace-nowrap">本地订单号</th>
                        <th class="px-6 py-4 font-semibold whitespace-nowrap">平台支付单号</th>
                        <th class="px-6 py-4 font-semibold whitespace-nowrap">支付金额</th>
                        <th class="px-6 py-4 font-semibold whitespace-nowrap">状态</th>
                        <th class="px-6 py-4 font-semibold whitespace-nowrap">支付时间</th>
                        <th class="px-6 py-4 font-semibold whitespace-nowrap">开通时间</th>
                        <th class="px-6 py-4 font-semibold whitespace-nowrap text-right">创建时间</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse ($logs as $log)
                        @php
                            $statusClass = match ($log->status) {
                                'paid' => 'bg-emerald-100 text-emerald-700',
                                'pending' => 'bg-amber-100 text-amber-700',
                                'closed' => 'bg-gray-100 text-gray-500',
                                'failed' => 'bg-rose-100 text-rose-700',
                                default => 'bg-slate-100 text-slate-700',
                            };
                        @endphp
                        <tr class="hover:bg-gray-50 transition cursor-default">
                            <td class="px-6 py-4 text-sm text-gray-500">#{{ $log->id }}</td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="font-bold text-gray-800 whitespace-nowrap">{{ $log->user?->username ?? '未知用户' }}</div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <span style="color: {{ $log->vipLevel?->color ?: '#111827' }}">{{ $log->vipLevel?->icon ?: '👑' }}</span>
                                <span class="font-bold text-gray-800 ml-1">{{ $log->vip_name }}</span>
                            </td>
                            <td class="px-6 py-4 font-mono text-xs text-gray-600 whitespace-nowrap">{{ $log->order_no }}</td>
                            <td class="px-6 py-4 font-mono text-xs text-gray-600 whitespace-nowrap">{{ $log->payment_order_no ?: '-' }}</td>
                            <td class="px-6 py-4 font-bold text-rose-600 whitespace-nowrap">¥{{ number_format((float) $log->amount, 2) }}</td>
                            <td class="px-6 py-4 whitespace-nowrap"><span class="inline-flex px-2.5 py-1 rounded-full text-xs font-bold {{ $statusClass }}">{{ $statusOptions[$log->status] ?? $log->status }}</span></td>
                            <td class="px-6 py-4 text-sm text-gray-500 whitespace-nowrap">{{ $log->paid_at?->format('Y-m-d H:i:s') ?? '未支付' }}</td>
                            <td class="px-6 py-4 text-sm text-gray-500 whitespace-nowrap">{{ $log->opened_vip_at?->format('Y-m-d H:i:s') ?? '未开通' }}</td>
                            <td class="px-6 py-4 text-sm text-gray-500 text-right whitespace-nowrap">{{ $log->created_at->format('Y-m-d H:i:s') }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="10" class="px-6 py-8 text-center text-gray-500">📭 暂无会员购买记录</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

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