@using RazorLight
@inherits TemplatePage<Elwig.Documents.DeliveryDepreciationList>
@model Elwig.Documents.DeliveryDepreciationList
@{ Layout = "Document"; }
<link rel="stylesheet" href="file:///@Raw(Model.DocumentsPath)\DeliveryDepreciationList.css" />
<main>
    <h1>Abwertungsliste</h1>
    <h2>@Model.Filter</h2>
    <table class="journal">
        <colgroup>
            <col style="width: 25mm;"/>
            <col style="width:  6mm;"/>
            <col style="width: 20mm;"/>
            <col style="width: 15mm;"/>
            <col style="width: 35mm;"/>
            <col style="width: 30mm;"/>
            <col style="width: 10mm;"/>
            <col style="width: 10mm;"/>
            <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">Datum</th>
                <th rowspan="2">Zeit</th>
                <th rowspan="2" style="text-align: left;">Sorte</th>
                <th rowspan="2" style="text-align: left;">Attr./Bewirt.</th>
                <th colspan="2">Gradation</th>
                <th>Menge</th>
            </tr>
            <tr>
                <th class="unit">[°Oe]</th>
                <th class="unit narrow">[°KMW]</th>
                <th class="unit">[kg]</th>
            </tr>
        </thead>
        <tbody>
            @{
                int? lastMember = null;
            }
            @foreach (var p in Model.Deliveries) {
                if (lastMember != p.MgNr) {
                    <tr class="subheading @(lastMember != null ? "new" : "")">
                        @{
                            var memberDeliveries = Model.Deliveries.Where(d => d.MgNr == p.MgNr).ToList();
                            var memberKmw = Elwig.Helpers.Utils.AggregateDeliveryPartsKmw(memberDeliveries);
                            var memberOe = Elwig.Helpers.Utils.KmwToOe(memberKmw);
                        }
                        <th colspan="5">
                            @($"{p.MgNr}, {p.AdministrativeName}")
                        </th>
                        <td>Teil-Lfrg.: <span style="float: right;">@($"{memberDeliveries.Count():N0}")</span></td>
                        <td class="center">@($"{memberOe:N0}")</td>
                        <td class="center">@($"{memberKmw:N1}")</td>
                        <td class="number">@($"{memberDeliveries.Sum(p => p.Weight):N0}")</td>
                    </tr>
                }
                <tr>
                    <td>@p.LsNr</td>
                    <td class="center narrow">@p.Pos</td>
                    <td>@($"{p.Date:dd.MM.yyyy}")</td>
                    <td>@($"{p.Time:HH:mm}")</td>
                    <td>@p.Variety</td>
                    <td>@p.Attribute@(p.Attribute != null && p.Cultivation != null ? " / " : "")@p.Cultivation</td>
                    <td class="center">@($"{p.Oe:N0}")</td>
                    <td class="center">@($"{p.Kmw:N1}")</td>
                    <td class="number">@($"{p.Weight:N0}")</td>
                </tr>
                lastMember = p.MgNr;
            }
            @{
                var branches = Model.Deliveries.Select(d => d.DeliveryBranch).Distinct().Order().ToArray();
                if (branches.Length > 1) {
                    foreach (var b in branches) {
                        <tr class="@(branches[0] == b ? "sum" : "") bold">
                            @{
                                var branchDeliveries = Model.Deliveries.Where(d => d.DeliveryBranch == b).ToList();
                                var branchKmw = Elwig.Helpers.Utils.AggregateDeliveryPartsKmw(branchDeliveries);
                                var branchOe = Elwig.Helpers.Utils.KmwToOe(branchKmw);
                            }
                            <td colspan="2">@b:</td>
                            <td colspan="4">(Teil-)Lieferungen: @($"{branchDeliveries.DistinctBy(p => p.LsNr).Count():N0}") (@($"{branchDeliveries.Count():N0}"))</td>
                            <td class="center">@($"{branchOe:N0}")</td>
                            <td class="center">@($"{branchKmw:N1}")</td>
                            <td class="number">@($"{branchDeliveries.Sum(p => p.Weight):N0}")</td>
                        </tr>
                    }
                }
            }
            <tr class="sum bold">
                @{
                    var kmw = Elwig.Helpers.Utils.AggregateDeliveryPartsKmw(Model.Deliveries);
                    var oe = Elwig.Helpers.Utils.KmwToOe(kmw);
                }
                <td colspan="2">Gesamt:</td>
                <td colspan="4">(Teil-)Lieferungen: @($"{Model.Deliveries.DistinctBy(p => p.LsNr).Count():N0}") (@($"{Model.Deliveries.Count():N0}"))</td>
                <td class="center">@($"{oe:N0}")</td>
                <td class="center">@($"{kmw:N1}")</td>
                <td class="number">@($"{Model.Deliveries.Sum(p => p.Weight):N0}")</td>
            </tr>
        </tbody>
    </table>
</main>
