<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
@page {
    size: A4 landscape;
    margin: 5mm;
}
body {
    font-family: DejaVu Sans, Helvetica, sans-serif;
    font-size: 7pt;
    margin: 0;
    padding: 0;
    color: #000;
}
table {
    width: 100%;
    border-collapse: collapse;
}
td, th {
    border: 1px solid #000;
    padding: 1px 2px;
    vertical-align: middle;
}
.header-row td {
    padding: 2px 4px;
    font-size: 7.5pt;
    text-align: left;
}
.label {
    font-weight: bold;
    background: #f2f2f2;
    white-space: nowrap;
    text-align: left;
}
.sm-header th {
    background: #2F5496;
    color: #fff;
    font-weight: bold;
    text-align: center;
    font-size: 6pt;
    padding: 2px 3px;
}
#pos-tbl td {
    padding: 2px 3px;
}
tr.trenner td {
    background: #f5f5f5;
    height: 3px;
}
tr.sum-row td {
    font-weight: bold;
    font-size: 7pt;
}
.summary-title {
    background: #D6E4F0;
    color: #2F5496;
    font-weight: bold;
    font-size: 9pt;
    text-align: center;
    padding: 3px;
}
.summary-header th {
    background: #2F5496;
    color: #fff;
    font-weight: bold;
    text-align: center;
    font-size: 6pt;
    padding: 2px;
}
.summary-data td {
    font-size: 7pt;
    padding: 1px 3px;
}
.summary-sum td {
    font-weight: bold;
    font-size: 7pt;
    border-bottom: 1px solid #000;
}
.num {
    text-align: right;
}
.center {
    text-align: center;
}
.left {
    text-align: left;
}
</style>
</head>
<body>

<!-- Row 1: Logo / Firmenname / Aufmaß -->
<table style="margin-bottom:5mm; width:100%">
<tr>
{% if company and company.logo %}
    <td style="border:none; width:30mm">
        <img src="{{ company.logo }}" width="120">
    </td>
    <td style="border:none; text-align:center; vertical-align:middle; font-size:14pt; font-weight:bold; color:#2F5496">
        Aufmaß
    </td>
</tr>
{% else %}
    <td style="border:none; font-size:14pt; font-weight:bold; color:#2F5496" colspan="2">
        {% if company and company.name %}{{ company.name }}{% else %}Aufmaß{% endif %}
    </td>
</tr>
{% endif %}
</table>

<!-- Header rows (Label-Spalten: 1,3,5,7 → jeweils 8%; Value-Spalten: 2,4,6,8 → Rest) -->
<table class="header-row">
<tr>
    <td class="label" style="width:8%">Vertrag:</td>
    <td style="width:17%">{{ _val(project.vertrag) or '' }}</td>
    <td class="label" style="width:8%">LV-Name:</td>
    <td colspan="3" style="width:42%">{{ _val(project.lv_name) or '' }}</td>
    <td class="label" style="width:8%">Aufmaß-Datum:</td>
    <td style="width:17%">{{ _fmt_date(project.datum) or '' }}</td>
</tr>
<tr>
    <td class="label">Projekt:</td>
    <td>{{ _val(project.bezeichnung) or '' }}</td>
    <td class="label">Baustelle:</td>
    <td colspan="5">{{ _val(project.baustelle) or '' }}</td>
</tr>
<tr>
    <td class="label">Typ:</td>
    <td>{{ _val(aufmass.typ if aufmass else none) or '' }}</td>
    <td class="label">Bauabschnitt:</td>
    <td colspan="5">{{ _val(project.bauabschnitt) or '' }}</td>
</tr>
<tr>
    <td class="label">SM-Nr.:</td>
    <td>{{ _val(project.sm_nr) or '' }}</td>
    <td class="label">Startdatum:</td>
    <td>{{ _fmt_date(project.datum_start) or '' }}</td>
    <td class="label">Name:</td>
    <td>{{ _val(ap_name) or '' }}</td>
    <td class="label">Tel:</td>
    <td>{{ _val(project.ansprechpartner_tel) or '' }}</td>
</tr>
<tr>
    <td class="label">Abruf-Nr.:</td>
    <td>{{ _val(project.abruf_nr) or '' }}</td>
    <td class="label">Enddatum:</td>
    <td>{{ _fmt_date(project.datum_ende) or '' }}</td>
    <td class="label">Email:</td>
    <td colspan="3">{{ _val(project.ansprechpartner_email) or '' }}</td>
</tr>
</table>

<br>

<!-- Position table (Spalten-% wie Excel max_widths) -->
<table id="pos-tbl">
<thead>
<tr class="sm-header">
    <th style="width:7%">Abschn.</th>
    <th style="width:6%">Pos-Nr</th>
    <th style="width:4%">Fakt.</th>
    <th style="width:5%">Länge</th>
    <th style="width:5%">Breite</th>
    <th style="width:5%">Tiefe</th>
    <th style="width:6%">Menge</th>
    <th style="width:3%">EH</th>
    <th style="width:23%">Kurztext</th>
    <th style="width:18%">Bemerkung</th>
    <th style="width:6%">Menge</th>
    <th style="width:6%">EP (€)</th>
    <th style="width:6%">GP (€)</th>
</tr>
</thead>
<tbody>
{% if positionen %}
{% set ns = namespace(pos_counter=0, gesamt=0) %}
{% for pos in positionen %}
{% if _ist_trenner(pos) %}
<tr class="trenner"><td colspan="13"></td></tr>
{% else %}
{% set ns.pos_counter = ns.pos_counter + 1 %}
{% set menge = pos.menge if pos.menge else none %}
{% set menge_hinten = pos.menge_hinten if pos.menge_hinten else none %}
{% if pos.einheit in ('ST', 'LE', 'STD', 'h', 'Psch') %}
{% set menge = pos.faktor * 1 if pos.faktor else none %}
{% endif %}
{% set ns.gesamt = ns.gesamt + (pos.gesamtpreis or 0) %}
<tr>
    <td class="center">{{ _val(pos.abschnitt) or '' }}</td>
    <td>{{ pos.pos_nr or '' }}</td>
    <td class="num">{{ '%.2f'|format(pos.faktor) if pos.faktor else '' }}</td>
    <td class="num">{{ '%.2f'|format(pos.laenge) if pos.laenge else '' }}</td>
    <td class="num">{{ '%.2f'|format(pos.breite) if pos.breite else '' }}</td>
    <td class="num">{{ '%.2f'|format(pos.tiefe) if pos.tiefe else '' }}</td>
    <td class="num">{{ '%.2f'|format(menge) if menge else '' }}</td>
    <td class="center">{{ pos.einheit or '' }}</td>
    <td class="left">{{ _val(pos.kurztext) or '' }}</td>
    <td class="left">{{ _val(pos.bemerkung) or '' }}</td>
    <td class="num">{{ '%.2f'|format(menge_hinten) if menge_hinten else '' }}</td>
    <td class="num">{{ '%.2f'|format(pos.einzelpreis) if pos.einzelpreis else '' }}</td>
    <td class="num">{{ '%.2f'|format(pos.gesamtpreis) if pos.gesamtpreis else '' }}</td>
</tr>
{% endif %}
{% endfor %}
<tr class="sum-row">
    <td colspan="11"></td>
    <td class="num">Summe:</td>
    <td class="num">{{ '%.2f'|format(ns.gesamt) }}</td>
</tr>
{% endif %}
</tbody>
</table>

<!-- Summary section -->
{% if ns.pos_counter > 0 %}
<br>
<table style="width:100%">
<tr><td class="summary-title" colspan="5">Mengen- und Positions-Zusammenfassung</td></tr>
<tr class="summary-header">
    <th style="width:7%">Pos-Nr</th>
    <th style="width:48%">Kurztext</th>
    <th style="width:11%">Menge</th>
    <th style="width:20%">EP (€)</th>
    <th style="width:14%">GP (€)</th>
</tr>
{% set total_gp = namespace(val=0) %}
{% for key in seen_pos %}
{% set g = groups[key] %}
{% set total_gp.val = total_gp.val + g['gp'] %}
<tr class="summary-data">
    <td class="center">{{ key }}</td>
    <td class="left" style="max-width:0; overflow:hidden; text-overflow:ellipsis; white-space:nowrap" title="{{ g['kurztext'] }}">{{ g['kurztext'][:120] + '...' if g['kurztext']|length > 120 else g['kurztext'] }}</td>
    <td class="num">{{ '%.2f'|format(g['menge']) }}</td>
    <td class="num">{{ '%.2f'|format(g['ep']) }}</td>
    <td class="num">{{ '%.2f'|format(g['gp']) }}</td>
</tr>
{% endfor %}
<tr class="summary-sum">
    <td colspan="2"></td>
    <td class="num">Summe:</td>
    <td class="num">{{ '%.2f'|format(total_gp.val) }}</td>
</tr>
</table>
{% endif %}

</body>
</html>
