<?php $editing = !empty($order); ?>
<div class="max-w-3xl mx-auto">
    <div class="flex items-center gap-3 mb-6"><a href="?page=workorders" class="text-gray-400 hover:text-gray-600"><svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"/></svg></a><h1 class="text-2xl font-bold text-gray-900"><?= $editing ? 'Edit Work Order' : 'Create Work Order' ?></h1></div>
    <form method="POST" action="?page=workorders&action=<?= $editing ? 'update&id='.$order['id'] : 'store' ?>" class="space-y-6"><?= CSRF::field() ?>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div class="md:col-span-2"><label class="block text-sm font-medium text-gray-700 mb-1">Title <span class="text-red-500">*</span></label><input type="text" name="title" value="<?= htmlspecialchars(old('title', $order['title'] ?? '')) ?>" required class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Linked Request</label>
                    <select name="maintenance_request_id" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"><option value="">None</option>
                    <?php foreach ($maintenanceRequests ?? [] as $mr): ?><option value="<?= $mr['id'] ?>" <?= old('maintenance_request_id', $order['maintenance_request_id'] ?? $_GET['maintenance_id'] ?? '') == $mr['id'] ? 'selected' : '' ?>>#<?= $mr['id'] ?> - <?= htmlspecialchars($mr['title']) ?></option><?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Vendor</label>
                    <select name="vendor_id" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"><option value="">Select</option>
                    <?php foreach ($vendors ?? [] as $vn): ?><option value="<?= $vn['id'] ?>" <?= old('vendor_id', $order['vendor_id'] ?? '') == $vn['id'] ? 'selected' : '' ?>><?= htmlspecialchars($vn['company_name']) ?></option><?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Property</label>
                    <select name="property_id" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"><option value="">Select</option>
                    <?php foreach ($properties ?? [] as $p): ?><option value="<?= $p['id'] ?>" <?= old('property_id', $order['property_id'] ?? '') == $p['id'] ? 'selected' : '' ?>><?= htmlspecialchars($p['name']) ?></option><?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Priority</label>
                    <select name="priority" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500">
                    <?php foreach (['low'=>'Low','medium'=>'Medium','high'=>'High','emergency'=>'Emergency'] as $v=>$l): ?><option value="<?= $v ?>" <?= old('priority', $order['priority'] ?? 'medium') === $v ? 'selected' : '' ?>><?= $l ?></option><?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Status</label>
                    <select name="status" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500">
                    <?php foreach (['new'=>'New','assigned'=>'Assigned','in_progress'=>'In Progress','on_hold'=>'On Hold','completed'=>'Completed','cancelled'=>'Cancelled'] as $v=>$l): ?><option value="<?= $v ?>" <?= old('status', $order['status'] ?? 'new') === $v ? 'selected' : '' ?>><?= $l ?></option><?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Scheduled Date</label><input type="date" name="scheduled_date" value="<?= old('scheduled_date', $order['scheduled_date'] ?? '') ?>" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Completed Date</label><input type="date" name="completed_date" value="<?= old('completed_date', $order['completed_date'] ?? '') ?>" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Cost</label><input type="number" name="cost" value="<?= old('cost', $order['cost'] ?? '') ?>" step="0.01" min="0" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
            </div>
            <div class="mt-4"><label class="block text-sm font-medium text-gray-700 mb-1">Description</label><textarea name="description" rows="4" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"><?= htmlspecialchars(old('description', $order['description'] ?? '')) ?></textarea></div>
        </div>
        <div class="flex justify-end gap-3"><a href="?page=workorders" class="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 text-sm font-medium">Cancel</a><button type="submit" class="px-6 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 text-sm font-medium"><?= $editing ? 'Update' : 'Create' ?></button></div>
    </form>
</div>
