<?php
$report = $report ?? [];
$employees = $employees ?? [];
$visits = $visits ?? [];
$workOrders = $workOrders ?? [];

$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', $report['title'] ?? '')) ?>" placeholder="e.g. Safety inspection — Unit A">
    </div>
    <div class="enterprise-field enterprise-field--full">
        <label class="enterprise-field-label">Findings</label>
        <textarea name="findings" class="form-control" rows="4"><?= esc(old('findings', $report['findings'] ?? '')) ?></textarea>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Result</label>
        <select name="result" class="form-select">
            <?php foreach (['pending', 'pass', 'fail', 'conditional'] as $r): ?>
                <option value="<?= $r ?>" <?= old('result', $report['result'] ?? 'pending') === $r ? 'selected' : '' ?>>
                    <?= esc(ucfirst($r)) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Inspector</label>
        <select name="inspector_employee_id" class="form-select">
            <option value="">Unassigned</option>
            <?php foreach ($employees as $e): ?>
                <option value="<?= (int) $e['id'] ?>"
                    <?= (string) old('inspector_employee_id', $report['inspector_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', $report['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">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', $report['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">Inspected At</label>
        <input type="datetime-local" name="inspected_at" class="form-control"
               value="<?= esc(old('inspected_at', $toLocal($report['inspected_at'] ?? 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', $report['notes'] ?? '')) ?></textarea>
    </div>
</div>
