<div class="page-header">
    <h1>Households</h1>
    <a class="button button-primary button-sm" href="/households/new">+ New Household</a>
</div>

<div class="section-panel">
    <form class="filter-bar" method="GET" action="/households">
        <div class="field" style="flex:2;min-width:200px">
            <label for="search">Search</label>
            <input class="input" type="search" id="search" name="search"
                   value="<?= e($search) ?>" placeholder="Address or street name…">
        </div>

        <div class="field" style="flex:1;min-width:160px">
            <label for="territory_id">Territory</label>
            <select class="input" id="territory_id" name="territory_id">
                <option value="">All territories</option>
                <?php foreach ($territories as $t): ?>
                    <option value="<?= e($t['id']) ?>"
                        <?= $territoryId === (string) $t['id'] ? 'selected' : '' ?>>
                        <?= e($t['name']) ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </div>

        <div class="field" style="flex:1;min-width:140px">
            <label for="do_not_call">Do Not Call</label>
            <select class="input" id="do_not_call" name="do_not_call">
                <option value="">All</option>
                <option value="1" <?= $doNotCall === '1' ? 'selected' : '' ?>>DNC only</option>
                <option value="0" <?= $doNotCall === '0' ? 'selected' : '' ?>>Non-DNC only</option>
            </select>
        </div>

        <div class="form-actions" style="align-self:flex-end">
            <button class="button button-primary button-sm" type="submit">Filter</button>
            <?php if ($search !== '' || $territoryId !== '' || $doNotCall !== ''): ?>
                <a class="button button-secondary button-sm" href="/households">Clear</a>
            <?php endif; ?>
        </div>
    </form>

    <?php if (empty($households)): ?>
        <div class="empty-state">
            <p>No households found.</p>
        </div>
    <?php else: ?>
        <div class="table-responsive">
            <table class="data-table">
                <thead>
                    <tr>
                        <th>Address</th>
                        <th>Street</th>
                        <th>Territory</th>
                        <th>Type</th>
                        <th>DNC</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($households as $h): ?>
                        <tr>
                            <td>
                                <a href="/households/<?= e($h['id']) ?>">
                                    <?= e($h['address']) ?>
                                </a>
                            </td>
                            <td class="text-secondary"><?= e((string) ($h['street_name'] ?? '')) ?></td>
                            <td>
                                <a href="/territories/<?= e($h['territory_id']) ?>">
                                    <?= e($h['territory_name']) ?>
                                </a>
                            </td>
                            <td>
                                <?php if ($h['is_business']): ?>
                                    <span class="badge badge-warning">Business</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if ($h['do_not_call']): ?>
                                    <span class="badge badge-danger">DNC</span>
                                    <?php if ($h['do_not_call_date']): ?>
                                        <span class="text-secondary" style="font-size:0.8rem">
                                            <?= e($h['do_not_call_date']) ?>
                                        </span>
                                    <?php endif; ?>
                                <?php endif; ?>
                            </td>
                            <td class="table-actions">
                                <a class="button button-secondary button-sm"
                                   href="/households/<?= e($h['id']) ?>">View</a>
                                <a class="button button-secondary button-sm"
                                   href="/households/<?= e($h['id']) ?>/edit">Edit</a>
                                <form method="POST" action="/households/<?= e($h['id']) ?>/delete"
                                      class="inline-form"
                                      onsubmit="return confirm('Delete this household?')">
                                    <?= 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>
