@using Elwig.Documents
@using RazorLight
@inherits TemplatePage<Elwig.Documents.DeliveryConfirmation>
@model Elwig.Documents.DeliveryConfirmation
@{ Layout = "BusinessDocument"; }
<link rel="stylesheet" href="file:///@Raw(Model.DataPath)\resources\DeliveryConfirmation.css"/>
<main>
    <h1>@Model.Title</h1>
    <table class="delivery-confirmation">
        <colgroup>
            <col style="width: 25mm;"/>
            <col style="width:  5mm;"/>
            <col style="width: 24mm;"/>
            <col style="width: 16mm;"/>
            <col style="width: 17mm;"/>
            <col style="width: 10mm;"/>
            <col style="width: 10mm;"/>
            <col style="width: 15mm;"/>
            <col style="width: 12mm;"/>
            <col style="width: 14mm;"/>
            <col style="width:  3mm;"/>
            <col style="width: 14mm;"/>
        </colgroup>
        <thead>
            <tr>
                <th rowspan="2" style="text-align: left;">Lieferschein-Nr.</th>
                <th rowspan="2" class="narrow">Pos.</th>
                <th rowspan="2" style="text-align: left;">Sorte</th>
                <th rowspan="2" style="text-align: left;">Attr./Bewirt.</th>
                <th rowspan="2" style="text-align: left;">Qualitätsstufe</th>
                <th colspan="2">Gradation</th>
                <th colspan="2">Flächenbindung</th>
                <th>Gewicht</th>
                <th rowspan="3" style="padding: 0;">
                    <svg width="10" height="40" xmlns="http://www.w3.org/2000/svg">
                        <text x="-40" y="5" transform="rotate(270)" font-size="8pt" font-style="italic" font-family="Times New Roman"
                              style="text-anchor: start; alignment-baseline: middle;">
                            bto./nto.
                        </text>
                    </svg>
                </th>
                <th>Davon<br/>abzuwerten</th>
            </tr>
            <tr>
                <th class="unit">[°Oe]</th>
                <th class="unit narrow">[°KMW]</th>
                <th class="unit" colspan="2">[kg]</th>
                <th class="unit">[kg]</th>
                <th class="unit">[kg]</th>
            </tr>
        </thead>
        <tbody>
            @{
                var lastVariety = "";
            }
            @foreach (var p in Model.Data.Rows) {
                var rows = Math.Max(p.Buckets.Length, p.Modifiers.Length + 1);
                var first = true;
                @for (int i = 0; i < rows; i++) {
                    <tr class="@(first ? "first" : "") @(p.Variety != lastVariety && lastVariety != "" ? "new": "") @(rows > i + 1 ? "last" : "")">
                        @if (first) {
                            <td rowspan="@rows">@p.LsNr</td>
                            <td rowspan="@rows">@p.DPNr</td>
                            <td class="small">@p.Variety</td>
                            <td class="small">@p.Attribute@(p.Attribute != null && p.Cultivation != null ? " / " : "")@p.Cultivation</td>
                            <td class="small">@p.QualityLevel</td>
                            <td rowspan="@rows" class="center">@($"{p.Gradation.Oe:N0}")</td>
                            <td rowspan="@rows" class="center">@($"{p.Gradation.Kmw:N1}")</td>
                        }
                        @if (i > 0 && i <= p.Modifiers.Length) {
                            <td colspan="3" class="small mod">@(p.Modifiers[i - 1])</td>
                        } else if (i > 0) {
                            <td colspan="3"></td>
                        }
                        @if (i < p.Buckets.Length) {
                            var bucket = p.Buckets[i];
                            <td class="small">@bucket.Name:</td>
                            <td class="number">@($"{bucket.Value:N0}")</td>
                        } else {
                            <td colspan="2"></td>
                        }
                        @if (i == p.Buckets.Length - 1) {
                            <td class="number">@($"{p.Weight:N0}")</td>
                            <td class="small">@(p.IsNetWeight ? "n" : "b")</td>
                        } else {
                            <td></td>
                            <td></td>
                        }
                        @if (first) {
                            <td rowspan="@rows" class="number"></td>
                            first = false;
                        }
                    </tr>
                    lastVariety = p.Variety;
                }
            }
            <tr class="sum bold">
                <td colspan="8">Gesamt:</td>
                <td colspan="2" class="number">@($"{Model.Data.Rows.Sum(p => p.Weight):N0}")</td>
                <td></td>
                <td></td>
            </tr>
        </tbody>
    </table>
    @Raw(BusinessDocument.PrintSortenaufteilung(Model.MemberStats))
    @Raw(Model.PrintBucketTable(Model.Season, Model.MemberBuckets, includePayment: true))
    <div style="margin-top: 2em;">
        @if (Model.Text != null) {
            <p class="custom comment">@Model.Text</p>
        }
    </div>
</main>