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

@{
	var summaryUserHideEditBillingAddress = Pageview.CurrentParagraph?.Item["HideEditBillingAddress"] != null ? Convert.ToBoolean(Pageview.CurrentParagraph.Item["HideEditBillingAddress"].ToString()) : false;

	int summaryUserFieldCount = 0; //Helps us render addresses that contains more than just the auto set country

	string stepSummaryAddressString = string.Empty;

	Dynamicweb.Security.UserManagement.User user = Pageview.User;
	List<string> stepSummaryAddress = new List<string>();
	if (!string.IsNullOrEmpty(user.Company))
	{
		stepSummaryAddress.Add(user.Company);
		summaryUserFieldCount++;
	}
	if (!string.IsNullOrEmpty(user.Name))
	{
		stepSummaryAddress.Add(user.Name);
		summaryUserFieldCount++;
	}
	if (!string.IsNullOrEmpty(user.Address))
	{
		stepSummaryAddress.Add(user.Address);
		summaryUserFieldCount++;
	}
	if (!string.IsNullOrEmpty(user.Address2))
	{
		stepSummaryAddress.Add(user.Address2);
		summaryUserFieldCount++;
	}
	if (!string.IsNullOrEmpty(user.Zip))
	{
		stepSummaryAddress.Add(user.Zip);
		summaryUserFieldCount++;
	}
	if (!string.IsNullOrEmpty(user.City))
	{
		stepSummaryAddress.Add(user.City);
		summaryUserFieldCount++;
	}
	if (!string.IsNullOrEmpty(user.State))
	{
		var countryCode = user.CountryCode;
		string stateName = Dynamicweb.Ecommerce.Services.Countries.GetRegion(countryCode, user.State)?.GetName(Dynamicweb.Ecommerce.Common.Context.LanguageID) ?? string.Empty;

		stepSummaryAddress.Add(stateName);
		summaryUserFieldCount++;
	}
	if (!string.IsNullOrEmpty(user.CountryCode))
	{
		string country = user.Region.DisplayName;
		stepSummaryAddress.Add(country);
		summaryUserFieldCount++;
	}
	stepSummaryAddressString = (string.Join(",", stepSummaryAddress.Select(x => x.ToString()).ToArray())).Replace(",", ", ");

	string editStep = Pageview.CurrentParagraph.Item["UserAccountPageLink"] != null ? Pageview.CurrentParagraph.Item["UserAccountPageLink"].ToString() + "&GoBackToPage=" + Pageview.Page.ID : "";
}

@if (!string.IsNullOrEmpty(stepSummaryAddressString) && summaryUserFieldCount > 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">@stepSummaryAddressString</dd>
			@if (!summaryUserHideEditBillingAddress)
			{
				<dd class="g-col-4 g-col-sm-1 order-1 order-sm-2 mb-0 text-end">
					<a href="@editStep" 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>
}
