@using RazorLight
@using Elwig.Helpers
@inherits TemplatePage<Elwig.Documents.MemberDataSheet>
@model Elwig.Documents.MemberDataSheet
@{ Layout = "BusinessDocument"; }
<link rel="stylesheet" href="file:///@Raw(Model.DocumentsPath)\MemberDataSheet.css" />
<main>
    <h1>@Model.Title</h1>
    <table class="member border">
        <colgroup>
            <col style="width: 30.0mm;"/>
            <col style="width: 51.5mm;"/>
            <col style="width: 20.0mm;"/>
            <col style="width: 12.0mm;"/>
            <col style="width: 18.0mm;"/>
            <col style="width: 31.5mm;"/>
        </colgroup>
        <tbody>
            <tr class="sectionheading"><th colspan="6">Persönliche Daten</th></tr>
            <tr>
                @if (Model.Member.IsJuridicalPerson) {
                    <th colspan="3" class="small">Name</th>
                    <th colspan="3" class="small">Zu Handen</th>
                } else {
                    <th class="small">Titel (vorangestellt)</th>
                    <th class="small">Vorname</th>
                    <th colspan="3" class="small">Nachname</th>
                    <th class="small">Titel (nachgestellt)</th>
                }
            </tr>
            <tr>
                @if (Model.Member.IsJuridicalPerson) {
                    <td colspan="3" class="large">@Model.Member.Name</td>
                    <td colspan="3" class="large">@Model.Member.ForTheAttentionOf</td>
                } else {
                    <td class="large">@Model.Member.Prefix</td>
                    <td class="large">@Model.Member.GivenName @Model.Member.MiddleName</td>
                    <td class="large" colspan="3">@Model.Member.Name</td>
                    <td class="large">@Model.Member.Suffix</td>
                }
            </tr>
            <tr>
                <th>Mitglieds-Nr.:</th>
                <td>@Model.Member.MgNr</td>
                <th colspan="2">@(Model.Member.IsJuridicalPerson ? "Gründungsjahr/-tag" : "Geburtsjahr/-tag"):</th>
                <td colspan="2">@(string.Join('.', Model.Member.Birthday?.Split('-')?.Reverse() ?? Array.Empty<string>()))</td>
            </tr>
            <tr>
                <th>Adresse:</th>
                <td colspan="5">@Model.Member.Address</td>
            </tr>
            <tr>
                <th>PLZ/Ort:</th>
                <td colspan="5">
                    @Model.Member.PostalDest.AtPlz?.Plz
                    @Model.Member.PostalDest.AtPlz?.Dest
                    (@Model.Member.PostalDest.AtPlz?.Ort.Name)
                </td>
            </tr>
            <tr class="sectionheading"><th colspan="6">Rechnungsadresse (optional)</th></tr>
            <tr>
                <th>Name:</th>
                <td colspan="5">@Model.Member.BillingAddress?.FullName</td>
            </tr>
            <tr>
                <th>Adresse:</th>
                <td colspan="5">@Model.Member.BillingAddress?.Address</td>
            </tr>
            <tr>
                <th>PLZ/Ort:</th>
                <td colspan="5">
                    @if (Model.Member.BillingAddress != null) {
                        @Model.Member.BillingAddress.PostalDest.AtPlz?.Plz
                        @(" ")@Model.Member.BillingAddress.PostalDest.AtPlz?.Dest
                        @(" (")@Model.Member.BillingAddress.PostalDest.AtPlz?.Ort.Name@(")")
                    }
                </td>
            </tr>
            <tr class="sectionheading">
                <th colspan="3">Kontaktdaten</th>
                <th colspan="3" class="lborder">Bankverbindung</th>
            </tr>
            @{
                List<string?[]> subTbl1 = new();
                subTbl1.AddRange(Model.Member.EmailAddresses.Select(a => new[] { "E-Mail-Adresse", a.Address }));
                subTbl1.AddRange(Model.Member.TelephoneNumbers.Select(n => new[] { Utils.PhoneNrTypeToString(n.Type), n.Number, n.Comment }));
                subTbl1.Add(new[] { "Tel.-Nr./E-Mail-Adr.", null });

                List<string?[]> subTbl2 = new();
                subTbl2.Add(new[] { "IBAN", Model.Member.Iban != null ? Elwig.Helpers.Utils.FormatIban(Model.Member.Iban) : null });
                subTbl2.Add(new[] { "BIC", Model.Member.Bic });
            }
            @for (int i = 0; i < Math.Max(subTbl1.Count, subTbl2.Count); i++) {
                <tr>
                    <th>@(i < subTbl1.Count ? subTbl1[i][0] + ":" : "")</th>
                    @if (i < subTbl1.Count && subTbl1[i].Length >= 3 && subTbl1[i][2] != null) {
                        <td>@subTbl1[i][1]</td>
                        <td>(@subTbl1[i][2])</td>
                    } else {
                        <td colspan="2">@(i < subTbl1.Count ? subTbl1[i][1] : "")</td>
                    }

                    <th class="lborder">@(i < subTbl2.Count ? subTbl2[i][0] + ":" : "")</th>
                    <td colspan="2">@(i < subTbl2.Count ? subTbl2[i][1] : "")</td>
                </tr>
            }
            <tr class="sectionheading"><th colspan="6">Betrieb</th></tr>
            <tr>
                <th>Betriebs-Nr.:</th>
                <td>@Model.Member.LfbisNr</td>
                <th colspan="2">UID:</th>
                <td colspan="2">@Model.Member.UstIdNr</td>
            </tr>
            <tr>
                <th>Stammgemeinde:</th>
                <td>@Model.Member.DefaultKg?.Name</td>
                <th colspan="2">Buchführend:</th>
                <td colspan="2">@(Model.Member.IsBuchführend ? "Ja" : "Nein") <span class="small">(@((Model.Member.IsBuchführend ? Model.Season.VatNormal : Model.Season.VatFlatrate) * 100)% USt.)</span></td>
            </tr>
            <tr>
                <th colspan="2" class="small">(Katastralgemeinde mit dem größten Anteil an Weinbauflächen)</th>
                <th colspan="2">Bio:</th>
                <td colspan="2">@(Model.Member.IsOrganic ? "Ja" : "Nein")</td>
            </tr>
            <tr class="sectionheading"><th colspan="6">Genossenschaft</th></tr>
            <tr>
                <th>Status:</th>
                <td>
                    @(Model.Member.IsActive ? "Aktiv" : "Nicht aktiv")
                    <span class="small">
                        (@(Model.Member.ExitDate != null ?
                        $"{Model.Member.EntryDate:dd.MM.yyyy}–{Model.Member.ExitDate:dd.MM.yyyy}" :
                        $"seit {Model.Member.EntryDate:dd.MM.yyyy}"))
                    </span>
                </td>
                <th colspan="2">Geschäftsanteile:</th>
                <td colspan="2">@Model.Member.BusinessShares</td>
            </tr>
            <tr>
                <th>Stamm-Zweigstelle:</th>
                <td>@Model.Member.Branch?.Name</td>
                <th colspan="2">Volllierferant:</th>
                <td colspan="2">@(Model.Member.IsVollLieferant ? "Ja" : "Nein")</td>
            </tr>
            <tr>
                <th>Zusendungen via...</th>
                <td>
                    <i>Post:</i> @(Model.Member.ContactViaPost ? "Ja" : "Nein") –
                    <i>E-Mail:</i> @(Model.Member.ContactViaEmail ? "Ja" : "Nein")
                </td>
                <th colspan="2">Funktionär:</th>
                <td colspan="2">@(Model.Member.IsFunktionär ? "Ja" : "Nein")</td>
            </tr>
        </tbody>
    </table>

    @Raw(Model.PrintBucketTable(Model.Season, Model.MemberBuckets, includeDelivery: false))

    @{
        var areaComs = Model.ActiveAreaCommitments.GroupBy(a => a.AreaComType).Select(group => new {
            AreaComType = group.Key,
            AreaComs = group.OrderBy(c => c.Kg.AtKg.Name),
            Size = group.Sum(c => c.Area)
        }).OrderByDescending(a => a.Size).ToList();
        var lastContract = "";
    }

    @if (areaComs.Count != 0) {
        <br class="area-commitements"/>
        <h2>Flächenbindungen per @($"{Model.Date:dd.MM.yyyy}")</h2>
        <table class="area-commitements">
            <colgroup>
                <col style="width: 40mm;"/>
                <col style="width: 30mm;"/>
                <col style="width: 35mm;"/>
                <col style="width: 15mm;"/>
                <col style="width: 25mm;"/>
                <col style="width: 20mm;"/>
            </colgroup>
            <thead>
                <tr>
                    <th rowspan="2" style="text-align: left;">Katastralgemeinde</th>
                    <th rowspan="2" style="text-align: left;">Ried</th>
                    <th rowspan="2" style="text-align: left;">Parzelle(n)</th>
                    <th>Fläche</th>
                    <th rowspan="2" style="text-align: center;">Bewirt.</th>
                    <th rowspan="2" style="text-align: center;">Laufzeit</th>
                </tr>
                <tr>
                    <th>[m²]</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var contractType in areaComs) {
                    <tr class="subheading @(contractType.AreaComType.DisplayName != lastContract && lastContract != "" ? "new" : "")">
                        <th colspan="3">
                            @($"{contractType.AreaComType.WineVar.Name} {(contractType.AreaComType.WineAttr != null ? "(" + contractType.AreaComType.WineAttr + ")" : "")}")
                        </th>
                        <td class="number">@($"{contractType.Size:N0}")</td>
                        <td colspan="2"></td>
                    </tr>
                    @foreach (var areaCom in contractType.AreaComs) {
                        <tr class="area-commitment">
                            <td>@areaCom.Kg.AtKg.Name <span style="font-size: 8pt;">(@($"{areaCom.Kg.AtKg.KgNr:00000}"))</span></td>
                            <td>@areaCom.Rd?.Name</td>
                            <td class="text">@areaCom.GstNr.Replace(",", ", ").Replace("-", "–")</td>
                            <td class="number">@($"{areaCom.Area:N0}")</td>
                            <td class="center">@areaCom.WineCult?.Name</td>
                            <td class="center">@(areaCom.YearTo == null ? (areaCom.YearFrom == null ? "unbefristet" : $"ab {areaCom.YearFrom}") : (areaCom.YearFrom == null ? $"bis {areaCom.YearTo}" : $"{areaCom.YearFrom}–{areaCom.YearTo}"))</td>
                        </tr>
                        lastContract = contractType.AreaComType.DisplayName;
                    }
                }
                <tr class="sum bold">
                    <td colspan="3">Gesamt:</td>
                    <td class="number">@($"{Model.ActiveAreaCommitments.Sum(a => a.Area):N0}")</td>
                    <td colspan="2"></td>
                </tr>
            </tbody>
        </table>
    }
</main>