<?php
$signature = $signature ?? [];
$companies = $companies ?? [];
$visits = $visits ?? [];
$reports = $reports ?? [];

$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">
        <label class="enterprise-field-label">Signer Name <span class="text-danger">*</span></label>
        <input type="text" name="signer_name" class="form-control" maxlength="150" required
               value="<?= esc(old('signer_name', $signature['signer_name'] ?? '')) ?>" placeholder="Customer contact name">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Signer Title</label>
        <input type="text" name="signer_title" class="form-control" maxlength="100"
               value="<?= esc(old('signer_title', $signature['signer_title'] ?? '')) ?>" placeholder="e.g. Facilities Manager">
    </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', $signature['company_id'] ?? '') === (string) $c['id'] ? 'selected' : '' ?>>
                    <?= esc($c['name']) ?>
                </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', $signature['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">Inspection Report</label>
        <select name="inspection_report_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($reports as $r): ?>
                <option value="<?= (int) $r['id'] ?>"
                    <?= (string) old('inspection_report_id', $signature['inspection_report_id'] ?? '') === (string) $r['id'] ? 'selected' : '' ?>>
                    <?= esc($r['number']) ?> — <?= esc($r['title']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Signed At</label>
        <input type="datetime-local" name="signed_at" class="form-control"
               value="<?= esc(old('signed_at', $toLocal($signature['signed_at'] ?? null))) ?>">
    </div>
    <div class="enterprise-field enterprise-field--full">
        <label class="enterprise-field-label">Signature Notes</label>
        <textarea name="signature_notes" class="form-control" rows="3"><?= esc(old('signature_notes', $signature['signature_notes'] ?? '')) ?></textarea>
    </div>
</div>
