@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: 20mm;"/>
            <col style="width: 21mm;"/>
            <col style="width: 19mm;"/>
            <col style="width: 10mm;"/>
            <col style="width: 10mm;"/>
            <col style="width: 15mm;"/>
            <col style="width: 12mm;"/>
            <col style="width: 14mm;"/>
            <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;">Attribut</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>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 lastVariant = "";
            }
            @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.Variant != lastVariant && lastVariant != "" ? "new": "") @(rows > i + 1 ? "last" : "")">
                        @if (first) {
                            <td rowspan="@rows">@p.LsNr</td>
                            <td rowspan="@rows">@p.DPNr</td>
                            <td class="small">@p.Variant</td>
                            <td class="small">@p.Attribute</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>
                        } else {
                            <td></td>
                        }
                        @if (first) {
                            <td rowspan="@rows" class="number"></td>
                            first = false;
                        }
                    </tr>
                    lastVariant = p.Variant;
                }
            }
            <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>
            </tr>
        </tbody>
    </table>
    @Raw(BusinessDocument.PrintSortenaufteilung(Model.MemberBuckets))
    @Raw(Model.PrintBucketTable(Model.Season, Model.MemberBuckets, includePayment: true))
    <div class="text" style="margin-top: 2em;">
        @if (Model.Text != null) {
            <p class="comment" style="white-space: pre-wrap; break-inside: avoid;">@Model.Text</p>
        }
    </div>
</main>