@inherits Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>>
@using Dynamicweb.Frontend

@functions {
	private string GetFontStack()
	{
		return "font-family:initial,Arial,Helvetica Neue,Helvetica,sans-serif;";
	}

	private string GetTableReset()
	{
		if (Dynamicweb.Context.Current.Items.Contains("CurrentPageId"))
		{
			Dynamicweb.Context.Current.Items["CurrentPageId"] = Pageview.ID;
		}
		else
		{
			Dynamicweb.Context.Current.Items.Add("CurrentPageId", Pageview.ID);
		}

		return "border:0;margin:0;outline:0;padding:0;";
	}

}

@{
	string textPrice = "font-variant-numeric:tabular-nums;break-inside:avoid;word-break:keep-all;white-space:nowrap;";
	string showPricesWithVat = Pageview?.Area?.EcomPricesWithVat != null ? Dynamicweb.Ecommerce.Common.Context.DisplayPricesWithVat.ToString() : "false";
	bool neverShowVat = string.IsNullOrEmpty(showPricesWithVat);

	@* Order lines *@
	<table style="table-layout:fixed;@GetTableReset()" cellpadding="0" cellspacing="0" width="100%" role="presentation">
		<tbody>

			@foreach (LoopItem orderline in GetLoop("OrderLines"))
			{
				string name = orderline.GetString("Ecom:Order:OrderLine.ProductName");
				string serverPath = $"{Dynamicweb.Context.Current?.Request.Url.Scheme}://{Dynamicweb.Context.Current?.Request.Url.Host}{(Dynamicweb.Context.Current?.Request.Url.Port > 0 ? ":" + Dynamicweb.Context.Current.Request.Url.Port : null)}";
				string image = $"{serverPath}/Admin/Public/GetImage.ashx?image={orderline.GetString("Ecom:Product.PrimaryImage")}&width=100&height=100&crop=7";
				var uri = Dynamicweb.Context.Current?.Request?.Url;
				var hostname = uri?.Scheme + Uri.SchemeDelimiter + uri?.Host;
				string priceTotalWithDiscounts = !string.IsNullOrEmpty(orderline.GetString("Ecom:Order:OrderLine.TotalPriceWithProductDiscounts.PriceFormatted")) ? orderline.GetString("Ecom:Order:OrderLine.TotalPriceWithProductDiscounts.PriceFormatted") : string.Empty;
				string unitPrice = !string.IsNullOrEmpty(orderline.GetString("Ecom:Order:OrderLine.UnitPrice.PriceFormatted")) ? orderline.GetString("Ecom:Order:OrderLine.UnitPrice.PriceFormatted") : string.Empty;
				string discountPrice = !string.IsNullOrEmpty(orderline.GetString("Ecom:Order:OrderLine.UnitPriceWithProductDiscount.PriceFormatted")) ? orderline.GetString("Ecom:Order:OrderLine.UnitPriceWithProductDiscount.PriceFormatted") : string.Empty;
				string discountTotal = !string.IsNullOrEmpty(orderline.GetString("Ecom:Order:OrderLine.TotalDiscount.PriceFormatted")) ? orderline.GetString("Ecom:Order:OrderLine.TotalDiscount.PriceFormatted") : string.Empty;
				string variantText = orderline.GetString("Ecom:Order:OrderLine.ProductVariantText");
				var quantity = orderline.GetDouble("Ecom:Order:OrderLine.Quantity");
				bool isProduct = orderline.GetBoolean("Ecom:Order:OrderLine.IsProduct");

				if (isProduct)
				{
					<tr style="@GetTableReset()">

						@* Image *@
						<td valign="top" width="108" style="@GetTableReset()padding-bottom:8px;">
							<div align="left" style="font-size:2px;">
								<img src="@image" alt="@name">
							</div>
						</td>

						<td valign="top" style="@GetTableReset()padding-bottom:8px;">

							<table style="border-collapse:collapse;table-layout:fixed;@GetTableReset()" cellpadding="0" cellspacing="0" width="100%" role="presentation">
								<tbody>
									@* Title *@
									<tr style="@GetTableReset()">
										<td style="@GetTableReset()@GetFontStack()line-height:18px;font-size:14px;font-weight:bold;text-decoration:none;">
											@name
										</td>
									</tr>

									@* Variants *@
									<tr style="@GetTableReset()">
										<td style="@GetTableReset()@GetFontStack()line-height:18px;font-size:13px;text-decoration:none;">
											@variantText
										</td>
									</tr>

									@* Unit price *@
									<tr style="@GetTableReset()">
										<td style="@GetTableReset()@GetFontStack()line-height:18px;font-size:13px;text-decoration:none;">
											@if (discountPrice == unitPrice)
											{
												<span style="@(textPrice)">@unitPrice</span>
											}
											else
											{
												<span style="@(textPrice)">@discountPrice</span>
												<span style="@(textPrice) text-decoration:line-through;">@unitPrice</span>
											}
										</td>
									</tr>

									@* Quantity *@
									<tr style="@GetTableReset()">
										<td style="@GetTableReset()@GetFontStack()line-height:18px;font-size:13px;text-decoration:none;">
											@Translate("Qty"): @quantity
										</td>
									</tr>
								</tbody>
							</table>
						</td>

						@* Orderline total *@
						<td valign="top" width="108" style="@GetTableReset()padding-bottom:8px;">
							<table style="border-collapse:collapse;table-layout:fixed;@GetTableReset()text-align:right;" cellpadding="0" cellspacing="0" width="100%" role="presentation">
								<tbody>
									<tr style="@GetTableReset()">
										<td style="@GetTableReset()@GetFontStack()line-height:18px;font-size:14px;font-weight:bold;text-decoration:none;@(textPrice)">
											@priceTotalWithDiscounts
										</td>
									</tr>

									@if (orderline.GetDouble("Ecom:Order:OrderLine.TotalDiscount.Price.Value") != 0)
									{
										<tr style="@GetTableReset()">
											<td style="@GetTableReset()@GetFontStack()line-height:18px;font-size:13px;text-decoration:none;@(textPrice)">
												@discountTotal
											</td>
										</tr>
									}
								</tbody>
							</table>
						</td>
					</tr>
				}
			}
		</tbody>
	</table>

	@* Summary *@
	<table style="table-layout:fixed;@GetTableReset()padding-top:16px;border-top:1px solid;" cellpadding="0" cellspacing="0" width="100%" role="presentation">
		<tbody>
			<tr style="@GetTableReset()">
				@{
					double totalPriceWithoutOrderDiscountsFeesAndTaxesValue = GetDouble("Ecom:Order.TotalPriceWithoutDiscountsFeesAndTaxes.Price.Value") + GetDouble("Ecom:Order.TotalProductDiscount.Price.Value");
					var orderCurrency = Dynamicweb.Ecommerce.Services.Currencies.GetCurrency(GetString("Ecom:Order.Currency.Code"));
					string totalPriceWithoutOrderDiscountsFeesAndTaxes = Dynamicweb.Ecommerce.Services.Currencies.Format(orderCurrency, totalPriceWithoutOrderDiscountsFeesAndTaxesValue);
				}

				@* Subtotal *@
				<td valign="top" width="35%" style="@GetTableReset()@GetFontStack()line-height:18px;font-size:15px;font-weight:bold;text-decoration:none;padding-bottom:8px;">
					@Translate("Subtotal")
				</td>
				<td valign="top" width="65%" style="@GetTableReset()@GetFontStack()line-height:18px;font-size:14px;text-decoration:none;text-align:right;padding-bottom:8px;">
					@if (GetDouble("Ecom:Order.TotalPriceWithoutDiscountsFeesAndTaxes.Price.Value") != totalPriceWithoutOrderDiscountsFeesAndTaxesValue)
					{
						<span style="@(textPrice)text-decoration:line-through;">@GetString("Ecom:Order.TotalPriceWithoutDiscountsFeesAndTaxes.PriceFormatted")</span>
					}
					<span style="@(textPrice) font-weight:bold;">@totalPriceWithoutOrderDiscountsFeesAndTaxes</span>

					@if (!neverShowVat && showPricesWithVat == "false")
					{
						<div>@Translate("Excl. VAT")</div>
					}
					@if (!neverShowVat && showPricesWithVat == "true")
					{
						<div>@Translate("Incl. VAT")</div>
					}
				</td>
			</tr>

			@* Discounts (only order discounts) *@
			@foreach (LoopItem orderline in GetLoop("OrderLines"))
			{
				bool isDiscount = orderline.GetBoolean("Ecom:Order:OrderLine.IsDiscount");
				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 isOrderAmountDiscount = orderline.GetInteger("Ecom:Order:OrderLine.Type") == 1 && !isProductIdOrNumber;
				if (isDiscount && isOrderAmountDiscount)
				{
					<tr style="@GetTableReset()">
						<td valign="top" width="35%" style="@GetTableReset()@GetFontStack()line-height:18px;font-size:14px;text-decoration:none;padding-bottom:8px;">
							@orderline.GetString("Ecom:Order:OrderLine.ProductName")
						</td>
						<td valign="top" width="65%" style="@GetTableReset()@GetFontStack()@(textPrice)line-height:18px;font-size:14px;text-decoration:none;text-align:right;padding-bottom:8px;">
							@orderline.GetString("Ecom:Order:OrderLine.TotalPriceWithProductDiscounts.PriceFormatted")
						</td>
					</tr>
				}
			}

			@* Shipping *@
			@if (!string.IsNullOrEmpty(GetString("Ecom:Order.ShippingMethodID")) && GetDouble("Ecom:Order.ShippingFee.Price.Value") != 0.0)
			{
				<tr style="@GetTableReset()">
					<td valign="top" width="35%" style="@GetTableReset()@GetFontStack()line-height:18px;font-size:14px;text-decoration:none;padding-bottom:8px;">
						@Translate("Delivery")
					</td>
					<td valign="top" width="65%" style="@GetTableReset()@GetFontStack()@(textPrice)line-height:18px;font-size:14px;text-decoration:none;text-align:right;padding-bottom:8px;">
						@GetString("Ecom:Order.ShippingFee.PriceFormatted")
					</td>
				</tr>
			}

			@* Payment *@
			@if (!string.IsNullOrEmpty(GetString("Ecom:Order.PaymentMethodID")))
			{
				if (GetDouble("Ecom:Order.PaymentFee.Price.Value") != 0)
				{

					<tr style="@GetTableReset()">
						<td valign="top" width="35%" style="@GetTableReset()@GetFontStack()line-height:18px;font-size:14px;text-decoration:none;padding-bottom:8px;">
							@Translate("Payment")
						</td>
						<td valign="top" width="65%" style="@GetTableReset()@GetFontStack()@(textPrice)line-height:18px;font-size:14px;text-decoration:none;text-align:right;padding-bottom:8px;">
							@GetString("Ecom:Order.PaymentFee.PriceFormatted")
						</td>
					</tr>
				}
			}

			@* Taxes *@
			@if (GetDouble("Ecom:Order.Taxes.Total.Price.Value") != 0)
			{
				<tr style="@GetTableReset()">
					<td valign="top" width="35%" style="@GetTableReset()@GetFontStack()line-height:18px;font-size:14px;text-decoration:none;padding-bottom:8px;">
						@Translate("Sales tax")
					</td>
					<td valign="top" width="65%" style="@GetTableReset()@GetFontStack()@(textPrice)line-height:18px;font-size:14px;text-decoration:none;text-align:right;padding-bottom:8px;">
						@GetString("Ecom:Order.Taxes.Total.PriceFormatted")
					</td>
				</tr>
			}

			@* Total *@
			<tr style="@GetTableReset()">
				<td valign="top" width="35%" style="@GetTableReset()@GetFontStack()line-height:18px;font-size:18px;font-weight:bold;text-decoration:none;padding-bottom:8px;padding-top:16px;border-top:1px solid;">
					@Translate("Total")
				</td>
				<td valign="top" width="65%" style="@GetTableReset()@GetFontStack()line-height:18px;font-size:14px;text-decoration:none;text-align:right;padding-bottom:8px;padding-top:16px;border-top:1px solid;">
					<div style="@(textPrice)font-size:22px;font-weight:bold;">@GetString("Ecom:Order.Price.PriceFormatted")</div>
					@if (!neverShowVat && showPricesWithVat == "false")
					{
						<div>@Translate("Excl. VAT")</div>
					}
					@if (!neverShowVat && showPricesWithVat == "true")
					{
						<div>@Translate("Incl. VAT")</div>
					}
				</td>
			</tr>

			@if (!neverShowVat)
			{
				<tr style="@GetTableReset()">
					<td valign="top" width="35%" style="@GetTableReset()@GetFontStack()line-height:18px;font-size:14px;text-decoration:none;padding-bottom:8px;">
						@Translate("VAT")
					</td>
					<td valign="top" width="65%" style="@GetTableReset()@GetFontStack()@(textPrice)line-height:18px;font-size:14px;text-decoration:none;text-align:right;padding-bottom:8px;">
						@GetString("Ecom:Order.Price.VATFormatted")
					</td>
				</tr>
				if (showPricesWithVat == "false")
				{
					<tr style="@GetTableReset()">
						<td valign="top" width="35%" style="@GetTableReset()@GetFontStack()line-height:18px;font-size:14px;text-decoration:none;padding-bottom:8px;">
							@Translate("Total incl. VAT")
						</td>
						<td valign="top" width="65%" style="@GetTableReset()@GetFontStack()@(textPrice)line-height:18px;font-size:14px;text-decoration:none;text-align:right;padding-bottom:8px;">
							@GetString("Ecom:Order.Price.PriceWithVATFormatted")
						</td>
					</tr>
				}
			}

			@if (GetDouble("Ecom:Order.TotalDiscount.Price.Value") != 0)
			{
				<tr style="@GetTableReset()">
					<td valign="top" width="35%" style="@GetTableReset()@GetFontStack()line-height:18px;font-size:14px;text-decoration:none;padding-bottom:8px;">
						@Translate("Discount")
					</td>
					<td valign="top" width="65%" style="@GetTableReset()@GetFontStack()@(textPrice)line-height:18px;font-size:14px;text-decoration:none;text-align:right;padding-bottom:8px;">
						@GetString("Ecom:Order.TotalDiscount.PriceFormatted")
					</td>
				</tr>
			}
		</tbody>
	</table>
}
