@using RazorLight
@inherits TemplatePage<Elwig.Documents.MemberList>
@model Elwig.Documents.MemberList
@{ Layout = "Document"; }
<link rel="stylesheet" href="file:///@Raw(Model.DocumentsPath)\MemberList.css" />
<main>
    <h1>Mitgliederliste</h1>
    <h2>@Model.Filter</h2>
    <table class="members">
        <colgroup>
            <col style="width:  8mm;"/>
            @if (Model.AreaComFilters.Length > 1) {
                <col style="width: 38mm;"/>
            } else {
                <col style="width: 42mm;"/>
            }
            @if (Model.AreaComFilters.Length > 1) {
                <col style="width: 36mm;"/>
            } else {
                <col style="width: 40mm;"/>
            }
            <col style="width:  8mm;"/>
            @if (Model.AreaComFilters.Length > 1) {
                <col style="width: 18mm;"/>
            } else {
                <col style="width: 20mm;"/>
            }
            <col style="width: 12mm;"/>
            <col style="width:  5mm;"/>
            @if (Model.AreaComFilters.Length > 1) {
                <col style="width: 16mm;"/>
            } else {
                <col style="width: 18mm;"/>
            }
            <col style="width: 12mm;"/>
            @if (Model.AreaComFilters.Length > 1) {
                <col style="width: 12mm;"/>
            }
        </colgroup>
        <thead>
            <tr>
                @{
                    var headerSpan = Model.FilterAreaComs ? 3 : 2;
                }
                <th rowspan="@headerSpan">Nr.</th>
                <th rowspan="@headerSpan" style="text-align: left;">Name</th>
                <th rowspan="@headerSpan" style="text-align: left;">Adresse</th>
                <th rowspan="@headerSpan">PLZ</th>
                <th rowspan="@headerSpan" style="text-align: left;">Ort</th>
                <th rowspan="@headerSpan">Betr.-Nr.</th>
                <th rowspan="@headerSpan">GA</th>
                <th rowspan="@headerSpan" style="text-align: left;">Stamm-KG</th>
                <th colspan="@(Model.FilterAreaComs ? Model.AreaComFilters.Length : 1)">Geb. Fl.</th>
            </tr>
            @if (Model.FilterAreaComs) {
                <tr>
                    @foreach (var vtrgId in Model.AreaComFilters) {
                        <th>@vtrgId</th>
                    }
                </tr>
            }
            <tr>
                @for (int i = 0; i < Math.Max(Model.AreaComFilters.Length, 1); i++) {
                    <th class="unit">[m²]</th>
                }
            </tr>
        </thead>
        <tbody class="small">
            @{
                string? lastBranch = Model.Members.Select(m => m.Branch).Distinct().Count() == 1 ? null : "";
            }
            @foreach (var m in Model.Members) {
                if (lastBranch != null && m.Branch != lastBranch) {
                    <tr class="spacing"><td colspan="@(8 + Math.Max(Model.AreaComFilters.Length, 1))"></td></tr>
                    <tr class="header">
                        <th colspan="@(8 + Math.Max(Model.AreaComFilters.Length, 1))">@m.Branch</th>
                    </tr>
                    lastBranch = m.Branch;
                }
                <tr>
                    <td class="number" rowspan="@(m.BillingName != null ? 2 : 1)">@m.MgNr</td>
                    <td>@m.AdminName1 @m.Name2</td>
                    <td>@m.Address</td>
                    <td>@m.Plz</td>
                    <td class="tiny">@m.Locality</td>
                    <td>@m.LfbisNr</td>
                    <td class="number">@m.BusinessShares</td>
                    <td class="tiny">@m.DefaultKg</td>
                    @if (Model.AreaComFilters.Length > 0) {
                        foreach (var v in Model.AreaComFilters) {
                            <td class="number">@($"{m.AreaCommitmentsFiltered.FirstOrDefault(c => c.VtrgId == v).Area:N0}")</td>
                        }
                    } else {
                        <td class="number">@($"{m.AreaCommitment:N0}")</td>
                    }
                </tr>
                if (m.BillingName != null) {
                    <tr>
                        <td>@m.BillingName</td>
                        <td>@m.BillingAddress</td>
                        <td>@m.BillingPlz</td>
                        <td class="tiny">@m.BillingLocality</td>
                        <td colspan="4"></td>
                    </tr>
                }
            }
        </tbody>
    </table>
</main>
