<div class="page-header">
    <h1>Export Territories</h1>
    <a class="button button-secondary button-sm" href="/territories">← Territories</a>
</div>

<div class="section-panel">
    <div class="panel-header">
        <h2>Select Territories to Export</h2>
        <p>Each selected territory will be exported as an XLSX workbook (one sheet per street, split by even/odd house numbers) and a PDF. All files are packaged in a ZIP archive.</p>
    </div>

    <?php if (empty($territories)): ?>
        <div class="empty-state">
            <p>No territories found. <a href="/territories/new">Create a territory</a> first.</p>
        </div>
    <?php else: ?>
        <form method="POST" action="/export">
            <?= csrf_field() ?>

            <div style="margin-bottom:1rem">
                <button type="button" class="button button-secondary button-sm"
                        onclick="document.querySelectorAll('.territory-check').forEach(cb => cb.checked = true)">
                    Select All
                </button>
                <button type="button" class="button button-secondary button-sm"
                        onclick="document.querySelectorAll('.territory-check').forEach(cb => cb.checked = false)">
                    Deselect All
                </button>
            </div>

            <div class="territory-checklist">
                <?php foreach ($territories as $t): ?>
                    <label class="checkbox-label territory-item">
                        <input type="checkbox" class="territory-check"
                               name="territory_ids[]"
                               value="<?= e($t['id']) ?>">
                        <?= e($t['name']) ?>
                        <?php if ($t['description']): ?>
                            <span class="text-secondary" style="font-size:0.85rem">
                                — <?= e((string) ($t['description'] ?? '')) ?>
                            </span>
                        <?php endif; ?>
                    </label>
                <?php endforeach; ?>
            </div>

            <div class="form-actions" style="margin-top:1.5rem">
                <button class="button button-primary" type="submit">
                    Download ZIP
                </button>
            </div>
        </form>
    <?php endif; ?>
</div>
