@using RazorLight
@inherits TemplatePage<Elwig.Documents.CreditNote>
@model Elwig.Documents.CreditNote
@{ Layout = "BusinessDocument"; }
<link rel="stylesheet" href="file:///@Raw(Model.DataPath)\resources\style-creditnote.css"/>
@{
    var bucketNum = Model.BucketNames.Length;
}
<main>
    <h1>@Model.Title</h1>
    <table class="credit">
        <colgroup>
            <col style="width: 24mm;"/>
            <col style="width: 6mm;"/>
            <col style="width: 31mm;"/>
            <col style="width: 15mm;"/>
            <col style="width: 8mm;"/>
            <col style="width: 10mm;"/>
            <col style="width: 12mm;"/>
            <col style="width: 10mm;"/>
            <col style="width: 12mm;"/>
            <col style="width: 15mm;"/>
            <col style="width: 17mm;"/>
        </colgroup>
        <thead>
            <tr>
                <th rowspan="3" style="text-align: left;">Lieferschein-Nr.</th>
                <th rowspan="3">Pos.</th>
                <th rowspan="3" style="text-align: left;">Sorte</th>
                <th rowspan="3" style="text-align: left;">Attribut(e)</th>
                <th rowspan="2" colspan="2">Gradation</th>
                <th colspan="2">Zu-/Abschläge</th>
                <th colspan="2">@Raw(string.Join("<br/>", Model.BucketNames))</th>
                <th rowspan="2">Betrag</th>
            </tr>
            <tr>
                <th>Abs.</th>
                <th>Rel.</th>
                <th>Gewicht</th>
                <th>Preis</th>
            </tr>
            <tr>
                <th>[°Oe]</th>
                <th>[°KMW]</th>
                <th>[@Model.CurrencySymbol/kg]</th>
                <th>[%]</th>
                <th>[kg]</th>
                <th>[@Model.CurrencySymbol/kg]</th>
                <th>[@Model.CurrencySymbol]</th>
            </tr>
        </thead>
        <tbody>
            @{
                string FormatRow(int? weight, decimal? amount) {
                    var w = weight == null || weight == 0 ? "-" : $"{weight:N0}";
                    return $"<td class='weight'>{w}</td><td class='amount'>{amount?.ToString("0." + string.Concat(Enumerable.Repeat('0', Model.Precision)))}</td>";
                }
                string? last = null;
            }
            @foreach (var part in Model.Parts) {
                var pmt = part.Payment;
                var abs = pmt?.ModAbs == null || pmt?.ModAbs == 0 ? "-" : pmt?.ModAbs.ToString("0." + string.Concat(Enumerable.Repeat('0', Model.Precision)));
                var rel = pmt?.ModRel == null || pmt?.ModRel == 0 ? "-" : $"{pmt?.ModRel * 100:0.00##}";
                <tr class="first @(bucketNum <= 1 ? "last" : "") @(last != null && last != part.SortId ? "new" : "")">
                    <td rowspan="@bucketNum" class="lsnr">@part.Delivery.LsNr</td>
                    <td rowspan="@bucketNum" class="dpnr">@part.DPNr</td>
                    <td rowspan="@bucketNum" class="variant">@part.Variant.Name</td>
                    <td rowspan="@bucketNum" class="attribute">@string.Join(" / ", part.PartAttributes.Select(a => a.AttrId))</td>
                    <td rowspan="@bucketNum" class="oe">@($"{part.Oe:N0}")</td>
                    <td rowspan="@bucketNum" class="kmw">@($"{part.Kmw:N1}")</td>
                    <td rowspan="@bucketNum" class="abs">@abs</td>
                    <td rowspan="@bucketNum" class="rel">@rel</td>
                    @Raw(FormatRow(pmt?.Buckets?.ElementAtOrDefault(0), pmt?.Prices?.ElementAtOrDefault(0)))
                    <td rowspan="@bucketNum" class="amount sum">@($"{pmt?.Amount:N2}")</td>
                </tr>
                @for (int i = 1; i < bucketNum; i++) {
                    <tr class="@(i == bucketNum - 1 ? "last" : "")">
                        @Raw(FormatRow(pmt?.Buckets?.ElementAtOrDefault(i), pmt?.Prices?.ElementAtOrDefault(i)))
                    </tr>
                }
                last = part.SortId;
            }
        </tbody>
    </table>
</main>