{{template "admin/layout_head" (dict "ctxData" . "pageClass" "admin edit user")}}
	<div class="admin-setting-content">
		<h4 class="ui top attached header">
			{{ctx.Locale.Tr "admin.users.edit_account"}}
		</h4>
		<div class="ui attached segment">
			{{if or .AccountRequestStatus .AccountRequestValidationMailAttemptStatus}}
			<div class="ui info message">
				{{if .AccountRequestStatus}}<p><strong>{{ctx.Locale.Tr "admin.users.account_request.status"}}</strong>: {{ctx.Locale.Tr (printf "admin.users.account_request.status.%s" .AccountRequestStatus)}}</p>{{end}}
				{{if .AccountRequestSignupAttempts}}<p>{{ctx.Locale.Tr "admin.users.account_request.attempts" .AccountRequestSignupAttempts}}</p>{{end}}
				{{if .AccountRequestValidationMailAttemptStatus}}<p><strong>{{ctx.Locale.Tr "admin.users.account_request.validation_mail"}}</strong>: <span {{if eq .AccountRequestValidationMailAttemptStatus "succeeded"}}class="tw-text-green-600"{{else}}class="tw-text-red-600"{{end}}>{{if eq .AccountRequestValidationMailAttemptStatus "succeeded"}}✔{{else}}❌{{end}}</span> {{DateUtils.FullTime .AccountRequestValidationMailAttemptUnix}}</p>{{end}}
			</div>
			{{end}}

			{{if .AccountRequestStatus}}
				{{if eq .AccountRequestStatus "pending_admin_review"}}
				<div class="field tw-flex tw-flex-wrap tw-gap-2 tw-mb-4">
					<form method="post" action="./request/activate">
						{{.CsrfTokenHtml}}
						<button class="ui primary button">{{ctx.Locale.Tr "admin.users.account_request.activate"}}</button>
					</form>
					<form method="post" action="./request/reject">
						{{.CsrfTokenHtml}}
						<button class="ui red button">{{ctx.Locale.Tr "admin.users.account_request.reject"}}</button>
					</form>
				</div>
				{{else if or (eq .AccountRequestStatus "blocked") (eq .AccountRequestStatus "rejected")}}
				<div class="field tw-mb-4">
					<form method="post" action="./request/unblock">
						{{.CsrfTokenHtml}}
						<button class="ui primary button">{{ctx.Locale.Tr "admin.users.account_request.unblock"}}</button>
					</form>
				</div>
				{{end}}
			{{end}}

			<form class="ui form" action="./edit" method="post">
				{{template "base/disable_form_autofill"}}
				<div class="field {{if .Err_UserName}}error{{end}}">
					<label for="user_name">{{ctx.Locale.Tr "username"}}</label>
					<input id="user_name" name="user_name" value="{{.User.Name}}" maxlength="40">
				</div>
				<!-- Types and name -->
				<div class="inline required field {{if .Err_LoginType}}error{{end}}">
					<label>{{ctx.Locale.Tr "admin.users.auth_source"}}</label>
					<div class="ui selection type dropdown">
						<input type="hidden" id="login_type" name="login_type" value="{{.LoginSource.Type.Int}}-{{.LoginSource.ID}}" required>
						<div class="text">{{ctx.Locale.Tr "admin.users.local"}}</div>
						{{svg "octicon-triangle-down" 14 "dropdown icon"}}
						<div class="menu">
							<div class="item" data-value="0-0">{{ctx.Locale.Tr "admin.users.local"}}</div>
							{{range .Sources}}
								<div class="item" data-value="{{.Type.Int}}-{{.ID}}">{{.Name}}</div>
							{{end}}
						</div>
					</div>
				</div>

				<div class="inline field {{if .Err_Visibility}}error{{end}}">
					<span class="inline required field"><label for="visibility">{{ctx.Locale.Tr "settings.visibility"}}</label></span>
					<div class="ui selection type dropdown">
						{{if .User.Visibility.IsPublic}}<input type="hidden" id="visibility" name="visibility" value="0">{{end}}
						{{if .User.Visibility.IsLimited}}<input type="hidden" id="visibility" name="visibility" value="1">{{end}}
						{{if .User.Visibility.IsPrivate}}<input type="hidden" id="visibility" name="visibility" value="2">{{end}}
						<div class="text">
							{{if .User.Visibility.IsPublic}}{{ctx.Locale.Tr "settings.visibility.public"}}{{end}}
							{{if .User.Visibility.IsLimited}}{{ctx.Locale.Tr "settings.visibility.limited"}}{{end}}
							{{if .User.Visibility.IsPrivate}}{{ctx.Locale.Tr "settings.visibility.private"}}{{end}}
						</div>
						{{svg "octicon-triangle-down" 14 "dropdown icon"}}
						<div class="menu">
							{{range $mode := .AllowedUserVisibilityModes}}
								{{if $mode.IsPublic}}
									<div class="item" data-tooltip-content="{{ctx.Locale.Tr "settings.visibility.public_tooltip"}}" data-value="0">{{ctx.Locale.Tr "settings.visibility.public"}}</div>
								{{else if $mode.IsLimited}}
									<div class="item" data-tooltip-content="{{ctx.Locale.Tr "settings.visibility.limited_tooltip"}}" data-value="1">{{ctx.Locale.Tr "settings.visibility.limited"}}</div>
								{{else if $mode.IsPrivate}}
									<div class="item" data-tooltip-content="{{ctx.Locale.Tr "settings.visibility.private_tooltip"}}" data-value="2">{{ctx.Locale.Tr "settings.visibility.private"}}</div>
								{{end}}
							{{end}}
						</div>
					</div>
				</div>

				<div class="required non-local field {{if .Err_LoginName}}error{{end}} {{if eq .User.LoginSource 0}}tw-hidden{{end}}">
					<label for="login_name">{{ctx.Locale.Tr "admin.users.auth_login_name"}}</label>
					<input id="login_name" name="login_name" value="{{.User.LoginName}}">
				</div>
				<div class="field {{if .Err_FullName}}error{{end}}">
					<label for="full_name">{{ctx.Locale.Tr "settings.full_name"}}</label>
					<input id="full_name" name="full_name" value="{{.User.FullName}}" maxlength="100">
				</div>
				<div class="required field {{if .Err_Email}}error{{end}}">
					<label for="email">{{ctx.Locale.Tr "email"}}</label>
					<input id="email" name="email" type="email" value="{{.User.Email}}" required>
				</div>
				<div class="local field {{if .Err_Password}}error{{end}} {{if not (or (.User.IsLocal) (.User.IsOAuth2))}}tw-hidden{{end}}">
					<label for="password">{{ctx.Locale.Tr "password"}}</label>
					<input id="password" name="password" type="password" autocomplete="new-password">
					<p class="help">{{ctx.Locale.Tr "admin.users.password_helper"}}</p>
				</div>

				<div class="field {{if .Err_Language}}error{{end}}">
					<label for="language">{{ctx.Locale.Tr "settings.language"}}</label>
					<div class="ui selection dropdown">
						<input name="language" type="hidden" value="{{.User.Language}}">
						{{svg "octicon-triangle-down" 14 "dropdown icon"}}
						<div class="text">{{range .AllLangs}}{{if eq $.User.Language .Lang}}{{.Name}}{{end}}{{end}}</div>
						<div class="menu">
						{{range .AllLangs}}
							<div class="item{{if eq $.User.Language .Lang}} active selected{{end}}" data-value="{{.Lang}}">{{.Name}}</div>
						{{end}}
						</div>
					</div>
				</div>

				<div class="field {{if .Err_Website}}error{{end}}">
					<label for="website">{{ctx.Locale.Tr "settings.website"}}</label>
					<input id="website" name="website" type="url" value="{{.User.Website}}" placeholder="http://mydomain.com or https://mydomain.com" maxlength="255">
				</div>
				<div class="field {{if .Err_Location}}error{{end}}">
					<label for="location">{{ctx.Locale.Tr "settings.location"}}</label>
					<input id="location" name="location" value="{{.User.Location}}" maxlength="50">
				</div>

				<div class="divider"></div>

				<div class="inline field {{if .Err_MaxRepoCreation}}error{{end}}">
					<label for="max_repo_creation">{{ctx.Locale.Tr "admin.users.max_repo_creation"}}</label>
					<input id="max_repo_creation" name="max_repo_creation" type="number" min="-1" value="{{.User.MaxRepoCreation}}">
					<p class="help">{{ctx.Locale.Tr "admin.users.max_repo_creation_desc"}}</p>
				</div>

				<div class="divider"></div>

				<div class="inline field">
					<div class="ui checkbox">
						<label><strong>{{ctx.Locale.Tr "admin.users.is_activated"}}</strong></label>
						<input name="active" type="checkbox" data-admin-status-reason-toggle="#active-reason-field" data-admin-status-reason-inverted="true" {{if .User.IsActive}}checked{{end}} {{if or .AccountRequestStatus (eq .User.ID .SignedUserID)}}disabled{{end}}>
						{{if .AccountRequestStatus}}<p class="help">{{ctx.Locale.Tr "admin.users.account_request.active_managed_externally"}}</p>{{end}}
					</div>
				</div>
				<div class="field {{if .Err_ActiveReason}}error{{end}}" id="active-reason-field">
					<label for="active_reason">
						{{ctx.Locale.Tr "admin.users.status_change.reason"}}
						{{if .ActiveReasonByAdmin}}{{if .ActiveReasonByAdmin.Name}}<span class="ui mini label tw-whitespace-nowrap" {{if .ActiveReasonByAdmin.Email}}data-tooltip-content="{{.ActiveReasonByAdmin.Email}}"{{end}}>by {{.ActiveReasonByAdmin.Name}}</span>{{end}}{{end}}
					</label>
					<textarea id="active_reason" name="active_reason" rows="2" maxlength="500" placeholder="{{ctx.Locale.Tr "admin.users.status_change.reason"}}">{{.active_reason}}</textarea>
				</div>
				<div class="inline field">
					<div class="ui checkbox">
						<label><strong>{{ctx.Locale.Tr "admin.users.prohibit_login"}}</strong></label>
						<input name="prohibit_login" type="checkbox" data-admin-status-reason-toggle="#prohibit-login-reason-field" {{if .User.ProhibitLogin}}checked{{end}} {{if (eq .User.ID .SignedUserID)}}disabled{{end}}>
					</div>
				</div>
				<div class="field {{if .Err_ProhibitLoginReason}}error{{end}}" id="prohibit-login-reason-field">
					<label for="prohibit_login_reason">
						{{ctx.Locale.Tr "admin.users.status_change.reason"}}
						{{if .ProhibitLoginReasonByAdmin}}{{if .ProhibitLoginReasonByAdmin.Name}}<span class="ui mini label tw-whitespace-nowrap" {{if .ProhibitLoginReasonByAdmin.Email}}data-tooltip-content="{{.ProhibitLoginReasonByAdmin.Email}}"{{end}}>by {{.ProhibitLoginReasonByAdmin.Name}}</span>{{end}}{{end}}
					</label>
					<textarea id="prohibit_login_reason" name="prohibit_login_reason" rows="2" maxlength="500" placeholder="{{ctx.Locale.Tr "admin.users.status_change.reason"}}">{{.prohibit_login_reason}}</textarea>
				</div>
				{{if ne .User.ID .SignedUserID}}
				<div class="inline field">
					<div class="ui checkbox">
						<label>
							<strong>{{ctx.Locale.Tr "admin.users.is_admin"}}</strong>
							{{if .AdminReasonByAdmin}}{{if .AdminReasonByAdmin.Name}}<span class="ui mini label tw-whitespace-nowrap" {{if .AdminReasonByAdmin.Email}}data-tooltip-content="{{.AdminReasonByAdmin.Email}}"{{end}}>by {{.AdminReasonByAdmin.Name}}</span>{{end}}{{end}}
						</label>
						<input name="admin" type="checkbox" data-admin-status-reason-toggle="#admin-reason-field" data-admin-status-reason-inverted="true" data-admin-status-reason-requires-history="true" data-admin-status-reason-has-history="{{if .admin_reason}}true{{else}}false{{end}}" data-admin-status-reason-initial-checked="{{if .User.IsAdmin}}true{{else}}false{{end}}" {{if .User.IsAdmin}}checked{{end}} {{if not .CanEditAdminStatus}}disabled{{end}}>
					</div>
				</div>
				<div class="field {{if .Err_AdminReason}}error{{end}}" id="admin-reason-field">
					<label for="admin_reason">
						{{ctx.Locale.Tr "admin.users.status_change.reason"}}
						{{if .AdminReasonByAdmin}}{{if .AdminReasonByAdmin.Name}}<span class="ui mini label tw-whitespace-nowrap" {{if .AdminReasonByAdmin.Email}}data-tooltip-content="{{.AdminReasonByAdmin.Email}}"{{end}}>by {{.AdminReasonByAdmin.Name}}</span>{{end}}{{end}}
					</label>
					<textarea id="admin_reason" name="admin_reason" rows="2" maxlength="500" placeholder="{{ctx.Locale.Tr "admin.users.status_change.reason"}}">{{.admin_reason}}</textarea>
				</div>
				{{end}}
				{{if and .SuperAdminEnabled (or .CanEditSuperAdmin .IsUserSuperAdmin)}}
				<div class="inline field">
					<div class="ui checkbox">
						<label>
							<strong>{{ctx.Locale.Tr "admin.users.is_super_admin"}}</strong>
							{{if .SuperAdminGrantByAdmin}}{{if .SuperAdminGrantByAdmin.Name}}<span class="ui mini label tw-whitespace-nowrap" {{if .SuperAdminGrantByAdmin.Email}}data-tooltip-content="{{.SuperAdminGrantByAdmin.Email}}"{{end}}>by {{.SuperAdminGrantByAdmin.Name}}</span>{{end}}{{end}}
						</label>
						<input name="super_admin" type="checkbox" data-admin-status-reason-toggle="#super-admin-reason-field" data-admin-status-reason-inverted="true" data-admin-status-reason-requires-history="true" data-admin-status-reason-has-history="{{if .super_admin_reason}}true{{else}}false{{end}}" data-admin-status-reason-initial-checked="{{if .IsUserSuperAdmin}}true{{else}}false{{end}}" {{if .IsUserSuperAdmin}}checked{{end}} {{if or (not .CanEditSuperAdmin) .IsLastSuperAdminUser}}disabled{{end}}>
					</div>
				</div>
				<div class="field {{if .Err_SuperAdminReason}}error{{end}}" id="super-admin-reason-field">
					<label for="super_admin_reason">
						{{ctx.Locale.Tr "admin.users.status_change.reason"}}
						{{if .SuperAdminReasonByAdmin}}{{if .SuperAdminReasonByAdmin.Name}}<span class="ui mini label tw-whitespace-nowrap" {{if .SuperAdminReasonByAdmin.Email}}data-tooltip-content="{{.SuperAdminReasonByAdmin.Email}}"{{end}}>by {{.SuperAdminReasonByAdmin.Name}}</span>{{end}}{{end}}
					</label>
					<textarea id="super_admin_reason" name="super_admin_reason" rows="2" maxlength="500" placeholder="{{ctx.Locale.Tr "admin.users.status_change.reason"}}">{{.super_admin_reason}}</textarea>
				</div>
				{{end}}
				<div class="inline field">
					<div class="ui checkbox">
						<label><strong>{{ctx.Locale.Tr "admin.users.is_restricted"}}</strong></label>
						<input name="restricted" type="checkbox" data-admin-status-reason-toggle="#restricted-reason-field" {{if .User.IsRestricted}}checked{{end}} {{if eq .User.ID .SignedUserID}}disabled{{end}}>
					</div>
				</div>
				<div class="field {{if .Err_RestrictedReason}}error{{end}}" id="restricted-reason-field">
					<label for="restricted_reason">
						{{ctx.Locale.Tr "admin.users.status_change.reason"}}
						{{if .RestrictedReasonByAdmin}}{{if .RestrictedReasonByAdmin.Name}}<span class="ui mini label tw-whitespace-nowrap" {{if .RestrictedReasonByAdmin.Email}}data-tooltip-content="{{.RestrictedReasonByAdmin.Email}}"{{end}}>by {{.RestrictedReasonByAdmin.Name}}</span>{{end}}{{end}}
					</label>
					<textarea id="restricted_reason" name="restricted_reason" rows="2" maxlength="500" placeholder="{{ctx.Locale.Tr "admin.users.status_change.reason"}}">{{.restricted_reason}}</textarea>
				</div>
				<div class="inline field {{if .DisableGitHooks}}tw-hidden{{end}}">
					<div class="ui checkbox" data-tooltip-content="{{ctx.Locale.Tr "admin.users.allow_git_hook_tooltip"}}">
						<label><strong>{{ctx.Locale.Tr "admin.users.allow_git_hook"}}</strong></label>
						<input name="allow_git_hook" type="checkbox" {{if .User.CanEditGitHook}}checked{{end}} {{if .DisableGitHooks}}disabled{{end}}>
					</div>
				</div>
				<div class="inline field {{if or (.DisableImportLocal) (.DisableMigrations)}}tw-hidden{{end}}">
					<div class="ui checkbox">
						<label><strong>{{ctx.Locale.Tr "admin.users.allow_import_local"}}</strong></label>
						<input name="allow_import_local" type="checkbox" {{if .User.CanImportLocal}}checked{{end}} {{if .DisableImportLocal}}disabled{{end}}>
					</div>
				</div>
				{{if not .DisableRegularOrgCreation}}
				<div class="inline field">
					<div class="ui checkbox">
						<label><strong>{{ctx.Locale.Tr "admin.users.allow_create_organization"}}</strong></label>
						<input name="allow_create_organization" type="checkbox" {{if .User.CanCreateOrganization}}checked{{end}}>
					</div>
				</div>
				{{end}}

				{{if .TwoFactorEnabled}}
				<div class="divider"></div>
				<div class="inline field">
					<div class="ui checkbox">
						<label><strong>{{ctx.Locale.Tr "admin.users.reset_2fa"}}</strong></label>
						<input name="reset_2fa" type="checkbox">
					</div>
				</div>
				{{end}}

				<div class="divider"></div>

				{{if not .CanDeleteUser}}
				<p class="text left tw-mb-3">{{svg "octicon-alert"}} {{.DeleteUserDisabledReason}}</p>
				{{end}}
				<div class="field">
					<button class="ui primary button">{{ctx.Locale.Tr "admin.users.update_profile"}}</button>
					{{if not .CanDeleteUser}}
					<button class="ui red button disabled" disabled data-tooltip-content="{{.DeleteUserDisabledReason}}">{{ctx.Locale.Tr "admin.users.delete_account"}}</button>
					{{else}}
					<button class="ui red button show-modal" data-modal="#delete-user-modal">{{ctx.Locale.Tr "admin.users.delete_account"}}</button>
					{{end}}
				</div>
			</form>
		</div>

		<h4 class="ui top attached header">
			{{ctx.Locale.Tr "settings.avatar"}}
		</h4>
		<div class="ui attached segment">
			<form class="ui form" action="./avatar" method="post" enctype="multipart/form-data">
				{{if not .DisableGravatar}}
				<div class="inline field">
					<div class="ui radio checkbox">
						<input name="source" value="lookup" type="radio" {{if not .User.UseCustomAvatar}}checked{{end}}>
						<label>{{ctx.Locale.Tr "settings.lookup_avatar_by_mail"}}</label>
					</div>
				</div>
				<div class="field tw-pl-4 {{if .Err_Gravatar}}error{{end}}">
					<label for="gravatar">Avatar {{ctx.Locale.Tr "email"}}</label>
					<input id="gravatar" name="gravatar" value="{{.User.AvatarEmail}}">
				</div>
				{{end}}

				<div class="inline field">
					<div class="ui radio checkbox">
						<input name="source" value="local" type="radio" {{if .User.UseCustomAvatar}}checked{{end}}>
						<label>{{ctx.Locale.Tr "settings.enable_custom_avatar"}}</label>
					</div>
				</div>

				<div class="inline field tw-pl-4">
					{{template "shared/avatar_upload_crop" dict "LabelText" (ctx.Locale.Tr "settings.choose_new_avatar")}}
				</div>

				<div class="field">
					<button class="ui primary button">{{ctx.Locale.Tr "settings.update_avatar"}}</button>
					<button class="ui red button link-action" data-url="./avatar/delete">{{ctx.Locale.Tr "settings.delete_current_avatar"}}</button>
				</div>
			</form>
		</div>
	</div>

{{if .CanDeleteUser}}
	<div class="ui g-modal-confirm delete modal" id="delete-user-modal">
		<div class="header">
			{{svg "octicon-trash"}}
			{{ctx.Locale.Tr "settings.delete_account_title"}}
		</div>
		<form class="ui form" method="post" action="./delete">
			<div class="content">
				<p>{{ctx.Locale.Tr "settings.delete_account_desc"}}</p>
				<div class="field">
					<div class="ui checkbox">
						<label for="purge">{{ctx.Locale.Tr "admin.users.purge"}}</label>
						<input name="purge" type="checkbox" checked>
					</div>
					<p class="help">{{ctx.Locale.Tr "admin.users.purge_help"}}</p>
				</div>
			</div>
			{{template "base/modal_actions_confirm" .}}
		</form>
	</div>
{{end}}

<script>
	for (const checkbox of document.querySelectorAll('[data-admin-status-reason-toggle]')) {
		const reasonField = document.querySelector(checkbox.getAttribute('data-admin-status-reason-toggle'));
		if (!reasonField) continue;
		const syncReasonField = () => {
			const inverted = checkbox.getAttribute('data-admin-status-reason-inverted') === 'true';
			let showReason = inverted ? !checkbox.checked : checkbox.checked;
			if (checkbox.getAttribute('data-admin-status-reason-requires-history') === 'true') {
				const hasHistory = checkbox.getAttribute('data-admin-status-reason-has-history') === 'true';
				const initiallyChecked = checkbox.getAttribute('data-admin-status-reason-initial-checked') === 'true';
				showReason = showReason && (hasHistory || initiallyChecked);
			}
			reasonField.classList.toggle('tw-hidden', !showReason);
		};
		checkbox.addEventListener('change', syncReasonField);
		syncReasonField();
	}
</script>

{{template "admin/layout_footer" .}}
