@extends('layouts.app')
@php
$editing = $order->exists;
// Rows to render: what was just submitted (after an error) or the saved items.
$rows = old('items') ?? $items->map(fn ($i) => [
'design_name' => $i->design_name, 'material' => $i->material, 'color' => $i->color,
'length' => $i->length !== null ? (float) $i->length : null, 'width' => $i->width !== null ? (float) $i->width : null,
'dimension_unit' => $i->dimension_unit ?: 'ft', 'quantity' => (int) $i->quantity, 'pricing' => $i->pricing ?: 'piece', 'unit_price' => $i->unit_price + 0,
// legacy rows that only have an area and no length × width
'legacy_area' => ($i->length === null && $i->area_sqft !== null) ? (float) $i->area_sqft : null,
])->all();
$rows = array_values($rows);
$formData = [
'rows' => $rows,
'designs' => $designs,
'errors' => $errors->messages(),
'isEdit' => $editing,
'originalClient' => $editing ? $order->client_id : null,
'originalTotal' => $editing && ! $order->isCancelled() ? (float) $order->total : 0,
'creditError' => $errors->has('confirm_credit'),
'units' => \App\Models\OrderItem::DIMENSION_UNITS,
];
$presets = ['3x5' => '3 × 5 ft', '4x6' => '4 × 6 ft', '5x8' => '5 × 8 ft', '6x9' => '6 × 9 ft', '8x10' => '8 × 10 ft', '9x12' => '9 × 12 ft', '2.5x10' => '2.5 × 10 ft (runner)', '2x6' => '2 × 6 ft (runner)'];
@endphp
@section('title', $editing ? 'Edit '.$order->order_no : 'New order')
@section('content')