@using RazorLight
@inherits TemplatePage<Elwig.Documents.DeliveryNote>
@model Elwig.Documents.DeliveryNote
@{ Layout = "BusinessDocument"; }
<link rel="stylesheet" href="file:///@Raw(Model.DataPath)\resources\DeliveryNote.css" />
<main>
<h1>@Model.Title</h1>
<table class="delivery large">
    <colgroup>
        <col style="width: 10.00mm;"/>
        <col style="width: 21.00mm;"/>
        <col style="width: 25.00mm;"/>
        <col style="width: 19.50mm;"/>
        <col style="width: 19.50mm;"/>
        <col style="width: 30.00mm;"/>
        <col style="width: 12.50mm;"/>
        <col style="width: 12.50mm;"/>
        <col style="width: 15.00mm;"/>
    </colgroup>
    <thead>
        <tr>
            <th class="main center narrow" rowspan="2">Pos.</th>
            <th class="main" rowspan="2" colspan="2">Sorte</th>
            <th class="main" rowspan="2" colspan="2">Attribut</th>
            <th class="main" rowspan="2">Qualitätsstufe</th>
            <th colspan="2">Gradation</th>
            <th>Gewicht</th>
        </tr>
        <tr>
            <th class="unit">[°Oe]</th>
            <th class="unit narrow">[°KMW]</th>
            <th class="unit">[kg]</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var part in Model.Delivery.Parts.OrderBy(p => p.DPNr)) {
            <tr class="main">
                <td class="center">@part.DPNr</td>
                <td colspan="2">@part.Variety.Name</td>
                <td colspan="2">@part.Attribute?.Name</td>
                <td>@part.Quality.Name</td>
                <td class="center">@($"{part.Oe:N0}")</td>
                <td class="center">@($"{part.Kmw:N1}")</td>
                <td class="number">@($"{part.Weight:N0}")</td>
            </tr>
            @if (part.Cultivation != null) {
                <tr><td></td><td><i>Bewirtschaftung:</i></td><td colspan="4"><b>
                    @part.Cultivation.Name
                    @if(part.Cultivation.Description != null) {
                        @("(")@part.Cultivation.Description@(")")
                    }
                    </b></td></tr>
            }
            <tr><td></td><td colspan="5" style="white-space: pre;"><i>Herkunft:</i> @part.OriginString</td></tr>
            @if (part.Modifiers.Count() > 0) {
                var first = true;
                foreach (var mod in part.Modifiers) {
                    <tr class="tight @(first ? "first" : "")"><td></td><td>@Raw(first ? "<i>Zu-/Abschläge:</i>" : "")</td><td colspan="3"><b>@mod.Name</b></td><td style="white-space: pre;">@mod.ValueStr</td></tr>
                    first = false;
                }
            }
            <tr><td></td><td colspan="5">
                @Raw(part.IsManualWeighing ? "<i>Handwiegung</i>" : $"<i>Waage:</i> {part.ScaleId ?? "?"}, <i>ID:</i> {part.WeighingId ?? "?"}")
                (@(part.IsNetWeight ? "netto/gerebelt gewogen" : "brutto/nicht gerebelt gewogen"))@Raw(part.WeighingReason != null ? $", <i>Begründung:</i>" : "") @part.WeighingReason
            </td></tr>
            @if (part.Comment != null) {
                <tr><td></td><td colspan="5"><i>Anmerkung:</i> @part.Comment</td></tr>
            }
            @if (part.Temperature != null || part.Acid != null) {
                <tr><td></td><td colspan="5">@Raw(part.Temperature != null ? $"<i>Temperatur:</i> {part.Temperature:N1} °C" : "")@(part.Temperature != null && part.Acid != null ? ", " : "")@Raw(part.Acid != null ? $"<i>Säure:</i> {part.Acid:N1} g/l" : "")</td></tr>
            }
        }
        @if (Model.Delivery.Parts.Count() > 1) {
            <tr class="main sum bold">
                <td colspan="6">Gesamt:</td>
                <td class="center">@($"{Model.Delivery.Oe:N0}")</td>
                <td class="center">@($"{Model.Delivery.Kmw:N1}")</td>
                <td class="number">@($"{Model.Delivery.Weight:N0}")</td>
            </tr>
        }
    </tbody>
</table>
@if (Model.Delivery.Comment != null) {
    <p class="comment">Amerkung zur Lieferung: @Model.Delivery.Comment</p>
}
@if (Model.DisplayStats > 0) {
    @Raw(Model.PrintBucketTable(
        Model.Delivery.Season, Model.MemberBuckets, isTiny: true,
        filter: Model.DisplayStats > 2 ? null :
                Model.DisplayStats == 1 ? new List<string>() :
                Model.Delivery.Parts.Select(p => p.SortId).Distinct().ToList()
    ))
}
</main>
@for (int i = 0; i < 2; i++) {
    <div class="text @(i == 0 ? "hidden" : "bottom")">
        @if (Model.Text != null) {
            <p class="comment">@Model.Text</p>
        }
        <div class="signatures">
            <div>Genossenschaft</div>
            <div>Mitglied</div>
        </div>
    </div>
}
