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

@{
	int frontPageId = Dynamicweb.Content.Services.Pages.GetFirstPageForArea(Pageview.AreaID).ID;
}

<style>
	.sticky-receipt {
		position: sticky;
		background-color: var(--bs-body-bg);
	}

	@@media print {
		.sticky-receipt {
			position: relative;
		}
	}
</style>

<div class="grid p-3 h-100" data-dw-colorscheme="@Pageview.CurrentParagraph?.ColorSchemeId">
	<div class="g-col-12 g-col-sm-10 g-start-sm-2 g-col-md-8 g-start-md-3 g-col-xl-6 g-start-xl-4 position-relative">
		<form name="ordersubmit" id="ordersubmit" method="post" autocomplete="off">

			<header>
				<div class="mt-3 mt-md-4">
					@Include("Helpers/Logo.cshtml")
				</div>
				<div class="border-bottom mt-4 pb-3">
					<div class="d-flex justify-content-between mb-2">
						<div class="flex-fill">
							<h1 class="fs-1 fs-md-3 m-0">@Translate("Thank you for your order")</h1>
						</div>
						<div>
							<div class="d-flex d-print-none justify-content-end align-items-center d-none d-lg-block">
								<button type="button" class="btn btn-secondary text-uppercase" data-dw-button="secondary" onclick='window.print()'>@Translate("Print")</button>
							</div>
						</div>
					</div>

					<p class="d-block mb-0">@Translate("We are processing your order now, here are the details")</p>

				</div>

				@if (!string.IsNullOrEmpty(GetString("Ecom:Order.Delivery.Email")))
				{
					<p class="my-3">
						@Translate("We sent an email to")
						<strong>@GetString("Ecom:Order.Delivery.Email")</strong>
						@Translate("with your order confirmation and receipt. If the email has not arrived within two minutes, please check your spam folder to see if the email was routed there.")
					</p>
				}
			</header>

			<section class="my-3 my-md-4">
				<div class="list-group">

					@if (!string.IsNullOrEmpty(GetString("Ecom:Order.ID")))
					{
						<div class="list-group-item py-2">
							<div class="grid">
								<dt class="g-col-4 lh-base fw-normal text-nowrap">@Translate("Order number")</dt>
								<dd class="g-col-12 g-col-md-8 mb-0">
									@if (!string.IsNullOrEmpty(GetString("Ecom:Order.IntegrationID")))
									{
										<span>@GetString("Ecom:Order.IntegrationID") (@GetString("Ecom:Order.ID"))</span>
									}
									else
									{
										<span>@GetString("Ecom:Order.ID")</span>
									}
								</dd>
							</div>
						</div>
					}

					@if (!string.IsNullOrEmpty(GetString("Ecom:Order.Completed")))
					{
						<div class="list-group-item py-2">
							<div class="grid">
								<dt class="g-col-4 lh-base fw-normal text-nowrap">@Translate("Order date")</dt>
								<dd class="g-col-12 g-col-md-8 mb-0">@GetString("Ecom:Order.Completed")</dd>
							</div>
						</div>
					}

					<div class="list-group-item py-2">
						<div class="grid">
							<dt class="g-col-4 lh-base fw-normal text-nowrap">@Translate("Contact")</dt>
							<dd class="g-col-12 g-col-md-8 mb-0">
								@if (!string.IsNullOrEmpty(@GetString("Ecom:Order.Customer.Email")))
								{
									<span>@GetString("Ecom:Order.Customer.Email"), </span>
								}
								@if (!string.IsNullOrEmpty(GetString("Ecom:Order.Customer.Phone")))
								{
									<span>@GetString("Ecom:Order.Customer.Phone")</span>
								}
							</dd>
						</div>
					</div>

					<div class="list-group-item py-2">
						<div class="grid">
							<dt class="g-col-4 lh-base fw-normal text-nowrap">@Translate("Billing")</dt>
							<dd class="g-col-12 g-col-md-8 mb-0">
								@{
									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.ToString()).ToArray()));
									@billingDetailsString
								}
							</dd>
						</div>
					</div>

					<div class="list-group-item py-2">
						<div class="grid">
							<dt class="g-col-4 lh-base fw-normal text-nowrap">@Translate("Delivery")</dt>
							<dd class="g-col-12 g-col-md-8 mb-0">
								@{
									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
								}
								
								<div class="opacity-75 fs-8">@GetString("Ecom:Order.ShippingMethod")</div>
							</dd>
						</div>
					</div>

					@if (!string.IsNullOrEmpty(GetString("Ecom:Order.ShippingDate")))
					{
						<div class="list-group-item py-2">
							<div class="grid">
								<dt class="g-col-4 lh-base fw-normal text-nowrap">@Translate("Delivery date")</dt>
								<dd class="g-col-12 g-col-md-8 mb-0">@GetDate("Ecom:Order.ShippingDate").ToShortDateString()</dd>
							</div>
						</div>
					}

					@if (!string.IsNullOrEmpty(GetString("Ecom:Order.PaymentMethod")))
					{
						string paymentLabel = !string.IsNullOrEmpty(GetString("Ecom:Order.TransactionCardType")) ? GetString("Ecom:Order.TransactionCardType") + " " + GetString("Ecom:Order.TransactionCardNumber") : GetString("Ecom:Order.PaymentMethod");

						<div class="list-group-item py-2">
							<div class="grid">
								<dt class="g-col-4 lh-base fw-normal text-nowrap">@Translate("Payment")</dt>
								<dd class="g-col-12 g-col-md-8 mb-0">@paymentLabel</dd>
							</div>
						</div>
					}

					@if (!string.IsNullOrEmpty(GetString("Ecom:Order.Customer.Comment")))
					{
						<div class="list-group-item py-2">
							<div class="mb-2">@Translate("Comments")</div>
							<div>@GetString("Ecom:Order.Customer.Comment")</div>
						</div>
					}

					@if (!string.IsNullOrEmpty(GetString("Ecom:Order.Reference")))
					{
						<div class="list-group-item py-2">
							<div class="grid">
								<dt class="g-col-4 lh-base fw-normal text-nowrap">@Translate("Order reference")</dt>
								<dd class="g-col-12 g-col-md-8 mb-0">@GetString("Ecom:Order.Reference")</dd>
							</div>
						</div>
					}

					@if (!string.IsNullOrEmpty(GetString("Ecom:Order.Customer.VoucherCode")))
					{
						<div class="list-group-item py-2">
							<div class="grid">
								<dt class="g-col-4 lh-base fw-normal text-nowrap">@Translate("Voucher code")</dt>
								<dd class="g-col-12 g-col-md-8 mb-0">@GetString("Ecom:Order.Customer.VoucherCode")</dd>
							</div>
						</div>
					}
				</div>
			</section>

			<section>
				<div class="list-group">
					<div class="list-group-item p-3">
						<div class="grid grid-1 py-3 pt-md-0">
							@Include("Helpers/OrderSummaryOrderLineItem.cshtml")
						</div>
						<div class="sticky-receipt bottom-0">
							@Include("Helpers/OrderSummary.cshtml")

							<section class="py-3 d-print-none">
								<div class="d-flex justify-content-end align-items-center">
									<a href="/Default.aspx?id=@frontPageId" class="btn btn-primary w-100" data-dw-button="primary" id="ReturnToFrontpage">@Translate("Return to frontpage")</a>
								</div>
							</section>
						</div>
					</div>
				</div>
			</section>

		</form>
	</div>
</div>

<script>
	window.dataLayer = window.dataLayer || [];
	window.dataLayer.push({
		event: "purchase",
		ecommerce: {
			transaction_id: "@GetString("Ecom:Order.ID")",
			value: @GetDouble("Ecom:Order.Price.Price.Value").ToString("0.00", System.Globalization.CultureInfo.InvariantCulture),
			tax: @GetDouble("Ecom:Order.Taxes.Total.Price.Value").ToString("0.00", System.Globalization.CultureInfo.InvariantCulture),
			shipping: @GetDouble("Ecom:Order.ShippingFee.Price.Value").ToString("0.00", System.Globalization.CultureInfo.InvariantCulture),
			currency: "@GetString("Ecom:Order.Price.Currency.Code")",
			items: [
				@foreach (LoopItem orderline in GetLoop("OrderLines"))
				{
					<text>
					{
						item_id: "@orderline.GetString("Ecom:Order:OrderLine.ProductID")",
						item_name: "@Dynamicweb.Core.Encoders.HtmlEncoder.JavaScriptStringEncode(orderline.GetString("Ecom:Order:OrderLine.ProductName"))",
						currency: "@orderline.GetString("Ecom:Order:OrderLine.UnitPrice.CurrencyCode")",
						price: @orderline.GetDouble("Ecom:Order:OrderLine.UnitPriceWithProductDiscount.Price.Value").ToString("0.00", System.Globalization.CultureInfo.InvariantCulture),
						discount: @Math.Abs(orderline.GetDouble("Ecom:Order:OrderLine.UnitDiscount.Price.Value")).ToString("0.00", System.Globalization.CultureInfo.InvariantCulture),
						quantity: @orderline.GetDouble("Ecom:Order:OrderLine.Quantity").ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)
					},
					</text>
				}
			]
		}
	});
</script>
