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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Contracts',
    'workspaceCrumbCurrent' => 'Site History',
    'workspaceTitle'        => 'Site History',
    'workspaceDesc'         => 'Activity log for customer sites — create, update, assign, and status changes.',
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Site History</h2>
        <p class="enterprise-data-panel__subtitle">Chronological record of site changes from the existing activity log.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('contracts/sites/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="Action, description, site"
                           value="<?= esc($filters['search']) ?>">
                </div>
                <div>
                    <label class="enterprise-field-label">Site</label>
                    <select name="site_id" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach ($sites as $s): ?>
                            <option value="<?= $s['id'] ?>" <?= (string) $filters['siteId'] === (string) $s['id'] ? 'selected' : '' ?>><?= esc($s['name']) ?></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>Site</th>
                        <th>Action</th>
                        <th>Description</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($entries as $e): ?>
                        <tr>
                            <td class="text-nowrap small"><?= esc($e['created_at'] ?? '—') ?></td>
                            <td>
                                <?php if (! empty($e['record_id'])): ?>
                                    <a href="<?= base_url('contracts/sites/' . $e['record_id']) ?>" class="text-decoration-none">
                                        <?= esc($e['site_name'] ?: ('Site #' . $e['record_id'])) ?>
                                    </a>
                                <?php else: ?>
                                    —
                                <?php endif; ?>
                            </td>
                            <td><span class="badge bg-secondary-subtle text-secondary"><?= esc(ucfirst($e['action'] ?? '—')) ?></span></td>
                            <td class="text-muted small"><?= esc($e['description'] ?: '—') ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($entries) === 0): ?>
                        <tr><td colspan="4" class="text-center text-muted py-5">No site activity logged 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() ?>
