<div class="page-header">
    <h1><?= e($household['address']) ?></h1>
    <div class="page-header-actions">
        <a class="button button-secondary button-sm"
           href="/households/<?= e($household['id']) ?>/edit">Edit</a>
        <form method="POST" action="/households/<?= e($household['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>
        <a class="button button-secondary button-sm" href="/households">← Back</a>
    </div>
</div>

<div class="content-stack">
    <div class="section-panel">
        <div class="panel-header"><h2>Details</h2></div>
        <dl class="detail-list">
            <dt>Territory</dt>
            <dd>
                <a href="/territories/<?= e($household['territory_id']) ?>">
                    <?= e($household['territory_name']) ?>
                </a>
            </dd>

            <?php if ($household['street_number'] || $household['street_name']): ?>
                <dt>Street</dt>
                <dd>
                    <?= e((string) ($household['street_number'] ?? '')) ?>
                    <?= e((string) ($household['street_name'] ?? '')) ?>
                </dd>
            <?php endif; ?>

            <dt>Type</dt>
            <dd>
                <?php if ($household['is_business']): ?>
                    <span class="badge badge-warning">Business</span>
                <?php else: ?>
                    Residential
                <?php endif; ?>
            </dd>

            <dt>Do Not Call</dt>
            <dd>
                <?php if ($household['do_not_call']): ?>
                    <span class="badge badge-danger">Yes</span>
                    <?php if ($household['do_not_call_date']): ?>
                        <span class="text-secondary">(since <?= e($household['do_not_call_date']) ?>)</span>
                    <?php endif; ?>
                    <?php if ($household['do_not_call_notes']): ?>
                        <p style="margin:0.5rem 0 0"><?= e($household['do_not_call_notes']) ?></p>
                    <?php endif; ?>
                <?php else: ?>
                    No
                <?php endif; ?>
            </dd>

            <?php if ($household['latitude'] && $household['longitude']): ?>
                <dt>Coordinates</dt>
                <dd><?= e((string) $household['latitude']) ?>, <?= e((string) $household['longitude']) ?></dd>
            <?php endif; ?>
        </dl>
    </div>

    <?php if ($household['latitude'] && $household['longitude'] && $maps['maptiler_api_key'] !== ''): ?>
        <div class="section-panel">
            <div class="panel-header"><h2>Map</h2></div>
            <link rel="stylesheet" href="<?= e($maps['maptiler_sdk_css']) ?>">
            <div id="household-map" style="height:380px;border-radius:1rem;overflow:hidden"></div>
            <script src="<?= e($maps['maptiler_sdk_js']) ?>"></script>
            <script>
                maptilersdk.config.apiKey = <?= json_encode($maps['maptiler_api_key']) ?>;
                const map = new maptilersdk.Map({
                    container: 'household-map',
                    style:     <?= json_encode($maps['maptiler_style']) ?>,
                    center:    [<?= (float) $household['longitude'] ?>, <?= (float) $household['latitude'] ?>],
                    zoom:      15
                });
                new maptilersdk.Marker()
                    .setLngLat([<?= (float) $household['longitude'] ?>, <?= (float) $household['latitude'] ?>])
                    .addTo(map);
            </script>
        </div>
    <?php endif; ?>

    <div class="section-panel">
        <div class="panel-header" style="display:flex;justify-content:space-between;align-items:center">
            <h2 style="margin:0">Householder Names (<?= count($names) ?>)</h2>
            <a class="button button-primary button-sm"
               href="/householder-names/new?household_id=<?= e($household['id']) ?>">+ Add Name</a>
        </div>

        <?php if (empty($names)): ?>
            <div class="empty-state">
                <p>No names recorded for this household.</p>
            </div>
        <?php else: ?>
            <div class="table-responsive">
                <table class="data-table">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Added</th>
                            <th>Letter Returned</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($names as $n): ?>
                            <tr>
                                <td><?= e($n['name']) ?></td>
                                <td class="text-secondary">
                                    <?= e((string) ($n['created_at'] ?? '')) ?>
                                </td>
                                <td>
                                    <?php if ($n['letter_returned']): ?>
                                        <span class="badge badge-success">Returned</span>
                                        <?php if ($n['return_date']): ?>
                                            <span class="text-secondary" style="font-size:0.8rem">
                                                <?= e($n['return_date']) ?>
                                            </span>
                                        <?php endif; ?>
                                    <?php else: ?>
                                        <span class="text-secondary">—</span>
                                    <?php endif; ?>
                                </td>
                                <td class="table-actions">
                                    <form method="POST"
                                          action="/householder-names/<?= e($n['id']) ?>/mark-returned"
                                          class="inline-form">
                                        <?= csrf_field() ?>
                                        <button class="button button-secondary button-sm" type="submit">
                                            <?= $n['letter_returned'] ? 'Unmark' : 'Mark Returned' ?>
                                        </button>
                                    </form>
                                    <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 endif; ?>
    </div>
</div>
