@extends('admin.layouts.app')
@section('title', 'Vendors')
@section('content')
<div class="page-header d-print-none">
    <div class="container-xl">
        <div class="row g-2 align-items-center">
            <div class="col"><h2 class="page-title">Vendors</h2></div>
            <div class="col-auto ms-auto d-print-none">
                <a href="{{ route('admin.vendors.create') }}" class="btn btn-primary">
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 5l0 14" /><path d="M5 12l14 0" /></svg>
                    Add Vendor
                </a>
            </div>
        </div>
    </div>
</div>
<div class="page-body">
    <!-- Filters -->
    <div class="card mb-3">
        <div class="card-body">
            <form method="GET" class="row g-3" id="filter-form">
                <div class="col-md-4">
                    <input type="text" name="search" class="form-control" placeholder="Search name, email..." value="{{ request('search') }}">
                </div>
                <div class="col-md-2">
                    <select name="status" class="form-select">
                        <option value="">All Statuses</option>
                        @foreach(['pending','active','suspended','rejected'] as $s)
                            <option value="{{ $s }}" {{ request('status')==$s?'selected':'' }}>{{ ucfirst($s) }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-2">
                    <select name="kyc_status" class="form-select">
                        <option value="">All KYC</option>
                        @foreach(['pending','approved','rejected'] as $s)
                            <option value="{{ $s }}" {{ request('kyc_status')==$s?'selected':'' }}>{{ ucfirst($s) }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-2">
                    <select name="vendor_type_id" class="form-select">
                        <option value="">All Types</option>
                        @foreach($vendorTypes as $type)
                            <option value="{{ $type->id }}" {{ request('vendor_type_id')==$type->id?'selected':'' }}>{{ $type->name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-2 d-flex gap-2">
                    <button type="submit" class="btn btn-primary">Filter</button>
                    <a href="{{ route('admin.vendors.index') }}" class="btn btn-outline-secondary">Clear</a>
                </div>
            </form>
        </div>
    </div>

    <div class="card">
        <div class="card-header">
            <h3 class="card-title">All Vendors ({{ $vendors->total() }})</h3>
        </div>
        <div class="table-responsive">
            <table class="table table-vcenter card-table">
                <thead>
                    <tr>
                        <th>Business</th>
                        <th>Owner</th>
                        <th>Type</th>
                        <th>Status</th>
                        <th>KYC</th>
                        <th>Orders</th>
                        <th>Joined</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($vendors as $vendor)
                    <tr>
                        <td>
                            <div class="d-flex align-items-center gap-2">
                                <span class="avatar avatar-sm">{{ strtoupper(substr($vendor->business_name,0,2)) }}</span>
                                <div>
                                    <div class="fw-medium">{{ $vendor->business_name }}</div>
                                    <div class="text-muted small">{{ $vendor->business_email }}</div>
                                </div>
                            </div>
                        </td>
                        <td>
                            <div>{{ $vendor->user?->name }}</div>
                            <div class="text-muted small">{{ $vendor->user?->email }}</div>
                        </td>
                        <td><span class="badge bg-blue-lt">{{ $vendor->vendorType?->name ?? '—' }}</span></td>
                        <td>
                            @php $sc = ['pending'=>'warning','active'=>'success','suspended'=>'danger','rejected'=>'secondary']; @endphp
                            <span class="badge bg-{{ $sc[$vendor->status]??'secondary' }}-lt text-capitalize">{{ $vendor->status }}</span>
                        </td>
                        <td>
                            @php $kc = ['pending'=>'warning','approved'=>'success','rejected'=>'danger']; @endphp
                            <span class="badge bg-{{ $kc[$vendor->kyc_status]??'secondary' }}-lt text-capitalize">{{ $vendor->kyc_status }}</span>
                        </td>
                        <td>{{ $vendor->orders_count }}</td>
                        <td>{{ $vendor->created_at->format('d M Y') }}</td>
                        <td>
                            <div class="d-flex gap-1">
                                <a href="{{ route('admin.vendors.show', $vendor) }}" class="btn btn-sm btn-outline-secondary">View</a>
                                @if($vendor->status === 'pending')
                                    <form action="{{ route('admin.vendors.approve', $vendor) }}" method="POST" class="d-inline">
                                        @csrf
                                        <button class="btn btn-sm btn-success" onclick="return confirm('Approve this vendor?')">Approve</button>
                                    </form>
                                @endif
                                @if($vendor->status === 'active')
                                    <button class="btn btn-sm btn-warning" data-bs-toggle="modal" data-bs-target="#suspendModal{{ $vendor->id }}">Suspend</button>
                                @endif
                                @if($vendor->status === 'suspended')
                                    <form action="{{ route('admin.vendors.reactivate', $vendor) }}" method="POST" class="d-inline">
                                        @csrf
                                        <button class="btn btn-sm btn-success">Reactivate</button>
                                    </form>
                                @endif
                            </div>
                        </td>
                    </tr>

                    <!-- Suspend Modal -->
                    <div class="modal modal-blur fade" id="suspendModal{{ $vendor->id }}" tabindex="-1">
                        <div class="modal-dialog modal-sm modal-dialog-centered">
                            <div class="modal-content">
                                <form action="{{ route('admin.vendors.suspend', $vendor) }}" method="POST">
                                    @csrf
                                    <div class="modal-header"><h5 class="modal-title">Suspend Vendor</h5></div>
                                    <div class="modal-body">
                                        <div class="mb-3">
                                            <label class="form-label">Reason (optional)</label>
                                            <textarea name="reason" class="form-control" rows="3"></textarea>
                                        </div>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                                        <button type="submit" class="btn btn-warning">Suspend</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                    @empty
                    <tr><td colspan="8" class="text-center text-muted py-4">No vendors found</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="card-footer d-flex align-items-center">
            {{ $vendors->links() }}
        </div>
    </div>
</div>
@endsection
