{% extends "base/base.html" %}
{% block content %}
<div class="d-flex mb-3">
  <a class="btn btn-secondary" href="/export/xlsx">Export XLSX</a>
</div>

<form class="row g-3" id="liste-filter"
      hx-get=""
      hx-params="*"
      hx-trigger="change"
      hx-push-url="true" 
      hx-swap="innerHTML" 
      hx-target="body">
  <div class="col-md-2">
    <label for="Hauptbetrieb" class="form-label">Hauptbetrieb</label>
    <select id="Hauptbetrieb" name="Hauptbetrieb" class="form-select">
        <option value="">-</option>
      {% for key, value in filter.Hauptbetrieb.items() %}
        <option value="{{ key }}">{{ value }}</option>
      {% endfor %}
    </select>
  </div>
  <div class="col-md-3">
    <label for="Standort" class="form-label">Standort</label>
    <select id="Standort" name="Standort" class="form-select">
        <option value="">-</option>
      {% for key, value in filter.Standort.items() %}
        <option value="{{ key }}">{{ value }}</option>
      {% endfor %}
    </select>
  </div>
  <div class="col-md-3">
    <label for="Bereich" class="form-label">Bereich</label>
    <select id="Bereich" name="Bereich" class="form-select">
        <option value="">-</option>
    </select>
  </div>
  <div class="col-md-4">
    <label for="Verursacher" class="form-label">Verursacher</label>
    <select id="Verursacher" name="Verursacher" class="form-select">
        <option value="">-</option>
      {% for key, value in filter.Verursacher.items() %}
        {% if key == defaults.Verursacher %}
        <option value="{{ key }}" selected>{{ value }}</option>
        {% else %}
        <option value="{{ key }}">{{ value }}</option>
        {% endif %}
      {% endfor %}
    </select>
  </div>


  <div class="col-md-3">
    <label for="Rechnungsnummer" class="form-label">Rechnungsnummer</label>
    <input type="email" class="form-control" id="Rechnungsnummer" name="Rechnungsnummer">
  </div>
  <div class="col-md-2">
    <label for="RechnungsdatumVon" class="form-label">Rechnungsdatum von</label>
    <input type="date" class="form-control" id="RechnungsdatumVon" name="RechnungsdatumVon">
  </div>
  <div class="col-md-2">
    <label for="RechnungsdatumBis" class="form-label">Rechnungsdatum bis</label>
    <input type="date" class="form-control" id="RechnungsdatumBis" name="RechnungsdatumBis">
  </div>
  <div class="col-md-5">
    <label for="Kunde" class="form-label">Kunde</label>
    <select id="Kunde" name="Kunde" class="form-select">
        <option value="">-</option>
      {% for key, value in filter.Kunde.items() %}
        {% if key == defaults.Kunde %}
        <option value="{{ key }}" selected>{{ value }}</option>
        {% else %}
        <option value="{{ key }}">{{ value }}</option>
        {% endif %}
      {% endfor %}
    </select>
  </div>


  <div class="col-md-2">
    <label for="WiedervorlageVon" class="form-label">Wiedervorlage von</label>
    <input type="date" class="form-control" id="WiedervorlageVon" name="WiedervorlageVon">
  </div>
  <div class="col-md-2">
    <label for="WiedervorlageBis" class="form-label">Wiedervorlage bis</label>
    <input type="date" class="form-control" id="WiedervorlageBis" name="WiedervorlageBis">
  </div>

  <div class="col-md-4">
    <label for="Fahrzeug" class="form-label">Fahrzeug</label>
    <select id="Fahrzeug" name="Fahrzeug" class="form-select">
        <option value="">-</option>
      {% for key, value in filter.Fahrzeug.items() %}
        <option value="{{ key }}">{{ value }}</option>
      {% endfor %}
    </select>
  </div>
  <div class="col-md-2">
    <label for="Staffel" class="form-label">Staffel</label>
    <select id="Staffel" name="Staffel" class="form-select">
        <option value="">-</option>
      {% for key, value in filter.Staffel.items() %}
        <option value="{{ key }}">{{ value }}</option>
      {% endfor %}
    </select>
  </div>
  <div class="col-md-2">
    <label for="Mahnstufe" class="form-label">Mahnstufe</label>
    <select id="Mahnstufe" name="Mahnstufe" class="form-select">
        <option value="">-</option>
      {% for key, value in filter.Mahnstufe.items() %}
        {% if value == defaults[key] %}
        <option value="{{ key }}" selected>{{ value }}</option>
        {% else %}
        <option value="{{ key }}">{{ value }}</option>
        {% endif %}
      {% endfor %}
    </select>
  </div>
  <div class="col-12">
    <div class="form-check">
      <input class="form-check-input" type="checkbox" id="gridCheck">
      <label class="form-check-label" for="gridCheck">
        Guthaben anzeigen
      </label>
    </div>
  </div>
</form>

<div  id="liste-content">
<table class="table table-striped table-bordered">
  <thead>
    <tr>
        <th>.</th>
        <th>Filiale</th>
        <th>Kunde</th>
        <th>Bereich</th>
        <th>Verursacher</th>
        <th>RG-Nr.</th>
        <th>RG-Datum</th>
        <th>Tage</th>
        <th>Mahnstufe</th>
        <th>offen</th>
        <th>Kunde<br>offen ges.</th>
        <th>Kommentar</th>
    </tr>
  </thead>
  <tbody>
    {% for row in forderungen_liste %}
    <tr>
      <td>
        <a href="/app/forderungen/details/{{ row.Client_DB }}_{{ row.Document_No }}" class="btn btn-sm btn-primary"><i class="icon cil-pencil"></i></a>
      </td>
      <td>{{ row.Standort_Name }}</td>
      <td>
        <a href="/app/forderungen/liste?Kunde={{ row.Kunde|urlencode }}">{{ row.Kunde }}</a>
        {% if row.Kunde.startswith("K") %}
        <a href="mailto:dummy@xyz.de?subject={{ row.Document_No }}" class="btn btn-sm btn-outline-secondary">
            <i class="cil-at"></i>
        </a>
        {% endif %}
      </td>
      <td>{{ row.Bereich }}</td>
      <td>
        <a href="/app/forderungen/liste?Verursacher={{ row.Verursacher|urlencode }}">{{ row.Verursacher }}</a>
        {% if row.Verursacher == 'N.N.' %}
        <a class="btn btn-sm btn-outline-secondary">
            <i class="cil-user-follow"></i>
        </a>
        {% endif %}
      </td>
      <td>{{ row.Document_No }}</td>
      <td>{{ row.Invoice_Date|date_format }}</td>
      <td class="text-end">{{ row.Tage }}</td>
      <td class="text-end">{{ row.Mahnstufe }}</td>
      <td class="text-end">{{ row.offen|number_format }}</td>
      <td class="text-end">{{ row.offen_Kunde_gesamt|number_format }}</td>
      <td>{{ row.Kommentar_Fibu or '' }}</td>
    </tr>
    {% endfor %}
  </tbody>
</table>
</div>
{% endblock %}