@inherits ViewModelTemplate<OrderViewModel>
@using Dynamicweb.Rendering
@using Dynamicweb.Ecommerce.Frontend
@using Dynamicweb.Ecommerce.Orders
@using Dynamicweb.Ecommerce.ProductCatalog

@{ 
	string showPricesWithVat = Dynamicweb.Ecommerce.Common.Context.DisplayPricesWithVat.ToString();
	bool neverShowVat = string.IsNullOrEmpty(showPricesWithVat);

	string badgeColor = "bg-dark text-white";

	if (Model.StateId == "OS2")
	{
		badgeColor = "bg-success text-white";
	}
	else if (Model.StateId == "OS3")
	{
		badgeColor = "bg-danger text-white";
	}
}

<div data-dw-colorscheme="@Pageview.CurrentParagraph?.ColorSchemeId">
	<header class="p-3 border-bottom">
		<div class="d-flex flex-row align-items-center gap-3">
			<h1 class="h6 m-0 flex-fill text-muted">@Translate("Your order")</h1>
			<button onclick="javascript:print()" class="btn btn-link p-0">@Translate("Print")</button>
		</div>
	</header>

	<table class="table d-print-table">
		@if (!string.IsNullOrEmpty(Model.Id))
		{
			<tr>
				<td class="">@Translate("Order number")</td>
				<td class="text-end">
					@if (!string.IsNullOrEmpty(Model.IntegrationOrderId) && Model.IntegrationOrderId != Model.Id)
					{
						<text>@Model.IntegrationOrderId (@Model.Id)</text>
					}
					else
					{
						@Model.Id
					}
				</td>
			</tr>
		}

		@if (!string.IsNullOrEmpty(Model.CompletedDate.ToString()))
		{
			string orderDate = Model.CompletedDate?.TimeOfDay.Ticks > 0 ? Model.CompletedDate.ToString() : Model.CompletedDate?.ToString(Pageview.Area.CultureInfo.DateTimeFormat.ShortDatePattern);
			<tr>
				<td>@Translate("Order date")</td>
				<td class="text-end">@orderDate</td>
			</tr>
		}

		@if (!string.IsNullOrEmpty(Model.StateName))
		{
			<tr>
				<td>@Translate("Order state")</td>
				<td class="text-end">
					<div class="badge opacity-75 @badgeColor">@Model.StateName</div>
				</td>
			</tr>
		}

		<tr>
			<td>@Translate("Billing")</td>
			<td class="text-end">
				@{
					if (!string.IsNullOrEmpty(Model.CustomerCompany))
					{
						<div>@Model.CustomerCompany</div>
					}
					if (!string.IsNullOrEmpty(Model.CustomerName))
					{
						<div>@Model.CustomerName</div>
					}
					if (!string.IsNullOrEmpty(Model.CustomerAddress))
					{
						<div>@Model.CustomerAddress</div>
					}
					if (!string.IsNullOrEmpty(Model.CustomerAddress2))
					{
						<div>@Model.CustomerAddress2</div>
					}

					<div>
						@if (!string.IsNullOrEmpty(Model.CustomerZip))
						{
							<span>@Model.CustomerZip</span>
						}
						@if (!string.IsNullOrEmpty(Model.CustomerCity))
						{
							<span>@Model.CustomerCity</span>
						}
					</div>

					<div>
						@if (!string.IsNullOrEmpty(Model.CustomerRegion))
						{
							var countryCode = Model.CustomerCountryCode;
							string stateName = Dynamicweb.Ecommerce.Services.Countries.GetRegion(countryCode, Model.CustomerRegion)?.GetName(Dynamicweb.Ecommerce.Common.Context.LanguageID) ?? string.Empty;

							<span>@stateName</span>
						}
						@if (!string.IsNullOrEmpty(Model.CustomerCountry))
						{
							<span>@Model.CustomerCountry</span>
						}
					</div>

					if (!string.IsNullOrEmpty(Model.CustomerEmail))
					{
						<div>@Model.CustomerEmail</div>
					}
					if (!string.IsNullOrEmpty(Model.CustomerPhone))
					{
						<div>@Model.CustomerPhone</div>
					}
				}
			</td>
		</tr>

		<tr>
			<td>@Translate("Delivery")</td>
			<td class="text-end">
				@{
					if (!string.IsNullOrEmpty(Model.DeliveryCompany))
					{
						<div>@Model.DeliveryCompany</div>
					}
					if (!string.IsNullOrEmpty(Model.DeliveryName))
					{
						<div>@Model.DeliveryName</div>
					}
					if (!string.IsNullOrEmpty(Model.DeliveryAddress))
					{
						<div>@Model.DeliveryAddress</div>
					}
					if (!string.IsNullOrEmpty(Model.DeliveryAddress2))
					{
						<div>@Model.DeliveryAddress2</div>
					}

					<div>
						@if (!string.IsNullOrEmpty(Model.DeliveryZip))
						{
							<span>@Model.DeliveryZip</span>
						}
						@if (!string.IsNullOrEmpty(Model.DeliveryCity))
						{
							<span>@Model.DeliveryCity</span>
						}
					</div>

					<div>
						@if (!string.IsNullOrEmpty(Model.DeliveryRegion))
						{
							var countryCode = Model.DeliveryCountryCode;
							string stateName = Dynamicweb.Ecommerce.Services.Countries.GetRegion(countryCode, Model.DeliveryRegion)?.GetName(Dynamicweb.Ecommerce.Common.Context.LanguageID) ?? string.Empty;

							<span>@stateName</span>
						}
						@if (!string.IsNullOrEmpty(Model.DeliveryCountry))
						{
							<span>@Model.DeliveryCountry</span>
						}
					</div>

					if (!string.IsNullOrEmpty(Model.DeliveryEmail))
					{
						<div>@Model.DeliveryEmail</div>
					}
					if (!string.IsNullOrEmpty(Model.DeliveryPhone))
					{
						<div>@Model.DeliveryPhone</div>
					}
				}
			</td>
		</tr>

		@if (Model.ShippingMethod != null)
		{
			<tr>
				<td>@Translate("Delivery method")</td>
				<td class="text-end">
					@if (!string.IsNullOrEmpty(Model.ShippingMethod.Name))
					{
						@Model.ShippingMethod.Name
					}
				</td>
			</tr>
		}

		@if (Model.ShippingDate != null)
		{
			<tr>
				<td>@Translate("Delivery date")</td>
				<td class="text-end">
					@if (Model.ShippingDate.HasValue)
					{
						@Model.ShippingDate.Value.ToShortDateString()
					}
				</td>
			</tr>
		}

		@if (Model.PaymentMethod != null)
		{
			<tr>
				<td>@Translate("Payment method")</td>
				<td class="text-end">
					@if (!string.IsNullOrEmpty(Model.PaymentMethod.Name))
					{
						<span>@Model.PaymentMethod.Name</span>
						if (!string.IsNullOrEmpty(Model.TransactionCardType))
						{
							<span>@Model.TransactionNumber</span>
						}
					}
				</td>
			</tr>
		}

		@if (!string.IsNullOrEmpty(Model.CustomerComment))
		{
			<tr>
				<td colspan="2">
					<div class="mb-2">@Translate("Comments")</div>
					<div>@Model.CustomerComment</div>
				</td>
			</tr>
		}

		@if (!string.IsNullOrEmpty(Model.Reference))
		{
			<tr>
				<td>@Translate("Order reference")</td>
				<td class="text-end">
					@Model.Reference
				</td>
			</tr>
		}

		@if (!string.IsNullOrEmpty(Model.VoucherCode))
		{
			<tr>
				<td>@Translate("Voucher code")</td>
				<td class="text-end">
					@Model.VoucherCode
				</td>
			</tr>
		}

		@if (!string.IsNullOrEmpty(Model.TrackAndTraceNumber) || !string.IsNullOrEmpty(Model.TrackAndTraceURL))
		{
			string label = Model.TrackAndTraceName;
			label += !string.IsNullOrEmpty(Model.TrackAndTraceNumber) ? ": " + Model.TrackAndTraceNumber : "";

			<tr>
				<td>@Translate("Track and trace")</td>
				<td class="text-end">
					@if (!string.IsNullOrEmpty(Model.TrackAndTraceURL))
					{
						<a href="@Model.TrackAndTraceURL" title="@Model.TrackAndTraceName">@label</a>
					}
					else
					{
						@label
					}
				</td>
			</tr>
		}
	</table>

	<section class="p-3 mt-3 d-flex flex-column gap-3">
		@foreach (OrderLineViewModel orderline in Model.OrderLines)
		{
			string productName = !string.IsNullOrEmpty(orderline.ProductVariantId) ? $"{orderline.ProductName} {orderline.ProductVariantName}" : orderline.ProductName;
			string productImage = "/Admin/Public/GetImage.ashx?image=" + orderline.ProductImage + "&width=315&height=315&format=webp";
			productImage = !orderline.ProductImage.StartsWith("/Files/", StringComparison.OrdinalIgnoreCase) ? orderline.ProductImage : productImage;
			string productPriceTotalWithDiscounts = orderline.TotalPriceWithProductDiscounts.PriceFormatted;

			var orderlineQuantity = orderline.Quantity;
			var orderlineUnit = orderline.UnitName;

			var orderlineType = orderline.OrderLineType;
			bool isProductIdOrNumber = !string.IsNullOrEmpty(orderline.ProductId) || !string.IsNullOrEmpty(orderline.ProductNumber);  //Live Cart orderlines may not return ProductId. Manually entered products may not have ProductNumber
			bool isDiscount = orderline.OrderLineType == OrderLineType.Discount || orderline.OrderLineType == OrderLineType.ProductDiscount;
			bool isFreeProduct = isDiscount && isProductIdOrNumber;

			if (orderlineType == OrderLineType.Product || orderlineType == OrderLineType.GiftCard || orderlineType == OrderLineType.Fixed || isFreeProduct)
			{
				var product = orderline?.GetProduct();
				string link = product?.GetProductLink(GetPageIdByNavigationTag("Shop"), false) ?? string.Empty;

				<article class="grid">
					<div class="g-col-3">
						@if (string.IsNullOrEmpty(link))
						{
							<span class="ratio ratio-1x1 d-block bg-light">
								<img src="@productImage" style="object-fit: contain;" alt="@productName" height="315" width="315">
							</span>
						}
						else
						{
							<a href="@link" class="ratio ratio-1x1 d-block bg-light">
								<img src="@productImage" style="object-fit: contain;" alt="@productName" height="315" width="315">
							</a>
						}
					</div>

					<div class="g-col-9">
						<div class="grid grid-9 h-100">
							<div class="g-col-9 g-col-md-5">
								<h2 class="h6 mb-3 fw-bold">
									@if (string.IsNullOrEmpty(link))
									{
										<span class="text-dark">@productName</span>
									}
									else
									{
										<a href="@link" class="text-dark text-decoration-none">@productName</a>
									}
								</h2>

								<ul class="list-unstyled text-muted mb-0">
									@if (!string.IsNullOrEmpty(orderline.ProductVariantName))
									{
										<li>
											<span>@orderline.ProductVariantName</span>
										</li>
									}
									@if (!string.IsNullOrEmpty(orderlineUnit))
									{
										<li>
											<span>@Translate("Unit"):</span>
											<span>@orderlineUnit</span>
										</li>
									}
									<li>
										@if (isFreeProduct)
										{
											@* Free product notice *@
											<p class="mt-2 mb-0 fs-7 fs-md-6 opacity-75">@Translate("Free product")</p>
										}
										else {
											<span>@Translate("Price"):</span>
											<span class="text-price">@orderline.UnitPriceWithProductDiscount.PriceFormatted</span>
											if (orderline.UnitPriceWithProductDiscount.Price != orderline.UnitPrice.Price && orderline.UnitPrice.Price != 0.0)
											{
												<span class="text-decoration-line-through fs-7">
													<span class="text-price">@orderline.UnitPrice.PriceFormatted</span>
												</span>
											}
										}
									</li>
									<li>
										<span>@Translate("Quantity"):</span>
										<span>@orderlineQuantity</span>
									</li>
								</ul>
							</div>

							<div class="g-col-9 g-col-md-4 text-end">
								<h6 class="h6 fw-bold m-0">
									<span class="text-price">@productPriceTotalWithDiscounts</span>
								</h6>
								@if (!string.IsNullOrEmpty(productPriceTotalWithDiscounts) && orderline.TotalDiscount.Price != 0.0)
								{
									<small class="fs-7">
										@orderline.TotalDiscount.PriceFormatted
									</small>
								}
							</div>
						</div>
					</div>

					@if (orderline?.BomOrderLines.Count() > 0) {
						@* BOM items *@
						foreach (OrderLineViewModel bomOrderline in orderline?.BomOrderLines) {
							var bomProduct = bomOrderline?.GetProduct();
							string bomImage = bomOrderline.ProductImage;
							bomImage = "/Admin/Public/GetImage.ashx?width=" + 300 + "&height=" + 300 + "&image=" + bomImage + "&format=webp";
							bomImage = !bomOrderline.ProductImage.StartsWith("/Files/", StringComparison.OrdinalIgnoreCase) ? bomOrderline.ProductImage : bomImage;

							<div class="g-col-3 d-flex gap-2">
								<div class="icon-2 p-0 p-lg-3 opacity-25">
									@ReadFile("/Files/Images/Icons/arrow-turn-down-right.svg")
								</div>
								@if (!string.IsNullOrEmpty(bomImage))
								{
									<span class="ratio ratio-1x1 d-block bg-light">
										<img src="@bomImage" style="object-fit: contain;" alt="@bomOrderline.ProductName" height="300" width="300">
									</span>
								}
							</div>

							<div class="g-col-9">
								<div class="grid grid-9 h-100">
									<div class="g-col-9 g-col-md-5">
										<h2 class="h6 mb-3 fw-bold"><span class="text-dark">@bomOrderline.ProductName</span></h2>

										<ul class="list-unstyled text-muted mb-0">
											<li>
												<span>@Translate("Quantity"):</span>
												<span>@bomOrderline.Quantity</span>
											</li>
										</ul>
									</div>

									<div class="g-col-9 g-col-md-4 text-end">
										@if (product.PriceType.ToString().ToLower() != "fixedprice") { 
											<h6 class="h6 fw-bold m-0">
												<span class="text-price">@bomOrderline.Price.PriceFormatted</span>
											</h6>
										}
									</div>
								</div>
							</div>
						}
					}
				</article>
			}
		}

		<div class="cart-total grid mt-3">
			<div class="g-col-12">
				<dl class="m-0">
					<div class="d-flex my-1 pt-4 border-dark border-top">
						<dt class="flex-fill fw-bold">@Translate("Subtotal")</dt>
						<dd class="flex-fill mb-0 text-end">
							@if (Model.TotalPriceWithoutDiscountsFeesAndTaxes.Price != Model.TotalPriceWithoutOrderDiscountsAndFeesAndTaxes.Price)
							{
								<span class="text-price text-decoration-line-through">@(Model.TotalPriceWithoutDiscountsFeesAndTaxes.PriceFormatted)</span>
							}
							<span class="textPrice">@(Model.TotalPriceWithoutOrderDiscountsAndFeesAndTaxes.PriceFormatted)</span>
							@if (!neverShowVat && showPricesWithVat == "false")
							{
								<span class="fs-7 fw-normal d-block">@Translate("Excl. VAT")</span>
							}
							@if (!neverShowVat && showPricesWithVat == "true")
							{
								<span class="fs-7 fw-normal d-block">@Translate("Incl. VAT")</span>
							}
						</dd>
					</div>

					@* Discounts (only order discounts) *@
					@foreach (OrderLineViewModel orderline in Model.OrderLines)
					{
						bool isProductIdOrNumber = !string.IsNullOrEmpty(orderline.ProductId) || !string.IsNullOrEmpty(orderline.ProductNumber);  //Live Cart orderlines may not return ProductId. Manually entered products may not have ProductNumber
						bool isOrderAmountDiscount = orderline.OrderLineType == OrderLineType.Discount && !isProductIdOrNumber;
						
						if (isOrderAmountDiscount)
						{
							<div class="d-flex my-1">
								<dt class="flex-fill fw-normal">@orderline.ProductName</dt>
								<dd class="flex-fill mb-0 text-end">
									@orderline.Price.PriceFormatted
								</dd>
							</div>
						}
					}

					@* Delivery *@
					@if (Model.ShippingFee.Price != 0.0) 
					{
						<div class="d-flex my-1">
							<dt class="flex-fill fw-normal">@Translate("Delivery")</dt>
							<dd class="flex-fill mb-0 text-end">
								<span>@Model.ShippingFee.PriceFormatted</span>
							</dd>
						</div>
					}

					@* Payment *@
					@if (Model.PaymentFee.Price != 0.0)
					{
						<div class="d-flex my-1">
							<dt class="flex-fill fw-normal">@Translate("Payment")</dt>
							<dd class="flex-fill mb-0 text-end">
								<span>@Model.PaymentFee.PriceFormatted</span>
							</dd>
						</div>
					}
					
					@* Taxes *@
					@if (Model.TotalTaxes.Price != 0.0)
					{
						<div class="d-flex my-1">
							<dt class="flex-fill fw-normal">@Translate("Sales tax")</dt>
							<dd class="flex-fill mb-0 text-end">
								<span>@Model.TotalTaxes.PriceFormatted</span>
							</dd>
						</div>
					}

					<div class="my-4 border-dark border-top"></div>

					<div class="d-flex my-1 h5 fw-bold">
						<dt class="flex-fill">@Translate("Total")</dt>
						<dd class="flex-fill mb-0 text-end">
							<span>@Model.Price.PriceFormatted</span>
							@if (!neverShowVat && showPricesWithVat == "false")
							{
								<span class="fs-7 fw-normal d-block">@Translate("Excl. VAT")</span>
							}
							@if (!neverShowVat && showPricesWithVat == "true")
							{
								<span class="fs-7 fw-normal d-block">@Translate("Incl. VAT")</span>
							}
						</dd>
					</div>

					@if (!neverShowVat && Model.Price.Vat != 0.0)
					{
						<div class="d-flex my-1">
							<dt class="flex-fill fw-normal">@Translate("VAT")</dt>
							<dd class="flex-fill mb-0 text-end">
								@if (!string.IsNullOrEmpty(Model.Price.VatFormatted))
								{
									<span>@Model.Price.VatFormatted</span>
								}
							</dd>
						</div>

						if (showPricesWithVat == "false")
						{
							<div class="d-flex my-1">
								<dt class="flex-fill fw-normal">@Translate("Total incl. VAT")</dt>
								<dd class="flex-fill mb-0 text-end">
									@if (!string.IsNullOrEmpty(Model.Price.PriceWithVatFormatted))
									{
										<span>@Model.Price.PriceWithVatFormatted</span>
									}
								</dd>
							</div>
						}
					}

					@if (Model.TotalDiscount.Price != 0.0)
					{
						<div class="d-flex my-1">
							<dt class="flex-fill fw-normal">@Translate("Discount")</dt>
							<dd class="flex-fill mb-0 text-end">
								<span>@Model.TotalDiscount.PriceFormatted</span>
							</dd>
						</div>
					}
				</dl>
			</div>
		</div>
	</section>
</div>
