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

@{ 
	string pageId = GetGlobalValue("Global:Page.ID");
	string settingsType = Dynamicweb.Context.Current.Request.GetString("SettingsType");
	string goBackToPage = Dynamicweb.Context.Current.Request.GetString("GoBackToPage");
	string invalidOldPassword = !string.IsNullOrEmpty(GetString("UserManagement:User.OldPassword.Input.Error")) ? "is-invalid" : "";
	string invalidPassword = !string.IsNullOrEmpty(GetString("UserManagement:User.NewPassword.Input.Error")) ? "is-invalid" : "";
	string invalidConfirmPassword = !string.IsNullOrEmpty(GetString("UserManagement:User.NewPasswordConfirm.Input.Error")) ? "is-invalid" : "";
	string currentCountry = GetString("UserManagement:User.CountryCode");
	var regions = Dynamicweb.Ecommerce.Services.Countries.GetRegions(currentCountry);
	string countrySelectorColumnSize = "g-col-12 g-col-lg-6";
	string regionLabel = "State";
	
	bool isImpersonating = Dynamicweb.Security.UserManagement.User.GetCurrentSecondaryUser() != null;
	bool isExternalAuthentication = !string.IsNullOrEmpty(Dynamicweb.Core.Converter.ToString(Dynamicweb.Context.Current.Session["DW_extranet_ExternalLoginProviderID"]));
}

@GetString("UserManagement:User.FormStart").Replace("<form", "<form novalidate id='UserManagementEditForm'")
	@if (settingsType == "profile" || settingsType == "")
	{
		<header class="d-flex flex-row align-items-center gap-3 px-2 py-3 border-bottom">
			<h1 class="h6 m-0 flex-fill">@Pageview.CurrentParagraph.Item["Title"]</h1>
			@if (!isImpersonating && !isExternalAuthentication)
			{
				<a href="/Default.aspx?ID=@pageId&SettingsType=changepassword" class="btn btn-link p-0" data-dw-button="link">@Translate("Change password")</a>
			}
		</header>
	}
	else
	{
		<header class="py-1">
			<h1 class="h6 m-0 p-3 border-bottom">@Translate("Change password")</h1>
		</header>
	}

	@if (settingsType == "profile" || settingsType == "") {
		<div class="grid p-3">

			@foreach (LoopItem error in GetLoop("FormValidationErrors"))
			{
				<div class="alert alert-danger m-0" role="alert">
					@error.GetString("UserManagement:User.FormValidationError.Message")
				</div>
			}

			@if (!string.IsNullOrEmpty(Pageview.User.Address) || !string.IsNullOrEmpty(Pageview.User.Country) || !string.IsNullOrEmpty(Pageview.User.Zip) || !string.IsNullOrEmpty(Pageview.User.State)) {

				<div class="g-col-12 g-col-lg-6">
					@if (!string.IsNullOrEmpty(GetString("UserManagement:User.Name")))
					{
						<h2 class="h6 mb-3">@GetString("UserManagement:User.Name")</h2>
					}
					@{
						if (!string.IsNullOrEmpty(GetString("UserManagement:User.Company")))
						{
							<div>@GetString("UserManagement:User.Company")</div>
						}
						if (!string.IsNullOrEmpty(GetString("UserManagement:User.Address")))
						{
							<div>@GetString("UserManagement:User.Address")</div>
						}
						if (!string.IsNullOrEmpty(GetString("UserManagement:User.Address2")))
						{
							<div>@GetString("UserManagement:User.Address2")</div>
						}
						if (!string.IsNullOrEmpty(GetString("UserManagement:User.Zip")) || !string.IsNullOrEmpty(GetString("UserManagement:User.City")))
						{
							<div><span>@GetString("UserManagement:User.Zip")</span> <span>@GetString("UserManagement:User.City")</span></div>
						}
						if (!string.IsNullOrEmpty(GetString("UserManagement:User.State")))
						{
							string stateName = Dynamicweb.Ecommerce.Services.Countries.GetRegion(currentCountry, GetString("UserManagement:User.State"))?.GetName(Dynamicweb.Ecommerce.Common.Context.LanguageID) ?? string.Empty;

							<div>@stateName</div>
						}
						if (!string.IsNullOrEmpty(GetString("UserManagement:User.Country")))
						{
							<div>@GetString("UserManagement:User.Country")</div>
						}
						
	                    if (!string.IsNullOrEmpty(GetString("UserManagement:User.Email")))
	                    {
		                    <div>@GetString("UserManagement:User.Email")</div>
	                    }
	                    if (!string.IsNullOrEmpty(GetString("UserManagement:User.Phone")))
	                    {
		                    <div>@GetString("UserManagement:User.Phone")</div>
	                    }
                    }
				</div>
			}
		

			<div class="g-col-12">
				@if (!string.IsNullOrEmpty(goBackToPage)) {
					<a href="/Default.aspx?ID=@goBackToPage" class="btn btn-secondary" data-dw-button="secondary">@Translate("Go back to checkout")</a>
				}
			</div>
			
		</div>
	} else {
		<div class="grid gap-3 px-2 py-3">

			<div class="g-col-12">
			@foreach (LoopItem error in GetLoop("FormValidationErrors"))
			{
				<div class="alert alert-danger" role="alert">
					@error.GetString("UserManagement:User.FormValidationError.Message")
				</div>
			}
			</div>

			<div class="g-col-12">
				<div class="form-floating">
					<input type="password" class="form-control @invalidOldPassword" id="UserManagement_Form_OldPassword" name="UserManagement_Form_OldPassword" placeholder="@Translate("Old password")" required>
					<label for="UserManagement_Form_OldPassword">@Translate("Current password")</label>
					<div class="invalid-feedback">@GetString("UserManagement:User.OldPassword.Input.Error")</div>
				</div>
			</div>

			<div class="g-col-12">
				<div class="form-floating">
					<input type="password" class="form-control @invalidPassword" id="UserManagement_Form_NewPassword" name="UserManagement_Form_NewPassword" placeholder="@Translate("Old password")" required>
					<label for="UserManagement_Form_NewPassword">@Translate("New password")</label>
					<div class="invalid-feedback">@GetString("UserManagement:User.NewPassword.Input.Error")</div>
				</div>
			</div>

			<div class="g-col-12">
				<div class="form-floating">
					<input type="password" class="form-control @invalidConfirmPassword" id="UserManagement_Form_NewPasswordConfirm" name="UserManagement_Form_NewPasswordConfirm" placeholder="@Translate("Old password")" required>
					<label for="UserManagement_Form_NewPasswordConfirm">@Translate("Confirm new password")</label>
					<div class="invalid-feedback">@GetString("UserManagement:User.NewPasswordConfirm.Input.Error")</div>
				</div>
			</div>

			<div class="g-col-12">
				<button type="submit" class="btn btn-primary" data-dw-button="primary">@Translate("Update password")</button>
			</div>

		</div>
	}

	<input type="hidden" name="ID" value="@pageId">
@GetString("UserManagement:User.FormEnd")

<script>
	function submitForm() {
		var form = document.querySelector("#UserManagementEditForm");
		form.requestSubmit();
	}
</script>
