@extends('layouts.app')

@section('title', 'Daftar Pesanan')

@section('content')
<div class="max-w-7xl mx-auto py-6 sm:px-6 lg:px-8">
    <div class="px-4 py-6 sm:px-0">
        <div class="flex justify-between items-center mb-8">
            <div>
                <h1 class="text-3xl font-bold text-gray-900">Daftar Pesanan</h1>
                <p class="text-gray-600">Kelola pesanan pelanggan.</p>
            </div>
            <a href="{{ route('orders.create') }}" class="bg-green-600 hover:bg-green-700 text-white px-4 py-2 rounded-md font-medium">
                Tambah Pesanan Baru
            </a>
        </div>

        @if(session('success'))
            <div class="mb-6 rounded-lg bg-green-50 border border-green-200 p-4 text-green-800">
                {{ session('success') }}
            </div>
        @endif
        @if(session('error'))
            <div class="mb-6 rounded-lg bg-red-50 border border-red-200 p-4 text-red-800">
                {{ session('error') }}
            </div>
        @endif

        @if(!empty($migrationMissing) && $migrationMissing)
            <div class="rounded-lg border border-dashed border-gray-300 bg-white p-10 text-center">
                <h2 class="text-2xl font-semibold text-gray-900 mb-2">Tabel pesanan belum dibuat</h2>
                <p class="text-gray-600 mb-6">Jalankan <code>php artisan migrate</code> untuk membuat tabel <strong>orders</strong>.</p>
            </div>
        @elseif($orders->isEmpty())
            <div class="rounded-lg border border-dashed border-gray-300 bg-white p-10 text-center">
                <h2 class="text-2xl font-semibold text-gray-900 mb-2">Belum ada pesanan</h2>
                <p class="text-gray-600 mb-6">Tambahkan pesanan baru untuk mulai mengelola.</p>
                <a href="{{ route('orders.create') }}" class="inline-flex items-center justify-center rounded-md bg-green-600 px-5 py-3 text-white hover:bg-green-700">
                    Tambah Pesanan Baru
                </a>
            </div>
        @else
            <div class="bg-white shadow overflow-hidden sm:rounded-md">
                <ul class="divide-y divide-gray-200">
                    @foreach($orders as $order)
                        <li>
                            <div class="px-4 py-4 sm:px-6">
                                <div class="flex items-center justify-between">
                                    <div class="flex items-center">
                                        <div class="flex-shrink-0 h-10 w-10">
                                            <div class="h-10 w-10 rounded-full bg-gray-300 flex items-center justify-center">
                                                <span class="text-sm font-medium text-gray-700">{{ substr($order->customer_name, 0, 1) }}</span>
                                            </div>
                                        </div>
                                        <div class="ml-4">
                                            <div class="text-sm font-medium text-gray-900">
                                                {{ $order->customer_name }}
                                            </div>
                                            <div class="text-sm text-gray-500">
                                                {{ $order->product->name }} ({{ $order->quantity }} pcs)
                                            </div>
                                        </div>
                                    </div>
                                    <div class="flex items-center space-x-4">
                                        <div class="text-right">
                                            <div class="text-sm font-medium text-gray-900">
                                                Rp {{ number_format($order->total_price, 0, ',', '.') }}
                                            </div>
                                            <div class="text-sm text-gray-500">
                                                <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium
                                                    @if($order->status == 'pending') bg-yellow-100 text-yellow-800
                                                    @elseif($order->status == 'processing') bg-blue-100 text-blue-800
                                                    @elseif($order->status == 'shipped') bg-purple-100 text-purple-800
                                                    @elseif($order->status == 'delivered') bg-green-100 text-green-800
                                                    @else bg-red-100 text-red-800 @endif">
                                                    {{ ucfirst($order->status) }}
                                                </span>
                                            </div>
                                        </div>
                                        <div class="flex space-x-2">
                                            <a href="{{ route('orders.show', $order) }}" class="text-blue-600 hover:text-blue-900 text-sm">
                                                Lihat
                                            </a>
                                            <a href="{{ route('orders.edit', $order) }}" class="text-yellow-600 hover:text-yellow-900 text-sm">
                                                Edit
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </li>
                    @endforeach
                </ul>
            </div>
        @endif
    </div>
</div>
@endsection