<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<meta charset="UTF-8"/>
	<title>Tigase XMPP Server</title>
	${ util["inlineCss"].apply('bootstrap.min.css') }
	${ util["inlineCss"].apply('custom.css') }
	${ util["inlineCss"].apply('http-api-custom.css') }
</head>
<body>
<noscript>
	<div style="border: 2px solid red; margin: 3px">
		<div style="color: red; font-weight: bold; padding: 10px "><span>WARNING:</span><span>Your browser does not support JavaScript or JavaScript is disabled.</span></div>
		<div style="font-size: 0.9em; padding: 5px 15px">Tigase XMPP Server uses JavaScript to validate chosen configuration options - it may not work properly without JavaScript and may result in an incorrect configuration.<br/>We strongly recommend changing web browser or enabling JavaScript in web browser configuration.</div>
	</div>
</noscript>
<div id="wrapper">
	<nav class="navbar navbar-default navbar-static-top" role = "navigation" style="margin-bottom: 0;">
		<div class="navbar-header">
			<a class="navbar-brand" href="/admin/" id="logo">
				<p>Tigase XMPP Server - Admin console<% def currentCommand = model.commands.find { command -> command.node == request.getParameter('_node') && command.jid == request.getParameter('_jid') };
					if (currentCommand) { %>: ${currentCommand.name}<% } %></p>
			</a>
		</div>
		<div class="navbar-default sidebar" role="navigation">
			<div class="sidebar-nav navbar-collapse">
				<ul class="nav" id="side-menu">
					<% model.commands.findAll { !it.group }.each { it.group = "Other" };
					def groups = model.commands.collect { it.group }.unique().sort().each { group -> %>
					<li>
						<a class="sidebar-group-header ${(group == request.getParameter('_group')) ? 'active' : ''}" href="?_group=${java.net.URLEncoder.encode(group ?: '')}"><i class="fa fa-fw"></i>${group}</a><% if (request.getParameter("_group") == group) { %>
						<ul><% model.commands.findAll { it.group == group }.sort { it.name }.each { command -> %>
							<li ${ (command.node == request.getParameter('_node') && command.jid == request.getParameter('_jid')) ? 'class="active"' : ''}><a class="command-name" href="?_group=${java.net.URLEncoder.encode(group ?: '')}&_jid=${java.net.URLEncoder.encode(command.jid)}&_node=${java.net.URLEncoder.encode(command.node)}">${command.name}</a><br/><span class="command-jid">${command.jid}</span></li>
						<% } %></ul><% } %>
					</li>
					<% } %>
				</ul>
			</div>
		</div>
	</nav>
	<div id="page-wrapper">
	<div class="row">
		<div class="col-sm-12 col-md-12 col-lg-12">
			<% if (model.formFields) { %>
			<% def table = null; %>
			<form method="POST" class="form-horizontal">
				<div class="panel panel-default">
					<% def title = model.formFields?.find { it.getName() == 'title' }?.getCData();
					if (title != null) { %>
					<div class="panel-heading panel-xs">
						<h4>${title}</h4>
					</div>
					<% } %>
					<div class="panel-body">
						<% instructions = model.formFields?.find { it.getName() == 'instructions' }?.getCData();
						if (instructions != null) { %>
						<h5>${instructions}</h5>
						<% } %>
						<% if (1==0) { %></div><% } %>
						<% model.formFields?.findAll { it.getName() != 'title' && it.getName() != 'instructions' }.each { formField -> %>
							<% if (formField.getName() == 'reported') {
							if (table != null) { table = null; %></tbody></table></div><% }
								table = formField.getChildren().findAll { it.getName() == 'field' }.collectEntries { [ (it.getAttribute('var')) : ['align' : it.getAttribute('align')] ] };
								%>
								<div class="panel panel-default">
									<% if (formField.getAttribute("label") != null) { %><div class="panel-heading"><h5 class="panel-title"><%= formField.getAttribute("label") %></h5></div><% } %>
									
								<table class="table">
									<thead>
										<tr>
											<% formField.getChildren().each { %><th>${ it.getAttribute("label") ?: it.getAttribute("var") }</th><% } %>
										</tr>
									</thead>
									<tbody>
							<% } else if (formField.getName() == 'item' ) { %>
								<tr>
									<% formField.getChildren().findAll { it.getName() == 'field' }.each {
									def align = table[it.getAttribute('var')]?.align;
									def style = "";
									if (align) {
										style += "text-align: " + align + ";";
									} %>
									<td style="${style}">${ it.getChildAttribute('value', 'label') ?: it.getChildCData('field/value') ?: '' }</td>
									<% } %>
								</tr>
							<% } else if (formField.getName() == 'field') { %>
								<% if (table != null) { table = null; %></tbody></table></div><% } %>
								<% def type = formField.getAttribute('type') ?: 'text-single';
								if (type == 'hidden') { %>
									<input type='hidden' name="${formField.getAttribute('var')}" value="${formField.getChildCData('field/value') ?: ''}" /><br/>
								<% } else if (type == 'boolean') { %>
									<div class="form-group">
										<div class="col-sm-offset-3 col-sm-9">
											<div class="checkbox">
												<label>
													<input type="checkbox" name='${formField.getAttribute("var")}' ${(formField.getChildCData('field/value') == 'true' || formField.getChildCData('field/value') == '1') ? 'checked' : ''} ${formField.getChild('required') ? 'required' : ''}><%= formField.getAttribute('label') ?: formField.getAttribute("var") %></input>
												</label>
											</div>
										</div>
										<% String desc = formField.getChildCData("field/desc");
										if (desc != null) { %>
										<small class="text-muted col-sm-offset-3 col-sm-9">${desc}</small>
										<% } %>
									</div>
								<% } else if (type == 'text-private') { %>
									<div class="form-group">
										<label class="col-sm-3 control-label" for='${formField.getAttribute("var")}'><%= formField.getAttribute('label') ?: formField.getAttribute("var") %></label>
										<div class="col-sm-9">
											<input class="form-control" type='password' size='90' name='${formField.getAttribute("var")}' value="${formField.getChildCData('field/value') ?: ''}" ${formField.getChild('required') ? 'required' : ''}/>
										</div>
										<% String desc = formField.getChildCData("field/desc");
										if (desc != null) { %>
										<small class="text-muted col-sm-offset-3 col-sm-9">${desc}</small>
										<% } %>
									</div>
								<% } else if (type == 'text-multi' || type == 'jid-multi') { %>
									<div class="form-group">
										<label class="col-sm-3 control-label" for='${formField.getAttribute("var")}'><%= formField.getAttribute('label') ?: formField.getAttribute("var") %></label>
										<div class="col-sm-9">
											<textarea class="form-control" rows='5' name='${formField.getAttribute("var")}' ${formField.getChild('required') ? 'required' : ''}>${formField.getChildren()?.findAll{el -> el.getName() == 'value'}.collect{el -> el.getCData() ?: ''}.join('\n') ?: ''}</textarea>
										</div>
										<% String desc = formField.getChildCData("field/desc");
										if (desc != null) { %>
										<small class="text-muted col-sm-offset-3 col-sm-9">${desc}</small>
										<% } %>
									</div>
								<% } else if (type == 'list-single' || type == 'list-multi') { %>
									<div class="form-group">
										<label class="col-sm-3 control-label" for='${formField.getAttribute("var")}'><%= formField.getAttribute('label') ?: formField.getAttribute("var") %></label>
										<div class="col-sm-9">
											<select class="form-control" name='${formField.getAttribute("var")}' ${ (type == 'list-multi') ? 'multiple' : ''} ${formField.getChild('required') ? 'required' : ''}><% formField.getChildren().findAll { it.getName() == 'option' }.each { %>
											<option value='${it.getChildCData(it.getName()+'/value') ?: ''}' ${formField.getChildren().find{ child -> (child.getName() == 'value') && (child.getCData() == (it.getChildCData(it.getName()+'/value') ?: it.getAttribute('label'))) } ? 'selected' : ''}>${it.getAttribute('label') ?: it.getChildCData(it.getName()+'/value')}</option>
											<% } %>
											</select>
										</div>
										<% String desc = formField.getChildCData("field/desc");
										if (desc != null) { %>
										<small class="text-muted col-sm-offset-3 col-sm-9">${desc}</small>
										<% } %>
									</div>
								<% } else if (type == 'text-single' || type == 'jid-single') { %>
									<% def subtype = formField.getAttribute('subtype');
									if ('datetime' == subtype) {
										def valueDate = new tigase.util.datetime.TimestampHelper().parseTimestamp(formField.getChildCData('field/value'));
										def value = valueDate == null ? null : java.time.LocalDateTime.ofInstant(valueDate.toInstant(), java.time.ZoneId.systemDefault());
									%>
										<div class="form-group">
											<label class="col-sm-3 control-label" for='${formField.getAttribute("var")}'><%= formField.getAttribute('label') ?: formField.getAttribute("var") %></label>
											<div class="col-sm-3">
												<input class="form-control" type='date' name='${formField.getAttribute("var")}_date' value="${value?.toLocalDate()?.toString() ?: ''}" ${formField.getChild('required') ? 'required' : ''}/>
											</div>
											<div class="col-sm-2">
												<input class="form-control" type='time' name='${formField.getAttribute("var")}_time' value="${value?.toLocalTime()?.toString() ?: ''}" ${formField.getChild('required') ? 'required' : ''}/>
											</div>
											<div class="col-sm-3">
												<select class="form-control" name='${formField.getAttribute("var")}_tz' ${formField.getChild('required') ? 'required' : ''}>
													<% def zoneIds = java.util.TimeZone.getAvailableIDs();
													zoneIds.each { zoneId ->
                                                        def tz = java.util.TimeZone.getTimeZone(zoneId);
													%>
														<option ${(tz == java.util.TimeZone.getDefault()) ? "selected" : ""}><%= zoneId %></option>
													<% } %>
												</select>
											</div>
											<% String desc = formField.getChildCData("field/desc");
											if (desc != null) { %>
											<small class="text-muted col-sm-offset-3 col-sm-9">${desc}</small>
											<% } %>
										</div>
									<% } else { %>
									<div class="form-group">
										<label class="col-sm-3 control-label" for='${formField.getAttribute("var")}'><%= formField.getAttribute('label') ?: formField.getAttribute("var") %></label>
										<div class="col-sm-9">
											<input class="form-control" type='text' size='90' name='${formField.getAttribute("var")}' value="${formField.getChildCData('field/value') ?: ''}" ${formField.getChild('required') ? 'required' : ''}/>
										</div>
									</div>
									<% } %>
								<% } else if (type == 'fixed') { %>
									<div class="form-group">
										<label class="col-sm-3 control-label" for='${formField.getAttribute("var")}'><%= formField.getAttribute('label') ?: formField.getAttribute("var") %></label>
										<div class="col-sm-9">
											<input class="form-control" disabled type='text' size='90' name='${formField.getAttribute("var")}' value="${formField.getChildCData('field/value') ?: ''}" ${formField.getChild('required') ? 'required' : ''}/>
										</div>
										<% String desc = formField.getChildCData("field/desc");
										if (desc != null) { %>
										<small class="text-muted col-sm-offset-3 col-sm-9">${desc}</small>
										<% } %>
									</div>
								<% } %>
							<% } %>
						<% } %>
						<% if (table != null) { table = null; %></tbody></table></div><% } %>
					</div>
				</div>
		
				<input class="btn btn-default" name="submit" type="submit"/>
			</form>
			<% } else { %>
			<div class="page-header text-center">
				<h1>Welcome to <big>Tigase XMPP Server</big> admin panel</h1>
			</div>
			<div class="panel panel-default">
				<div class="panel-heading">
					<h1 class="panel-title">First steps</h1>
				</div>
				<div class="panel-body lead">
					<ul>
						<li>Main configuration categories are located on the left.</li>
						<li>After expanding them you can see a list of items from the group.</li>
						<li>Each item has name (upper line) and associated component (lower line).</li>
						<li>For more details please refer to the documentation included in the distribution archive or <a href="https://docs.tigase.net/">online</a> </li>
					</ul>
				</div>
			</div>
			<div class="panel panel-default">
				<div class="panel-heading">
					<h1 class="panel-title">Most common actions</h1>
				</div>
				<div class="panel-body lead">
					<ul>
						<% model.defaultCommands.each { command -> %>
							<li><a href="?_group=${java.net.URLEncoder.encode(command.group ?: '')}&_jid=${java.net.URLEncoder.encode(command.jid)}&_node=${java.net.URLEncoder.encode(command.node)}">${command.name}</a></li>
						<% } %>
					</ul>
				</div>
			</div>
			<% } %>
		</div>
	</div>
</div>
<div id="footer" class="container-fluid">
	<div class="row">
		<div class="panel-footer text-right">
			<p>Powered by Tigase XMPP Server ${tigase.server.XMPPServer.getImplementationVersion()}</p>
		</div>
	</div>
</div>
</body>
</html>