<?php
$entry = $entry ?? [];
$toLocal = static function (?string $dt): string {
    if (! $dt) {
        return '';
    }

    return str_replace(' ', 'T', substr($dt, 0, 16));
};
?>
<div class="enterprise-settings-fields enterprise-settings-fields--single">
    <div class="enterprise-field">
        <label class="enterprise-field-label">Subject <span class="text-danger">*</span></label>
        <input type="text" name="subject" class="form-control" value="<?= esc(old('subject', $entry['subject'] ?? '')) ?>" required>
    </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="<?= $c['id'] ?>" <?= (string) old('company_id', $entry['company_id'] ?? '') === (string) $c['id'] ? 'selected' : '' ?>>
                    <?= esc($c['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Opportunity ID</label>
        <input type="number" name="opportunity_id" class="form-control" min="1"
               value="<?= esc(old('opportunity_id', $entry['opportunity_id'] ?? '')) ?>" placeholder="Optional">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Status</label>
        <select name="status" class="form-select">
            <?php foreach (['planned', 'completed', 'cancelled'] as $s): ?>
                <option value="<?= $s ?>" <?= old('status', $entry['status'] ?? 'planned') === $s ? 'selected' : '' ?>><?= ucfirst($s) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Priority</label>
        <select name="priority" class="form-select">
            <?php foreach (['low', 'normal', 'high'] as $p): ?>
                <option value="<?= $p ?>" <?= old('priority', $entry['priority'] ?? 'normal') === $p ? 'selected' : '' ?>><?= ucfirst($p) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Scheduled At</label>
        <input type="datetime-local" name="scheduled_at" class="form-control"
               value="<?= esc(old('scheduled_at', $toLocal($entry['scheduled_at'] ?? null))) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Due At</label>
        <input type="datetime-local" name="due_at" class="form-control"
               value="<?= esc(old('due_at', $toLocal($entry['due_at'] ?? null))) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Duration (minutes)</label>
        <input type="number" name="duration_minutes" class="form-control" min="0"
               value="<?= esc(old('duration_minutes', $entry['duration_minutes'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Description</label>
        <textarea name="description" class="form-control" rows="4"><?= esc(old('description', $entry['description'] ?? '')) ?></textarea>
    </div>
</div>
