{% load helpers %}

<div class="modal-header">
    <h5 class="modal-title">Resolve Address for {{ site.name }}</h5>
    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
    {# Search form #}
    <form hx-post="{% url 'plugins:netbox_rir_manager:site_resolve_address' site.pk %}"
          hx-target="#htmx-modal-content" hx-swap="innerHTML"
          class="mb-3">
        {% csrf_token %}
        <div class="input-group">
            <input type="text" class="form-control" name="query"
                   value="{{ search_query }}" placeholder="Enter an address to search...">
            <button type="submit" class="btn btn-outline-primary">
                <i class="mdi mdi-magnify"></i> Search
            </button>
        </div>
    </form>

    {% if candidates %}
    <div class="list-group">
        {% for c in candidates %}
        <div class="list-group-item">
            <div class="d-flex justify-content-between align-items-start">
                <div>
                    <strong>{{ c.street_address|default:"—" }}</strong>,
                    {{ c.city|default:"—" }},
                    {{ c.state_province|default:"—" }}
                    {{ c.postal_code|default:"" }}
                    <span class="badge text-bg-secondary">{{ c.country }}</span>
                    {% if c.display_name %}
                    <div class="text-muted small mt-1">{{ c.display_name }}</div>
                    {% endif %}
                </div>
                <form hx-post="{% url 'plugins:netbox_rir_manager:site_select_address' site.pk %}"
                      hx-swap="none" class="ms-3 flex-shrink-0">
                    {% csrf_token %}
                    <input type="hidden" name="street_address" value="{{ c.street_address }}">
                    <input type="hidden" name="city" value="{{ c.city }}">
                    <input type="hidden" name="state_province" value="{{ c.state_province }}">
                    <input type="hidden" name="postal_code" value="{{ c.postal_code }}">
                    <input type="hidden" name="country" value="{{ c.country }}">
                    <input type="hidden" name="raw_geocode" value="{{ c.raw_json }}">
                    <button type="submit" class="btn btn-sm btn-primary">
                        <i class="mdi mdi-check"></i> Select
                    </button>
                </form>
            </div>
        </div>
        {% endfor %}
    </div>
    {% else %}
    <div class="alert alert-warning mb-0">
        <i class="mdi mdi-alert"></i>
        No address candidates found. Try entering a different address in the search field above.
    </div>
    {% endif %}
</div>
