2024-04-24 10:22:00 +02:00
@using Microsoft.AspNetCore.Mvc.TagHelpers
2022-11-25 02:42:55 +01:00
@model (Dictionary<string, object> Items, int Level)
@functions {
private bool IsValidURL(string source)
{
2023-05-08 11:02:13 +02:00
return Uri.TryCreate(source, UriKind.Absolute, out var uriResult) &&
(uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
2022-11-25 02:42:55 +01:00
}
}
2023-05-08 11:02:13 +02:00
2023-08-10 14:57:54 +03:00
@if (Model.Items.Any())
2023-04-11 13:21:02 +09:00
{
2024-04-24 10:22:00 +02:00
@* Use titlecase and lowercase versions for backwards-compatibility *@
string[] cartKeys = ["cart", "subtotal", "discount", "tip", "total"];
2023-05-08 11:02:13 +02:00
<table class="table my-0" v-pre>
2024-04-24 10:22:00 +02:00
@if (Model.Items.Keys.Any(key => cartKeys.Contains(key.ToLowerInvariant())))
2023-05-08 11:02:13 +02:00
{
2024-04-24 10:22:00 +02:00
_ = Model.Items.TryGetValue("cart", out var cart) || Model.Items.TryGetValue("Cart", out cart);
var hasTotal = Model.Items.TryGetValue("total", out var total) || Model.Items.TryGetValue("Total", out total);
var hasSubtotal = Model.Items.TryGetValue("subtotal", out var subtotal) || Model.Items.TryGetValue("subTotal", out subtotal) || Model.Items.TryGetValue("Subtotal", out subtotal);
var hasDiscount = Model.Items.TryGetValue("discount", out var discount) || Model.Items.TryGetValue("Discount", out discount);
var hasTip = Model.Items.TryGetValue("tip", out var tip) || Model.Items.TryGetValue("Tip", out tip);
if (cart is Dictionary<string, object> { Keys.Count: > 0 } cartDict)
2023-09-26 15:50:04 +02:00
{
<tbody>
2024-04-24 10:22:00 +02:00
@foreach (var (key, value) in cartDict)
2022-11-25 02:42:55 +01:00
{
2023-08-10 14:57:54 +03:00
<tr>
2023-08-26 13:48:48 +02:00
<th>@key</th>
2023-08-10 14:57:54 +03:00
<td class="text-end">@value</td>
</tr>
2023-05-08 11:02:13 +02:00
}
2023-09-26 15:50:04 +02:00
</tbody>
}
2024-04-24 10:22:00 +02:00
else if (cart is ICollection<object> { Count: > 0 } cartCollection)
{
<tbody>
@foreach (var value in cartCollection)
2023-08-10 14:57:54 +03:00
{
<tr>
2024-04-24 10:22:00 +02:00
<td>@value</td>
2023-08-10 14:57:54 +03:00
</tr>
}
2024-04-24 10:22:00 +02:00
</tbody>
}
<tfoot style="border-top-width:0">
@if (hasSubtotal && (hasDiscount || hasTip))
{
<tr style="border-top-width:3px">
<th>Subtotal</th>
<td class="text-end">@subtotal</td>
</tr>
}
@if (hasDiscount)
{
<tr>
<th>Discount</th>
<td class="text-end">@discount</td>
</tr>
}
@if (hasTip)
{
<tr>
<th>Tip</th>
<td class="text-end">@tip</td>
</tr>
}
@if (hasTotal)
{
<tr style="border-top-width:3px">
<th>Total</th>
<td class="text-end">@total</td>
</tr>
}
2023-08-10 14:57:54 +03:00
</tfoot>
}
else
{
foreach (var (key, value) in Model.Items)
{
<tr>
@if (value is string str)
{
if (!string.IsNullOrEmpty(key))
2023-05-08 11:02:13 +02:00
{
2023-08-26 13:48:48 +02:00
<th>@key</th>
2023-08-10 14:57:54 +03:00
}
<td style="white-space:pre-wrap">@* Explicitely remove whitespace at front here *@@if (IsValidURL(str)){<a href="@str" target="_blank" rel="noreferrer noopener">@str</a>}else {@str.Trim()}</td>
}
else if (value is Dictionary<string, object> { Count: > 0 } subItems)
{
<td colspan="2">
@{
@if (!string.IsNullOrEmpty(key))
{
2023-08-26 13:48:48 +02:00
Write(Html.Raw($"<h{Model.Level + 3} class=\"mb-3 fw-semibold\">"));
2023-08-10 14:57:54 +03:00
Write(key);
Write(Html.Raw($"</h{Model.Level + 3}>"));
}
}
<partial name="PosData" model="@((subItems, Model.Level + 1))" />
</td>
}
else if (value is IEnumerable<object> valueArray)
{
<td colspan="2">
@{
@if (!string.IsNullOrEmpty(key))
{
2023-08-26 13:48:48 +02:00
Write(Html.Raw($"<h{Model.Level + 3} class=\"mb-3 fw-semibold\">"));
2023-08-10 14:57:54 +03:00
Write(key);
Write(Html.Raw($"</h{Model.Level + 3}>"));
}
2023-05-08 11:02:13 +02:00
}
2023-08-10 14:57:54 +03:00
@foreach (var item in valueArray)
2023-05-08 11:02:13 +02:00
{
2023-08-10 14:57:54 +03:00
@if (item is Dictionary<string, object> { Count: > 0 } subItems2)
{
<partial name="PosData" model="@((subItems2, Model.Level + 1))" />
}
else
{
<partial name="PosData" model="@((new Dictionary<string, object> { { "", item } }, Model.Level + 1))" />
}
2023-05-08 11:02:13 +02:00
}
2023-08-10 14:57:54 +03:00
</td>
}
</tr>
}
2023-05-08 11:02:13 +02:00
}
</table>
2023-04-11 13:21:02 +09:00
}