<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8" />
	<title>Igor Plugins</title>
	<style>
	table, th, td {
		border: 1px solid black;
		border-collapse: collapse;
	}
	</style>
</head>
<body>
	{% if action == 'list' or not action %}
		<h1>Igor Plugins</h1>
		{% set user = igor.app.getSessionItem('user', None) %}
		{% if user %}
			<p>
				You are logged in as {{user}}.
				{% if user != 'admin' %}
					You may not have sufficient permissions for the commands below unless you login as user <i>admin</i>.
				{% endif %}
			</p>
		{% else %}
			<p>
				You are not logged in. This page will probably not render correctly.
			</p>
		{% endif %}
		
		<h2>Installed Igor Plugins</h2>
		
		<table style="width:100%">
			<tr>
				<th>Plugin</th>
				<th>Standard Plugin?</th>
				<th>Documentation</th>
				<th>Pages</th>
				<th>Plugin data (global)</th>
				<th>Plugin data (per-user)</th>
				<th>Action</th>
			</tr>
			{% set pluginNames = igor.plugins.list(token) %}
			{% for pluginName in  pluginNames|sort %}
				{% set pluginData = igor.plugins.info(pluginName, token) %}
				<tr>
					<td>{{pluginName}}</td>
					<td>{{pluginData.get('stdName', '')}}</td>
					<td>
						{% if 'doc' in pluginData %}
							<a href="{{pluginData.doc}}">readme.md</a>
						{% endif %}
					</td>
					<td>
						{% for page in pluginData.pages %}
							<a href="{{page}}">{{page.split('/')[-1]}}</a><br>
						{% endfor %}
					</td>
					<td>
						{% if 'pluginData' in pluginData %}
							<a href="{{pluginData.pluginData}}">{{pluginData.pluginData}}</a>
						{% endif %}
					</td>
					<td>
						{% for userData in pluginData.get('userData', []) %}
							<a href="{{userData}}">{{userData}}</a><br>
						{% endfor %}
					</td>
					<td>
						<form action="/plugins.html">
							<input type="hidden" name="action" value="delete">
							<input type="hidden" name="pluginName" value="{{pluginName}}">
							<input type="submit" value="Remove">
						</form>
					</td>
				</tr>
			{% endfor %}
		</table>
		
		<h2>Available Standard Igor Plugins</h2>
		
		<ul>
			{% for pluginName in igor.plugins.liststd(token) %}
				<li>{{pluginName}}</li>
			{% endfor %}
		</ul>
		
		<h2>Add Standard Plugin</h2>
		<form action="/plugins.html">
			<input type="hidden" name="action" value="addstd">
			Standard plugin to install:<br>
			<select name="stdName" required>
				<option value="">- select -</option>
				{% for stdName in igor.plugins.liststd(token) %}
					<option value="{{stdName}}">{{stdName}}</option>
				{% endfor %}
			</select><br>
			Plugin Name (default: standard name)<br>
			<input type="text" name="pluginName"><br>
			<input type="submit" value="Add">
		</form>
		
		<h2>Add Plugin</h2>
		
		<p>Plugins from external sources have full control over your Igor database (and even over the machine Igor is running on).
		For this reason you must use the command-line interface to install such plugins. 
		See the <a target="_blank" href="https://igor-iot.readthedocs.io/en/latest/programs.html#igorsetup">igorSetup command line tool documentation</p> for details.</p>
		
	{% elif action == 'delete' %}
		<p>{{igor.plugins.uninstall(pluginName, token)}}</p>
		<p><a href="/plugins.html">Return to plugin listing</a></p>
		
	{% elif action == 'addstd' %}
		{% set pluginName = pluginName if pluginName else stdName %}
		{% set rv = igor.plugins.installstd(pluginName, stdName, token) %}
		{% if rv %}
			<p><b>Note:</b> installing plugin returned: {{rv}}</p>
		{% endif %}
		{% set pluginData = igor.plugins.info(pluginName, token) %}
		{% if pluginData.pages %}
			<p>Standard plugin <i>{{stdName}}</i> installed under name <i>{{pluginName}}</i>. This plugin provides
			a number of user-interface web pages. Depending on the plugin, these pages may help you to complete the
			installation process for this plugin:</p>
			<ul>
			{% for page in pluginData.pages|sort %}
				<li><a href="{{page}}">{{page.split('/')[-1]}}</a></li>
			{% endfor %}
			</ul>
		{% else %}
			<p>Standard plugin <i>{{stdName}}</i> installed under name <i>{{pluginName}}</i>. This plugin does not provide user interface pages.</p>
		{% endif %}
		<hr>
		<p><a href="/plugins.html">Return to plugin listing</a></p>
		{% if kwargs.get('fromDevicePage') %}
			<p><a href="/plugin/device/page/index.html">Return to devices listing</a></p>
		{% endif %}
		
	{% else %}
		<p>Unknown action <i>{{action}}</i>.</p>
		<p><a href="/plugins.html">Return to plugin listing</a></p>
	{% endif %}
	<hr>
	<p><a href="/">Return to Igor homepage</a></p>
	{% if 'returnTo' in kwargs %}
		<p><a href="{{kwargs['returnTo']}}">Return to {{kwargs['returnTo']}}.</a></p>
	{% endif %}
</body>
</html>
