<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-clipboard-check me-2 text-primary"></i><?= $wo ? 'Edit Work Order' : 'New Work Order' ?></h5>
    <a href="<?= url('/WorkOrders') ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<form method="post" action="<?= url('/WorkOrders/save') ?>">
    <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
    <input type="hidden" name="work_order_id" value="<?= (int) ($wo['work_order_id'] ?? 0) ?>">

    <div class="rs-card p-3 mb-3">
        <h6 class="fw-bold mb-2">Overview</h6>
        <div class="row g-2">
            <div class="col-md-8"><label class="form-label">Title *</label><input type="text" class="form-control form-control-sm" name="title" required value="<?= e($wo['title'] ?? '') ?>"></div>
            <div class="col-md-4"><label class="form-label">Code</label><input type="text" class="form-control form-control-sm" name="code" value="<?= e($wo['code'] ?? '') ?>"></div>
            <div class="col-md-3"><label class="form-label">Type</label>
                <select name="wo_type" class="form-select form-select-sm"><?php foreach (['preventive','corrective','adhoc','inspection'] as $t): ?><option <?= ($wo['wo_type'] ?? 'corrective') === $t ? 'selected' : '' ?>><?= $t ?></option><?php endforeach; ?></select>
            </div>
            <div class="col-md-3"><label class="form-label">Priority</label>
                <select name="priority" class="form-select form-select-sm"><?php foreach (['low','medium','high','urgent'] as $p): ?><option <?= ($wo['priority'] ?? 'medium') === $p ? 'selected' : '' ?>><?= $p ?></option><?php endforeach; ?></select>
            </div>
            <div class="col-md-3"><label class="form-label">Status</label>
                <select name="status" class="form-select form-select-sm"><?php foreach (['draft','assigned','in_progress','on_hold','completed','cancelled'] as $s): ?><option <?= ($wo['status'] ?? 'draft') === $s ? 'selected' : '' ?>><?= $s ?></option><?php endforeach; ?></select>
            </div>
            <div class="col-md-3 d-flex align-items-end">
                <div class="form-check">
                    <input type="checkbox" class="form-check-input" name="permit_required" value="1" id="woPermit" <?= !empty($wo['permit_required']) ? 'checked' : '' ?>>
                    <label for="woPermit" class="form-check-label">Permit required</label>
                </div>
            </div>
            <div class="col-12"><label class="form-label">Description</label><textarea class="form-control form-control-sm" name="description" rows="3"><?= e($wo['description'] ?? '') ?></textarea></div>
        </div>
    </div>

    <div class="rs-card p-3 mb-3">
        <h6 class="fw-bold mb-2">Assignment</h6>
        <div class="row g-2">
            <div class="col-md-4"><label class="form-label">Site</label><select name="site_id" class="form-select form-select-sm"><option value="">— none —</option><?php foreach ($sites as $s): ?><option value="<?= (int) $s['site_id'] ?>" <?= (int) ($wo['site_id'] ?? 0) === (int) $s['site_id'] ? 'selected' : '' ?>><?= e($s['name']) ?></option><?php endforeach; ?></select></div>
            <div class="col-md-4"><label class="form-label">Asset</label><select name="asset_id" class="form-select form-select-sm"><option value="">— none —</option><?php foreach ($assets as $a): ?><option value="<?= (int) $a['asset_id'] ?>" <?= (int) ($wo['asset_id'] ?? 0) === (int) $a['asset_id'] ? 'selected' : '' ?>><?= e($a['name']) ?></option><?php endforeach; ?></select></div>
            <div class="col-md-4"><label class="form-label">Related Incident</label><select name="incident_id" class="form-select form-select-sm"><option value="">— none —</option><?php foreach ($incidents as $i): ?><option value="<?= (int) $i['incident_id'] ?>" <?= (int) ($wo['incident_id'] ?? 0) === (int) $i['incident_id'] ? 'selected' : '' ?>><?= e($i['code'] ?? '#' . $i['incident_id']) ?> · <?= e($i['title']) ?></option><?php endforeach; ?></select></div>
            <div class="col-md-3"><label class="form-label">Assigned To</label><select name="assigned_to" class="form-select form-select-sm"><option value="">— unassigned —</option><?php foreach ($users as $u): ?><option value="<?= (int) $u['user_id'] ?>" <?= (int) ($wo['assigned_to'] ?? 0) === (int) $u['user_id'] ? 'selected' : '' ?>><?= e($u['username']) ?></option><?php endforeach; ?></select></div>
            <div class="col-md-3"><label class="form-label">Scheduled</label><input type="datetime-local" class="form-control form-control-sm" name="scheduled_at" value="<?= e(str_replace(' ', 'T', (string) ($wo['scheduled_at'] ?? ''))) ?>"></div>
            <div class="col-md-3"><label class="form-label">Due</label><input type="datetime-local" class="form-control form-control-sm" name="due_at" value="<?= e(str_replace(' ', 'T', (string) ($wo['due_at'] ?? ''))) ?>"></div>
            <div class="col-md-3"><label class="form-label">Est. Hours</label><input type="number" step="0.25" class="form-control form-control-sm" name="estimated_hours" value="<?= e((string) ($wo['estimated_hours'] ?? '')) ?>"></div>
            <div class="col-md-3"><label class="form-label">Actual Hours</label><input type="number" step="0.25" class="form-control form-control-sm" name="actual_hours" value="<?= e((string) ($wo['actual_hours'] ?? '')) ?>"></div>
            <div class="col-12"><label class="form-label">Notes</label><textarea class="form-control form-control-sm" name="notes" rows="2"><?= e($wo['notes'] ?? '') ?></textarea></div>
        </div>
    </div>

    <div class="d-flex gap-2">
        <button type="submit" class="btn btn-sm btn-primary"><i class="fas fa-save me-1"></i>Save</button>
        <?php if (!empty($wo['work_order_id'])): ?><a class="btn btn-sm btn-outline-danger" href="<?= url('/WorkOrders/delete/' . (int) $wo['work_order_id']) ?>" onclick="return confirm('Delete?')"><i class="fas fa-trash me-1"></i>Delete</a><?php endif; ?>
    </div>
</form>
