{% extends "base.html" %}
{% load custom_tags_and_filters %}
{% block title %}Configuration agenda{% endblock %}
{% block content %}
    <h1>
        Configuration agenda
        <small>for
            {% if time_period == 'today' %}
                today
            {% elif time_period == 'near_future' %}
                the near future
            {% endif %}
        </small>
    </h1>
    {% if tools %}
        <p>
            Below is a list of reservations which require tool configurations.
            The reservations are grouped by tool and ordered by start time.
            Reservations that have a red background were created on short notice,
            thus there is no guarantee the tool will be configured properly when the user arrives.
            Reservations that have already started or have no configuration information are excluded.
            You can also
            {% if time_period == 'today' %}
                <a href="{% url 'configuration_agenda_near_future' %}">view reservations in the near future</a>
            {% elif time_period == 'near_future' %}
                <a href="{% url 'configuration_agenda' %}">view reservations for today</a>
            {% endif %}
            that will require configuration changes.
        </p>
        {% for tool in tools %}
            <table class="table table-bordered">
                <thead>
                    <tr style="border:2px solid black" class="info">
                        <th colspan="2" style="text-align:center; font-size:large; border:2px solid black">
                            <a href="{% url 'calendar' item_type='tool' item_id=tool.id %}"
                               title="View the {{ tool }} calendar"
                               target="_blank">{{ tool.name }}</a>
                        </th>
                    </tr>
                </thead>
                {% regroup reservations|dictsort:"tool.id" by tool as regrouped_reservations %}
                {% for t in regrouped_reservations %}
                    {% if t.grouper.id == tool.id %}
                        {% for reservation in t.list %}
                            <tr style="border:2px solid black">
                                <td style="width:50%;border:2px solid black" {% if reservation.short_notice %}class="danger"{% endif %}>
                                    {{ reservation.user }}
                                    <br>
                                    {% if reservation.project %}
                                        {{ reservation.project }}
                                        <br>
                                    {% endif %}
                                    {{ reservation.start }}
                                    <br>
                                    {{ reservation.end }}
                                    <br>
                                    {{ reservation.additional_information|linebreaksbr }}
                                    {% for config_option in reservation.configurationoption_set.all %}
                                        {% if forloop.first %}
                                            {% if reservation.additional_information %}<br>{% endif %}
                                            <br>
                                        {% endif %}
                                        {% if config_option.precursor_configuration %}
                                            <div>
                                                {% if config_option.current_position is not None %}
                                                    {{ config_option.name }} position {{ config_option.current_position|default_if_none:"Any" }} needs to be set to <span {% if config_option.get_color %}class="configuration-reservation-option" style="display: inline-block;background-color: {{ config_option.get_color }}" {% else %}style="color: inherit"{% endif %}>{{ config_option.current_setting }}</span>
                                                {% else %}
                                                    {{ config_option.name }} <span {% if config_option.get_color %}class="configuration-reservation-option" style="display: inline-block;background-color: {{ config_option.get_color }}" {% else %}style="color: inherit"{% endif %}>{{ config_option.current_setting }}</span> needs to be set to any position
                                                {% endif %}
                                            </div>
                                        {% else %}
                                            <div>
                                                {{ config_option.name }} needs to be set to <span {% if config_option.get_color %}class="configuration-reservation-option" style="display: inline-block;background-color: {{ config_option.get_color }}" {% else %}style="color: inherit"{% endif %}>{{ config_option.current_setting }}</span>
                                            </div>
                                        {% endif %}
                                    {% endfor %}
                                    {% if not reservation.tool.in_use and reservation.project %}
                                        <div style="margin-top: 8px">
                                            {% url 'enable_tool' tool_id=reservation.tool.id user_id=reservation.user.id project_id=reservation.project.id staff_charge='false' as enable_tool_for_user_url %}
                                            {% button type="info" icon="glyphicon-play" value="Enable the tool on behalf of "|concat:reservation.user.first_name onclick="enable_tool('"|concat:enable_tool_for_user_url|concat:"');" %}
                                            {% if "remote_work"|customization:"remote_work_on_behalf_of_user" == "ask" %}
                                                <div style="margin-top: 5px">
                                                    {% button type="info" icon="glyphicon-play" value="Enable the tool on behalf of "|concat:reservation.user.first_name|concat:" for remote work" onclick="enable_tool('"|concat:enable_tool_for_user_url|concat:"', true);" %}
                                                </div>
                                            {% endif %}
                                        </div>
                                    {% endif %}
                                </td>
                                {% if forloop.first %}
                                    <td style="width:50%;border:2px solid black" rowspan="100">
                                        {% for id, widget in configuration_widgets.items %}
                                            {% if tool.id == id %}{{ widget }}{% endif %}
                                        {% endfor %}
                                        {% if reservation.tool.in_use %}
                                            <div style="margin-top: 8px">
                                                {% if reservation.tool.get_current_usage_event.operator.id == user.id %}
                                                    {% url 'disable_tool' reservation.tool.id as disable_tool_url %}
                                                    {% button type="delete" id="interlock" icon="glyphicon-stop" onclick="disable_tool('"|concat:disable_tool_url|concat:"')" value="Disable tool" %}
                                                {% else %}
                                                    {% button type="warn" disabled="disabled" icon="glyphicon-ban-circle" value="Tool in use" %}
                                                {% endif %}
                                            </div>
                                        {% else %}
                                            <div style="margin-top: 8px">
                                                {% if user.active_project_count == 1 %}
                                                    {% url 'enable_tool' tool_id=reservation.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 the tool for myself" 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 the tool
                                                            for myself <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=reservation.tool.id user_id=user.id project_id=project.id staff_charge='false' %}')">{% project_selection_display project %}</a>
                                                                </li>
                                                            {% endfor %}
                                                        </ul>
                                                    </div>
                                                {% endif %}
                                            </div>
                                        {% endif %}
                                    </td>
                                {% endif %}
                            </tr>
                        {% endfor %}
                    {% endif %}
                {% endfor %}
            </table>
        {% endfor %}
        <script>
			function enable_tool(url, remote_work)
			{
				let failure_dialog = ajax_failure_callback("Unable to enable tool");
				ajax_post(url, {"remote_work": remote_work || ""}, reload_page, failure_dialog);
			}

			function disable_tool(url)
			{
				let failure_dialog = ajax_failure_callback("Unable to disable tool");
				ajax_post(url, undefined, reload_page, failure_dialog);
			}

			function reload_page(response, status, xml_http_request)
			{
				location.reload();
			}

        </script>
    {% else %}
        <p>
            No tools require configuration
            {% if time_period == 'today' %}
                today
            {% elif time_period == 'near_future' %}
                for the near future
            {% endif %}
            .
        </p>
        <p>
            You can also
            {% if time_period == 'today' %}
                <a href="{% url 'configuration_agenda_near_future' %}">view reservations in the near future</a>
            {% elif time_period == 'near_future' %}
                <a href="{% url 'configuration_agenda' %}">view reservations for today</a>
            {% endif %}
            that will require configuration changes.
        </p>
    {% endif %}
{% endblock %}
