<html>
	<head>
		<meta charset="UTF-8">
    	<title>3DPrinterOS Client</title>
		<link rel="stylesheet" type="text/css" href="/static/styles/style.css">
	</head>
	<body>
		<div id='host_version' align='right' style='align: right; color: #006666'>!!!URL!!! !!!VERSION!!!</div>
		<div class="block1" style="width: 60%; margin:-160px 0 0 -30%;">
			<div class="block2">
				3DPrinterOS Client
			</div>
			<center>
			<div class="block3">
				<p>
					Plugins list
				</p>
				<table class="network_printers">
					<thead>
					<tr>
						<th style="width: 70%;"></th>
						<th style="width: 30%;"></th>
					</tr>
					</thead>
					<tbody id="plugins_list">
					<tr id="empty_list" style="display: none;">
						<td colspan="2" style="width: 100%; text-align: center;">No plugins</td>
					</tr>
					</tbody>
				</table>
                <input type="button" onclick="location.href='/manage_settings'" value="Back" class="big_button">
				<input type="button" onclick="location.href='/install_plugin_page'" value="Install" class="big_button">
			</div>
		</center>
		</div>
	<script src="/static/scripts/jquery.js"></script>
	<script type="text/javascript">
		$(function() {
			//Send request for plugins list
			$.ajax({
				'url': '/plugins_list',
				'type': 'GET',
				'dataType': 'json',
				'success': function(response) {
					//Check plugins count
					if (!response.length) {
						//show message - empty list
						$('#empty_list').show();
						return;
					}
					//Draw table with plugins list
					var $tableBody = $('#plugins_list');
					for (var i in response) {
						var plugin = response[i];
						var $column = $('<tr>');
						$column.append('<td class="title">' + plugin.display_name + '</td>');
						if (plugin.enabled)
						    $column.append('<td class="action"><button class="small_button" id="disable" onclick="control_plugin(\'' + plugin.name + '\', \'disable\')">Disable</button></td>');
						else
						    $column.append('<td class="action"><button class="small_button" id="enable" onclick="control_plugin(\'' + plugin.name + '\', \'enable\')">Enable</button></td>');
						$column.append('<td class="action"><button class="small_button" id="uninstall" onclick="control_plugin(\'' + plugin.name + '\', \'uninstall\')">Uninstall</button></td>');
						$tableBody.append($column);
					}
				}
			})
		});

		/**
		 * Send request to perform action on a plugin
         */
		function control_plugin(name, action) {
			$.ajax({
				'url': '/control_plugin',
				'type': 'POST',
				'data': {'command': JSON.stringify({'plugin_name': name, 'action': action})},
				'success': function(response) {
					window.location.reload();
				}
			})
		}
	</script>
	</body>
</html>
