@inherits Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>>
@using Dynamicweb.Ecommerce.ProductCatalog

@functions {
	private static string GetPrimaryImage(LoopItem orderline, string shopId)
	{
		bool isProductIdOrNumber = !string.IsNullOrEmpty(orderline.GetString("Ecom:Order:OrderLine.ProductID")) || !string.IsNullOrEmpty(orderline.GetString("Ecom:Order:OrderLine.ProductNumber"));  //Live Cart orderlines may not return ProductId. Manually entered products may not have ProductNumber
		bool isProduct = orderline.GetBoolean("Ecom:Order:OrderLine.IsProduct") || (orderline.GetBoolean("Ecom:Order:OrderLine.IsDiscount") && isProductIdOrNumber);
		string productId = orderline.GetString("Ecom:Order:OrderLine.ProductID");
		string variantId = orderline.GetString("Ecom:Order:OrderLine.ProductVariantID");

		if (!isProduct || string.IsNullOrEmpty(productId)) return "";

		ProductViewModelSettings productSetting = new ProductViewModelSettings
			{
				LanguageId = Dynamicweb.Ecommerce.Common.Context.LanguageID,
				CurrencyCode = Dynamicweb.Ecommerce.Common.Context.Currency?.Code,
				CountryCode = Dynamicweb.Ecommerce.Common.Context.Country?.Code2,
				ShopId = shopId
			};
		ProductViewModel product = ViewModelFactory.CreateView(productSetting, productId, variantId);
		if (product != null && product.DefaultImage != null && !string.IsNullOrEmpty(product.DefaultImage.Value))
			return product.DefaultImage.Value;
		return string.Empty;
	}
}

@{
	var orderlines = GetLoop("OrderLines");
}

@for (int i = orderlines.Count - 1; i >= 0; i--)
{
	var orderline = orderlines[i];
	string name = orderline.GetString("Ecom:Order:OrderLine.ProductName");
	string orderlineImage = GetPrimaryImage(orderline, GetString("Ecom:Order.ShopID"));
	string image = $"/Admin/Public/GetImage.ashx?width=180&height=180&crop=5&fillcanvas=true&Background=ffffff&image={Dynamicweb.Context.Current.Server.UrlEncode(orderlineImage)}&format=webp";
	image = !orderlineImage.StartsWith("/Files/", StringComparison.OrdinalIgnoreCase) ? orderlineImage : image;
	string removeFromBasketLink = orderline.GetString("Ecom:Order:OrderLine.DeleteLink");
	string priceTotalWithDiscounts = orderline.GetString("Ecom:Order:OrderLine.TotalPriceWithProductDiscounts.PriceFormatted");
	string priceBefore = orderline.GetString("Ecom:Product.InformativePrice.PriceFormatted");
	string unitPrice = orderline.GetString("Ecom:Order:OrderLine.UnitPrice.PriceFormatted");
	string discountPrice = orderline.GetString("Ecom:Order:OrderLine.UnitPriceWithProductDiscount.PriceFormatted");
	string discountTotal = orderline.GetString("Ecom:Order:OrderLine.TotalDiscount.PriceFormatted");
	string variantText = orderline.GetString("Ecom:Order:OrderLine.ProductVariantText");
	var orderlineId = orderline.GetValue("Ecom:Order:OrderLine.Id");
	var quantity = orderline.GetDouble("Ecom:Order:OrderLine.Quantity");
	var unitName = orderline.GetString("Ecom:Order:OrderLine.UnitName");
	string primaryGroupId = orderline.GetString("Ecom:Order:OrderLine.PrimaryOrDefaultGroupId");
	string productId = orderline.GetString("Ecom:Order:OrderLine.ProductID");
	string variantId = orderline.GetString("Ecom:Order:OrderLine.ProductVariantID");

	string link = "Default.aspx?ID=" + GetPageIdByNavigationTag("Shop");
	link += $"&GroupID={System.Net.WebUtility.UrlEncode(primaryGroupId)}";
	link += $"&ProductID={System.Net.WebUtility.UrlEncode(productId)}";
	link += !string.IsNullOrEmpty(variantId) ? $"&VariantID={System.Net.WebUtility.UrlEncode(variantId)}" : "";

	bool isProductIdOrNumber = !string.IsNullOrEmpty(orderline.GetString("Ecom:Order:OrderLine.ProductID")) || !string.IsNullOrEmpty(orderline.GetString("Ecom:Order:OrderLine.ProductNumber"));  //Live Cart orderlines may not return ProductId. Manually entered products may not have ProductNumber
	bool isProduct = orderline.GetBoolean("Ecom:Order:OrderLine.IsProduct") || (orderline.GetBoolean("Ecom:Order:OrderLine.IsDiscount") && isProductIdOrNumber);

	if (isProduct)
	{
		var bomItems = orderline.GetLoop("BOMItems");

		<article class="grid grid-12">

			<div class="g-col-2">
				<a href="@link" class="ratio ratio-1x1 d-block " title="@name">
					<img src="@image" style="object-fit: contain;" alt="@name">
				</a>
			</div>

			<div class="g-col-10">

				<div class="d-flex flex-column gap-1">
					@* Title *@
					<h3 class="h6 fs-7 m-0">
						<a href="@link" class="text-decoration-none" title="@name">@name</a>
					</h3>

					@* Variants *@
					@if (!string.IsNullOrEmpty(variantText))
					{
						<p class="fs-8 m-0">@variantText</p>
					}

					@if (orderline.GetBoolean("Ecom:Order:OrderLine.IsDiscount"))
					{
						@* Free product notice *@
						<p class="fs-8 m-0">@Translate("Free product")</p>
					}
					else
					{
						@* Unit price *@
						<p class="fs-8 m-0">
							@if (!string.IsNullOrEmpty(unitName))
							{
								<span>@unitName: </span>
							}
							@if (discountPrice == unitPrice)
							{
								<span class="text-price">@unitPrice</span>
							}
							else
							{
								<span class="text-price">@discountPrice</span>
								<span class="text-decoration-line-through">
									<span class="text-price">@unitPrice</span>
								</span>
							}
						</p>
					}
					<div class="d-flex flex-row justify-content-between">
						@* Quantity *@
						<span class="fs-8 d-block">@Translate("Qty"): @quantity</span>

						<div class="text-end flex-fill">
							@* Total *@
							<div class="fs-7 m-0">
								<span class="text-price">@priceTotalWithDiscounts</span>
							</div>
							@if (orderline.GetDouble("Ecom:Order:OrderLine.TotalDiscount.Price.Value") != 0.0)
							{
								<div class="fs-8">
									<span class="text-price">@discountTotal</span>
								</div>
							}
						</div>
					</div>
				</div>
			</div>

			@if (bomItems.Count > 0)
			{
				@* BOM items *@
				@foreach (var bomItem in bomItems)
				{
					string bomImage = bomItem.GetString("Ecom:Product.PrimaryImage");
					bomImage = !bomImage.StartsWith("/Files/", StringComparison.OrdinalIgnoreCase) ? bomImage : $"/Admin/Public/GetImage.ashx?width=120&height=100&image={bomImage}&format=webp";

					string bomPriceTotalWithDiscounts = bomItem.GetString("Ecom:Order:OrderLine.TotalPriceWithProductDiscounts.PriceFormatted");
					string bomDiscountTotal = bomItem.GetString("Ecom:Order:OrderLine.TotalDiscount.PriceFormatted");

					<div class="g-col-3 g-col-lg-2 d-flex align-items-center gap-2">
						<div class="icon-1 opacity-25">
							@ReadFile("/Files/Images/Icons/arrow-turn-down-right.svg")
						</div>
						@if (!string.IsNullOrEmpty(bomImage))
						{
							<div class="ratio ratio-1x1 d-block" title="@name">
								<img class="cart-item-img" src="@bomImage" style="object-fit: contain;" alt="@name">
							</div>
						}
					</div>
					<div class="g-col-9 g-col-lg-10 grid" style="row-gap: 0.5rem; --bs-gap:0;">
						<div class="g-col-12 g-col-md-8">
							<h3 class="fs-7 m-0">@bomItem.GetString("Ecom:Product.Name")</h3>
							<p class="fs-8 mb-0">@Translate("Qty"): @bomItem.GetDouble("Ecom:Order:OrderLine.Quantity")</p>
						</div>
						<div class="g-col-12 g-col-md-4 text-end">
							@* Total - Hide when PriceType = Fixed *@
							@if (orderline.GetInteger("Ecom:Product.PriceType") != 1)
							{
								<div class="fs-7">
									<span class="text-price">@bomPriceTotalWithDiscounts</span>
								</div>
								if (bomItem.GetDouble("Ecom:Order:OrderLine.TotalDiscount.Price.Value") != 0)
								{
									<div class="fs-8">
										<span class="text-price">@bomDiscountTotal</span>
									</div>
								}
							}
						</div>
					</div>
				}
			}
		</article>
	}
}
