<div class="page-header">
    <h1>Householder Names</h1>
    <a class="button button-primary button-sm" href="/householder-names/new">+ New Name</a>
</div>

<div class="section-panel">
    <form class="filter-bar" method="GET" action="/householder-names">
        <div class="field" style="flex:1;min-width:200px">
            <label for="search">Search name</label>
            <input class="input" type="search" id="search" name="search"
                   value="<?= e($search) ?>" placeholder="Name…">
        </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="/householder-names">Clear</a>
            <?php endif; ?>
        </div>
    </form>

    <?php if (empty($names)): ?>
        <div class="empty-state">
            <p>No names found.</p>
        </div>
    <?php else: ?>
        <div class="table-responsive">
            <table class="data-table">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Household</th>
                        <th>Territory</th>
                        <th>Letter Returned</th>
                        <th>Added</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($names as $n): ?>
                        <tr>
                            <td>
                                <a href="/householder-names/<?= e($n['id']) ?>">
                                    <?= e($n['name']) ?>
                                </a>
                            </td>
                            <td>
                                <a href="/households/<?= e($n['household_id']) ?>">
                                    <?= e($n['household_address']) ?>
                                </a>
                            </td>
                            <td class="text-secondary"><?= e($n['territory_name']) ?></td>
                            <td>
                                <?php if ($n['letter_returned']): ?>
                                    <span class="badge badge-success">Returned</span>
                                <?php else: ?>
                                    <span class="text-secondary">—</span>
                                <?php endif; ?>
                            </td>
                            <td class="text-secondary" style="font-size:0.85rem">
                                <?= e((string) ($n['created_at'] ?? '')) ?>
                            </td>
                            <td class="table-actions">
                                <a class="button button-secondary button-sm"
                                   href="/householder-names/<?= e($n['id']) ?>">View</a>
                                <a class="button button-secondary button-sm"
                                   href="/householder-names/<?= e($n['id']) ?>/edit">Edit</a>
                                <form method="POST"
                                      action="/householder-names/<?= e($n['id']) ?>/delete"
                                      class="inline-form"
                                      onsubmit="return confirm('Delete this 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>
