{{define "view-user"}}
{{template "header" .}}
<style>
table.classy th {
	text-align: left;
}
h3 {
	font-weight: normal;
}
td.active-silence {
	display: flex;
	align-items: center;
}

td.active-silence > input[type="submit"] {
	margin-left: auto;
	margin-right: 5%;
}

@media only screen and (max-width: 500px) {
	td.active-silence {
		flex-wrap: wrap;
	}
	td.active-silence > input[type="submit"] {
		margin: auto;
	}
}
input.copy-text {
	text-align: center;
	font-size: 1.2em;
	color: #555;
	width: 100%;
	box-sizing: border-box;
}
.modal {
	position: fixed;
}
</style>
<div class="snug content-container">
	<div id="overlay"></div>

	{{template "admin-header" .}}

	<h2 id="posts-header">{{.User.Username}}</h2>
	{{if .NewPassword}}<div class="alert success">
		<p>This user's password has been reset to:</p>
		<p><input type="text" class="copy-text" value="{{.NewPassword}}" onfocus="if (this.select) this.select(); else this.setSelectionRange(0, this.value.length);" readonly /></p>
		<p>They can use this new password to log into their account. <strong>This will only be shown once</strong>, so be sure to copy it and send it to them now.</p>
		{{if .ClearEmail}}<p>Their email address is: <a href="mailto:{{.ClearEmail}}">{{.ClearEmail}}</a></p>{{end}}
		</div>
	{{end}}
	<table class="classy export">
		<tr>
			<th>No.</th>
			<td>{{.User.ID}}</td>
		</tr>
		<tr>
			<th>Type</th>
			<td>{{if .User.IsAdmin}}Admin{{else}}User{{end}}</td>
		</tr>
		<tr>
			<th>Username</th>
			<td>{{.User.Username}}</td>
		</tr>
		<tr>
			<th>Joined</th>
			<td>{{.User.CreatedFriendly}}</td>
		</tr>
		<tr>
			<th>Total Posts</th>
			<td>{{.TotalPosts}}</td>
		</tr>
		<tr>
			<th>Last Post</th>
			<td>{{if .LastPost}}{{.LastPost}}{{else}}Never{{end}}</td>
		</tr>
		<tr>
			<form action="/admin/user/{{.User.Username}}/status" method="POST" {{if not .User.IsSilenced}}onsubmit="return confirmSilence()"{{end}}>
				<th><a id="status"></a>Status</th>
				<td class="active-silence">
				{{if .User.IsSilenced}}
					<p>Silenced</p>
					<input type="submit" value="Unsilence"/>
				{{else}}
					<p>Active</p>
					<input class="danger" type="submit" value="Silence" {{if .User.IsAdmin}}disabled{{end}}/>
				{{end}}
				</td>
			</form>
		</tr>
		<tr>
			<th>Password</th>
			<td>
				{{if ne .Username .User.Username}}
				<form id="reset-form" action="/admin/user/{{.User.Username}}/passphrase" method="post" autocomplete="false">
					<input type="hidden" name="user" value="{{.User.ID}}"/>
					<button type="submit">Reset</button>
				</form>
				{{else}}
				<a href="/me/settings" title="Go to reset password page">Change your password</a>
				{{end}}
			</td>
		</tr>
	</table>

	<h2>Blogs</h2>

	{{range .Colls}}
	<h3><a href="/{{.Alias}}/">{{.Title}}</a></h3>
	<table class="classy export">
		<tr>
			<th>Alias</th>
			<td>{{.Alias}}</td>
		</tr>
		<tr>
			<th>Title</th>
			<td>{{.Title}}</td>
		</tr>
		<tr>
			<th>Description</th>
			<td>{{.Description}}</td>
		</tr>
		<tr>
			<th>Visibility</th>
			<td>{{.FriendlyVisibility}}</td>
		</tr>
		<tr>
			<th>Views</th>
			<td>{{.Views}}</td>
		</tr>
		<tr>
			<th>Posts</th>
			<td>{{.TotalPosts}}</td>
		</tr>
		<tr>
			<th>Last Post</th>
			<td>{{if .LastPost}}{{.LastPost}}{{else}}Never{{end}}</td>
		</tr>
		{{if $.Config.Federation}}
		<tr>
			<th>Fediverse Followers</th>
			<td>{{.Followers}}</td>
		</tr>
		{{end}}
	</table>
	{{end}}

	{{ if not .User.IsAdmin }}
	<h2>Incinerator</h2>
	<div class="alert danger">
		<div class="row">
			<div>
				<h3>Delete this user</h3>
				<p>Permanently erase all user data, with no way to recover it.</p>
			</div>
			<button class="cta danger" onclick="prepareDeleteUser()">Delete this user...</button>
		</div>
	</div>
	{{end}}
</div>

<div id="modal-delete-user" class="modal">
	<h2>Are you sure?</h2>
	<div class="body">
		<p style="text-align:left">This action <strong>cannot</strong> be undone. It will permanently erase all traces of this user, <strong>{{.User.Username}}</strong>, including their account information, blogs, and posts.</p>
		<p>Please type <strong>{{.User.Username}}</strong> to confirm.</p>

		<ul id="delete-errors" class="errors"></ul>

		<form action="/admin/user/{{.User.Username}}/delete" method="post" onsubmit="confirmDeletion()">
			<input id="confirm-text" placeholder="{{.User.Username}}" type="text" class="confirm boxy" name="confirm-username" style="margin-top: 0.5em;" />
			<div style="text-align:right; margin-top: 1em;">
				<a id="cancel-delete" style="margin-right:2em" href="#">Cancel</a>
				<input class="danger" type="submit" id="confirm-delete" value="Delete this user" disabled />
			</div>
	</div>
</div>

<script src="/js/h.js"></script>
<script src="/js/modals.js"></script>
<script type="text/javascript">
H.getEl('cancel-delete').on('click', closeModals);

let $confirmDelBtn = document.getElementById('confirm-delete');
let $confirmText = document.getElementById('confirm-text')
$confirmText.addEventListener('input', function() {
	$confirmDelBtn.disabled = this.value !== '{{.User.Username}}'
});

function prepareDeleteUser() {
	$confirmText.value = ''
	showModal('delete-user')
	$confirmText.focus()
}

function confirmDeletion() {
	$confirmDelBtn.disabled = true
	$confirmDelBtn.value = 'Deleting...'
}

function confirmSilence() {
	return confirm("Silence this user? They'll still be able to log in and access their posts, but no one else will be able to see them anymore. You can reverse this decision at any time.");
}

	form = document.getElementById("reset-form");
	form.addEventListener('submit', function(e) {
		e.preventDefault();
		agreed = confirm("Reset this user's password? This will generate a new temporary password that you'll need to share with them, and invalidate their old one.");
		if (agreed === true) {
			form.submit();
		}
	});
</script>
{{template "footer" .}}
{{end}}
