{% extends 'base.html' %}
{% load custom_tags_and_filters %}
{% block title %}{% if form.instance.id %}Modify user{% else %}New user{% endif %}{% endblock %}
{% block extrahead %}
	{% load static %}
	<script type="text/javascript" src="{% static "datetimepicker/bootstrap-datetimepicker.js" %}"></script>
	<link rel="stylesheet" type="text/css" href="{% static "datetimepicker/bootstrap-datetimepicker.css" %}"/>
{% endblock %}
{% block content %}
	{% if form.errors %}
		<div class="alert alert-danger">
			Oops! Something went wrong. Please correct the errors highlighted below.<br>
			{{ form.non_field_errors }}
		</div>
	{% endif %}
	{% if warning %}
		<div class="alert alert-warning">
			{{ warning }}
		</div>
	{% endif %}
	<div class="row">
		<div class="col-sm-12">
			<div class="form-group">
				{% if form.instance.id %}
					<a href="{% url 'users' %}">Search for another user</a><br>
					<a href="{% url 'history' 'user' form.instance.id %}">View user history</a><br>
					{% if form.is_active.value %}<a href="{% url 'deactivate_user' form.instance.id %}">Safely deactivate this user</a><br>{% endif %}
				{% endif %}
			</div>
		</div>
	</div>
	<h1>{% if form.instance.id %}Modify user{% else %}New user{% endif %}</h1>
	<form class="form-horizontal" method="post" action="{% if form.instance.id %}{% url 'create_or_modify_user' form.instance.id %}{% else %}{% url 'create_or_modify_user' 'new' %}{% endif %}">
		{% csrf_token %}
		{% if form.instance.id %}<input type="hidden" value="{{ form.instance.id }}" name="user_id">{% endif %}
		<div class="form-group">
			<label class="control-label col-sm-2" for="first_name"><strong>First name</strong></label>
			<div class="col-sm-4">
				<input id="first_name" name="first_name" type="text" autocomplete="off" class="form-control" value="{{ form.first_name.value|default_if_none:"" }}" autofocus required>
			</div>
			{% if form.first_name.errors %}
				<div class="col-sm-6 form-control-static danger-highlight">
					{{ form.first_name.errors|striptags }}
				</div>
			{% endif %}
		</div>
		<div class="form-group">
			<label class="control-label col-sm-2" for="last_name"><strong>Last name</strong></label>
			<div class="col-sm-4">
				<input id="last_name" name="last_name" type="text" autocomplete="off" class="form-control" value="{{ form.last_name.value|default_if_none:"" }}" required>
			</div>
			{% if form.last_name.errors %}
				<div class="col-sm-6 form-control-static danger-highlight">
					{{ form.last_name.errors|striptags }}
				</div>
			{% endif %}
		</div>
		<div class="form-group">
			<label class="control-label col-sm-2" for="username"><strong>User name</strong></label>
			<div class="col-sm-4">
				<input id="username" name="username" type="text" autocomplete="off" class="form-control" value="{{ form.username.value|default_if_none:"" }}" required>
			</div>
			{% if form.username.errors %}
				<div class="col-sm-6 form-control-static danger-highlight">
					{{ form.username.errors|striptags }}
				</div>
			{% endif %}
		</div>
		<div class="form-group">
			<label class="control-label col-sm-2" for="email"><strong>E-mail address</strong></label>
			<div class="col-sm-4">
				<input id="email" name="email" type="text" autocomplete="off" class="form-control" value="{{ form.email.value|default_if_none:"" }}">
			</div>
			{% if form.email.errors %}
				<div class="col-sm-6 form-control-static danger-highlight">
					{{ form.email.errors|striptags }}
				</div>
			{% endif %}
		</div>
		<div class="form-group">
			<label class="control-label col-sm-2" for="type"><strong>Type</strong></label>
			<div class="col-sm-4">
				<select id="type" name="type" class="form-control" required>
					{% for t in form.fields.type.choices %}
						<option value="{{ t.0 }}" {% if t.0 == form.type.value|to_int %}selected{% endif %}>{{ t.1 }}</option>
					{% endfor %}
				</select>
			</div>
		</div>
		<div class="form-group">
			<label class="control-label col-sm-2" for="badge_number">Badge number</label>
			<div class="col-sm-4">
				<input id="badge_number" name="badge_number" autocomplete="off" type="text" class="form-control" value="{{ form.badge_number.value|default_if_none:"" }}">
			</div>
			{% if form.badge_number.errors %}
				<div class="col-sm-6 form-control-static danger-highlight">
					{{ form.badge_number.errors|striptags }}
				</div>
			{% endif %}
		</div>
		<div class="form-group">
			<label class="control-label col-sm-2" for="access_expiration">Access expiration</label>
			<div class="col-sm-2">
				<input id="access_expiration" name="access_expiration" type="text" autocomplete="off" class="form-control" value="{{ form.access_expiration.value|default_if_none:'' }}">
			</div>
			<div class="control-label col-sm-2" style="text-align:left">
				<a href="javascript:void(0)" onclick="$('#access_expiration').val('{{ one_year_from_now|date }}')">One year from now</a>
			</div>
			{% if form.access_expiration.errors %}
				<div class="col-sm-6 form-control-static danger-highlight">
					{{ form.access_expiration.errors|striptags }}
				</div>
			{% endif %}
		</div>
		<div class="form-group">
			<div class="col-sm-offset-2 col-sm-10">
				<div class="checkbox">
					<label {% if form.is_active.value %}data-toggle="tooltip" data-placement="left" title="Pro-tip: you can safely deactivate this user using the link at the top of this page so that no future costs are incurred (such as missed reservations)."{% endif %}>
						<input id="is_active" name="is_active" type="checkbox" {% if form.is_active.value %}checked{% endif %}> Active
					</label>
				</div>
			</div>
			<div class="col-sm-offset-2 col-sm-2">
				<div class="checkbox">
					<label><input id="training_required" name="training_required" type="checkbox" {% if form.training_required.value %}checked{% endif %}> Training required</label>
				</div>
			</div>
		</div>
		{% if form.instance.id %}
			<div class="form-group">
				<div class="control-label col-sm-2">Date joined</div>
				<div class="col-sm-4 form-control-static">{{ form.instance.date_joined }}</div>
			</div>
			<div class="form-group">
				<div class="control-label col-sm-2">Last login</div>
				<div class="col-sm-4 form-control-static">
					{{ form.instance.last_login|default_if_none:"Never" }}
				</div>
			</div>
		{% endif %}
		<div class="form-group" style="margin-left:0">
			{% if identity_service_domains|length == 1 %}
				<div class="row">
					<label class="control-label col-sm-2">Domain</label>
					<div class="col-sm-4 form-control-static">{{ identity_service_domains.0 }}</div>
					<input type="hidden" name="domain" value="{{ identity_service_domains.0 }}">
				</div>
			{% elif identity_service_domains|length > 1 %}
				<div class="row">
					<label class="control-label col-sm-2"><strong>Domain</strong></label>
					<div class="col-sm-4">
						{% for d in identity_service_domains %}
							<div class="radio">
								<label><input type="radio" name="domain" value="{{ d }}" {% if form.instance.domain == d %}checked{% endif %} required> {{ d }}</label>
							</div>
						{% endfor %}
					</div>
				</div>
			{% endif %}
		</div>
		<div class="form-group">
			<label class="control-label col-sm-2" for="add_a_project">Projects</label>
			<div class="col-sm-4">
				<input id="add_a_project" type="text" autocomplete="off" class="form-control" placeholder="Add a project">
			</div>
		</div>
		<div class="form-group">
			<div id="project-list" class="col-sm-offset-2 col-sm-4">
				This user has no assigned projects.
			</div>
		</div>
		<div class="form-group">
			<label class="control-label col-sm-2" for="add_a_tool_qualification">Tool qualifications</label>
			<div class="col-sm-4">
				<input id="add_a_tool_qualification" type="text" autocomplete="off" class="form-control" placeholder="Add a qualification">
			</div>
		</div>
		<div class="form-group">
			<div id="qualification-list" class="col-sm-offset-2 col-sm-4">
				This user is not qualified to use any tools.
			</div>
		</div>
		{% if physical_access_levels or externally_managed_access_levels %}
			<div class="form-group">
				<span class="control-label col-sm-2">Physical access levels</span>
				<div class="col-sm-4">
					{% if physical_access_levels %}
						<div class="panel panel-info">
							<div class="panel-heading">
								<span class="panel-title" style="font-size:inherit">Tablet controlled</span>
							</div>
							<div class="panel-body">
								{% for level in physical_access_levels %}
									<div class="checkbox">
										<label><input name="physical_access_levels" type="checkbox" value="{{ level.id }}" {% if form.instance in level.users.all %}checked{% endif %}> {{ level }}</label>
									</div>
								{% endfor %}
							</div>
						</div>
					{% endif %}
					{% if externally_managed_physical_access_levels %}
						<div class="panel panel-success">
							<div class="panel-heading">
								<span class="panel-title" style="font-size:inherit">Badge-reader controlled</span>
							</div>
							<div class="panel-body">
								{% for level in externally_managed_physical_access_levels %}
									<div class="checkbox">
										<label><input name="externally_managed_access_levels" type="checkbox" {% if level in user_identity_information.area_access %}checked{% endif %} value="{{ level }}"> {{ level }}</label>
									</div>
								{% endfor %}
							</div>
						</div>
					{% endif %}
				</div>
			</div>
		{% endif %}
		<div class="form-group">
			<div class="col-sm-2">
				<input type="submit" class="btn btn-success pull-right" value="{% if form.instance.id %}Save changes{% else %}Create user{% endif %}">
			</div>
		</div>
	</form>
	{% if identity_service_available and form.instance.id and user_identity_information.can_reset_password_and_unlock_account %}
		<form class="form-horizontal" method="post" action="{% url 'reset_password' form.instance.id %}">
			{% csrf_token %}
			<div class="form-group">
				<div class="col-sm-2">
					<input type="submit" class="btn btn-warning pull-right" value="Reset password">
				</div>
			</div>
		</form>
		{# Account unlock can only be performed if the account is currently in the locked state. #}
		{% if user_identity_information.account_locked %}
			<form class="form-horizontal" method="post" action="{% url 'unlock_account' form.instance.id %}">
				{% csrf_token %}
				<div class="form-group">
					<div class="col-sm-2">
						<input type="submit" class="btn btn-warning pull-right" value="Unlock account">
					</div>
				</div>
			</form>
		{% endif %}
	{% endif %}

	<script>
		function on_search_selection(jquery_event, search_selection, dataset_name)
		{
			$(this).typeahead('val', '').focus();
			if(dataset_name === 'projects')
				add_project(search_selection.name, search_selection.id);
			else if(dataset_name === 'tools')
				add_tool_qualification(search_selection.name, search_selection.id);
		}
		function add_project(project_name, project_id)
		{
			add_to_list("#project-list", "remove_project", project_id, project_name, "Remove this user from " + project_name, "projects");
		}
		function remove_project(project_id)
		{
			remove_from_list("#project-list", "#projects_" + project_id, "This user has no assigned projects.");
		}
		function add_tool_qualification(tool_name, tool_id)
		{
			add_to_list("#qualification-list", "remove_tool_qualification", tool_id, tool_name, "Disqualify this user from using the " + tool_name, "qualifications");
		}
		function remove_tool_qualification(tool_id)
		{
			remove_from_list("#qualification-list", "#qualifications_" + tool_id, "This user is not qualified to use any tools.");
		}
		function on_load()
		{
			$('[data-toggle="tooltip"]').tooltip();
			$("#access_expiration").datetimepicker({format: 'MM/DD/YYYY'});
			$('#add_a_project').autocomplete('projects', on_search_selection, {% json_search_base_with_extra_fields projects 'application_identifier' %});
			$('#add_a_tool_qualification').autocomplete('tools', on_search_selection, {{ tools|json_search_base }});
			{% if form.instance.id %} {# Only look for projects and qualifications if this is an existing user (and not a new user). #}
				{% for project in form.instance.projects.all %}
					add_project('{{ project.name }}', {{ project.id }});
				{% endfor %}
				{% for tool in form.instance.qualifications.all %}
					add_tool_qualification('{{ tool.name }}', {{ tool.id }});
				{% endfor %}
			{% endif %}
		}
		$(on_load);
	</script>
{% endblock %}