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

@{ 
	//Hack to set the token on the user correcty
	string token = !string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.QueryString.Get("RecoveryToken")) ? Dynamicweb.Context.Current.Request.QueryString.Get("RecoveryToken") : "";
	int newUserId = !string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.QueryString.Get("UserID")) ? Convert.ToInt32(Dynamicweb.Context.Current.Request.QueryString.Get("UserID")) : 0;
	bool setTokenCorrectly = !string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.QueryString.Get("SetTokenCorrectly")) ? Convert.ToBoolean(Dynamicweb.Context.Current.Request.QueryString.Get("SetTokenCorrectly")) : false;

	if (setTokenCorrectly) {
		Dynamicweb.Security.UserManagement.User newUser = Dynamicweb.Security.UserManagement.User.GetUserByID(newUserId);

		DateTime time = DateTime.UtcNow.AddHours(48);
		newUser.PasswordRecoveryTokenExpirationTime = time;
		newUser.PasswordRecoveryToken = token;
		newUser.Save();

		Dynamicweb.Context.Current.Response.Redirect("/Default.aspx?ID=" + Pageview.Page.ID + "&RecoveryToken=" + token + "&LoginAction=changepassword");
	}

	string pageTitle = Pageview.Page.GetDisplayName();
}


@if (!string.IsNullOrEmpty(GetString("DW_extranet_error_uk")))
{
	<div class="alert alert-danger" role="alert">
		@GetString("DW_extranet_error_uk")
	</div>
}

@if (GetString("UserManagement:User.Login.Action") == "NewPasswordForm")
{
	if (GetBoolean("UserManagement:User.Login.RecoveryToken.FoundUser"))
	{
		<div class="card border-0 mx-auto" style="max-width: 30rem;">
			<div class="card-header text-center p-4 border-bottom">
				<h4 class="display-6 m-0">@pageTitle</h4>
				<p class="card-text mt-2">@Translate("You have been invited to create an account. You just need to create your password")</p>
			</div>
	
			<div class="card-body p-4">
				<form method="post" class="grid grid-1 gap-4">
					<input value="1" type="hidden" name="CreateProfileAutoLogin">

					<div>
						<div class="form-label fs-7 mb-0 opacity-75">@Translate("Password")</div>
						<div class="form-floating">
							<input type="password" class="form-control" id="UserManagement_Form_NewPassword" name="UserManagement_Form_NewPassword" autocomplete="off" placeholder="@Translate("Password")">
							<div class="invalid-feedback">@GetString("UserManagement:User.Login.Field.NewPassword.Error")</div>
							<label for="UserManagement_Form_NewPassword">@Translate("Password")</label>
						</div>
					</div>

					<div>
						<div class="form-label fs-7 mb-0 opacity-75">@Translate("Confirm password")</div>
						<div class="form-floating">
							<input type="password" class="form-control" id="UserManagement_Form_NewPasswordConfirm" name="UserManagement_Form_NewPasswordConfirm" autocomplete="off" placeholder="@Translate("Confirm password")">
							<div class="invalid-feedback">@GetString("UserManagement:User.Login.Field.NewPassword.Error")</div>
							<label for="UserManagement_Form_NewPasswordConfirm">@Translate("Confirm password")</label>
						</div>
					</div>

					<div class="d-grid">
						<button type="submit" name="LoginAction" value="Recovery" class="btn btn-primary" data-dw-button="primary" id="SignInButton">@Translate("Submit")</button>
					</div>
				</form>
			</div>
		</div>
	}
} else if (GetString("UserManagement:User.Login.Action") == "PasswordChanged") {
	string signInPage = GetPageIdByNavigationTag("SignInPage") != 0 ? $"/Default.aspx?ID={GetPageIdByNavigationTag("SignInPage")}" : null;

	<div class="card border-0 mx-auto" style="max-width: 30rem;">
		<div class="card-header text-center p-4 border-bottom">
			<h4 class="display-6 m-0">@Translate("Your password has been saved")</h4>
			<p class="card-text mt-2">@Translate("You are now ready to sign in and start browsing")</p>
		</div>

		@if (!string.IsNullOrEmpty(signInPage)) { 
			<div class="card-body p-4">
				<a href="@signInPage" class="btn btn-primary w-100" data-dw-button="primary" id="SignInLink">@Translate("Sign in")</a>
			</div>
		}
	</div>
}
