@inherits Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>>
@using Dynamicweb.Data
@using System.Diagnostics
@using System.Threading.Tasks

@if (Pageview?.User?.ID != null)
{
	string pageTitle = Pageview.Page.GetDisplayName();
	string pageId = GetGlobalValue("Global:Page.ID");
	string invalidName = !string.IsNullOrEmpty(GetString("UserManagement:User.Name.Input.Error")) ? "is-invalid" : "";
	string invalidEmail = !string.IsNullOrEmpty(GetString("UserManagement:User.Email.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 tempPassword = Guid.NewGuid().ToString();

	@* For the changes, when adding or deleting a user, to show up in the list, you need to go to the backend: Settings -> Control panel -> Users
		And enable "Auto build index when users update", and select the Users index *@

	string secretJavascript = GetString("UserManagement:User.FormSubmit").Replace("return s(this);", "");

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

	if (!String.IsNullOrEmpty(GetString("error")))
	{
		<div class="alert alert-danger mb-5" role="alert">
			@GetString("error")
		</div>
	}

	string action = !string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.Form.Get("Action")) ? Dynamicweb.Context.Current.Request.Form.Get("Action") : "";
	string confirmation = !string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.QueryString.Get("Confirmation")) ? Dynamicweb.Context.Current.Request.QueryString.Get("Confirmation") : "";

	if (action == "DeleteUser")
	{
		int userId = !string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.Form.Get("UserId")) ? Convert.ToInt32(Dynamicweb.Context.Current.Request.Form.Get("UserId")) : 0;
		Dynamicweb.Security.UserManagement.User newUser = Dynamicweb.Security.UserManagement.User.GetUserByID(userId);
		newUser.Delete();

		string repository = "Secondary users";
		string indexName = "Users.index";
		string buildName = "Users";

		var indexService = Dynamicweb.Extensibility.ServiceLocator.Current.GetInstance<Dynamicweb.Indexing.IIndexService>();
		var index = indexService.LoadIndex(repository, indexName);

		Dynamicweb.Indexing.IndexHelper.BuildIndexInstances(repository, indexName, buildName);
		Stopwatch stopwatch = new Stopwatch();
		stopwatch.Start();
		Task.WaitAll(Task.Delay(250));

		while (indexService.GetActiveBuildTasks(index).Any() && stopwatch.ElapsedMilliseconds < 5000)
		{
			Task.WaitAll(Task.Delay(1000));
		}
		stopwatch.Stop();

		Dynamicweb.Context.Current.Response.Redirect("/Default.aspx?ID=" + Pageview.Page.ID + "&LayoutTemplate=Swift-v2_PageClean.cshtml&Confirmation=UserDeleted");
	}

	<div class="modal fade" id="inviteUserModal" tabindex="-1" aria-labelledby="inviteUserModalLabel" aria-hidden="true">
		<div class="modal-dialog modal-dialog-centered">
			<div class="modal-content light">
				<form id="InviteUserForm">
					@GetString("UserManagement:User.FormHidden")

					<div class="modal-header">
						<h5 class="modal-title" id="inviteUserModalLabel">@Translate("Invite user")</h5>
						<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
					</div>
					<div class="modal-body">
						<p class="text-muted">@Translate("An email will be sent to the user. The user must accept the request to complete the sign-up")</p>

						<input type="hidden" name="UserManagementForm" value="1">
						<input type="hidden" name="UserManagement_Form_CustomerNumber" value="@Pageview.User.CustomerNumber">
						<input type="hidden" name="UserManagement_Form_NewPassword" value="@tempPassword">
						<input type="hidden" name="UserManagement_Form_NewPasswordConfirm" value="@tempPassword">

						<div class="form-floating mb-3">
							<input type="text" class="form-control @invalidName" placeholder="@Translate("Name")" name="UserManagement_Form_Name" id="UserManagement_Form_Name" value="@GetString("UserManagement:User.Name")" required />
							<div class="invalid-feedback">@GetString("UserManagement:User.Name.Input.Error")</div>
							<label for="UserManagement_Form_Name" class="form-label">@Translate("Name")</label>
						</div>

						<div class="form-floating mb-4">
							<input type="email" class="form-control @invalidEmail" placeholder="@Translate("Email")" name="UserManagement_Form_Email" id="UserManagement_Form_Email" value="@GetString("UserManagement:User.Email")" required />
							<div class="invalid-feedback">@GetString("UserManagement:User.Email.Input.Error")</div>
							<label for="UserManagement_Form_Email" class="form-label">@Translate("Email")</label>
						</div>

					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-secondary" data-dw-button="secondary" data-bs-dismiss="modal">@Translate("Close")</button>
						<button type="button" onclick="InviteUser(event)" class="btn btn-primary" data-dw-button="primary" id="InviteUserButton" data-bs-dismiss="modal">@Translate("Invite user")</button>
					</div>
				</form>
			</div>
		</div>
	</div>

	<div class="position-fixed bottom-0 end-0 p-3" style="z-index: 11">
		<div id="UserInviteNotificationToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
			<div class="toast-header">
				<strong class="me-auto" id="UserInviteNotificationToastLabel"></strong>
				<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
			</div>
		</div>
	</div>

	<script>
		@secretJavascript

		async function InviteUser(e) {
			const form = document.querySelector("#InviteUserForm");

			var overlayElement = document.createElement('div');
			overlayElement.className = "preloader-overlay";
			overlayElement.setAttribute('id', "overlay");
			var overlayElementIcon = document.createElement('div');
			overlayElementIcon.className = "spinner-border";
			overlayElementIcon.style.top = window.pageYOffset + "px";
			overlayElement.appendChild(overlayElementIcon);

			if (e.currentTarget) {
				e.currentTarget.parentNode.insertBefore(overlayElement, e.currentTarget);
			}

			//Required to prevent spam (Calls the secret js method)
			s(form);

			swift.PageUpdater.Update(form);
		}
	</script>

	if (Pageview.IsVisualEditorMode)
	{
		<div class="alert alert-info text-center mt-3" role="alert">
			@Translate("Invite user modal")
		</div>
	}
}
