@extends('layouts.admin')
@section('title', 'Members — JAM Admin')
@section('page-title', 'Member Directory')

@section('content')
<div class="flex items-center justify-between mb-6">
    <div>
        <h2 class="text-2xl font-semibold" style="font-family: 'Playfair Display', serif;">Directory</h2>
        <p class="text-white/40 text-sm mt-1">{{ $members->total() }} {{ Str::plural('member', $members->total()) }} registered.</p>
    </div>
    <a href="{{ route('admin.members.create') }}"
       class="bg-[#D4B84A] hover:bg-[#E8D080] text-black text-xs font-semibold tracking-widest uppercase px-5 py-3 rounded-full transition-colors">
        + New Member
    </a>
</div>

@if(session('success'))
<div class="bg-green-900/20 border border-green-700/40 rounded-xl px-5 py-3 mb-5 text-green-400 text-sm">
    {{ session('success') }}
</div>
@endif

{{-- Filters --}}
<form method="GET" action="{{ route('admin.members.index') }}" class="flex items-center gap-3 mb-5">
    <input type="text" name="search" value="{{ request('search') }}"
           placeholder="Search name, email, membership no…"
           class="bg-[#1a1a1a] border border-[#2a2a2a] rounded-lg px-4 py-2.5 text-sm text-white placeholder-white/20 outline-none focus:border-[#D4B84A] transition-colors w-72">
    <select name="tier"
            class="bg-[#1a1a1a] border border-[#2a2a2a] rounded-lg px-3 py-2.5 text-sm text-white/60 outline-none focus:border-[#D4B84A] transition-colors">
        <option value="">All Tiers</option>
        @foreach($tiers as $tier)
        <option value="{{ $tier->slug }}" {{ request('tier') === $tier->slug ? 'selected' : '' }}>{{ $tier->name }}</option>
        @endforeach
    </select>
    <select name="status"
            class="bg-[#1a1a1a] border border-[#2a2a2a] rounded-lg px-3 py-2.5 text-sm text-white/60 outline-none focus:border-[#D4B84A] transition-colors">
        <option value="">All Status</option>
        <option value="active"    {{ request('status') === 'active'    ? 'selected' : '' }}>Active</option>
        <option value="suspended" {{ request('status') === 'suspended' ? 'selected' : '' }}>Suspended</option>
        <option value="inactive"  {{ request('status') === 'inactive'  ? 'selected' : '' }}>Inactive</option>
        <option value="lapsed"    {{ request('status') === 'lapsed'    ? 'selected' : '' }}>Lapsed</option>
    </select>
    <button type="submit"
            class="border border-[#2a2a2a] hover:border-[#D4B84A]/50 text-white/50 hover:text-white text-xs uppercase tracking-wide px-4 py-2.5 rounded-lg transition-colors">
        Filter
    </button>
    @if(request()->hasAny(['search','tier','status']))
    <a href="{{ route('admin.members.index') }}" class="text-white/30 hover:text-white text-xs transition-colors">Clear</a>
    @endif
</form>

{{-- Table --}}
<div class="bg-[#111111] border border-[#1e1e1e] rounded-xl overflow-hidden">
    <div class="divide-y divide-[#1e1e1e]">
        @forelse($members as $member)
        <div class="flex items-center justify-between px-6 py-4 hover:bg-white/[0.02] transition-colors">
            <div class="flex items-center gap-4 min-w-0">
                <div class="w-9 h-9 rounded-full bg-[#1e1e1e] flex items-center justify-center flex-shrink-0">
                    <span class="text-white/60 text-sm font-medium">{{ strtoupper(substr($member->first_name, 0, 1)) }}</span>
                </div>
                <div class="min-w-0">
                    <p class="text-white text-sm font-medium">{{ $member->full_name }}</p>
                    <p class="text-white/40 text-xs truncate">{{ $member->email }} · {{ $member->membership_number }}</p>
                </div>
            </div>
            <div class="flex items-center gap-8 flex-shrink-0 ml-4">
                <div class="text-right w-20">
                    <p class="text-white text-sm font-medium">{{ number_format($member->points_balance) }}</p>
                    <p class="text-white/30 text-xs">pts</p>
                </div>
                <span class="w-20 text-center px-2.5 py-1 rounded-full text-xs border
                    {{ $member->currentTier?->tier?->slug === 'limitless' ? 'border-[#D4B84A]/50 text-[#D4B84A]' : ($member->currentTier?->tier?->slug === 'indulged' ? 'border-blue-400/30 text-blue-300' : 'border-white/20 text-white/50') }}">
                    {{ $member->currentTier?->tier?->name ?? 'Invited' }}
                </span>
                <span class="w-20 text-center px-2.5 py-1 rounded-full text-xs font-medium
                    {{ $member->status === 'active' ? 'bg-green-900/40 text-green-400' : ($member->status === 'suspended' ? 'bg-orange-900/40 text-orange-400' : 'bg-red-900/40 text-red-400') }}">
                    {{ ucfirst($member->status) }}
                </span>
                <p class="text-white/30 text-xs w-24 text-right">{{ $member->created_at->format('d M Y') }}</p>
                <a href="{{ route('admin.members.show', $member) }}"
                   class="text-white/30 hover:text-[#D4B84A] transition-colors">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M9 5l7 7-7 7"/></svg>
                </a>
            </div>
        </div>
        @empty
        <div class="px-6 py-12 text-center text-white/30 text-sm">
            @if(request()->hasAny(['search','tier','status']))
            No members match your filters.
            @else
            No members yet.
            @endif
        </div>
        @endforelse
    </div>
    @if($members->hasPages())
    <div class="px-6 py-4 border-t border-[#1e1e1e]">{{ $members->links() }}</div>
    @endif
</div>
@endsection
