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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Sales & CRM',
    'workspaceCrumbCurrent' => 'Customer History',
    'workspaceTitle'        => 'Customer Activity Timeline',
    'workspaceDesc'         => 'Calls, meetings, notes, and status changes across customer accounts.',
    'workspaceAction'       => [
        'href'  => base_url('sale/customer-history/create'),
        'label' => 'Log Event',
        'icon'  => 'fa-solid fa-plus',
    ],
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">History</h2>
        <p class="enterprise-data-panel__subtitle">Chronological record of customer interactions and system events.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('sale/customer-history') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">Search</label>
                    <input type="text" name="q" class="form-control form-control-sm" placeholder="Title or description"
                           value="<?= esc($filters['search']) ?>">
                </div>
                <div>
                    <label class="enterprise-field-label">Company</label>
                    <select name="company_id" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach ($companies as $c): ?>
                            <option value="<?= $c['id'] ?>" <?= (string) $filters['companyId'] === (string) $c['id'] ? 'selected' : '' ?>><?= esc($c['name']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="enterprise-field-label">Event Type</label>
                    <select name="event_type" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach (['note', 'call', 'email', 'meeting', 'status_change', 'document', 'other'] as $t): ?>
                            <option value="<?= $t ?>" <?= $filters['eventType'] === $t ? 'selected' : '' ?>><?= esc(ucwords(str_replace('_', ' ', $t))) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="enterprise-filter-actions">
                    <button type="submit" class="btn btn-sm btn-primary">Apply Filters</button>
                </div>
            </div>
        </form>
    </div>

    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>When</th>
                        <th>Company</th>
                        <th>Type</th>
                        <th>Title</th>
                        <th>Description</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($entries as $e): ?>
                        <tr>
                            <td class="text-nowrap small"><?= esc($e['event_at']) ?></td>
                            <td><?= esc($companyMap[$e['company_id']] ?? '—') ?></td>
                            <td><span class="badge bg-secondary-subtle text-secondary"><?= esc(ucwords(str_replace('_', ' ', $e['event_type']))) ?></span></td>
                            <td><span class="enterprise-data-table__primary"><?= esc($e['title']) ?></span></td>
                            <td class="text-muted small"><?= esc($e['description'] ?: '—') ?></td>
                            <td class="text-end enterprise-row-actions">
                                <form action="<?= base_url('sale/customer-history/' . $e['id'] . '/delete') ?>" method="post" onsubmit="return confirm('Delete this history entry?');">
                                    <?= csrf_field() ?>
                                    <button type="submit" class="btn btn-sm btn-outline-danger"><i class="fa-solid fa-trash"></i></button>
                                </form>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($entries) === 0): ?>
                        <tr><td colspan="6" class="text-center text-muted py-5">No history entries yet.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        <?php if ($pager): ?>
            <div class="enterprise-data-panel__footer"><?= $pager->links() ?></div>
        <?php endif; ?>
    </div>
</div>

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