@extends('layouts.app')

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

@section('content')
<div class="bg-gray-50 min-h-screen py-8">
    <div class="container mx-auto px-4">
        <div class="mb-8">
            <h1 class="text-3xl font-bold text-gray-900">Orders</h1>
            <p class="text-gray-600">Manage orders for your products</p>
        </div>

        @if(session('success'))
            <div class="bg-green-50 text-green-600 px-4 py-3 rounded-lg mb-6">
                {{ session('success') }}
            </div>
        @endif

        @if($orders->count() > 0)
            <div class="bg-white rounded-xl shadow-sm overflow-hidden">
                <div class="overflow-x-auto">
                    <table class="w-full">
                        <thead class="bg-gray-50">
                            <tr>
                                <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Order #</th>
                                <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Customer</th>
                                <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Products</th>
                                <th class="px-6 py-4 text-right text-sm font-semibold text-gray-900">Amount</th>
                                <th class="px-6 py-4 text-center text-sm font-semibold text-gray-900">Status</th>
                                <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Date</th>
                                <th class="px-6 py-4 text-right text-sm font-semibold text-gray-900">Actions</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-200">
                            @foreach($orders as $orderItem)
                                <tr class="hover:bg-gray-50">
                                    <td class="px-6 py-4 font-medium">{{ $orderItem->order->order_number }}</td>
                                    <td class="px-6 py-4">
                                        <p class="font-medium">{{ $orderItem->order->user->name }}</p>
                                        <p class="text-sm text-gray-500">{{ $orderItem->order->company?->company_name }}</p>
                                    </td>
                                    <td class="px-6 py-4">
                                        <p class="font-medium">{{ $orderItem->product_name }}</p>
                                        <p class="text-sm text-gray-500">Qty: {{ $orderItem->quantity }}</p>
                                    </td>
                                    <td class="px-6 py-4 text-right font-semibold">R {{ number_format($orderItem->total_price, 2) }}</td>
                                    <td class="px-6 py-4 text-center">
                                        @php
                                            $statusColors = [
                                                'pending' => 'bg-yellow-100 text-yellow-800',
                                                'confirmed' => 'bg-blue-100 text-blue-800',
                                                'processing' => 'bg-purple-100 text-purple-800',
                                                'shipped' => 'bg-indigo-100 text-indigo-800',
                                                'delivered' => 'bg-green-100 text-green-800',
                                                'cancelled' => 'bg-red-100 text-red-800',
                                            ];
                                        @endphp
                                        <span class="inline-flex px-2 py-1 rounded-full text-xs font-medium {{ $statusColors[$orderItem->order->status] ?? 'bg-gray-100 text-gray-800' }}">
                                            {{ ucfirst($orderItem->order->status) }}
                                        </span>
                                    </td>
                                    <td class="px-6 py-4 text-gray-600">{{ $orderItem->created_at->format('d M Y') }}</td>
                                    <td class="px-6 py-4 text-right">
                                        @if(in_array($orderItem->order->status, ['pending', 'confirmed', 'processing']))
                                            <form action="{{ route('supplier.orders.status', $orderItem->order->id) }}" method="POST" class="inline">
                                                @csrf
                                                @method('PATCH')
                                                <select name="status" onchange="this.form.submit()" class="text-sm border border-gray-300 rounded px-2 py-1">
                                                    <option value="">Update Status</option>
                                                    @if($orderItem->order->status === 'pending')
                                                        <option value="confirmed">Confirm</option>
                                                    @endif
                                                    @if($orderItem->order->status === 'confirmed')
                                                        <option value="processing">Processing</option>
                                                    @endif
                                                    @if($orderItem->order->status === 'processing')
                                                        <option value="shipped">Shipped</option>
                                                    @endif
                                                </select>
                                            </form>
                                        @endif
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>

                <div class="px-6 py-4 border-t">
                    {{ $orders->links() }}
                </div>
            </div>
        @else
            <div class="bg-white rounded-xl shadow-sm p-12 text-center">
                <div class="text-6xl mb-4">📦</div>
                <h2 class="text-2xl font-bold text-gray-900 mb-2">No orders yet</h2>
                <p class="text-gray-600">Orders for your products will appear here.</p>
            </div>
        @endif
    </div>
</div>
@endsection
