@extends('layouts.app')

@section('title', 'Supplier Dashboard')

@section('content')
<div class="bg-gray-50 min-h-screen py-8">
    <div class="container mx-auto px-4">
        <!-- Header -->
        <div class="flex flex-col md:flex-row justify-between items-start md:items-center mb-8 gap-4">
            <div>
                <h1 class="text-3xl font-bold text-gray-900">Supplier Dashboard</h1>
                <p class="text-gray-600">Welcome back, {{ $supplier->company_name }}</p>
            </div>
            <a href="{{ route('supplier.products.create') }}" class="bg-blue-600 text-white px-6 py-2 rounded-lg hover:bg-blue-700 transition">
                + Add New Product
            </a>
        </div>

        <!-- Stats Cards -->
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
            <div class="bg-white rounded-xl shadow-sm p-6">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-sm text-gray-500">Total Products</p>
                        <p class="text-3xl font-bold text-gray-900">{{ $stats['total_products'] }}</p>
                    </div>
                    <div class="w-12 h-12 bg-blue-100 rounded-full flex items-center justify-center text-2xl">📦</div>
                </div>
            </div>

            <div class="bg-white rounded-xl shadow-sm p-6">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-sm text-gray-500">Total Orders</p>
                        <p class="text-3xl font-bold text-gray-900">{{ $stats['total_orders'] }}</p>
                    </div>
                    <div class="w-12 h-12 bg-green-100 rounded-full flex items-center justify-center text-2xl">🛒</div>
                </div>
            </div>

            <div class="bg-white rounded-xl shadow-sm p-6">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-sm text-gray-500">Pending Orders</p>
                        <p class="text-3xl font-bold text-gray-900">{{ $stats['pending_orders'] }}</p>
                    </div>
                    <div class="w-12 h-12 bg-yellow-100 rounded-full flex items-center justify-center text-2xl">⏳</div>
                </div>
            </div>

            <div class="bg-white rounded-xl shadow-sm p-6">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-sm text-gray-500">Total Revenue</p>
                        <p class="text-3xl font-bold text-gray-900">R {{ number_format($stats['total_revenue'], 0) }}</p>
                    </div>
                    <div class="w-12 h-12 bg-purple-100 rounded-full flex items-center justify-center text-2xl">💰</div>
                </div>
            </div>
        </div>

        <div class="grid lg:grid-cols-3 gap-8">
            <!-- Recent Orders -->
            <div class="lg:col-span-2">
                <div class="bg-white rounded-xl shadow-sm overflow-hidden">
                    <div class="p-6 border-b flex justify-between items-center">
                        <h2 class="font-semibold text-lg">Recent Orders</h2>
                        <a href="{{ route('supplier.orders') }}" class="text-blue-600 hover:underline text-sm">View All</a>
                    </div>
                    @if($recentOrders->count() > 0)
                        <div class="divide-y">
                            @foreach($recentOrders as $order)
                                <div class="p-4 hover:bg-gray-50">
                                    <div class="flex justify-between items-start">
                                        <div>
                                            <p class="font-medium">{{ $order->order->order_number }}</p>
                                            <p class="text-sm text-gray-500">{{ $order->order->user->name }}</p>
                                            <p class="text-sm text-gray-500">{{ $order->created_at->format('d M Y, H:i') }}</p>
                                        </div>
                                        <div class="text-right">
                                            <p class="font-bold text-blue-600">R {{ number_format($order->total_price, 2) }}</p>
                                            @php
                                                $statusColors = [
                                                    'pending' => 'bg-yellow-100 text-yellow-800',
                                                    'confirmed' => 'bg-blue-100 text-blue-800',
                                                    'shipped' => 'bg-indigo-100 text-indigo-800',
                                                    'delivered' => 'bg-green-100 text-green-800',
                                                ];
                                            @endphp
                                            <span class="inline-flex px-2 py-1 rounded-full text-xs font-medium {{ $statusColors[$order->order->status] ?? 'bg-gray-100 text-gray-800' }}">
                                                {{ ucfirst($order->order->status) }}
                                            </span>
                                        </div>
                                    </div>
                                </div>
                            @endforeach
                        </div>
                    @else
                        <div class="p-8 text-center text-gray-500">
                            <p>No orders yet</p>
                        </div>
                    @endif
                </div>
            </div>

            <!-- Quick Links -->
            <div class="space-y-6">
                <div class="bg-white rounded-xl shadow-sm p-6">
                    <h2 class="font-semibold text-lg mb-4">Quick Actions</h2>
                    <div class="space-y-3">
                        <a href="{{ route('supplier.products') }}" class="flex items-center gap-3 p-3 rounded-lg hover:bg-gray-50 transition">
                            <span class="text-2xl">📦</span>
                            <span class="font-medium">Manage Products</span>
                        </a>
                        <a href="{{ route('supplier.orders') }}" class="flex items-center gap-3 p-3 rounded-lg hover:bg-gray-50 transition">
                            <span class="text-2xl">🛒</span>
                            <span class="font-medium">View Orders</span>
                        </a>
                        <a href="{{ route('supplier.billing') }}" class="flex items-center gap-3 p-3 rounded-lg hover:bg-gray-50 transition">
                            <span class="text-2xl">💳</span>
                            <span class="font-medium">Billing & Payouts</span>
                        </a>
                        <a href="{{ route('supplier.delivery-rates') }}" class="flex items-center gap-3 p-3 rounded-lg hover:bg-gray-50 transition">
                            <span class="text-2xl">🚚</span>
                            <span class="font-medium">Delivery Rates</span>
                        </a>
                        <a href="{{ route('supplier.profile') }}" class="flex items-center gap-3 p-3 rounded-lg hover:bg-gray-50 transition">
                            <span class="text-2xl">⚙️</span>
                            <span class="font-medium">Profile Settings</span>
                        </a>
                    </div>
                </div>

                <!-- Account Status -->
                <div class="bg-white rounded-xl shadow-sm p-6">
                    <h2 class="font-semibold text-lg mb-4">Account Status</h2>
                    <div class="space-y-3">
                        <div class="flex justify-between">
                            <span class="text-gray-600">Status</span>
                            <span class="font-medium {{ $supplier->is_active ? 'text-green-600' : 'text-red-600' }}">
                                {{ $supplier->is_active ? 'Active' : 'Inactive' }}
                            </span>
                        </div>
                        <div class="flex justify-between">
                            <span class="text-gray-600">Commission Rate</span>
                            <span class="font-medium">{{ $supplier->commission_rate }}%</span>
                        </div>
                        <div class="flex justify-between">
                            <span class="text-gray-600">Member Since</span>
                            <span class="font-medium">{{ $supplier->created_at->format('M Y') }}</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
