@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()
	{
		return "border:0;margin:0;outline:0;padding:0;";
	}
}

@{
	ParagraphViewModel currentParagraph = new ParagraphViewModel();
	if (Dynamicweb.Context.Current.Items.Contains("CurrentParagraph"))
	{
		currentParagraph = (ParagraphViewModel)Dynamicweb.Context.Current.Items["CurrentParagraph"];
	}

	int currentPageId = 0;
	if (Dynamicweb.Context.Current.Items.Contains("CurrentPageId"))
	{
		currentPageId = Convert.ToInt16(Dynamicweb.Context.Current.Items["CurrentPageId"]);
	}

	int currentGridRowId = 0;
	if (Dynamicweb.Context.Current.Items.Contains("CurrentGridRowId"))
	{
		currentGridRowId = Convert.ToInt16(Dynamicweb.Context.Current.Items["CurrentGridRowId"]);
	}

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

	var page = Dynamicweb.Content.Services.Pages.GetPage(Pageview.ID);
	var settings = Dynamicweb.Frontend.ContentViewModelFactory.CreatePageInfoViewModel(page?.Parent).Item;
	var block = currentParagraph.Item;

	var emailContentThemeId = settings.GetRawValueString("EmailContentTheme");
	var emailContentThemeParagraph = emailContentThemeId != null && emailContentThemeId != string.Empty ? Dynamicweb.Content.Services.Paragraphs.GetParagraph(Dynamicweb.Core.Converter.ToInt32(emailContentThemeId)) : null;
	var emailContentTheme = emailContentThemeParagraph != null && Dynamicweb.Frontend.ContentViewModelFactory.CreateParagraphInfoViewModel(emailContentThemeParagraph).Item.GetString("CssClassName") != string.Empty ? Dynamicweb.Frontend.ContentViewModelFactory.CreateParagraphInfoViewModel(emailContentThemeParagraph)?.Item : null;

	var rowId = currentGridRowId;
	var row = Dynamicweb.Content.Services.Grids.GetGridRowById(rowId);
	var rowItem = Dynamicweb.Content.Services.Items.GetItem(row.ItemType, row.ItemId);
	var rowThemeId = Dynamicweb.Core.Converter.ToString(rowItem["ColumnBackground"]);
	var rowThemeParagraph = rowThemeId != null && rowThemeId != string.Empty ? Dynamicweb.Content.Services.Paragraphs.GetParagraph(Dynamicweb.Core.Converter.ToInt32(rowThemeId)) : null;
	var rowTheme = rowThemeParagraph != null && Dynamicweb.Frontend.ContentViewModelFactory.CreateParagraphInfoViewModel(rowThemeParagraph).Item.GetString("CssClassName") != string.Empty ? Dynamicweb.Frontend.ContentViewModelFactory.CreateParagraphInfoViewModel(rowThemeParagraph)?.Item : null;

	var blockThemeId = block.GetRawValueString("Theme");
	var blockThemeParagraph = blockThemeId != null && blockThemeId != string.Empty ? Dynamicweb.Content.Services.Paragraphs.GetParagraph(Dynamicweb.Core.Converter.ToInt32(blockThemeId)) : null;
	var blockTheme = blockThemeParagraph != null && Dynamicweb.Frontend.ContentViewModelFactory.CreateParagraphInfoViewModel(blockThemeParagraph).Item.GetString("CssClassName") != string.Empty ? Dynamicweb.Frontend.ContentViewModelFactory.CreateParagraphInfoViewModel(blockThemeParagraph)?.Item : rowTheme is object ? rowTheme : emailContentTheme;

	string backgroundColor = blockTheme != null ? "background-color: " + blockTheme.GetString("BackgroundColor") + ";" : string.Empty;
	string foregroundColor = blockTheme != null ? "color:" + blockTheme.GetString("ForegroundColor", "inherit") + ";" : string.Empty;
	string borderColor = blockTheme != null ? blockTheme.GetString("BorderColor", "transparent") : string.Empty;
	string borders = block.GetRawValueString("Borders", "none");
	string borderThicknes = block.GetRawValueString("BorderThicknes", "0") + "px";
	string borderClass = string.Empty;

	switch (borders)
	{
		case "top":
			borderClass = $"border-top:{borderThicknes} solid {borderColor};";
			break;
		case "bottom":
			borderClass = $"border-bottom:{borderThicknes} solid {borderColor};";
			break;
		case "top-bottom":
			borderClass = $"border-top:{borderThicknes} solid {borderColor}; border-bottom:{borderThicknes} solid {borderColor}; border-left:0;border-right:0;";
			break;
		case "all":
			borderClass = $"border:{borderThicknes} solid {borderColor};";
			break;
	}

	int padding = Dynamicweb.Core.Converter.ToInt32(block.GetRawValueString("Padding", "0"));
	string align = block.GetRawValueString("Align", "left");
	string lineHeight = "line-height:" + (16 * Dynamicweb.Core.Converter.ToDouble(block.GetRawValueString("LineHeight", "1.5"))).ToString() + "px;";
}

<tbody>
	@foreach (LoopItem orderline in GetLoop("OrderLines"))
	{
		string name = orderline.GetString("Ecom:Order:OrderLine.ProductName");
		string image = "Admin/Public/GetImage.ashx?image=" + orderline.GetString("Ecom:Product.PrimaryImage") + "&width=100&height=100";
		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 isProduct = orderline.GetBoolean("Ecom:Order:OrderLine.IsProduct");

		if (isProduct)
		{
			<tr style="@GetTableReset()">
				<td style="word-wrap:anywhere;mso-line-height-rule:exactly;mso-table-lspace:0pt;mso-table-rspace:0pt;@GetTableReset()@GetFontStack()text-align:left;@(backgroundColor)@(foregroundColor)@(lineHeight)word-break:break-word;padding:@(padding)px;@(borderClass)" width="132">
					@* Image *@
					<img src="@imagePath" alt="@name">
				</td>
				<td style="word-wrap:anywhere;mso-line-height-rule:exactly;mso-table-lspace:0pt;mso-table-rspace:0pt;@GetTableReset()@GetFontStack()text-align:left;@(backgroundColor)@(foregroundColor)@(lineHeight)word-break:break-word;padding:@(padding)px;@(borderClass)">
					@* Title *@
					<h3>
						@name
					</h3>
					@* Variants *@
					<p>@variantText</p>
					<p>
						@* 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="word-wrap:anywhere;mso-line-height-rule:exactly;mso-table-lspace:0pt;mso-table-rspace:0pt;@GetTableReset()@GetFontStack()text-align:right;@(backgroundColor)@(foregroundColor)@(lineHeight)word-break:break-word;padding:@(padding)px;@(borderClass)">
					@* Total *@
					<span style="@(textPrice)">@priceTotalWithDiscounts</span>
					@if (orderline.GetDouble("Ecom:Order:OrderLine.TotalDiscount.Price.Value") != 0)
					{
						<span style="@(textPrice)">@discountTotal</span>}
				</td>
			</tr>
		}
	}
</tbody>
