<?php
$visit = $visit ?? [];
$employees = $employees ?? [];
$companies = $companies ?? [];
$workOrders = $workOrders ?? [];
$isEdit = ! empty($visit['id']);

$toLocal = static function (?string $dt): string {
    if (! $dt) {
        return '';
    }
    $ts = strtotime($dt);

    return $ts ? date('Y-m-d\TH:i', $ts) : '';
};
?>
<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', $visit['title'] ?? '')) ?>" placeholder="e.g. On-site equipment inspection">
    </div>
    <div class="enterprise-field enterprise-field--full">
        <label class="enterprise-field-label">Description</label>
        <textarea name="description" class="form-control" rows="3"><?= esc(old('description', $visit['description'] ?? '')) ?></textarea>
    </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', $visit['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
                ? ['draft', 'scheduled', 'en_route', 'on_site']
                : ['draft', 'scheduled'];
            foreach ($statusOptions as $s):
            ?>
                <option value="<?= $s ?>" <?= old('status', $visit['status'] ?? 'draft') === $s ? 'selected' : '' ?>>
                    <?= esc(ucwords(str_replace('_', ' ', $s))) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Company</label>
        <select name="company_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($companies as $c): ?>
                <option value="<?= (int) $c['id'] ?>"
                    <?= (string) old('company_id', $visit['company_id'] ?? '') === (string) $c['id'] ? 'selected' : '' ?>>
                    <?= esc($c['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <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', $visit['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">Technician</label>
        <select name="technician_employee_id" class="form-select">
            <option value="">Unassigned</option>
            <?php foreach ($employees as $e): ?>
                <option value="<?= (int) $e['id'] ?>"
                    <?= (string) old('technician_employee_id', $visit['technician_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">Location</label>
        <input type="text" name="location" class="form-control" maxlength="200"
               value="<?= esc(old('location', $visit['location'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Scheduled Date</label>
        <input type="date" name="scheduled_date" class="form-control"
               value="<?= esc(old('scheduled_date', $visit['scheduled_date'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Scheduled Start</label>
        <input type="datetime-local" name="scheduled_start" class="form-control"
               value="<?= esc(old('scheduled_start', $toLocal($visit['scheduled_start'] ?? null))) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Scheduled End</label>
        <input type="datetime-local" name="scheduled_end" class="form-control"
               value="<?= esc(old('scheduled_end', $toLocal($visit['scheduled_end'] ?? null))) ?>">
    </div>
    <div class="enterprise-field enterprise-field--full">
        <label class="enterprise-field-label">Notes</label>
        <textarea name="notes" class="form-control" rows="2"><?= esc(old('notes', $visit['notes'] ?? '')) ?></textarea>
    </div>
</div>
