@extends('layouts.admin')

@section('title', 'Pending Approvals')
@section('page-title', 'Vendor Pending Approvals')

@section('content')
<div class="bg-white rounded-xl shadow-sm p-6">
    <h2 class="font-bold text-gray-800 mb-6">Pending Vendor Applications</h2>

    @if($vendors->count() > 0)
        <div class="grid gap-4">
            @foreach($vendors as $vendor)
            <div class="border rounded-lg p-4 flex justify-between items-start">
                <div class="flex gap-4">
                    <div class="w-16 h-16 bg-gray-200 rounded-lg flex items-center justify-center text-2xl">
                        @if($vendor->logo)
                            <img src="{{ $vendor->logo_url }}" alt="" class="w-full h-full object-cover rounded-lg">
                        @else
                            {{ strtoupper(substr($vendor->shop_name, 0, 1)) }}
                        @endif
                    </div>
                    
                    <div>
                        <h3 class="font-bold text-lg">{{ $vendor->shop_name }}</h3>
                        <p class="text-gray-600">{{ $vendor->user->name }} ({{ $vendor->user->email }})</p>
                        <p class="text-sm text-gray-500 mt-1">{{ Str::limit($vendor->description, 100) }}</p>
                        
                        <div class="flex gap-4 mt-2 text-sm text-gray-500">
                            <span>📍 {{ $vendor->city }}, {{ $vendor->country }}</span>
                            <span>📞 {{ $vendor->phone ?: 'No phone' }}</span>
                        </div>
                        
                        <p class="text-xs text-gray-400 mt-2">
                            Applied {{ $vendor->created_at->diffForHumans() }}
                        </p>
                    </div>
                </div>

                <div class="flex gap-2">
                    <button onclick="document.getElementById('approve-modal-{{ $vendor->id }}').classList.remove('hidden')"
                            class="bg-green-600 text-white px-4 py-2 rounded hover:bg-green-700">
                        Approve
                    </button>
                    
                    <button onclick="document.getElementById('reject-modal-{{ $vendor->id }}').classList.remove('hidden')"
                            class="bg-red-600 text-white px-4 py-2 rounded hover:bg-red-700">
                        Reject
                    </button>
                </div>
            </div>

            <!-- Approve Modal -->
            <div id="approve-modal-{{ $vendor->id }}" class="hidden fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
                <div class="bg-white rounded-xl p-6 w-full max-w-md">
                    <h3 class="font-bold text-lg mb-4">Approve Vendor</h3>
                    
                    <form action="{{ route('admin.vendors.approve-with-note', $vendor) }}" method="POST">
                        @csrf
                        
                        <div class="mb-4">
                            <label class="block text-sm font-medium text-gray-700 mb-1">Commission Rate (%)</label>
                            <input type="number" name="commission_rate" value="10" step="0.01" min="0" max="100" required
                                class="w-full border border-gray-300 rounded-lg px-3 py-2">
                        </div>

                        <div class="mb-4">
                            <label class="block text-sm font-medium text-gray-700 mb-1">Notes (Optional)</label>
                            <textarea name="notes" rows="3" 
                                class="w-full border border-gray-300 rounded-lg px-3 py-2"
                                placeholder="Add approval notes..."></textarea>
                        </div>

                        <div class="flex gap-3">
                            <button type="submit" class="flex-1 bg-green-600 text-white py-2 rounded-lg">Approve</button>
                            <button type="button" onclick="document.getElementById('approve-modal-{{ $vendor->id }}').classList.add('hidden')"
                                    class="flex-1 bg-gray-200 text-gray-700 py-2 rounded-lg">Cancel</button>
                        </div>
                    </form>
                </div>
            </div>

            <!-- Reject Modal -->
            <div id="reject-modal-{{ $vendor->id }}" class="hidden fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
                <div class="bg-white rounded-xl p-6 w-full max-w-md">
                    <h3 class="font-bold text-lg mb-4 text-red-600">Reject Vendor Application</h3>
                    
                    <form action="{{ route('admin.vendors.update-status', $vendor->id) }}" method="POST">
                        @csrf
                        @method('PATCH')
                        
                        <input type="hidden" name="status" value="rejected">

                        <div class="mb-4">
                            <label class="block text-sm font-medium text-gray-700 mb-1">Reason for Rejection *</label>
                            <textarea name="reason" rows="3" required
                                class="w-full border border-gray-300 rounded-lg px-3 py-2"
                                placeholder="Explain why the application is being rejected..."></textarea>
                        </div>

                        <div class="flex gap-3">
                            <button type="submit" class="flex-1 bg-red-600 text-white py-2 rounded-lg">Reject</button>
                            <button type="button" onclick="document.getElementById('reject-modal-{{ $vendor->id }}').classList.add('hidden')"
                                    class="flex-1 bg-gray-200 text-gray-700 py-2 rounded-lg">Cancel</button>
                        </div>
                    </form>
                </div>
            </div>
            @endforeach
        </div>
    @else
        <div class="text-center py-12">
            <svg class="w-16 h-16 mx-auto text-gray-300 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
            </svg>
            <p class="text-xl text-gray-600">No pending applications</p>
            <p class="text-gray-500 mt-2">All vendor applications have been reviewed.</p>
        </div>
    @endif

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