@extends('admin.layouts.app')

@section('title', 'Dashboard')

@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">Dashboard</h2>
                <div class="text-muted mt-1">Overview of your platform activity</div>
            </div>
            <div class="col-auto ms-auto d-print-none">
                <span class="badge bg-indigo-lt fs-6">{{ now()->format('D, d M Y') }}</span>
            </div>
        </div>
    </div>
</div>

<div class="page-body">
    <!-- Stats Row 1 -->
    <div class="row row-deck row-cards mb-4">
        <div class="col-sm-6 col-lg-3">
            <div class="card stat-card">
                <div class="card-body">
                    <div class="d-flex align-items-center">
                        <div class="subheader">Total Customers</div>
                    </div>
                    <div class="h1 mb-3">{{ number_format($stats['total_customers']) }}</div>
                    <div class="d-flex mb-2">
                        <div>All registered customers</div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-sm-6 col-lg-3">
            <div class="card stat-card">
                <div class="card-body">
                    <div class="subheader">Total Vendors</div>
                    <div class="h1 mb-3">{{ number_format($stats['total_vendors']) }}</div>
                    <div class="d-flex mb-2">
                        <div class="me-auto">
                            <span class="text-green d-inline-flex align-items-center lh-1">
                                {{ $stats['active_vendors'] }} active
                            </span>
                        </div>
                        @if($stats['pending_vendors'])
                            <div><span class="badge bg-warning-lt">{{ $stats['pending_vendors'] }} pending</span></div>
                        @endif
                    </div>
                </div>
            </div>
        </div>
        <div class="col-sm-6 col-lg-3">
            <div class="card stat-card">
                <div class="card-body">
                    <div class="subheader">Total Orders</div>
                    <div class="h1 mb-3">{{ number_format($stats['total_orders']) }}</div>
                    <div class="d-flex gap-2">
                        <span class="badge bg-blue-lt">{{ $stats['ride_orders'] }} Rides</span>
                        <span class="badge bg-green-lt">{{ $stats['food_orders'] }} Food</span>
                        <span class="badge bg-orange-lt">{{ $stats['delivery_orders'] }} Delivery</span>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-sm-6 col-lg-3">
            <div class="card stat-card">
                <div class="card-body">
                    <div class="subheader">Wallet Volume</div>
                    <div class="h1 mb-3">₹{{ number_format($stats['wallet_volume'], 2) }}</div>
                    <div class="d-flex mb-2">
                        <div>Total credited to wallets</div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Pending Actions Row -->
    <div class="row row-deck row-cards mb-4">
        <div class="col-sm-6 col-lg-3">
            <div class="card stat-card border-warning">
                <div class="card-body">
                    <div class="subheader text-warning">Pending KYC</div>
                    <div class="h1 mb-2">{{ $stats['pending_kyc'] }}</div>
                    <a href="{{ route('admin.kyc.index') }}?status=pending" class="btn btn-sm btn-warning">Review KYC</a>
                </div>
            </div>
        </div>
        <div class="col-sm-6 col-lg-3">
            <div class="card stat-card border-info">
                <div class="card-body">
                    <div class="subheader text-info">Pending Ads</div>
                    <div class="h1 mb-2">{{ $stats['pending_ads'] }}</div>
                    <a href="{{ route('admin.ads.index') }}?status=pending" class="btn btn-sm btn-info">Review Ads</a>
                </div>
            </div>
        </div>
        <div class="col-sm-6 col-lg-3">
            <div class="card stat-card border-danger">
                <div class="card-body">
                    <div class="subheader text-danger">Pending Withdrawals</div>
                    <div class="h1 mb-2">{{ $stats['pending_withdrawals'] }}</div>
                    <a href="{{ route('admin.wallet.withdrawals') }}?status=pending" class="btn btn-sm btn-danger">Process</a>
                </div>
            </div>
        </div>
        <div class="col-sm-6 col-lg-3">
            <div class="card stat-card border-secondary">
                <div class="card-body">
                    <div class="subheader text-secondary">Disputed Orders</div>
                    <div class="h1 mb-2">{{ $stats['disputed_orders'] }}</div>
                    <a href="{{ route('admin.orders.index') }}?status=disputed" class="btn btn-sm btn-secondary">Resolve</a>
                </div>
            </div>
        </div>
    </div>

    <!-- Recent Orders & Vendors -->
    <div class="row row-deck row-cards">
        <div class="col-lg-8">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">Recent Orders</h3>
                    <div class="card-options">
                        <a href="{{ route('admin.orders.index') }}" class="btn btn-sm btn-outline-secondary">View all</a>
                    </div>
                </div>
                <div class="table-responsive">
                    <table class="table table-vcenter card-table">
                        <thead>
                            <tr>
                                <th>Order #</th>
                                <th>Customer</th>
                                <th>Vendor</th>
                                <th>Type</th>
                                <th>Amount</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($recent_orders as $order)
                            <tr>
                                <td><a href="{{ route('admin.orders.show', $order) }}" class="text-reset">{{ $order->order_number }}</a></td>
                                <td>{{ $order->user?->name ?? 'N/A' }}</td>
                                <td>{{ $order->vendor?->business_name ?? 'N/A' }}</td>
                                <td><span class="badge bg-blue-lt text-capitalize">{{ $order->type }}</span></td>
                                <td>₹{{ number_format($order->total, 2) }}</td>
                                <td>
                                    @php
                                        $colors = ['pending'=>'warning','accepted'=>'info','completed'=>'success','cancelled'=>'danger','disputed'=>'red','in_progress'=>'primary'];
                                        $color = $colors[$order->status] ?? 'secondary';
                                    @endphp
                                    <span class="badge bg-{{ $color }}-lt text-capitalize">{{ str_replace('_',' ',$order->status) }}</span>
                                </td>
                            </tr>
                            @empty
                            <tr><td colspan="6" class="text-center text-muted">No orders yet</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <div class="col-lg-4">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">Recent Vendors</h3>
                    <div class="card-options">
                        <a href="{{ route('admin.vendors.index') }}" class="btn btn-sm btn-outline-secondary">View all</a>
                    </div>
                </div>
                <div class="list-group list-group-flush">
                    @forelse($recent_vendors as $vendor)
                    <a href="{{ route('admin.vendors.show', $vendor) }}" class="list-group-item list-group-item-action">
                        <div class="row align-items-center">
                            <div class="col-auto">
                                <span class="avatar">{{ strtoupper(substr($vendor->business_name,0,2)) }}</span>
                            </div>
                            <div class="col text-truncate">
                                <span class="text-body d-block">{{ $vendor->business_name }}</span>
                                <span class="d-block text-muted text-truncate mt-n1">{{ $vendor->user?->email }}</span>
                            </div>
                            <div class="col-auto">
                                @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>
                            </div>
                        </div>
                    </a>
                    @empty
                    <div class="list-group-item text-muted text-center">No vendors yet</div>
                    @endforelse
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
