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

@{ 	
	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 class="h-100" data-dw-colorscheme="@Model.ColorScheme?.Id">
	<header class="px-3 py-2 border-bottom">
		<div class="grid grid-2 gap-3 align-items-center">
			<h1 class="h6 m-0">@Translate("Your order")</h1>
			<a href="javascript:print()" class="btn btn-link text-end px-0">@Translate("Print")</a>
		</div>
	</header>

	<table class="table d-print-table">
		@if (!string.IsNullOrEmpty(Model.Id))
		{
			<tr>
				<td>@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("Delivery address")</td>
			<td class="text-end">
				@{
					if (!string.IsNullOrEmpty(Model.DeliveryName))
					{
						<div>@Model.DeliveryName</div>
					}
					if (!string.IsNullOrEmpty(Model.DeliveryAddress))
					{
						<div>@Model.DeliveryAddress</div>
					}

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

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

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

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

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

	<section class="p-3 mt-3 d-flex flex-column gap-3">
		@foreach (OrderLineViewModel orderline in Model.OrderLines)
		{
			string productName = orderline.ProductName + " " + orderline.ProductVariantId;
			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.Price.PriceFormatted;

			var orderlineQuantity = orderline.Quantity;
			var orderlineUnit = orderline.UnitName;
			var productId = orderline.ProductId;
			var variantId = orderline.ProductVariantId;

			string link = orderline?.GetProduct()?.GetProductLink(GetPageIdByNavigationTag("Shop"), false) ?? "";

			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)
			{
				<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 (orderlineType == OrderLineType.ProductDiscount && !string.IsNullOrEmpty(orderline.ProductNumber))
										{
											@* 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>
											if (orderline.UnitPrice.Price != orderline.UnitPriceBeforeDiscount.Price && orderline.UnitPriceBeforeDiscount.Price != 0.0)
										{
											<span class="text-decoration-line-through text-muted">
												<span>@orderline.UnitPriceBeforeDiscount.PriceFormatted</span>
											</span>
										}
										<span>@orderline.UnitPrice.PriceFormatted</span>
										}

									</li>
									<li>
										<span>@Translate("Quantity"):</span>
										<span>@orderlineQuantity</span>
									</li>
								</ul>
							</div>

							<div class="g-col-9 g-col-md-4">
								<h6 class="h6 fw-bold text-end">
									<span class="text-price">@productPriceTotalWithDiscounts</span>
								</h6>
							</div>
						</div>
					</div>

				</article>
			}
		}

		@{
			double totalTaxes = Model.OrderLines.Where(o => o.OrderLineType == OrderLineType.Tax).Sum(o => o.Price.Price);
			double priceBeforeFeesDiscountAndTaxes = Model.PriceBeforeFees.Price - Model.TotalDiscount.Price - totalTaxes;
			string priceBeforeFeesDiscountAndTaxesFormatted = Dynamicweb.Ecommerce.Services.Currencies.Format(Dynamicweb.Ecommerce.Common.Context.Currency, priceBeforeFeesDiscountAndTaxes, true);
			string orderSubtotal = totalTaxes != 0 || Model.TotalDiscount.Price != 0 ? priceBeforeFeesDiscountAndTaxesFormatted : Model.PriceBeforeFees.PriceFormatted;
			string shippingFee = Model.ShippingFee.PriceFormatted;
			string orderTotalDiscount = Model.TotalDiscount.PriceFormatted;

			// Discounts
			bool orderHasDiscounts = false;
			if (!string.IsNullOrEmpty(orderTotalDiscount))
			{
				orderHasDiscounts = true;
			}

			<div class="cart-total grid mt-3">
				<div class="g-col-12">

					<div class="grid grid-2 my-1 pt-4 border-top">
						<span class="fw-bold">@Translate("Subtotal")</span>
						<span class="text-price text-end fw-bold">@orderSubtotal</span>
					</div>

					@if (!string.IsNullOrEmpty(shippingFee) && Model.ShippingFee.Price != 0.0)
					{
						<div class="grid grid-2 my-1">
							<span class="fw-normal">@Translate("Delivery")</span>
							<span class="text-end">@shippingFee</span>
						</div>
					}

					@if (Model.PaymentFee.Price != 0.0)
					{
						<div class="grid grid-2 my-1">
							<span class="fw-normal">@Translate("Payment")</span>
							<span class="text-end">@Model.PaymentFee.PriceFormatted</span>
						</div>
					}

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

					<div class="grid grid-2 my-1 h5 fw-bold">
						<span>@Translate("Total")</span>
						<span class="text-end">@Model.Price.PriceFormatted</span>
					</div>

					@if (Model.Price.Vat != 0.0)
					{
						<div class="grid grid-2 my-1">
							<span class="fw-normal">@Translate("VAT")</span>
							@if (!string.IsNullOrEmpty(Model.Price.VatFormatted))
							{
								<span class="text-end">@Model.Price.VatFormatted</span>
							}
						</div>
					}

					@if (orderHasDiscounts && Model.TotalDiscount.Price != 0.0)
					{
						<div class="grid grid-2 my-1">
							<span class="fw-normal">@Translate("Discount")</span>
							<span class="text-end">@orderTotalDiscount</span>
						</div>
					}

				</div>
			</div>
		}
	</section>
</div>
