<?php
$row     = $row ?? [];
$lookups = $lookups ?? [];
$employees = $lookups['employees'] ?? [];
$workOrders = $lookups['workOrders'] ?? [];
$serviceVisits = $lookups['serviceVisits'] ?? [];
$isCreate = empty($row['id']);
?>
<div class="enterprise-settings-fields">
    <div class="enterprise-field">
        <label class="enterprise-field-label">Employee <span class="text-danger">*</span></label>
        <select name="employee_id" class="form-select" required>
            <option value="">Select employee</option>
            <?php foreach ($employees as $e): ?>
                <option value="<?= (int) $e['id'] ?>"
                    <?= (string) old('employee_id', $row['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">Role</label>
        <input type="text" name="role" class="form-control" maxlength="100"
               value="<?= esc(old('role', $row['role'] ?? '')) ?>" placeholder="e.g. Lead technician">
    </div>
    <?php if ($isCreate): ?>
        <div class="enterprise-field">
            <label class="enterprise-field-label">Status</label>
            <select name="status" class="form-select">
                <?php foreach (['planned', 'active'] as $s): ?>
                    <option value="<?= $s ?>" <?= old('status', $row['status'] ?? 'planned') === $s ? 'selected' : '' ?>>
                        <?= esc(ucfirst($s)) ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </div>
    <?php endif; ?>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Work Order</label>
        <select name="work_order_id" class="form-select">
            <option value="">None</option>
            <?php foreach ($workOrders as $wo): ?>
                <option value="<?= (int) $wo['id'] ?>"
                    <?= (string) old('work_order_id', $row['work_order_id'] ?? '') === (string) $wo['id'] ? 'selected' : '' ?>>
                    <?= esc($wo['number']) ?> — <?= esc($wo['title']) ?>
                </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 ($serviceVisits as $sv): ?>
                <option value="<?= (int) $sv['id'] ?>"
                    <?= (string) old('service_visit_id', $row['service_visit_id'] ?? '') === (string) $sv['id'] ? 'selected' : '' ?>>
                    <?= esc($sv['number']) ?><?= ! empty($sv['title']) ? ' — ' . esc($sv['title']) : '' ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">From Date</label>
        <input type="date" name="from_date" class="form-control"
               value="<?= esc(old('from_date', $row['from_date'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">To Date</label>
        <input type="date" name="to_date" class="form-control"
               value="<?= esc(old('to_date', $row['to_date'] ?? '')) ?>">
    </div>
    <div class="enterprise-field enterprise-field--full">
        <label class="enterprise-field-label">Notes</label>
        <textarea name="notes" class="form-control" rows="3" maxlength="500"><?= esc(old('notes', $row['notes'] ?? '')) ?></textarea>
    </div>
</div>
