<?= $this->extend('layouts/enterprise') ?>

<?= $this->section('content') ?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Sales & CRM',
    'workspaceCrumbCurrent' => 'Log Event',
    'workspaceTitle'        => 'Log Customer Event',
    'workspaceDesc'         => 'Record a call, meeting, note, or other customer interaction.',
]) ?>

<article class="enterprise-settings-card" style="max-width:640px;">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-clock-rotate-left"></i>
        <h2 class="enterprise-settings-card__title">Event Details</h2>
    </div>

    <?php if (session()->getFlashdata('errors')): ?>
        <div class="alert alert-danger py-2 small">
            <ul class="mb-0 ps-3">
                <?php foreach ((array) session()->getFlashdata('errors') as $err): ?>
                    <li><?= esc(is_array($err) ? implode(', ', $err) : $err) ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>

    <form action="<?= base_url('sale/customer-history') ?>" method="post" novalidate>
        <?= csrf_field() ?>
        <div class="enterprise-settings-fields enterprise-settings-fields--single">
            <div class="enterprise-field">
                <label class="enterprise-field-label">Company <span class="text-danger">*</span></label>
                <select name="company_id" class="form-select" required>
                    <option value="">— Select —</option>
                    <?php foreach ($companies as $c): ?>
                        <option value="<?= $c['id'] ?>" <?= (string) old('company_id', $prefillCompanyId ?? '') === (string) $c['id'] ? 'selected' : '' ?>>
                            <?= esc($c['name']) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">Event Type <span class="text-danger">*</span></label>
                <select name="event_type" class="form-select" required>
                    <?php foreach (['note', 'call', 'email', 'meeting', 'status_change', 'document', 'other'] as $t): ?>
                        <option value="<?= $t ?>" <?= old('event_type', 'note') === $t ? 'selected' : '' ?>><?= esc(ucwords(str_replace('_', ' ', $t))) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">Title <span class="text-danger">*</span></label>
                <input type="text" name="title" class="form-control" value="<?= esc(old('title')) ?>" required>
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">When <span class="text-danger">*</span></label>
                <input type="datetime-local" name="event_at" class="form-control"
                       value="<?= esc(old('event_at', date('Y-m-d\TH:i'))) ?>" required>
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">Description</label>
                <textarea name="description" class="form-control" rows="4"><?= esc(old('description')) ?></textarea>
            </div>
        </div>
        <div class="enterprise-settings-card__footer">
            <a href="<?= base_url('sale/customer-history') ?>" class="btn btn-outline-secondary">Cancel</a>
            <button type="submit" class="btn btn-primary">Log Event</button>
        </div>
    </form>
</article>

<?= $this->endSection() ?>
