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

@{
	string lang = Pageview.Area.CultureInfo.TwoLetterISOLanguageName;

	string showPricesWithVat = Pageview?.Area?.EcomPricesWithVat != null ? Dynamicweb.Ecommerce.Common.Context.DisplayPricesWithVat.ToString() : "false";
	bool neverShowVat = string.IsNullOrEmpty(showPricesWithVat);

	string paddingTop = "padding-top: 8px;";
	string paddingBottom = "padding-bottom: 8px;";
	string paddingLeft = "padding-left: 16px;";
	string paddingRight = "padding-right: 16px;";

	string borderTop = "border-top: 1px solid;";
	string borderBottom = "border-bottom: 1px solid;";
	string borderLeft = "border-left: 1px solid;";
	string borderRight = "border-right: 1px solid;";

	string textPrice = "font-variant-numeric: tabular-nums; white-space: nowrap;";

	string verticalAlignTop = "vertical-align: top;";

	string textRight = "text-align: right;";
	string textLeft = "text-align: left;";

	string fontBold = "font-weight: bold;";
	string fontNormal = "font-weight: normal;";

	string tableHeadingStyle1 = "font-weight: normal; text-align: left; border-top: 1px solid; border-right: 1px solid; border-left: 1px solid; padding-top: 8px; padding-right: 16px; padding-left: 16px;";
	string tableHeadingStyle2 = "font-weight: normal; text-align: left; padding-top: 8px; padding-right: 16px; padding-bottom: 8px; padding-left: 16px;";
	string tableCellStyle1 = "text-align: left; border-right: 1px solid; border-bottom: 1px solid; border-left: 1px solid; padding-right: 16px; padding-top: 8px; padding-left: 16px;";
	string tableCellStyle2 = "text-align: right; padding-top: 8px; padding-right: 16px; padding-bottom: 8px; padding-left: 16px;";
	string tableCellStyle3 = "vertical-align: top; padding-top: 8px; padding-right: 16px; padding-bottom: 8px; padding-left: 16px;";
}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional //EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--[if IE]>
	<html xmlns="http://www.w3.org/1999/xhtml" class="ie" lang="@lang">
<![endif]-->
<!--[if !IE]><!-->
<html style="margin: 0;padding: 0;" xmlns="http://www.w3.org/1999/xhtml" lang="@lang">
<!--<![endif]-->
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title></title>
	<!--[if !mso]><!-->
	<meta http-equiv="X-UA-Compatible" content="IE=edge"><!--<![endif]-->
	<meta name="viewport" content="width=device-width">
	<meta name="x-apple-disable-message-reformatting">
	<meta name="robots" content="noindex,nofollow">
	<meta property="og:title" content="">
</head>
<!--[if mso]>
	<body class="mso">
<![endif]-->
<!--[if !mso]><!-->

<body style="-webkit-text-size-adjust: 100%;">
	<!--<![endif]-->
	<table cellpadding="0" cellspacing="0" role="presentation" style="border-collapse: collapse; table-layout: fixed; min-width: 320px; width: 100%; max-width: 560px;">
		<tr>
			<th style="@(textLeft)">
				<h1>@Translate("Thank you for your order")</h1>
			</th>
		</tr>
		<tr>
			<td style="@paddingBottom">@Translate("We are processing your order now, here are the details")</td>
		</tr>
	</table>

	<table cellpadding="0" cellspacing="0" role="presentation" style="border-collapse: collapse; table-layout: fixed; min-width: 320px; width: 100%; max-width: 560px;">
		@if (!string.IsNullOrEmpty(GetString("Ecom:Order.ID")))
		{
			<tr>
				<th style="@tableHeadingStyle1">@Translate("Order number")</th>
			</tr>
			<tr>
				<td style="@tableCellStyle1">
					@if (!string.IsNullOrEmpty(GetString("Ecom:Order.IntegrationID")))
					{
						<span>@GetString("Ecom:Order.IntegrationID") (@GetString("Ecom:Order.ID"))</span>
					}
					else
					{
						@GetString("Ecom:Order.ID")
					}
				</td>
			</tr>
		}

		@if (!string.IsNullOrEmpty(GetString("Ecom:Order.Completed")))
		{
			<tr>
				<th style="@tableHeadingStyle1">@Translate("Order date")</th>
			</tr>
			<tr>
				<td style="@tableCellStyle1">@GetString("Ecom:Order.Completed")</td>
			</tr>
		}

		<tr>
			<th style="@tableHeadingStyle1">@Translate("Contact")</th>
		</tr>
		@if (!string.IsNullOrEmpty(GetString("Ecom:Order.Delivery.Email")))
		{
			<tr>
				<td style="@tableCellStyle1">
					<span>@GetString("Ecom:Order.Delivery.Email"), </span>

					@if (!string.IsNullOrEmpty(GetString("Ecom:Order.Delivery.Phone")))
					{
						<span>@GetString("Ecom:Order.Delivery.Phone")</span>
					}
				</td>
			</tr>
		}

		<tr>
			<th style="@tableHeadingStyle1">@Translate("Billing")</th>
		</tr>
		<tr>
			<td style="@tableCellStyle1">
				@{
					List<string> billingDetails = new List<string>();
					if (!string.IsNullOrEmpty(GetString("Ecom:Order.Customer.Company")))
					{
						billingDetails.Add(GetString("Ecom:Order.Customer.Company"));
					}
					if (!string.IsNullOrEmpty(GetString("Ecom:Order.Customer.Name")))
					{
						billingDetails.Add(GetString("Ecom:Order.Customer.Name"));
					}
					if (!string.IsNullOrEmpty(GetString("Ecom:Order.Customer.Address")))
					{
						billingDetails.Add(GetString("Ecom:Order.Customer.Address"));
					}
					if (!string.IsNullOrEmpty(GetString("Ecom:Order.Customer.Address2")))
					{
						billingDetails.Add(GetString("Ecom:Order.Customer.Address2"));
					}
					if (!string.IsNullOrEmpty(GetString("Ecom:Order.Customer.Zip")))
					{
						billingDetails.Add(GetString("Ecom:Order.Customer.Zip"));
					}
					if (!string.IsNullOrEmpty(GetString("Ecom:Order.Customer.City")))
					{
						billingDetails.Add(GetString("Ecom:Order.Customer.City"));
					}
					if (!string.IsNullOrEmpty(GetString("Ecom:Order.Customer.Region")))
					{
						string regionName = GetString("Ecom:Order.Customer.Region");
						foreach (LoopItem region in GetLoop("CustomerRegions"))
						{
							if (region.GetString("Ecom:CustomerRegion.IsCustomerRegion") == "true")
							{
								regionName = region.GetString("Ecom:CustomerRegion.Name");
							}
						}
						billingDetails.Add(regionName);
					}
					if (!string.IsNullOrEmpty(GetString("Ecom:Order.Customer.Country")))
					{
						billingDetails.Add(GetString("Ecom:Order.Customer.Country"));
					}

					string billingDetailsString = (string.Join(", ", billingDetails.Select(x => x).ToArray()));
					@billingDetailsString
				}
			</td>
		</tr>

		<tr>
			<th style="@tableHeadingStyle1">@Translate("Delivery")</th>
		</tr>

		<tr>
			<td style="@tableCellStyle1">
				@{
					List<string> shippingDetails = new List<string>();

					if (!string.IsNullOrEmpty(GetString("Ecom:Order.Delivery.Company")))
					{
						shippingDetails.Add(GetString("Ecom:Order.Delivery.Company"));
					}
					if (!string.IsNullOrEmpty(GetString("Ecom:Order.Delivery.Name")))
					{
						if (GetString("Ecom:Order.Delivery.Company") != GetString("Ecom:Order.Delivery.Name"))
						{
							shippingDetails.Add(GetString("Ecom:Order.Delivery.Name"));
						}
					}
					if (!string.IsNullOrEmpty(GetString("Ecom:Order.Delivery.Address")))
					{
						shippingDetails.Add(GetString("Ecom:Order.Delivery.Address"));
					}
					if (!string.IsNullOrEmpty(GetString("Ecom:Order.Delivery.Address2")))
					{
						shippingDetails.Add(GetString("Ecom:Order.Delivery.Address2"));
					}
					if (!string.IsNullOrEmpty(GetString("Ecom:Order.Delivery.Zip")))
					{
						shippingDetails.Add(GetString("Ecom:Order.Delivery.Zip"));
					}
					if (!string.IsNullOrEmpty(GetString("Ecom:Order.Delivery.City")))
					{
						shippingDetails.Add(GetString("Ecom:Order.Delivery.City"));
					}
					if (!string.IsNullOrEmpty(GetString("Ecom:Order.Delivery.Region")))
					{
						string regionName = GetString("Ecom:Order.Delivery.Region");
						foreach (LoopItem region in GetLoop("DeliveryRegions"))
						{
							if (region.GetString("Ecom:DeliveryRegion.IsDeliveryRegion") == "true")
							{
								regionName = region.GetString("Ecom:DeliveryRegion.Name");
							}
						}
						shippingDetails.Add(regionName);
					}
					if (!string.IsNullOrEmpty(GetString("Ecom:Order.Delivery.Country")))
					{
						shippingDetails.Add(GetString("Ecom:Order.Delivery.Country"));
					}

					string shippingDetailsString = (string.Join(", ", shippingDetails.Select(x => x).ToArray()));

					@shippingDetailsString
				}
			</td>
		</tr>

		@if (!string.IsNullOrEmpty(GetString("Ecom:Order.ShippingMethod")))
		{
			<tr>
				<th style="@tableHeadingStyle1">@Translate("Delivery method")</th>
			</tr>
			<tr>
				<td style="@tableCellStyle1">@GetString("Ecom:Order.ShippingMethod")</td>
			</tr>
		}

		@if (!string.IsNullOrEmpty(GetString("Ecom:Order.ShippingDate")))
		{
			<tr>
				<th style="@tableHeadingStyle1">@Translate("Delivery date")</th>
			</tr>
			<tr>
				<td style="@tableCellStyle1">@GetDate("Ecom:Order.ShippingDate").ToShortDateString()</td>
			</tr>
		}

		@if (!string.IsNullOrEmpty(GetString("Ecom:Order.PaymentMethod")))
		{
			<tr>
				<th style="@tableHeadingStyle1">@Translate("Payment method")</th>
			</tr>
			<tr>
				<td style="@tableCellStyle1">@GetString("Ecom:Order.PaymentMethod")</td>
			</tr>
		}

		@if (!string.IsNullOrEmpty(GetString("Ecom:Order.Customer.Comment")))
		{
			<tr>
				<th style="@tableHeadingStyle1">@Translate("Comments")</th>
			</tr>
			<tr>
				<td style="@tableCellStyle1">@GetString("Ecom:Order.Customer.Comment")</td>
			</tr>
		}

		@if (!string.IsNullOrEmpty(GetString("Ecom:Order.Reference")))
		{
			<tr>
				<th style="@tableHeadingStyle1">@Translate("Order reference")</th>
			</tr>
			<tr>
				<td style="@tableCellStyle1">@GetString("Ecom:Order.Reference")</td>
			</tr>
		}

		@if (!string.IsNullOrEmpty(GetString("Ecom:Order.TrackTraceNumber")) || !string.IsNullOrEmpty(GetString("Ecom:Order.TrackTraceURL")))
		{
			string label = GetString("Ecom:Order.TrackTraceName");
			label += !string.IsNullOrEmpty(GetString("Ecom:Order.TrackTraceNumber")) ? ": " + GetString("Ecom:Order.TrackTraceNumber") : "";

			<div class="list-group-item py-2">
				<div class="grid">
					<dt class="g-col-4 lh-base fw-normal text-nowrap">@Translate("Track and trace")</dt>
					<dd class="g-col-12 g-col-md-8 mb-0">
						@if (!string.IsNullOrEmpty(GetString("Ecom:Order.TrackTraceURL")))
						{
							<a href="@GetString("Ecom:Order.TrackTraceURL")" title="@GetString("Ecom:Order.TrackTraceName")">@label</a>
						}
						else
						{
							@label
						}
					</dd>
				</div>
			</div>
		}
	</table>

	<table cellpadding="0" cellspacing="0" role="presentation" style="border-collapse: collapse; table-layout: fixed; min-width: 320px; width: 100%; max-width: 560px; margin-top: 32px; @(borderTop)@(borderLeft)@(borderRight)">

		@foreach (LoopItem orderline in GetLoop("OrderLines"))
		{
			string name = orderline.GetString("Ecom:Order:OrderLine.ProductName");
			string image = $"Admin/Public/GetImage.ashx?image={Dynamicweb.Context.Current.Server.UrlEncode(orderline.GetString("Ecom:Product.PrimaryImage"))}&width=45&height=45";
			var uri = Dynamicweb.Context.Current.Request.Url;
			var hostname = uri.Scheme + Uri.SchemeDelimiter + uri.Host;
			string imagePath = hostname + "/" + image;
			string priceTotalWithDiscounts = !string.IsNullOrEmpty(orderline.GetString("Ecom:Order:OrderLine.TotalPriceWithProductDiscounts.PriceFormatted")) ? orderline.GetString("Ecom:Order:OrderLine.TotalPriceWithProductDiscounts.PriceFormatted") : "";
			string unitPrice = !string.IsNullOrEmpty(orderline.GetString("Ecom:Order:OrderLine.UnitPrice.PriceFormatted")) ? orderline.GetString("Ecom:Order:OrderLine.UnitPrice.PriceFormatted") : "";
			string discountPrice = !string.IsNullOrEmpty(orderline.GetString("Ecom:Order:OrderLine.UnitPriceWithProductDiscount.PriceFormatted")) ? orderline.GetString("Ecom:Order:OrderLine.UnitPriceWithProductDiscount.PriceFormatted") : "";
			string discountTotal = !string.IsNullOrEmpty(orderline.GetString("Ecom:Order:OrderLine.TotalDiscount.PriceFormatted")) ? orderline.GetString("Ecom:Order:OrderLine.TotalDiscount.PriceFormatted") : "";
			string variantText = orderline.GetString("Ecom:Order:OrderLine.ProductVariantText");
			var quantity = orderline.GetDouble("Ecom:Order:OrderLine.Quantity");

			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)
			{
				<tr>
					<td style="@(tableCellStyle3)@(textLeft)" width="45">
						@* Image *@
						<img src="@imagePath" alt="@name">
					</td>
					<td style="@(tableCellStyle3)@(textLeft)">
						@* Title *@
						<h3 style="@(fontNormal)">
							@name
						</h3>
						@* Variants *@
						<p>@variantText</p>
						<p>
							@if (orderline.GetBoolean("Ecom:Order:OrderLine.IsDiscount"))
							{
								@* Free product notice *@
								<p class="mt-2 mb-0 fs-7 fs-md-6 opacity-75">@Translate("Free product")</p>
							} else { 
							@* Unit price *@
								if (discountPrice == unitPrice)
							{
								<span style="@(textPrice)">@unitPrice</span>
							}
							else
							{
								<span style="@(textPrice)">@discountPrice</span>
									<span style="@(textPrice) text-decoration: line-through;">@unitPrice</span>
								}
							}
						</p>
						@* Quantity *@
						<p>@Translate("Quantity"): @quantity</p>
					</td>
					<td style="@(tableCellStyle3)@(textRight)">
						@* Total *@
						<span style="@(textPrice)">@priceTotalWithDiscounts</span>
						@if (orderline.GetDouble("Ecom:Order:OrderLine.TotalDiscount.Price.Value") != 0)
						{
							<span style="@(textPrice)">@discountTotal</span>}
					</td>
				</tr>

				foreach (var bomItem in orderline.GetLoop("BOMItems")) {
					string bomImage = bomItem.GetString("Ecom:Product.PrimaryImage");
					bomImage = !bomImage.StartsWith("/Files/", StringComparison.OrdinalIgnoreCase) ? bomImage : $"/Admin/Public/GetImage.ashx?width=45&height=45&image={bomImage}&format=webp";

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

					<tr>
						<td style="@(tableCellStyle3)@(textLeft)" width="45">
							@* Image *@
							<img src="@bomImage" alt="@bomName">
						</td>
						<td style="@(tableCellStyle3)@(textLeft)">
							@* Title *@
							<h3 style="@(fontNormal)">
								@bomName
							</h3>
						
							@* Quantity *@
							<p>@Translate("Quantity"): @bomItem.GetDouble("Ecom:Order:OrderLine.Quantity")</p>
						</td>
						<td style="@(tableCellStyle3)@(textRight)">
							@* Total - Hide when PriceType = Fixed *@
							@if (orderline.GetInteger("Ecom:Product.PriceType") != 1) { 
								<span style="@(textPrice)">@bomPriceTotalWithDiscounts</span>
								if (bomItem.GetDouble("Ecom:Order:OrderLine.TotalDiscount.Price.Value") != 0)
								{
									<span style="@(textPrice)">@bomDiscountTotal</span>
								}
							}
						</td>
					</tr>
				}
			}
		}
	</table>

	<table cellpadding="0" cellspacing="0" role="presentation" style="border-collapse: collapse; table-layout: fixed; min-width: 320px; width: 100%; max-width: 560px;@(borderTop)@(borderBottom)@(borderLeft)@(borderRight)">
		<tr>
			@{
				double totalPriceWithoutOrderDiscountsFeesAndTaxesValue = GetDouble("Ecom:Order.TotalPriceWithoutDiscountsFeesAndTaxes.Price.Value") + GetDouble("Ecom:Order.TotalProductDiscount.Price.Value");
				Currency orderCurrency = Dynamicweb.Ecommerce.Services.Currencies.GetCurrency(GetString("Ecom:Order.Currency.Code"));
				string totalPriceWithoutOrderDiscountsFeesAndTaxes = Dynamicweb.Ecommerce.Services.Currencies.Format(orderCurrency, totalPriceWithoutOrderDiscountsFeesAndTaxesValue);
			}
		
			@* Subtotal *@
			<th style="@(textLeft)@(fontBold)@(paddingTop)@(paddingBottom)@(paddingLeft)@(paddingRight)">@Translate("Subtotal")</th>
			<td style="@(textRight)@(paddingTop)@(paddingBottom)@(paddingLeft)@(paddingRight)">
				<div>
					<span style="@(textPrice)@(fontBold);">@totalPriceWithoutOrderDiscountsFeesAndTaxes</span>
					@if (GetDouble("Ecom:Order.TotalPriceWithoutDiscountsFeesAndTaxes.Price.Value") != totalPriceWithoutOrderDiscountsFeesAndTaxesValue)
					{
						<span class="text-price" style="text-decoration: line-through;">@GetString("Ecom:Order.TotalPriceWithoutDiscountsFeesAndTaxes.PriceFormatted")</span>
					}
				</div>

				@if (!neverShowVat && showPricesWithVat == "false")
				{
					<div style="font-weight: 300">@Translate("Excl. VAT")</div>
				}
				@if (!neverShowVat && showPricesWithVat == "true")
				{
					<div style="font-weight: 300">@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>
					<th style="@tableHeadingStyle2">@orderline.GetString("Ecom:Order:OrderLine.ProductName")</th>
					<td style="@(tableCellStyle2)@(textPrice)">@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>
				<th style="@tableHeadingStyle2">@Translate("Delivery")</th>
				<td style="@(tableCellStyle2)@(textPrice)">@GetString("Ecom:Order.ShippingFee.PriceFormatted")</td>
			</tr>
		}

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

				<tr>
					<th style="@tableHeadingStyle2">@Translate("Payment")</th>
					<td style="@(tableCellStyle2)@(textPrice)">@GetString("Ecom:Order.PaymentFee.PriceFormatted")</td>
				</tr>
			}
		}

		@* Taxes *@
		@if (GetDouble("Ecom:Order.Taxes.Total.Price.Value") != 0)
		{
			<tr>
				<th style="@tableHeadingStyle2">@Translate("Sales tax")</th>
				<td style="@(tableCellStyle2)@(textPrice)">@GetString("Ecom:Order.Taxes.Total.PriceFormatted")</td>
			</tr>
		}

		<tr>
			<th style="@(tableCellStyle3)@(textLeft)@(fontBold)">@Translate("Total")</th>
			<td style="@(tableCellStyle3)@(textRight)@(fontBold)">
				<div>@GetString("Ecom:Order.Price.PriceFormatted")</div>
				@if (!neverShowVat && showPricesWithVat == "false")
				{
					<div style="font-weight: 300">@Translate("Excl. VAT")</div>
				}
				@if (!neverShowVat && showPricesWithVat == "true")
				{
					<div style="font-weight: 300">@Translate("Incl. VAT")</div>
				}
			</td>
		</tr>

		@if (!neverShowVat)
		{
			<tr>
				<th style="@tableHeadingStyle2">@Translate("VAT")</th>
				<td style="@(tableCellStyle2)@(textPrice)">@GetString("Ecom:Order.Price.VATFormatted")</td>
			</tr>
			if (showPricesWithVat == "false")
			{
				<tr>
					<th style="@tableHeadingStyle2">@Translate("Total incl. VAT")</th>
					<td style="@(tableCellStyle2)@(textPrice)">@GetString("Ecom:Order.Price.PriceWithVATFormatted")</td>
				</tr>
			}
		}

		@if (GetDouble("Ecom:Order.TotalDiscount.Price.Value") != 0)
		{
			<tr>
				<th style="@tableHeadingStyle2">@Translate("Discount")</th>
				<td style="@(tableCellStyle2)@(textPrice)">@GetString("Ecom:Order.TotalDiscount.PriceFormatted")</td>
			</tr>
		}
	</table>
</body>

</html>
