<div class="page-header">
    <h1>New Household</h1>
    <a class="button button-secondary button-sm" href="/households">← Back</a>
</div>

<div class="section-panel">
    <?php if (!empty($errors['_token'])): ?>
        <div class="alert alert-error"><?= e($errors['_token'][0]) ?></div>
    <?php endif; ?>

    <form method="POST" action="/households" novalidate>
        <?= csrf_field() ?>

        <div class="form-grid">
            <div class="field" style="grid-column:1/-1">
                <label for="territory_id">Territory <span style="color:#c0392b">*</span></label>
                <select class="input <?= isset($errors['territory_id']) ? 'input-error' : '' ?>"
                        id="territory_id" name="territory_id" required>
                    <option value="">Select a territory…</option>
                    <?php foreach ($territories as $t): ?>
                        <option value="<?= e($t['id']) ?>"
                            <?= ((string) ($old['territory_id'] ?? $defaultTerritoryId)) === (string) $t['id'] ? 'selected' : '' ?>>
                            <?= e($t['name']) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
                <?php if (isset($errors['territory_id'])): ?>
                    <span class="field-error"><?= e($errors['territory_id'][0]) ?></span>
                <?php endif; ?>
            </div>

            <div class="field" style="grid-column:1/-1">
                <label for="address">Address <span style="color:#c0392b">*</span></label>
                <input class="input <?= isset($errors['address']) ? 'input-error' : '' ?>"
                       type="text" id="address" name="address"
                       value="<?= e((string) ($old['address'] ?? '')) ?>" required autofocus>
                <?php if (isset($errors['address'])): ?>
                    <span class="field-error"><?= e($errors['address'][0]) ?></span>
                <?php endif; ?>
            </div>

            <div class="field">
                <label for="street_number">Street Number</label>
                <input class="input <?= isset($errors['street_number']) ? 'input-error' : '' ?>"
                       type="number" id="street_number" name="street_number"
                       value="<?= e((string) ($old['street_number'] ?? '')) ?>">
                <?php if (isset($errors['street_number'])): ?>
                    <span class="field-error"><?= e($errors['street_number'][0]) ?></span>
                <?php endif; ?>
            </div>

            <div class="field">
                <label for="street_name">Street Name</label>
                <input class="input" type="text" id="street_name" name="street_name"
                       value="<?= e((string) ($old['street_name'] ?? '')) ?>">
            </div>

            <div class="field">
                <label for="latitude">Latitude</label>
                <input class="input <?= isset($errors['latitude']) ? 'input-error' : '' ?>"
                       type="text" id="latitude" name="latitude"
                       value="<?= e((string) ($old['latitude'] ?? '')) ?>"
                       placeholder="e.g. 40.7128">
                <?php if (isset($errors['latitude'])): ?>
                    <span class="field-error"><?= e($errors['latitude'][0]) ?></span>
                <?php endif; ?>
            </div>

            <div class="field">
                <label for="longitude">Longitude</label>
                <input class="input <?= isset($errors['longitude']) ? 'input-error' : '' ?>"
                       type="text" id="longitude" name="longitude"
                       value="<?= e((string) ($old['longitude'] ?? '')) ?>"
                       placeholder="e.g. -74.0060">
                <?php if (isset($errors['longitude'])): ?>
                    <span class="field-error"><?= e($errors['longitude'][0]) ?></span>
                <?php endif; ?>
            </div>

            <div class="field" style="grid-column:1/-1">
                <label class="checkbox-label">
                    <input type="checkbox" name="is_business" value="1"
                           <?= !empty($old['is_business']) ? 'checked' : '' ?>>
                    This is a business
                </label>
            </div>

            <div class="field" style="grid-column:1/-1">
                <label class="checkbox-label">
                    <input type="checkbox" name="do_not_call" value="1" id="dnc_check"
                           <?= !empty($old['do_not_call']) ? 'checked' : '' ?>
                           onchange="document.getElementById('dnc_fields').style.display=this.checked?'grid':'none'">
                    Do Not Call
                </label>
            </div>

            <div id="dnc_fields" class="form-grid" style="grid-column:1/-1;display:<?= !empty($old['do_not_call']) ? 'grid' : 'none' ?>">
                <div class="field">
                    <label for="do_not_call_date">DNC Date</label>
                    <input class="input" type="date" id="do_not_call_date" name="do_not_call_date"
                           value="<?= e((string) ($old['do_not_call_date'] ?? '')) ?>">
                </div>

                <div class="field" style="grid-column:1/-1">
                    <label for="do_not_call_notes">Public Notes</label>
                    <textarea class="input" id="do_not_call_notes" name="do_not_call_notes"
                              rows="2"><?= e((string) ($old['do_not_call_notes'] ?? '')) ?></textarea>
                </div>

                <div class="field" style="grid-column:1/-1">
                    <label for="do_not_call_private_notes">Private Notes</label>
                    <textarea class="input" id="do_not_call_private_notes" name="do_not_call_private_notes"
                              rows="2"><?= e((string) ($old['do_not_call_private_notes'] ?? '')) ?></textarea>
                </div>
            </div>
        </div>

        <div class="form-actions" style="margin-top:1.5rem">
            <button class="button button-primary" type="submit">Create Household</button>
            <a class="button button-secondary" href="/households">Cancel</a>
        </div>
    </form>
</div>
