{% extends "base.html" %}

{% block title %}Phone Book (Address + Phone) · Delphi Database{% endblock %}

{% block content %}
<div class="row g-3">
  <div class="col-12 d-flex align-items-center">
    <a class="btn btn-sm btn-outline-secondary me-2" href="/rolodex">
      <i class="bi bi-arrow-left"></i> Back
    </a>
    <h2 class="mb-0">Phone Book (Address + Phone)</h2>
    <div class="ms-auto d-flex gap-2">
      <a class="btn btn-outline-secondary btn-sm" href="/reports/phone-book-address?format=csv{% for id in client_ids %}&client_ids={{ id }}{% endfor %}{% if q %}&q={{ q | urlencode }}{% endif %}{% if phone %}&phone={{ phone | urlencode }}{% endif %}">
        <i class="bi bi-filetype-csv me-1"></i>Download CSV
      </a>
      <a class="btn btn-outline-secondary btn-sm" href="/reports/phone-book-address?format=pdf{% for id in client_ids %}&client_ids={{ id }}{% endfor %}{% if q %}&q={{ q | urlencode }}{% endif %}{% if phone %}&phone={{ phone | urlencode }}{% endif %}">
        <i class="bi bi-file-earmark-pdf me-1"></i>Download PDF
      </a>
    </div>
  </div>

  <div class="col-12">
    <div class="table-responsive">
      <table class="table table-sm align-middle">
        <thead class="table-light">
          <tr>
            <th style="width: 220px;">Name</th>
            <th>Company</th>
            <th>Address</th>
            <th style="width: 160px;">City</th>
            <th style="width: 90px;">State</th>
            <th style="width: 110px;">ZIP</th>
            <th style="width: 200px;">Phone</th>
          </tr>
        </thead>
        <tbody>
          {% if clients and clients|length > 0 %}
            {% for c in clients %}
              {% if c.phones and c.phones|length > 0 %}
                {% for p in c.phones %}
                <tr>
                  <td><span class="fw-semibold">{{ c.last_name or '' }}, {{ c.first_name or '' }}</span></td>
                  <td>{{ c.company or '' }}</td>
                  <td>{{ c.address or '' }}</td>
                  <td>{{ c.city or '' }}</td>
                  <td>{{ c.state or '' }}</td>
                  <td>{{ c.zip_code or '' }}</td>
                  <td>{{ (p.phone_type ~ ': ' if p.phone_type) ~ (p.phone_number or '') }}</td>
                </tr>
                {% endfor %}
              {% else %}
                <tr>
                  <td><span class="fw-semibold">{{ c.last_name or '' }}, {{ c.first_name or '' }}</span></td>
                  <td>{{ c.company or '' }}</td>
                  <td>{{ c.address or '' }}</td>
                  <td>{{ c.city or '' }}</td>
                  <td>{{ c.state or '' }}</td>
                  <td>{{ c.zip_code or '' }}</td>
                  <td class="text-muted">—</td>
                </tr>
              {% endif %}
            {% endfor %}
          {% else %}
            <tr><td colspan="7" class="text-center text-muted py-4">No data.</td></tr>
          {% endif %}
        </tbody>
      </table>
    </div>
  </div>
</div>
{% endblock %}



