<div class="page-header">
    <h1><?= e($territory['name']) ?></h1>
    <div class="page-header-actions">
        <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 this territory?')">
            <?= csrf_field() ?>
            <button class="button button-danger button-sm" type="submit">Delete</button>
        </form>
        <a class="button button-secondary button-sm" href="/territories">← Back</a>
    </div>
</div>

<?php if ($territory['description']): ?>
    <div class="section-panel" style="margin-bottom:1.25rem">
        <p style="margin:0"><?= e($territory['description']) ?></p>
    </div>
<?php endif; ?>

<div class="content-stack">
    <div class="section-panel">
        <div class="panel-header">
            <h2>Streets</h2>
        </div>

        <?php if (empty($streets)): ?>
            <div class="empty-state">
                <p>No streets recorded yet. <a href="/households/new?territory_id=<?= e($territory['id']) ?>">Add a household</a> to this territory.</p>
            </div>
        <?php else: ?>
            <div class="street-chips">
                <?php foreach ($streets as $street): ?>
                    <span class="badge badge-success"><?= e($street) ?></span>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>

    <div class="section-panel">
        <div class="panel-header" style="display:flex;justify-content:space-between;align-items:center">
            <h2 style="margin:0">Households (<?= count($households) ?>)</h2>
            <a class="button button-primary button-sm"
               href="/households/new?territory_id=<?= e($territory['id']) ?>">+ Add Household</a>
        </div>

        <?php if (empty($households)): ?>
            <div class="empty-state">
                <p>No households in this territory yet.</p>
            </div>
        <?php else: ?>
            <div class="table-responsive">
                <table class="data-table">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Address</th>
                            <th>Street</th>
                            <th>Business</th>
                            <th>DNC</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($households as $h): ?>
                            <tr>
                                <td class="text-secondary"><?= e((string) ($h['street_number'] ?? '')) ?></td>
                                <td>
                                    <a href="/households/<?= e($h['id']) ?>"><?= e($h['address']) ?></a>
                                </td>
                                <td class="text-secondary"><?= e((string) ($h['street_name'] ?? '')) ?></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 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>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>
