<?php
$job = $job ?? [];
$employees = $employees ?? [];
$visits = $visits ?? [];
$isEdit = ! empty($job['id']);
?>
<div class="enterprise-settings-fields">
    <div class="enterprise-field enterprise-field--full">
        <label class="enterprise-field-label">Title <span class="text-danger">*</span></label>
        <input type="text" name="title" class="form-control" maxlength="200" required
               value="<?= esc(old('title', $job['title'] ?? '')) ?>" placeholder="e.g. Replace filter assembly">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Technician <span class="text-danger">*</span></label>
        <select name="employee_id" class="form-select" required>
            <option value="">Select technician</option>
            <?php foreach ($employees as $e): ?>
                <option value="<?= (int) $e['id'] ?>"
                    <?= (string) old('employee_id', $job['employee_id'] ?? '') === (string) $e['id'] ? 'selected' : '' ?>>
                    <?= esc($e['full_name']) ?> (<?= esc($e['employee_number']) ?>)
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Service Visit</label>
        <select name="service_visit_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($visits as $v): ?>
                <option value="<?= (int) $v['id'] ?>"
                    <?= (string) old('service_visit_id', $job['service_visit_id'] ?? '') === (string) $v['id'] ? 'selected' : '' ?>>
                    <?= esc($v['number']) ?> — <?= esc($v['title']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Role</label>
        <input type="text" name="role" class="form-control" maxlength="100"
               value="<?= esc(old('role', $job['role'] ?? '')) ?>" placeholder="e.g. Lead technician">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Priority</label>
        <select name="priority" class="form-select">
            <?php foreach (['low', 'normal', 'high', 'urgent'] as $p): ?>
                <option value="<?= $p ?>" <?= old('priority', $job['priority'] ?? 'normal') === $p ? 'selected' : '' ?>>
                    <?= esc(ucfirst($p)) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Status</label>
        <select name="status" class="form-select">
            <?php
            $statusOptions = $isEdit
                ? ['assigned', 'accepted', 'in_progress']
                : ['assigned', 'accepted'];
            foreach ($statusOptions as $s):
            ?>
                <option value="<?= $s ?>" <?= old('status', $job['status'] ?? 'assigned') === $s ? 'selected' : '' ?>>
                    <?= esc(ucwords(str_replace('_', ' ', $s))) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Due Date</label>
        <input type="date" name="due_date" class="form-control"
               value="<?= esc(old('due_date', $job['due_date'] ?? '')) ?>">
    </div>
    <div class="enterprise-field enterprise-field--full">
        <label class="enterprise-field-label">Notes</label>
        <textarea name="notes" class="form-control" rows="3"><?= esc(old('notes', $job['notes'] ?? '')) ?></textarea>
    </div>
</div>
