{% load humanize %}
{% load custom_tags_and_filters %}
<h2>{{ task.tool.name }} task</h2>
<p class="grey">
	Primary tool owner: {{ task.tool.primary_owner }}<br>
	{% if task.tool.backup_owners.all.exists %}Backup tool owner{{ task.tool.backup_owners.all.count|pluralize }}: {% for x in task.tool.backup_owners.all %}{{ x }}{% if not forloop.last %}, {% else %}{% endif %}{% endfor %}{% endif %}
</p>
<div style="margin-bottom: 12px">
	{% if task.tool.in_use %}
		{% if task.tool.get_current_usage_event.operator.id == user.id %}
            {% url 'disable_tool' task.tool.id as disable_tool_url %}
            {% button id="interlock" type="delete" onclick="disable_tool('"|concat:disable_tool_url|concat:"');" value="Disable tool" icon="glyphicon-stop" %}
		{% else %}
            {% button type="warn" disabled="disabled" icon="glyphicon-ban-circle" value="Tool in use" %}
		{% endif %}
	{% else %}
		{% if user.active_project_count == 1 %}
            {% url 'enable_tool' tool_id=task.tool.id user_id=user.id project_id=user.active_projects.0.id staff_charge='false' as enable_tool_one_project %}
            {% button id="interlock" type="save" onclick="enable_tool('"|concat:enable_tool_one_project|concat:"');" value="Enable tool" icon="glyphicon-play" %}
		{% elif user.active_project_count > 1 %}
			<div class="btn-group">
				<button id="interlock" type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown"><i class="glyphicon glyphicon-play"></i> Enable tool <span class="caret"></span></button>
				<ul class="dropdown-menu">
					<li class="dropdown-header">Using which project?</li>
					{% for project in user.active_projects %}
						<li><a href="javascript:void(0)" onclick="enable_tool('{% url 'enable_tool' tool_id=task.tool.id user_id=user.id project_id=project.id staff_charge='false' %}')">{% project_selection_display project %}</a></li>
					{% endfor %}
				</ul>
			</div>
		{% endif %}
	{% endif %}
    {% url 'calendar' item_type='tool' item_id=task.tool.id as tool_calendar_url %}
    {% button value="Calendar" icon="glyphicon-share-alt" url=tool_calendar_url target="_blank" %}
    {% url 'tool_control' tool_id=task.tool.id as tool_control_url %}
    {% button value="Tool control" icon="glyphicon-share-alt" url=tool_control_url target="_blank" %}
</div>
<span style="color:grey">Created by {{ task.creator }} on {{ task.creation_time }} ({{ task.creation_time|naturaltime }}).</span>
<p>
{% if task.last_updated and task.last_updated_by %}
	<span style="color:grey">Last updated by {{ task.last_updated_by }} on {{ task.last_updated }} ({{ task.last_updated|naturaltime }}).</span>
{% endif %}

<p>

<form class="form-horizontal" action="{% url 'update_task' task.id %}" method="post" enctype="multipart/form-data">
	{% csrf_token %}
	<input type="hidden" name="next_page" value="maintenance">
	<input type="hidden" name="tool" value="{{ task.tool.id }}">
	<div class="form-group">
		<label for="urgency" class="control-label col-lg-3">Urgency</label>
		<div class="col-lg-3">
			<select id="urgency" name="urgency" class="form-control">
				{% for level in task.Urgency.Choices %}
					<option value="{{ level.0 }}" {% if task.urgency == level.0 %}selected{% endif %}>{{ level.1 }}</option>
				{% endfor %}
			</select>
		</div>
	</div>
	{% if initial_assessment_categories %}
		<div class="form-group">
			<label for="problem_category" class="control-label col-lg-3">Problem category</label>
			<div class="col-lg-3">
				<select id="problem_category" name="problem_category" class="form-control">
					<option value="">&nbsp;</option>
					{% for category in initial_assessment_categories %}
						<option value="{{ category.id }}" {% if task.problem_category_id == category.id %}selected{% endif %}>{{ category.name }}</option>
					{% endfor %}
				</select>
			</div>
		</div>
	{% endif %}
	{% if task_statuses %}
		<div class="form-group">
			<label for="status" class="control-label col-lg-3">Status</label>
			<div class="col-lg-3">
				<select id="status" name="status" class="form-control">
					<option value="">&nbsp;</option>
					{% for status in task_statuses %}
						<option {% if task.current_status == status.name %}selected{% endif %}>{{ status.name }}</option>
					{% endfor %}
				</select>
			</div>
		</div>
	{% endif %}
	<div class="form-group">
		<label for="estimated_resolution_time" class="control-label col-lg-3">Estimated resolution</label>
		<div class="col-lg-3">
			<input id="estimated_resolution_time" name="estimated_resolution_time" type="text" class="form-control" autocomplete="off" value="{{ estimated_resolution_time|input_date_format }}">
		</div>
		<script>
			let timepicker_properties =
			{
                format: '{{ datetime_input_js_format }}',
			};
			$('#estimated_resolution_time').datetimepicker(timepicker_properties);
		</script>
	</div>
	<div class="form-group">
		<div class="col-lg-12">
			<div class="checkbox"><label><input type="checkbox" name="force_shutdown" {% if task.force_shutdown %}checked{% endif %}> This task requires the {{ task.tool.name }} to be shutdown</label></div>
			<div class="checkbox"><label><input type="checkbox" name="safety_hazard" {% if task.safety_hazard %}checked{% endif %}> This task is considered a potential safety hazard to the {{ facility_name }}</label></div>
		</div>
	</div>
	<div class="form-group">
		<div class="col-lg-12">
			<div class="radio">
				<label class="col-lg-2"><input type="radio" name="action" value="update" checked {% if completion_categories %}onchange="$('#resolution_category').toggle('collapsed')"{% endif %}> Update</label>
				<label class="col-lg-2"><input type="radio" name="action" value="resolve" {% if completion_categories %}onchange="$('#resolution_category').toggle('collapsed')"{% endif %}> Resolve</label>
			</div>
		</div>
	</div>
	<div class="form-group">
		<div class="col-lg-6">
			<textarea name="description" class="form-control" rows="3" aria-label="Resolution description" placeholder="Please describe any actions being taken to resolve this task."></textarea>
		</div>
	</div>
	<div id="resolution_category" class="form-group collapse">
		<div class="col-lg-12">
			<label for="category" class="control-label pull-left">Resolution category</label>
			<div class="col-lg-4">
				<select id="category" name="resolution_category" class="form-control">
					<option value="">&nbsp;</option>
					{% for c in completion_categories %}
						<option value="{{ c.id }}">{{ c.name }}</option>
					{% endfor %}
				</select>
			</div>
		</div>
	</div>
	{% if task.task_images %}
		<div class="form-group">
			<div class="col-lg-6">images:
				<ul>
					{% for image in task.task_images %}
						<li><a href="{{ image.image.url }}" target="_blank" style="margin-right: 5px">{{ image.filename }}</a></li>
					{% endfor %}
				</ul>
			</div>
		</div>
	{% endif %}
	<div class="form-group">
		<div class="col-lg-6">
			<input id="fileupload" type="file" name="image" multiple>
		</div>
	</div>
	<div class="form-group">
		<div class="col-lg-12">
            {% button type="save" value="Save task" %}
		</div>
	</div>
</form>

<p>
	<h4>Problem description</h4>
	{{ task.problem_description|linebreaksbr }}
<p>

{% if task.progress_description %}
	<p>
		<h4>Progress</h4>
		{{ task.progress_description|linebreaksbr }}
	<p>
{% endif %}

{% if rendered_configuration_html %}
	<p>
		<h4>Configuration</h4>
		{{ rendered_configuration_html }}
	<p>
{% endif %}