<div class="page-header">
    <h1>Territories</h1>
    <div class="page-header-actions">
        <a class="button button-primary button-sm" href="/territories/new">+ New Territory</a>
        <a class="button button-secondary button-sm" href="/export">Export</a>
    </div>
</div>

<div class="section-panel">
    <form class="filter-bar" method="GET" action="/territories">
        <div class="field" style="flex:1;min-width:220px">
            <label for="search">Search</label>
            <input class="input" type="search" id="search" name="search"
                   value="<?= e($search) ?>" placeholder="Name or description…">
        </div>
        <div class="form-actions" style="align-self:flex-end">
            <button class="button button-primary button-sm" type="submit">Filter</button>
            <?php if ($search !== ''): ?>
                <a class="button button-secondary button-sm" href="/territories">Clear</a>
            <?php endif; ?>
        </div>
    </form>

    <?php if (empty($territories)): ?>
        <div class="empty-state">
            <p>No territories found<?= $search !== '' ? ' matching "' . e($search) . '"' : '' ?>.</p>
            <?php if ($search === ''): ?>
                <p><a href="/territories/new">Create the first territory</a>.</p>
            <?php endif; ?>
        </div>
    <?php else: ?>
        <div class="table-responsive">
            <table class="data-table">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Description</th>
                        <th>Households</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($territories as $territory): ?>
                        <tr>
                            <td>
                                <a href="/territories/<?= e($territory['id']) ?>">
                                    <?= e($territory['name']) ?>
                                </a>
                            </td>
                            <td class="text-secondary"><?= e((string) ($territory['description'] ?? '')) ?></td>
                            <td><?= (int) ($counts[(int) $territory['id']] ?? 0) ?></td>
                            <td class="table-actions">
                                <a class="button button-secondary button-sm"
                                   href="/territories/<?= e($territory['id']) ?>">View</a>
                                <a class="button button-secondary button-sm"
                                   href="/territories/<?= e($territory['id']) ?>/edit">Edit</a>
                                <form method="POST" action="/territories/<?= e($territory['id']) ?>/delete"
                                      class="inline-form"
                                      onsubmit="return confirm('Delete territory \'<?= e(addslashes($territory['name'])) ?>\'?')">
                                    <?= csrf_field() ?>
                                    <button class="button button-danger button-sm" type="submit">Delete</button>
                                </form>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>

        <?php if ($pagination->hasPages()): ?>
            <div class="pagination">
                <?php if ($pagination->previousPage()): ?>
                    <a class="page-link" href="<?= e(paginate_url($pagination->previousPage())) ?>">&lsaquo; Prev</a>
                <?php endif; ?>

                <?php foreach ($pagination->pageRange() as $p): ?>
                    <a class="page-link <?= $p === $pagination->page ? 'is-active' : '' ?>"
                       href="<?= e(paginate_url($p)) ?>"><?= $p ?></a>
                <?php endforeach; ?>

                <?php if ($pagination->nextPage()): ?>
                    <a class="page-link" href="<?= e(paginate_url($pagination->nextPage())) ?>">Next &rsaquo;</a>
                <?php endif; ?>

                <span class="pagination-meta">
                    <?= number_format($pagination->total) ?> total
                </span>
            </div>
        <?php endif; ?>
    <?php endif; ?>
</div>
