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

@{
	var hideEditBillingAddress = Pageview.CurrentParagraph?.Item["HideEditBillingAddress"] != null ? Convert.ToBoolean(Pageview.CurrentParagraph.Item["HideEditBillingAddress"].ToString()) : false;
	int currentUserId = Dynamicweb.Core.Converter.ToInt32(GetGlobalValue("Global:Extranet.UserID"));

	List<string> contact = new List<string>();
	if (!string.IsNullOrEmpty(GetString("Ecom:Order.Customer.Email")) || !string.IsNullOrEmpty(GetString("Ecom:Order.Customer.Phone")))
	{
		if (!string.IsNullOrEmpty(GetString("Ecom:Order.Customer.Email")))
		{
			contact.Add(GetString("Ecom:Order.Customer.Email"));
		}
		if (!string.IsNullOrEmpty(GetString("Ecom:Order.Customer.Phone")))
		{
			contact.Add(GetString("Ecom:Order.Customer.Phone"));
		}
	}

	string contactString = (string.Join(",", contact.Select(x => x.ToString()).ToArray())).Replace(",", ", ");
	string userAccountPage = Pageview.CurrentParagraph.Item["UserAccountPageLink"] != null ? Pageview.CurrentParagraph.Item["UserAccountPageLink"].ToString() + "&GoBackToPage=" + Pageview.Page.ID : "";

	if (!string.IsNullOrEmpty(contactString))
	{
		<div class="list-group-item py-2 fs-7" id="ContactGroup">
			<dl class="grid gap-1 gap-sm-3 mb-0">
				<dt class="g-col-8 g-col-sm-4 fw-normal">@Translate("Contact")</dt>
				<dd class="g-col-12 g-col-sm-7 order-2 order-sm-1 mb-0">@contactString</dd>

				@if (!hideEditBillingAddress)
				{
					<dd class="g-col-4 g-col-sm-1 order-1 order-sm-2 mb-0 text-end">
						@if (currentUserId == 0)
						{
							<button type="submit" class="btn btn-sm btn-link fs-8 p-0" data-dw-button="link" name="CartV2.GotoStep0" id="EditBillingAddressButton">
								<span class="text-uppercase">@Translate("Edit")</span>
							</button>
						}
						else
						{
							<a href="@userAccountPage" class="btn btn-sm btn-link fs-8 p-0" data-dw-button="link" id="EditBillingAddressButton">
								<span class="text-uppercase">@Translate("Edit")</span>
							</a>
						}
					</dd>
				}
			</dl>
		</div>
	}

	int billingFieldCount = 0; //Helps us render addresses that contains more than just the auto set country
	List<string> customerAddressList = new List<string>();
	if (!string.IsNullOrEmpty(GetString("Ecom:Order.Customer.Company")))
	{
		customerAddressList.Add(GetString("Ecom:Order.Customer.Company"));
		billingFieldCount++;
	}
	if (!string.IsNullOrEmpty(GetString("Ecom:Order.Customer.Name")))
	{
		customerAddressList.Add(GetString("Ecom:Order.Customer.Name"));
		billingFieldCount++;
	}
	if (!string.IsNullOrEmpty(GetString("Ecom:Order.Customer.Address")))
	{
		customerAddressList.Add(GetString("Ecom:Order.Customer.Address"));
		billingFieldCount++;
	}
	if (!string.IsNullOrEmpty(GetString("Ecom:Order.Customer.Address2")))
	{
		customerAddressList.Add(GetString("Ecom:Order.Customer.Address2"));
		billingFieldCount++;
	}
	if (!string.IsNullOrEmpty(GetString("Ecom:Order.Customer.Zip")))
	{
		customerAddressList.Add(GetString("Ecom:Order.Customer.Zip"));
		billingFieldCount++;
	}
	if (!string.IsNullOrEmpty(GetString("Ecom:Order.Customer.City")))
	{
		customerAddressList.Add(GetString("Ecom:Order.Customer.City"));
		billingFieldCount++;
	}
	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");
			}
		}

		customerAddressList.Add(regionName);
		billingFieldCount++;
	}
	if (!string.IsNullOrEmpty(GetString("Ecom:Order.Customer.Country")))
	{
		customerAddressList.Add(GetString("Ecom:Order.Customer.Country"));
		billingFieldCount++;
	}
	string billingAddressString = (string.Join(",", customerAddressList.Select(x => x.ToString()).ToArray())).Trim(',').Replace(",", ", ");

	if (!string.IsNullOrEmpty(billingAddressString) && billingFieldCount > 1)
	{
		<div class="list-group-item py-2 fs-7">
			<dl class="grid gap-1 gap-sm-3 mb-0">
				<dt class="g-col-8 g-col-sm-4 fw-normal">@Translate("Billing")</dt>
				<dd class="g-col-12 g-col-sm-7 order-2 order-sm-1 mb-0">@billingAddressString</dd>

				@if (!hideEditBillingAddress)
				{
					<dd class="g-col-4 g-col-sm-1 order-1 order-sm-2 mb-0 text-end">
						@if (currentUserId == 0)
						{
							<button type="submit" class="btn btn-sm btn-link fs-8 p-0" data-dw-button="link" name="CartV2.GotoStep0" id="EditBillingAddressButton">
								<span class="text-uppercase">@Translate("Edit")</span>
							</button>
						}
						else
						{
							<a href="@userAccountPage" class="btn btn-sm btn-link fs-8 p-0" data-dw-button="link" id="EditBillingAddressButton">
								<span class="text-uppercase">@Translate("Edit")</span>
							</a>
						}
					</dd>
				}
			</dl>
		</div>
	}
}
