@extends('admin::layouts.master')

@section('title', "Edit Order #{$order->order_number}")

@section('content')
    <div class="flex justify-between items-center mb-6">
        <div>
            <h1 class="text-2xl font-bold">Edit Order #{{ $order->order_number }}</h1>
            <p class="text-gray-500 text-sm">Created {{ $order->created_at->format('d M Y H:i') }}</p>
        </div>
        <div class="space-x-2">
            <a href="{{ route('admin.orders.show', $order->id) }}" class="bg-gray-500 text-white px-4 py-2 rounded hover:bg-gray-600 text-sm">View</a>
            <a href="{{ route('admin.orders.index') }}" class="bg-gray-500 text-white px-4 py-2 rounded hover:bg-gray-600 text-sm">Back</a>
        </div>
    </div>

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        <div class="lg:col-span-2">
            <div class="bg-white rounded-lg shadow p-6">
                <h3 class="font-bold text-lg mb-4">Update Order</h3>
                <form method="POST" action="{{ route('admin.orders.update', $order->id) }}">
                    @csrf
                    @method('PUT')

                    <div class="grid grid-cols-2 gap-4 mb-4">
                        <div>
                            <label for="status" class="block text-sm font-medium text-gray-700 mb-1">Order Status</label>
                            <select name="status" id="status" required
                                class="w-full border border-gray-300 rounded px-3 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500">
                                @foreach(['pending', 'confirmed', 'processing', 'shipped', 'delivered', 'cancelled'] as $s)
                                    <option value="{{ $s }}" {{ old('status', $order->status) === $s ? 'selected' : '' }}>{{ ucfirst($s) }}</option>
                                @endforeach
                            </select>
                            @error('status') <p class="text-red-500 text-sm mt-1">{{ $message }}</p> @enderror
                        </div>
                        <div>
                            <label for="payment_status" class="block text-sm font-medium text-gray-700 mb-1">Payment Status</label>
                            <select name="payment_status" id="payment_status" required
                                class="w-full border border-gray-300 rounded px-3 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500">
                                @foreach(['pending', 'paid', 'failed', 'refunded'] as $ps)
                                    <option value="{{ $ps }}" {{ old('payment_status', $order->payment_status) === $ps ? 'selected' : '' }}>{{ ucfirst($ps) }}</option>
                                @endforeach
                            </select>
                            @error('payment_status') <p class="text-red-500 text-sm mt-1">{{ $message }}</p> @enderror
                        </div>
                    </div>

                    <div class="mb-4">
                        <label for="notes" class="block text-sm font-medium text-gray-700 mb-1">Notes</label>
                        <textarea name="notes" id="notes" rows="4"
                            class="w-full border border-gray-300 rounded px-3 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500">{{ old('notes', $order->notes) }}</textarea>
                        @error('notes') <p class="text-red-500 text-sm mt-1">{{ $message }}</p> @enderror
                    </div>

                    <div class="flex gap-4">
                        <button type="submit" class="bg-blue-600 text-white px-6 py-2 rounded hover:bg-blue-700">Update Order</button>
                        <a href="{{ route('admin.orders.index') }}" class="bg-gray-500 text-white px-6 py-2 rounded hover:bg-gray-600">Cancel</a>
                    </div>
                </form>
            </div>
        </div>

        <div class="space-y-6">
            <div class="bg-white rounded-lg shadow p-6">
                <h3 class="font-bold text-lg mb-4">Order Summary</h3>
                <div class="space-y-2 text-sm">
                    <div class="flex justify-between"><span class="text-gray-600">Order #</span><span class="font-medium">{{ $order->order_number }}</span></div>
                    <div class="flex justify-between"><span class="text-gray-600">Items</span><span>{{ $order->orderItems->count() }}</span></div>
                    <div class="flex justify-between"><span class="text-gray-600">Subtotal</span><span>&euro;{{ number_format($order->subtotal, 2) }}</span></div>
                    <div class="flex justify-between"><span class="text-gray-600">Delivery</span><span>&euro;{{ number_format($order->delivery_fee, 2) }}</span></div>
                    <div class="flex justify-between"><span class="text-gray-600">Service</span><span>&euro;{{ number_format($order->service_fee, 2) }}</span></div>
                    <div class="flex justify-between"><span class="text-gray-600">VAT</span><span>&euro;{{ number_format($order->vat_amount, 2) }}</span></div>
                    <hr>
                    <div class="flex justify-between font-bold"><span>Total</span><span>&euro;{{ number_format($order->total, 2) }}</span></div>
                </div>
            </div>

            <div class="bg-white rounded-lg shadow p-6">
                <h3 class="font-bold text-lg mb-4">Items</h3>
                <div class="space-y-3">
                    @foreach($order->orderItems as $item)
                        <div class="flex justify-between text-sm border-b pb-2">
                            <div>
                                <p class="font-medium">{{ $item->product_name }}</p>
                                <p class="text-xs text-gray-500">&euro;{{ number_format($item->price, 2) }} x {{ $item->quantity }}</p>
                            </div>
                            <span class="font-medium">&euro;{{ number_format($item->subtotal, 2) }}</span>
                        </div>
                    @endforeach
                </div>
            </div>
        </div>
    </div>
@endsection
