{{define "content"}}
<div class="space-y-6">
    <div class="sm:flex sm:items-center sm:justify-between">
        <div>
            <h1 class="text-2xl font-bold text-gray-900">Order #{{.Order.ID}}</h1>
            <p class="mt-1 text-sm text-gray-500">{{.Order.CustomerName}} &middot; {{formatDate .Order.OrderDate}}</p>
        </div>
        <div class="flex items-center space-x-3">
            <a href="/orders" class="text-sm text-gray-500 hover:text-gray-700">&larr; All Orders</a>
            {{if eq .Order.Status "draft"}}
                <a href="/orders/{{.Order.ID}}/edit"
                    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">Edit</a>
                <button class="rounded-md bg-blue-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-blue-500"
                    hx-post="/orders/{{.Order.ID}}/confirm"
                    hx-confirm="Confirm this order?">Confirm</button>
                <button class="rounded-md bg-red-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-red-500"
                    hx-post="/orders/{{.Order.ID}}/cancel"
                    hx-confirm="Cancel this order?">Cancel</button>
            {{end}}
            {{if eq .Order.Status "confirmed"}}
                <button class="rounded-md bg-green-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-green-500"
                    hx-post="/orders/{{.Order.ID}}/fulfill"
                    hx-confirm="Fulfill this order? This will generate an invoice and GL entries.">Fulfill</button>
                <button class="rounded-md bg-red-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-red-500"
                    hx-post="/orders/{{.Order.ID}}/cancel"
                    hx-confirm="Cancel this order?">Cancel</button>
            {{end}}
        </div>
    </div>

    <!-- Order Info -->
    <div class="bg-white shadow sm:rounded-lg">
        <div class="px-4 py-5 sm:p-6">
            <dl class="grid grid-cols-1 gap-x-4 gap-y-6 sm:grid-cols-4">
                <div>
                    <dt class="text-sm font-medium text-gray-500">Customer</dt>
                    <dd class="mt-1 text-sm text-gray-900"><a href="/customers/{{.Order.CustomerID}}" class="text-indigo-600 hover:text-indigo-900">{{.Order.CustomerName}}</a></dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-gray-500">Date</dt>
                    <dd class="mt-1 text-sm text-gray-900">{{formatDate .Order.OrderDate}}</dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-gray-500">Status</dt>
                    <dd class="mt-1">{{statusBadge .Order.Status}}</dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-gray-500">Total</dt>
                    <dd class="mt-1 text-lg font-semibold text-gray-900">{{formatMoney .Order.TotalAmount}}</dd>
                </div>
                {{if .Order.Notes}}
                <div class="sm:col-span-4">
                    <dt class="text-sm font-medium text-gray-500">Notes</dt>
                    <dd class="mt-1 text-sm text-gray-900">{{.Order.Notes}}</dd>
                </div>
                {{end}}
            </dl>
        </div>
    </div>

    <!-- Order Lines -->
    <div id="order-lines-section">
        {{template "order-lines" .Order}}
    </div>
</div>
{{end}}

{{define "order-lines"}}
<div class="bg-white shadow sm:rounded-lg">
    <div class="px-4 py-5 sm:p-6">
        <h2 class="text-lg font-medium text-gray-900 mb-4">Line Items</h2>

        <table class="min-w-full divide-y divide-gray-300 mb-4">
            <thead>
                <tr>
                    <th class="py-2 pr-3 text-left text-sm font-semibold text-gray-900">Description</th>
                    <th class="px-3 py-2 text-right text-sm font-semibold text-gray-900">Qty</th>
                    <th class="px-3 py-2 text-right text-sm font-semibold text-gray-900">Unit Price</th>
                    <th class="px-3 py-2 text-right text-sm font-semibold text-gray-900">Total</th>
                    {{if eq .Status "draft"}}
                    <th class="px-3 py-2 text-sm font-semibold text-gray-900"></th>
                    {{end}}
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-200">
                {{range .Lines}}
                <tr>
                    <td class="py-2 pr-3 text-sm text-gray-900">{{.Description}}</td>
                    <td class="px-3 py-2 text-sm text-gray-900 text-right">{{printf "%.0f" .Quantity}}</td>
                    <td class="px-3 py-2 text-sm text-gray-900 text-right">{{formatMoney .UnitPrice}}</td>
                    <td class="px-3 py-2 text-sm text-gray-900 text-right">{{formatMoney .LineTotal}}</td>
                    {{if eq $.Status "draft"}}
                    <td class="px-3 py-2 text-sm text-right">
                        <button class="text-red-600 hover:text-red-900"
                            hx-delete="/orders/{{$.ID}}/lines/{{.ID}}"
                            hx-target="#order-lines-section"
                            hx-confirm="Remove this line?">Remove</button>
                    </td>
                    {{end}}
                </tr>
                {{else}}
                <tr>
                    <td colspan="5" class="py-4 text-center text-sm text-gray-500">No line items yet.</td>
                </tr>
                {{end}}
            </tbody>
            <tfoot>
                <tr class="border-t-2 border-gray-900">
                    <td colspan="3" class="py-2 pr-3 text-sm font-semibold text-gray-900 text-right">Total</td>
                    <td class="px-3 py-2 text-sm font-semibold text-gray-900 text-right">{{formatMoney .TotalAmount}}</td>
                    {{if eq .Status "draft"}}<td></td>{{end}}
                </tr>
            </tfoot>
        </table>

        {{if eq .Status "draft"}}
        <!-- Add Line Form -->
        <form class="mt-4 p-4 bg-gray-50 rounded-lg"
            hx-post="/orders/{{.ID}}/lines"
            hx-target="#order-lines-section"
            hx-swap="innerHTML">
            <h3 class="text-sm font-medium text-gray-700 mb-3">Add Line Item</h3>
            <div class="grid grid-cols-1 gap-3 sm:grid-cols-4">
                <div class="sm:col-span-2">
                    <input type="text" name="description" placeholder="Description" required
                        class="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>
                    <input type="number" name="quantity" placeholder="Qty" value="1" min="1" step="1" required
                        class="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>
                    <input type="number" name="unit_price" placeholder="Unit Price" value="0.00" min="0" step="0.01" required
                        class="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 class="mt-3">
                <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">
                    Add Line
                </button>
            </div>
        </form>
        {{end}}
    </div>
</div>
{{end}}
