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

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

<?php
$statusPill = static function (string $status): string {
    $class = $status === 'active' ? 'active' : 'inactive';
    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">' . esc(ucfirst($status)) . '</span>';
};
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Contracts',
    'workspaceCrumbCurrent' => 'Site Contacts',
    'workspaceTitle'        => 'Site Contacts',
    'workspaceDesc'         => 'Contacts for customers assigned to existing sites.',
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Site Contacts</h2>
        <p class="enterprise-data-panel__subtitle">People linked to customers that belong to a site.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('contracts/sites/contacts') ?>" 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="Name, email, company, 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>
                    <label class="enterprise-field-label">Status</label>
                    <select name="status" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach (['active', 'inactive'] as $s): ?>
                            <option value="<?= $s ?>" <?= $filters['status'] === $s ? 'selected' : '' ?>><?= ucfirst($s) ?></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>Contact</th>
                        <th>Site</th>
                        <th>Customer</th>
                        <th>Title</th>
                        <th>Email</th>
                        <th>Phone</th>
                        <th>Status</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($contacts as $c): ?>
                        <tr>
                            <td>
                                <span class="enterprise-data-table__primary"><?= esc(trim($c['first_name'] . ' ' . $c['last_name'])) ?></span>
                                <?php if (! empty($c['is_primary'])): ?>
                                    <span class="badge bg-primary-subtle text-primary ms-1">Primary</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <a href="<?= base_url('contracts/sites/' . $c['site_id']) ?>" class="text-decoration-none">
                                    <?= esc($c['site_name'] ?: '—') ?>
                                </a>
                            </td>
                            <td>
                                <a href="<?= base_url('contracts/customers/' . $c['company_id']) ?>" class="text-decoration-none">
                                    <?= esc($c['company_name'] ?: '—') ?>
                                </a>
                            </td>
                            <td><?= esc($c['job_title'] ?: '—') ?></td>
                            <td><?= esc($c['email'] ?: '—') ?></td>
                            <td><?= esc($c['phone'] ?: '—') ?></td>
                            <td><?= $statusPill($c['status']) ?></td>
                            <td class="text-end">
                                <a href="<?= base_url('sale/contacts/' . $c['id'] . '/edit') ?>" class="btn btn-sm btn-outline-secondary" title="Edit in CRM">
                                    <i class="fa-solid fa-pen"></i>
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($contacts) === 0): ?>
                        <tr><td colspan="8" class="text-center text-muted py-5">No contacts found for customers assigned to sites.</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() ?>
