@extends('layouts.epelcon')
@section('title', 'Vendor Marketplace')

@section('content')
<div x-data="{ add: false, edit: null }">
    <div class="mb-5 flex items-center justify-between">
        <div>
            <h1 class="text-xl font-extrabold text-slate-900 lg:text-2xl">Vendor marketplace</h1>
            <p class="text-sm text-slate-500">{{ $summary['approved'] }} partners · {{ $summary['pending'] }} pending applications · {{ $summary['hotels'] }} partner hotels</p>
        </div>
        <button @click="add = true" class="rounded-xl bg-indigo-600 px-4 py-2.5 text-sm font-bold text-white shadow-lg shadow-indigo-600/25 hover:bg-indigo-700">+ Add vendor</button>
    </div>

    <div class="no-scrollbar mb-4 flex gap-2 overflow-x-auto">
        @foreach (['' => 'All', 'approved' => 'Approved', 'pending' => 'Pending', 'suspended' => 'Suspended'] as $value => $label)
            <a href="{{ route('epelcon.vendors.index', array_filter(['status' => $value])) }}"
               class="shrink-0 rounded-full px-4 py-2 text-xs font-bold {{ request('status', '') === $value ? 'bg-slate-900 text-white' : 'bg-white text-slate-600 ring-1 ring-slate-200' }}">{{ $label }}</a>
        @endforeach
    </div>

    <div class="grid gap-3 sm:grid-cols-2 xl:grid-cols-3">
        @forelse ($vendors as $vendor)
            <div class="rounded-2xl bg-white p-5 shadow-sm ring-1 ring-slate-200">
                <div class="flex items-start justify-between">
                    <div class="flex items-center gap-3">
                        <div class="flex h-11 w-11 items-center justify-center rounded-xl bg-gradient-to-br from-sky-500 to-indigo-600 text-lg text-white">🏨</div>
                        <div>
                            <p class="font-bold text-slate-900">{{ $vendor->name }}</p>
                            <p class="text-xs text-slate-500">{{ $vendor->city }}{{ $vendor->country ? ', ' . $vendor->country : '' }} · {{ ucfirst($vendor->type) }}</p>
                        </div>
                    </div>
                    @php $badge = ['approved' => 'emerald', 'pending' => 'amber', 'suspended' => 'rose'][$vendor->status]; @endphp
                    <span class="rounded-full bg-{{ $badge }}-100 px-2.5 py-1 text-[10px] font-bold uppercase text-{{ $badge }}-700">{{ $vendor->status }}</span>
                </div>
                <div class="mt-3 flex gap-4 text-xs text-slate-500">
                    <span>★ {{ $vendor->rating }}</span>
                    <span>🛏 {{ $vendor->rooms_count }} rooms</span>
                    <span>💼 {{ $vendor->commission_percent + 0 }}% commission</span>
                </div>
                <div class="mt-3 flex gap-1.5">
                    @if ($vendor->status === 'pending')
                        <form method="POST" action="{{ route('epelcon.vendors.status', $vendor) }}">@csrf<input type="hidden" name="status" value="approved">
                            <button class="rounded-lg bg-emerald-600 px-3 py-1.5 text-xs font-bold text-white hover:bg-emerald-700">Approve</button></form>
                    @elseif ($vendor->status === 'approved')
                        <form method="POST" action="{{ route('epelcon.vendors.status', $vendor) }}">@csrf<input type="hidden" name="status" value="suspended">
                            <button class="rounded-lg bg-rose-50 px-3 py-1.5 text-xs font-bold text-rose-600 hover:bg-rose-100">Suspend</button></form>
                    @else
                        <form method="POST" action="{{ route('epelcon.vendors.status', $vendor) }}">@csrf<input type="hidden" name="status" value="approved">
                            <button class="rounded-lg bg-emerald-50 px-3 py-1.5 text-xs font-bold text-emerald-700 hover:bg-emerald-100">Reinstate</button></form>
                    @endif
                    <button @click='edit = {{ json_encode($vendor->only(['id','name','type','email','phone','address','city','country','description','website','rooms_count','rating','commission_percent'])) }}'
                            class="rounded-lg bg-indigo-50 px-3 py-1.5 text-xs font-bold text-indigo-600 hover:bg-indigo-100">Edit</button>
                    <a href="{{ route('epelcon.vendors.spaces', $vendor) }}" class="rounded-lg bg-slate-100 px-3 py-1.5 text-xs font-bold text-slate-600 hover:bg-slate-200">Spaces</a>
                </div>
            </div>
        @empty
            <div class="col-span-full rounded-2xl bg-white p-10 text-center text-slate-400 shadow-sm ring-1 ring-slate-200">No vendors found.</div>
        @endforelse
    </div>

    <div class="mt-5">{{ $vendors->links() }}</div>

    {{-- Add/Edit modal --}}
    <template x-for="mode in ['add', 'edit']" :key="mode">
        <div x-cloak x-show="(mode === 'add' && add) || (mode === 'edit' && edit)" class="fixed inset-0 z-50 flex items-end justify-center sm:items-center">
            <div class="absolute inset-0 bg-slate-900/50 backdrop-blur-sm" @click="add = false; edit = null"></div>
            <div x-transition class="relative max-h-[92vh] w-full max-w-lg overflow-y-auto rounded-t-3xl bg-white p-6 shadow-2xl sm:rounded-3xl">
                <h2 class="mb-4 text-lg font-extrabold" x-text="mode === 'add' ? 'Add marketplace vendor' : 'Edit vendor'"></h2>
                <form method="POST" :action="mode === 'add' ? '{{ route('epelcon.vendors.store') }}' : '{{ url('epelcon/vendors') }}/' + edit?.id + '/update'" enctype="multipart/form-data" class="grid grid-cols-2 gap-4">@csrf
                    <label class="col-span-2"><span class="mb-1 block text-xs font-bold uppercase text-slate-500">Logo</span>
                        <input type="file" name="logo" accept="image/*" class="w-full rounded-xl border border-slate-200 p-2 text-sm file:mr-3 file:rounded-lg file:border-0 file:bg-indigo-50 file:px-3 file:py-1.5 file:text-xs file:font-bold file:text-indigo-600"></label>
                    <label class="col-span-2"><span class="mb-1 block text-xs font-bold uppercase text-slate-500">Business name</span>
                        <input name="name" required :value="mode === 'edit' ? edit?.name : ''" class="w-full rounded-xl border-slate-200 text-sm"></label>
                    <label><span class="mb-1 block text-xs font-bold uppercase text-slate-500">Type</span>
                        <select name="type" class="w-full rounded-xl border-slate-200 text-sm" x-init="mode === 'edit' && edit && ($el.value = edit.type)">
                            <option value="hotel">Hotel</option><option value="restaurant">Restaurant</option><option value="spa">Spa</option><option value="travel">Travel</option>
                        </select></label>
                    <label><span class="mb-1 block text-xs font-bold uppercase text-slate-500">Rooms</span>
                        <input type="number" name="rooms_count" min="0" :value="mode === 'edit' ? edit?.rooms_count : 0" class="w-full rounded-xl border-slate-200 text-sm"></label>
                    <label><span class="mb-1 block text-xs font-bold uppercase text-slate-500">City</span>
                        <input name="city" :value="mode === 'edit' ? edit?.city : ''" class="w-full rounded-xl border-slate-200 text-sm"></label>
                    <label><span class="mb-1 block text-xs font-bold uppercase text-slate-500">Country</span>
                        <input name="country" :value="mode === 'edit' ? edit?.country : 'Nigeria'" class="w-full rounded-xl border-slate-200 text-sm"></label>
                    <label><span class="mb-1 block text-xs font-bold uppercase text-slate-500">Email</span>
                        <input type="email" name="email" :value="mode === 'edit' ? edit?.email : ''" class="w-full rounded-xl border-slate-200 text-sm"></label>
                    <label><span class="mb-1 block text-xs font-bold uppercase text-slate-500">Phone</span>
                        <input name="phone" :value="mode === 'edit' ? edit?.phone : ''" class="w-full rounded-xl border-slate-200 text-sm"></label>
                    <label><span class="mb-1 block text-xs font-bold uppercase text-slate-500">Rating (0–5)</span>
                        <input type="number" step="0.1" min="0" max="5" name="rating" :value="mode === 'edit' ? edit?.rating : 4" class="w-full rounded-xl border-slate-200 text-sm"></label>
                    <label><span class="mb-1 block text-xs font-bold uppercase text-slate-500">Commission %</span>
                        <input type="number" step="0.1" min="0" name="commission_percent" :value="mode === 'edit' ? edit?.commission_percent : 10" class="w-full rounded-xl border-slate-200 text-sm"></label>
                    <label class="col-span-2"><span class="mb-1 block text-xs font-bold uppercase text-slate-500">Website</span>
                        <input name="website" :value="mode === 'edit' ? edit?.website : ''" class="w-full rounded-xl border-slate-200 text-sm"></label>
                    <label class="col-span-2"><span class="mb-1 block text-xs font-bold uppercase text-slate-500">Description</span>
                        <textarea name="description" rows="2" class="w-full rounded-xl border-slate-200 text-sm" x-init="mode === 'edit' && edit && ($el.value = edit.description ?? '')"></textarea></label>
                    <div class="col-span-2 flex justify-end gap-2">
                        <button type="button" @click="add = false; edit = null" class="rounded-xl px-4 py-2.5 text-sm font-bold text-slate-500">Cancel</button>
                        <button class="rounded-xl bg-indigo-600 px-5 py-2.5 text-sm font-bold text-white">Save vendor</button>
                    </div>
                </form>
            </div>
        </div>
    </template>
</div>
@endsection
