@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">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>[°Oe]</th>
                <th>[°KMW]</th>
                <th colspan="2">[kg]</th>
                <th>[kg]</th>
                <th>[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 ? "trailing" : "")">
                        @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="grad">@($"{p.Gradation.Oe:N0}")</td>
                            <td rowspan="@rows" class="grad">@($"{p.Gradation.Kmw:N1}")</td>
                        }
                        @if (i > 0 && i <= p.Modifiers.Length) {
                            <td colspan="3" class="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="geb">@bucket.Name:</td>
                            <td class="weight">@($"{bucket.Value:N0}")</td>
                        } else {
                            <td colspan="2"></td>
                        }
                        @if (i == p.Buckets.Length - 1) {
                            <td class="weight">@($"{p.Weight:N0}")</td>
                        } else {
                            <td></td>
                        }
                        @if (first) {
                            <td rowspan="@rows" class="weight"></td>
                            first = false;
                        }
                    </tr>
                }
                lastVariant = p.Variant;
            }
            <tr class="sum">
                <td colspan="8">Gesamt:</td>
                <td colspan="2" class="weight">@($"{Model.Data.Rows.Sum(p => p.Weight):N0}")</td>
                <td></td>
            </tr>
        </tbody>
    </table>
    <table class="delivery-confirmation-stats">
        <colgroup>
            <col style="width: 45mm;"/>
            <col style="width: 17mm;"/>
            <col style="width: 17mm;"/>
            <col style="width: 17mm;"/>
            <col style="width: 19mm;"/>
            <col style="width: 16mm;"/>
            <col style="width: 17mm;"/>
            <col style="width: 17mm;"/>
        </colgroup>
        <thead>
            <tr>
                <th><b>Lese @Model.Season.Year</b> per @($"{Model.Date:dd.MM.yyyy}") [kg]</th>
                <th>Lieferpflicht</th>
                <th>Lieferrecht</th>
                <th>Unterliefert</th>
                <th>Noch lieferbar</th>
                <th>Überliefert</th>
                <th>Zugeteilt</th>
                <th>Geliefert</th>
            </tr>
        </thead>
        <tbody>
            @{
                string FormatRow(int mode, int obligation, int right, int sum, int? payment = null) {
                    var isGa = mode == 0;
                    payment ??= sum;
                    return $"<td>{(mode == 1 ? "" : obligation == 0 ? "-" : $"{obligation:N0}")}</td>" +
                        $"<td>{(mode == 1 ? "" : right == 0 ? "-" : $"{right:N0}")}</td>" +
                        $"<td>{(mode == 1 ? "" : payment < obligation ? $"<b>{obligation - payment:N0}\x3c/b>" : "-")}</td>" +
                        $"<td>{(mode == 1 ? "" : payment >= obligation && sum <= right ? $"{right - sum:N0}" : "-")}</td>" +
                        $"<td>{(mode == 1 ? "" : obligation == 0 && right == 0 ? "-" : (sum > right ? ((isGa ? "<b>" : "") + $"{sum - right:N0}" + (isGa ? "</b>" : "")) : "-"))}</td>" +
                        $"<td>{(mode != 2 ? "" : obligation == 0 && right == 0 ? "-" : $"{payment:N0}")}</td>" +
                        $"<td>{sum:N0}</td>";
                }
                var mBuckets = Model.MemberBuckets.Where(b => b.Value.Right > 0 || b.Value.Obligation > 0 || b.Value.Delivery > 0).ToList();
                var fbVars = mBuckets.Where(b => b.Value.Right > 0 || b.Value.Obligation > 0).Select(b => b.Key.Replace("_", "")).Order().ToArray();
                var fbs = mBuckets.Where(b => fbVars.Contains(b.Key) && b.Key.Length == 2).OrderBy(b => b.Value.Name);
                var vtr = mBuckets.Where(b => fbVars.Contains(b.Key) && b.Key.Length > 2).OrderBy(b => b.Value.Name);
                var rem = mBuckets.Where(b => !fbVars.Contains(b.Key)).OrderBy(b => b.Value.Name);
            }
            <tr>
                <th>Gesamtlieferung lt. gez. GA</th>
                @Raw(FormatRow(
                    0,
                    Model.Member.BusinessShares * Model.Season.MinKgPerBusinessShare,
                    Model.Member.BusinessShares * Model.Season.MaxKgPerBusinessShare,
                    Model.Member.Deliveries.Where(d => d.Year == Model.Season.Year).Sum(d => d.Weight)
                ))
            </tr>
            @if (rem.Any()) {
                <tr class="subheading"><th colspan="8">Sortenaufteilung:</th></tr>
            }
            @foreach (var (id, b) in rem) {
                <tr>
                    <th>@b.Name</th>
                    @Raw(FormatRow(1, b.Obligation, b.Right, b.Delivery, b.Payment))
                </tr>
            }
            @if (fbs.Any()){
                <tr class="subheading"><th colspan="8">Flächenbindungen:</th></tr>
            }
            @foreach (var (id, b) in fbs) {
                <tr>
                    <th>@b.Name</th>
                    @Raw(FormatRow(2, b.Obligation, b.Right, b.Delivery, b.Payment))
                </tr>
            }
            @if (vtr.Any()) {
                <tr class="subheading"><th colspan="8">Verträge:</th></tr>
            }
            @foreach (var (id, b) in vtr) {
                <tr>
                    <th>@b.Name</th>
                    @Raw(FormatRow(2, b.Obligation, b.Right, b.Delivery, b.Payment))
                </tr>
            }
        </tbody>
    </table>
    <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>