{% extends "base.html" %}
{% block content %}
{% from 'include/input_macros.html' import input, select %}
<script src="/inc/users.js"></script>
<script src="/inc/fontawesome.min.js"></script>
{% include 'include/del_confirm.html' %}
<div id="tabs">
	<ul>
		<li><a href="#users" title="Admin area: Manage users - HAProxy-WI">Users</a></li>
		<li><a href="#groups" title="Admin area: Manage groups - HAProxy-WI">Groups</a></li>
		<li><a href="#servers" title="Admin area: Manage servers - HAProxy-WI">Servers</a></li>
		<li><a href="#ssh" title="Admin area: Manage SSH credentials - HAProxy-WI">SSH credentials</a></li>
		<li><a href="#checker" title="Admin area: Manage checker - HAProxy-WI">Checker</a></li>
		<li><a href="#openvpn" title="Admin area: OpenVPN - HAProxy-WI">OpenVPN</a></li>
		<li><a href="#settings" title="Admin area: Manage HAProxy-WI settings - HAProxy-WI">Settings</a></li>
		<li><a href="#services" title="Admin area: Manage HAProxy-WI services - HAProxy-WI">Services</a></li>
		<li><a href="#updatehapwi" title="Admin area: Update HAProxy-WI - HAProxy-WI">Update</a></li>
		<li><a href="#backup" title="Admin area: Backup configs - HAProxy-WI">Backup</a></li>
		<li><a href="#installmon" title="Servers: Monitoring service installation - HAProxy-WI">Monitoring installation</a></li>
		{% include 'include/login.html' %}
	</ul>
	<ul id='browse_histroy'></ul>

	<div id="users">
		{% include 'include/admin_users.html' %}
	</div>

	<div id="groups">
		<table class="overview" id="ajax-group">
			<thead>
			<tr class="overviewHead">
				<th class="padding10 first-collumn">Name</th>
				<th style="width: 100%;">Description</th>
				<th></th>
			</tr>
			<tbody>
		{% for group in groups %}
			<tr id="group-{{ group.0 }}" class="{{ loop.cycle('odd', 'even') }}"> 
			{% if group.1 == "All" %}
				<td class="padding10 first-collumn">{{ group.1 }}</td>
				<td>{{ group.2 }}</td>
				<td></td>
			{% else %}
				<td class="padding10 first-collumn">
					{% set id = 'name-' + group.0|string() %}
					{{ input(id, value=group.1) }}
				</td>
				<td>
					{% set id = 'descript-' + group.0|string() %}					
					{% if group.2  != "None" %}
						{{ input(id, value=group.2, size='60') }}
					{% else %}
						{{ input(id, value='', size='60') }}
					{% endif %}
				</td>
				<td>
					<a class="delete" onclick="confirmDeleteGroup({{ group.0 }})" title="Delete group {{group.1}}" style="cursor: pointer;"></a>
				</td>
			{% endif %}
			</tr>
		{% endfor %}
			</tbody>
		</table>
		<br /><span class="add-button" title="Add group" id="add-group-button">+ Add</span>
		<br /><br />
		<div class="add-note addName alert-info" style="width: inherit; margin-right: 15px;">
			 How to setup groups you can read in this <a href="https://haproxy-wi.org/howto.py?howto=setup" title="How to setup servers, group and SSH credentials" target="_blank">article</a>
		</div>
	</div>
	<div id="servers">
		{% include 'include/admin_servers.html' %}
	</div>

	<div id="ssh">
		{% include 'include/admin_ssh.html' %}
	</div>
	
	<div id="checker">
		{% include 'include/admin_telegram.html' %}
	</div>

	<div id="openvpn">
		{% if openvpn != '' %}
		<table id="openvpn_table" class="overview">
			<caption><h3>OpenVPN profiles</h3></caption>
			<tr class="overviewHead">
				<td class="padding10 first-collumn" style="width: 25%;">
					Profile name
				</td>
				<td class="padding10 first-collumn" style="width: 35%;">
					Time of creation
				</td>
				<td></td>
				<td></td>
			</tr>
			{% for c in openvpn_configs %}
			<tr class="{{ loop.cycle('odd', 'even') }}" id="{{c.split('/')[-1].split('.')[0]}}">
				<td class="padding10 first-collumn">
					{{c.split('/')[-1]}}
				</td>
				<td style="width: 100%">
					{{c.split('/')[0]}}
				</td>
				<td>
					<a class="service-start" onclick="OpenVpnSess('{{c.split('/')[-1].split('.')[0]}}', 'start')" title="Start OpenVPN with profile {{c.split('/')[-1]}}"></a>
				</td>
				<td>
					<a class="delete" onclick="confirmDeleteOpenVpnProfile('{{c.split('/')[-1].split('.')[0]}}')" title="Delete OpenVPN profile {{c.split('/')[-1]}}" style="cursor: pointer;"></a>
				</td>
			</tr>
			{% endfor %}
		</table>
		<br />
		<table id="openvpn_table" class="overview">
			<caption><h3>OpenVPN sessions</h3></caption>
			<tr class="overviewHead">
				<td class="padding10 first-collumn" style="width: 25%;">
					Session name
				</td>
				<td class="padding10 first-collumn" style="width: 35%;">
					Status
				</td>
				<td></td>
				<td></td>
			</tr>
			{% for c in openvpn_sess %}
			<tr class="{{ loop.cycle('odd', 'even') }}">
				<td class="padding10 first-collumn">
					{{c.split(' ')[0]}}
				</td>
				<td style="width: 100%">
					{{c.split(' ')[4]}}
				</td>
				<td>
					<a class="service-reload" onclick="OpenVpnSess('{{c.split(' ')[0]}}', 'restart')" title="Restart OpenVPN with profile {{c.split('/')[0]}}"></a>
				</td>
				<td>
					<a class="delete" onclick="OpenVpnSess('{{c.split(' ')[0]}}', 'disconnect')" title="Disconnect OpenVPN profile {{c.split('/')[2]}}" style="cursor: pointer;"></a>
				</td>
			</tr>
			{% endfor %}
		</table>
		<br />
		<table id="openvpn_upload_table" class="overview">
			<caption><h3>Uploading ovpn files</h3></caption>
			<tr class="overviewHead" style="width: 50%;">
				<td class="padding10 first-collumn" style="width: 25%;">Ovpn file name</td>
				<td>
					<span title="Ovpn file must be with an auto-login profile">Upload ovpn file (?)</span>
				</td>
			</tr>
			<tr style="width: 50%;">
				<td class="first-collumn" valign="top" style="padding-top: 15px;">
					{{ input('ovpn_upload_name', size='30') }}
				</td>
				<td style="padding-top: 15px;">
					<textarea id="ovpn_upload_file" cols="50" rows="5"></textarea><br /><br />
					<a class="ui-button ui-widget ui-corner-all" id="ovpn_upload" title="Upload ovpn file" onclick="uploadOvpn()">Upload</a>
					<br /><br />
				</td>
			</tr>
		</table>

		{% else %}
		<center>
			<br />
			<h3>You do not have installed OpenVPN client.
				Read <a href="https://haproxy-wi.org/services.py?service=openvpn" title="OpenVPN" style="color: #5d9ceb;" target="_blank">hear</a>
				how to install OpenVPN client</h3>
		</center>
		{% endif %}
	</div>
	
	<div id="settings">
		{% include 'include/admin_settings.html' %}
	</div>
	
	<div id="services">
		<table id="services_table" class="overview">
			<tr class="overviewHead">
				<td class="padding10 first-collumn" style="width: 25%;">
					Service
				</td>
				<td class="padding10 first-collumn" style="width: 35%;">
					Actions
				</td>
				<td>Description</td>
			</tr>
			{% for s in services %}
				<tr class="{{ loop.cycle('odd', 'even') }}" id="{{s.0}}">
					<td class="padding10 first-collumn">
						{% if s.1.0 == 'active' %}
							<span title="{{s.0}} is started"><span class="serverUp server-status"></span></span>
						{% else %}
							{% if s.1.0 == 'inactive' or s.1.0 == 'failed' %}
							<span title="{{s.0}} is stoped"><span class="serverDown server-status"></span></span>
							{% else %}
							<span title="{{s.0}} is not installed"><span class="serverNone server-status"></span></span>
							{% endif %}
						{% endif %}
						{{s.0}}
					</td>
					<td class="padding10 first-collumn">
						<a id="start-{{ s.0 }}" class="start" title="Start and enable {{s.0}} service">
							<span class="service-start" onclick="confirmAjaxServiceAction('start', '{{s.0}}')"></span>
						</a>
						<a id="restart-{{ s.2 }}" class="restart" title="Restart {{s.0}} service">
						<span class="service-reload service-restart" onclick="confirmAjaxServiceAction('restart', '{{s.0}}')"></span>
					</a>
						<a id="stop-{{ s.0 }}" class="stop" title="Stop and disable {{s.0}} service">
							<span class="service-stop" onclick="confirmAjaxServiceAction('stop', '{{s.0}}')"></span>
						</a>
					</td>
					<td>
						{{ s.2 }}
					</td>
				</tr>
			{% endfor %}
		</table>
		<div class="add-note addName alert-info" style="width: inherit; margin-right: 15px;">
			You can read about services <a href="https://haproxy-wi.org/services.py" title="HAProxy-WI services" target="_blank">here</a>
		</div>
	</div>
	
	<div id="updatehapwi">
		{% set current_ver = versions.0 %}
		{% set new_ver = versions.1 %}
		{% set current_ver_without_dots = versions.2 %}
		{% set new_ver_without_dots = versions.3 %}
		<table class="overview">
			<tr class="overviewHead">
				<td class="padding10 first-collumn" style="width: 25%;">Service</td>
				<td>Currnet version</td>
				<td class="padding10">Last version</td>
				<td></td>
				<td>
					Description
				</td>
			</tr>
			<tr>
				<td class="padding10 first-collumn">
					HAProxy-WI
				</td>
				<td>
					<b>{{current_ver}}</b>
				</td>
				<td class="padding10">
					<b>{{new_ver}}</b>
				</td>
				<td>
					{% if new_ver_without_dots > current_ver_without_dots and new_ver != "Sorry cannot get current version" %}
					<a class="ui-button ui-widget ui-corner-all" onclick="updateService('haproxy-wi')" title="Update HAProxy-WI">Update</a>
					{% endif %}
				</td>
				<td>
					The main application
				</td>
			</tr>
			{% for s in services %}
			{% if s.0 == 'smon' or s.0 == 'checker_haproxy' or s.0 == 'keep_alive' or s.0 == 'metrics_haproxy' %}
			<tr>
				{% set is_need_update = 0 %}
				{% if s.0 == 'smon' %}
					{% set service_name = 'SMON' %}
					{% set service_link = 'smon' %}
					{% set desc_link = 'https://haproxy-wi.org/services.py?service=smon' %}
					{% if s.3|float < smon_ver|float %}
						{% set is_need_update = 1 %}
					{% endif %}
				{% elif s.0 == 'checker_haproxy' %}
					{% set service_name = 'Checker' %}
					{% set service_link = 'checker' %}
					{% set desc_link = 'https://haproxy-wi.org/services.py?service=checker' %}
					{% if s.3|float < checker_ver|float %}
						{% set is_need_update = 1 %}
					{% endif %}
				{% elif s.0 == 'keep_alive' %}
					{% set service_name = 'Auto start' %}
					{% set service_link = 'auto_start' %}
					{% set desc_link = 'https://haproxy-wi.org/services.py?service=auto_start' %}
					{% if s.3|float < keep_ver|float %}
						{% set is_need_update = 1 %}
					{% endif %}
				{% elif s.0 == 'metrics_haproxy' %}
					{% set service_name = 'Metrics' %}
					{% set service_link = 'metrics' %}
					{% set desc_link = 'https://haproxy-wi.org/services.py?service=metrics' %}
					{% if s.3|float < metrics_ver|float %}
						{% set is_need_update = 1 %}
					{% endif %}
				{% endif %}
				<td class="padding10 first-collumn">
					{{service_name}}
				</td>
				<td>
					{% if s.3 != '* is not installed' %}
					<b>{{s.3}}</b>
					{% endif %}
				</td>
				<td class="padding10">
					<b>
						{% if s.0 == 'smon' %}
							{{smon_ver}}
						{% elif s.0 == 'checker_haproxy' %}
							{{checker_ver}}
						{% elif s.0 == 'keep_alive' %}
							{{keep_ver}}
						{% elif s.0 == 'metrics_haproxy' %}
							{{metrics_ver}}
						{% endif %}
					</b>
				</td>
				<td>
					{% if s.3 != '* is not installed' %}
					{% if is_need_update %}
					<a class="ui-button ui-widget ui-corner-all" onclick="updateService('{{s.0}}')" title="Update {{service_name}}">Update</a>
					{% endif %}
					{% else %}
					{{service_name}} service is not installed
					<a href="https://haproxy-wi.org/services.py?service={{service_link}}#installation" title="{{service_name}} installation" target="_blank" style="color: #5d9ceb;">Read about installation</a>
					{% endif %}
				</td>
				<td>
					Read more about <a href="{{desc_link}}" title="Read more about {{service_name}}" target="_blank" class="link">{{service_name}}</a>
				</td>
			</tr>
			{% endif %}
			{% endfor %}
		</table>
		<div class="add-note addName alert-info" style="width: inherit; margin-right: 15px;">
			<b style="font-size: 20px; display: block; padding-bottom: 10px;">Note:</b>
			For updating you have to use HAProxy-WI RPM. How to start using repository read <a href="https://haproxy-wi.org/installation.py" title="HAProxy-WI installation" target="_blank">here</a>
			<br /><br />
			If the HAProxy-WI server uses a proxy to connect to the Internet, add the proxy settings to yum.conf
			<br /><br />
			Read more about updating in <a href="https://haproxy-wi.org/updates.py" title="Doc" target="_blank">docs</a>
			and <a href="https://haproxy-wi.org/changelog.py" title="Changelog" target="_blank">changelog</a>
		</div>
		<div id="ajax-update"></div>
	</div>

	<div id="backup">
		{% include 'include/admin_backup.html' %}
	</div>

	<div id="installmon">
		{% include 'include/mon_installation.html' %}
	</div>
</div>
{% include 'include/admins_dialogs.html' %}
{% include 'include/change_pass_form.html' %}
<div id="change-user-groups-dialog" style="display: none;">
	<div id="change-user-groups-form"></div>
</div>
<style>
	.ui-selectmenu-button.ui-button {
		width: 10em;
	}
	.first-collumn {
		width: 10%;
	}
	.container {
		margin-right: 0;
	}
	@media all and (-webkit-min-device-pixel-ratio:0) and (min-resolution: .001dpcm) {
		.ui-checkboxradio-label {
			padding-bottom: 0.6em !important;
			padding-top: 0.6em !important;
		}
	}
</style>
<script>
	$( function() {
		{% for user in users %}
			$("#role-{{user.0}}" ).selectmenu({
				width: 100
			});
		{% endfor %}
		{% for server in servers %}
			$("#servergroup-{{ server.0}}" ).selectmenu({
				width: 100
			});
			$("#slavefor-{{server.0}}" ).selectmenu({
				width: 130
			});
			$("#credentials-{{server.0}}" ).selectmenu({
				width: 150
			});
		{% endfor %}
		{% for ssh in sshs %}
			if (window.matchMedia('(max-width: 1280px)').matches) {
				$("#sshgroup-{{ ssh.0}}" ).selectmenu({
					width: 100
				});
			}
		{% endfor %}
		{% for server in backups %}
			$("#backup-time-{{ server.0}}" ).selectmenu({
				width: 100
			});
			$("#backup-type-{{server.0}}" ).selectmenu({
				width: 130
			});
			$("#backup-credentials-{{server.0}}" ).selectmenu({
				width: 150
			});
		{% endfor %}
	});
</script>
{% endblock %}