{{define "content"}}
<div class="space-y-6">
    <div class="sm:flex sm:items-center sm:justify-between">
        <h1 class="text-2xl font-bold text-gray-900">{{if .IsNew}}New Order{{else}}Edit Order #{{.Order.ID}}{{end}}</h1>
        <a href="{{if .IsNew}}/orders{{else}}/orders/{{.Order.ID}}{{end}}" class="text-sm text-gray-500 hover:text-gray-700">&larr; Back</a>
    </div>

    {{if .Error}}
    <div class="rounded-md bg-red-50 p-4">
        <div class="text-sm text-red-700">{{.Error}}</div>
    </div>
    {{end}}

    <div class="bg-white shadow sm:rounded-lg">
        <form class="space-y-6 p-6"
            {{if .IsNew}}
                method="POST" action="/orders" hx-post="/orders"
            {{else}}
                method="POST" action="/orders/{{.Order.ID}}" hx-put="/orders/{{.Order.ID}}"
            {{end}}>

            <div class="grid grid-cols-1 gap-6 sm:grid-cols-2">
                <div>
                    <label for="customer_id" class="block text-sm font-medium text-gray-700">Customer *</label>
                    <select name="customer_id" id="customer_id" required
                        class="mt-1 block w-full rounded-md border border-gray-300 px-3 py-2 shadow-sm focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 sm:text-sm">
                        <option value="">Select a customer</option>
                        {{range .Customers}}
                        <option value="{{.ID}}" {{if eq .ID $.Order.CustomerID}}selected{{end}}>{{.Name}}</option>
                        {{end}}
                    </select>
                </div>

                <div>
                    <label for="order_date" class="block text-sm font-medium text-gray-700">Order Date</label>
                    <input type="date" name="order_date" id="order_date" value="{{.Order.OrderDate}}"
                        class="mt-1 block w-full rounded-md border border-gray-300 px-3 py-2 shadow-sm focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 sm:text-sm">
                </div>
            </div>

            <div>
                <label for="notes" class="block text-sm font-medium text-gray-700">Notes</label>
                <textarea name="notes" id="notes" rows="3"
                    class="mt-1 block w-full rounded-md border border-gray-300 px-3 py-2 shadow-sm focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 sm:text-sm">{{.Order.Notes}}</textarea>
            </div>

            <p class="text-sm text-gray-500">{{if .IsNew}}You can add line items after creating the order.{{else}}Line items can be managed from the order detail page.{{end}}</p>

            <div class="flex justify-end space-x-3">
                <a href="{{if .IsNew}}/orders{{else}}/orders/{{.Order.ID}}{{end}}"
                    class="rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50">Cancel</a>
                <button type="submit"
                    class="rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500">
                    {{if .IsNew}}Create Order{{else}}Update Order{{end}}
                </button>
            </div>
        </form>
    </div>
</div>
{{end}}
