@extends('layouts.admin')

@section('title', 'Japan Live Auctions & Sync – Admin CRM')
@section('page_title', 'Japan Live Auctions Management')

@section('content')
<div class="space-y-6">
    
    <!-- Top Action Bar & Sync Console -->
    <div class="bg-slate-900 border border-white/10 rounded-2xl p-5 flex flex-col md:flex-row items-start md:items-center justify-between gap-4 shadow-xl">
        <div>
            <div class="flex items-center gap-2">
                <span class="w-2.5 h-2.5 rounded-full bg-emerald-500 animate-pulse"></span>
                <h2 class="text-base font-bold text-white font-heading">Japanese Auction API Gateway</h2>
            </div>
            <p class="text-xs text-slate-400 mt-0.5">Automated synchronization with USS, TAA, CAA, and HAA auction feeds.</p>
        </div>

        <div class="flex items-center gap-3">
            <form method="POST" action="{{ route('admin.auctions.sync') }}">
                @csrf
                <button type="submit" class="px-4 py-2 rounded-xl bg-red-600 hover:bg-red-700 text-white font-bold text-xs shadow-lg shadow-red-900/30 transition flex items-center gap-2">
                    <i class="fa-solid fa-arrows-rotate"></i>
                    <span>Sync Auctions Now</span>
                </button>
            </form>
        </div>
    </div>

    <!-- Filters -->
    <div class="bg-slate-900 border border-white/10 rounded-2xl p-4 flex flex-col sm:flex-row items-center justify-between gap-4">
        <form method="GET" action="{{ route('admin.auctions.index') }}" class="w-full sm:w-auto flex items-center gap-2 flex-1">
            <div class="relative flex-1 sm:w-64">
                <i class="fa-solid fa-magnifying-glass absolute left-3 top-2.5 text-slate-400 text-xs"></i>
                <input type="text" name="search" value="{{ request('search') }}" placeholder="Search lot #, model, chassis..." class="w-full bg-slate-950 border border-white/10 rounded-xl pl-8 pr-3 py-1.5 text-white text-xs focus:border-red-500 focus:outline-none">
            </div>
            <select name="auction_house_id" class="bg-slate-950 border border-white/10 rounded-xl px-3 py-1.5 text-white text-xs">
                <option value="">All Auction Houses</option>
                @foreach($houses as $house)
                    <option value="{{ $house->id }}" {{ request('auction_house_id') == $house->id ? 'selected' : '' }}>{{ $house->name }}</option>
                @endforeach
            </select>
            <button type="submit" class="px-3 py-1.5 rounded-xl bg-white/10 hover:bg-white/20 text-white font-bold text-xs transition">
                Filter
            </button>
        </form>
    </div>

    <!-- Live Auction Lots Table -->
    <div class="bg-slate-900 border border-white/10 rounded-2xl overflow-hidden shadow-xl">
        <div class="overflow-x-auto">
            <table class="w-full text-left text-xs">
                <thead class="bg-slate-950 text-slate-400 uppercase tracking-wider text-[10px] border-b border-white/10">
                    <tr>
                        <th class="p-4">Lot # & Auction House</th>
                        <th class="p-4">Vehicle Details</th>
                        <th class="p-4">Grade & Mileage</th>
                        <th class="p-4">Starting Bid (JPY)</th>
                        <th class="p-4">Auction End Countdown</th>
                        <th class="p-4">Client Bids</th>
                        <th class="p-4 text-right">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-white/5 text-slate-300">
                    @forelse($vehicles as $item)
                        <tr class="hover:bg-white/5 transition">
                            <td class="p-4">
                                <span class="font-mono text-red-400 font-bold block text-sm">Lot #{{ $item->lot_number }}</span>
                                <span class="text-[10px] text-slate-400">{{ $item->auctionHouse->name ?? 'USS Tokyo' }}</span>
                            </td>
                            <td class="p-4">
                                <div class="flex items-center gap-3">
                                    <div class="w-12 h-10 rounded-lg overflow-hidden bg-slate-800 shrink-0">
                                        @if(!empty($item->images) && isset($item->images[0]))
                                            <img src="{{ $item->images[0] }}" alt="Car" class="w-full h-full object-cover">
                                        @else
                                            <div class="w-full h-full flex items-center justify-center text-slate-700"><i class="fa-solid fa-car"></i></div>
                                        @endif
                                    </div>
                                    <div>
                                        <p class="font-bold text-white">{{ $item->year ?? '' }} {{ $item->brand ?? '' }} {{ $item->model ?? '' }}</p>
                                        <span class="text-[10px] text-slate-500 font-mono">{{ $item->chassis_number ?? 'Pending' }}</span>
                                    </div>
                                </div>
                            </td>
                            <td class="p-4">
                                <span class="font-bold text-amber-400">Grade {{ $item->grade }}</span>
                                <span class="text-[10px] text-slate-400 block">{{ number_format($item->mileage ?? 0) }} km</span>
                            </td>
                            <td class="p-4 font-bold text-white text-sm">
                                ¥{{ number_format($item->starting_price_jpy ?? 0) }}
                            </td>
                            <td class="p-4">
                                <span class="font-mono text-slate-200 block text-[11px]">{{ $item->auction_ends_at ? $item->auction_ends_at->format('M d, h:i A') : 'Today' }}</span>
                                <span class="text-[10px] text-emerald-400">{{ $item->auction_ends_at ? $item->auction_ends_at->diffForHumans() : 'Live' }}</span>
                            </td>
                            <td class="p-4">
                                @php $bidsCount = $item->bids->count(); @endphp
                                @if($bidsCount > 0)
                                    <span class="px-2 py-0.5 rounded-full text-[10px] font-bold bg-red-600/20 text-red-400 border border-red-500/30">
                                        {{ $bidsCount }} bids placed
                                    </span>
                                @else
                                    <span class="text-slate-500 text-[10px]">No bids yet</span>
                                @endif
                            </td>
                            <td class="p-4 text-right">
                                <a href="{{ route('auction.show', $item->id) }}" target="_blank" class="px-3 py-1.5 rounded-lg bg-white/10 hover:bg-white/20 text-white font-bold transition">
                                    View Live Lot
                                </a>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="p-8 text-center text-slate-400">
                                No auction vehicles found. Click "Sync Auctions Now" to fetch live lots.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="p-4 border-t border-white/10">
            {{ $vehicles->links() }}
        </div>
    </div>

</div>
@endsection
