@extends('layouts.admin')

@section('title', 'Manage Vendors')
@section('page-title', 'Vendors')

@section('content')
<div class="bg-white rounded-xl shadow-sm p-6">
    <div class="flex justify-between items-center mb-6">
        <form method="GET" class="flex gap-3">
            <input type="text" name="search" value="{{ request('search') }}" placeholder="Search vendors..." class="border border-gray-300 rounded-lg px-3 py-2 text-sm w-64">
            <select name="status" class="border border-gray-300 rounded-lg px-3 py-2 text-sm">
                <option value="">All Status</option>
                <option value="pending" {{ request('status') == 'pending' ? 'selected' : '' }}>Pending</option>
                <option value="approved" {{ request('status') == 'approved' ? 'selected' : '' }}>Approved</option>
                <option value="rejected" {{ request('status') == 'rejected' ? 'selected' : '' }}>Rejected</option>
                <option value="suspended" {{ request('status') == 'suspended' ? 'selected' : '' }}>Suspended</option>
            </select>
            <button type="submit" class="bg-gray-800 text-white px-4 py-2 rounded-lg text-sm">Filter</button>
        </form>
        <a href="{{ route('admin.vendors.create') }}" class="bg-blue-600 text-white px-4 py-2 rounded-lg text-sm hover:bg-blue-700">+ Add Vendor</a>
    </div>

    <table class="w-full text-sm">
        <thead>
            <tr class="text-gray-500 border-b bg-gray-50">
                <th class="text-left p-3">Shop Name</th>
                <th class="text-left p-3">Owner</th>
                <th class="text-left p-3">Products</th>
                <th class="text-left p-3">Commission</th>
                <th class="text-left p-3">Status</th>
                <th class="text-left p-3">Actions</th>
            </tr>
        </thead>
        <tbody>
            @foreach($vendors as $vendor)
            <tr class="border-b hover:bg-gray-50">
                <td class="p-3">
                    <p class="font-medium">{{ $vendor->shop_name }}</p>
                    <p class="text-xs text-gray-400">{{ $vendor->created_at->format('M d, Y') }}</p>
                </td>
                <td class="p-3">
                    <p>{{ $vendor->user->name }}</p>
                    <p class="text-xs text-gray-400">{{ $vendor->user->email }}</p>
                </td>
                <td class="p-3">{{ $vendor->products_count }}</td>
                <td class="p-3">{{ $vendor->commission_rate }}%</td>
                <td class="p-3">
                    <span class="px-2 py-0.5 rounded-full text-xs font-medium
                        {{ $vendor->status === 'approved' ? 'bg-green-100 text-green-700' :
                           ($vendor->status === 'rejected' ? 'bg-red-100 text-red-700' :
                           ($vendor->status === 'suspended' ? 'bg-gray-100 text-gray-700' : 'bg-yellow-100 text-yellow-700')) }}">
                        {{ ucfirst($vendor->status) }}
                    </span>
                </td>
                <td class="p-3">
                    <div class="flex gap-2">
                        @if($vendor->status === 'pending')
                        <form action="{{ route('admin.vendors.approve', $vendor->id) }}" method="POST">
                            @csrf @method('PATCH')
                            <input type="hidden" name="status" value="approved">
                            <button type="submit" class="text-xs bg-green-600 text-white px-2 py-1 rounded hover:bg-green-700">Approve</button>
                        </form>
                        <form action="{{ route('admin.vendors.reject', $vendor->id) }}" method="POST">
                            @csrf @method('PATCH')
                            <input type="hidden" name="status" value="rejected">
                            <button type="submit" class="text-xs bg-red-600 text-white px-2 py-1 rounded hover:bg-red-700">Reject</button>
                        </form>
                        @elseif($vendor->status === 'approved')
                        <form action="{{ route('admin.vendors.suspend', $vendor->id) }}" method="POST">
                            @csrf @method('PATCH')
                            <input type="hidden" name="status" value="suspended">
                            <button type="submit" class="text-xs bg-orange-500 text-white px-2 py-1 rounded hover:bg-orange-600">Suspend</button>
                        </form>
                        @endif
                        <a href="{{ route('admin.vendors.show', $vendor->id) }}" class="text-xs bg-blue-600 text-white px-2 py-1 rounded hover:bg-blue-700">View</a>
                        <a href="{{ route('admin.vendors.edit', $vendor->id) }}" class="text-xs bg-orange-500 text-white px-3 py-1.5 rounded font-bold shadow border border-orange-600">Edit</a>
                       
                    </div>
                </td>
            </tr>
            @endforeach
        </tbody>
    </table>
    <div class="mt-4">{{ $vendors->links() }}</div>
</div>
@endsection
