{% extends "base.html" %}
{% load custom_tags_and_filters %}
{% block title %}Tool credentials{% endblock %}
{% block content %}
    <h1 style="margin-top: 0;margin-bottom: 25px">Tool credentials</h1>
    <div class="row">
        <div class="col-xs-9 col-sm-6" style="margin-bottom: 25px">
            <input id="search"
                   type="text"
                   aria-label="Credential search"
                   placeholder="Search for a tool credential"
                   class="form-control"
                   autofocus>
        </div>
        <div class="text-right col-xs-3 col-sm-6" style="margin-bottom: 25px">
            {% if user.is_facility_manager or user.is_superuser %}
                {% url "export_tool_credentials" as export_tool_credentials_url %}
                {% button type="export" value="Export" url=export_tool_credentials_url target="_blank" %}
            {% endif %}
            {% admin_add_url 'NEMO' 'toolcredentials' request.path as add_tool_credentials_url %}
            {% if add_tool_credentials_url %}
                {% button type="add" value="Add new credentials" url=add_tool_credentials_url %}
            {% endif %}
        </div>
    </div>
    {% if tool_credentials %}
        {% regroup tool_credentials by tool.category as tool_credentials_by_category %}
        <table id="table-to-filter" class="table table-bordered table-hover table-align-middle table-condensed">
            {% for category in tool_credentials_by_category %}
                <thead>
                    <tr class="info">
                        <th class="text-center" colspan="{% if perms.NEMO.change_toolcredentials %}5{% else %}4{% endif %}">
                            {{ category.grouper }}
                        </th>
                    </tr>
                    <tr class="warning">
                        <th>Tool</th>
                        <th>Username</th>
                        <th>Password</th>
                        <th>Comments</th>
                        {% if perms.NEMO.change_toolcredentials %}<th class="button-column-minimum"></th>{% endif %}
                    </tr>
                </thead>
                <tbody>
                    {% for tool_credential in category.list %}
                        <tr>
                            <td>{{ tool_credential.tool }}</td>
                            <td>{{ tool_credential.username|default_if_none:"" }}</td>
                            <td>{{ tool_credential.password|default_if_none:"" }}</td>
                            <td>{{ tool_credential.comments|default_if_none:"" }}</td>
                            {% if perms.NEMO.change_toolcredentials %}
                                <td>
                                    {% admin_edit_url tool_credential request.path as change_tool_credentials_url %}
                                    {% if change_tool_credentials_url %}
                                        {% button size="small" type="edit" value="Edit" url=change_tool_credentials_url %}
                                    {% endif %}
                                </td>
                            {% endif %}
                        </tr>
                    {% endfor %}
                </tbody>
            {% endfor %}
        </table>
    {% else %}
        <div class="pull-left form-control-static">
            <i>
                There are no tool credentials.
            </i>
        </div>
    {% endif %}
    <script>$("#search").keyup(table_search("table-to-filter", [1]));</script>
{% endblock %}
