{% extends "layout.html" %}
{% block body %}
<h2 class="p-2 text-left display-5">Users</h2>
<p>
    Create and modify users which can login and have access to a specific department.
</p>
<h3>
    Existing users
</h3>
<table class="table table-striped table-hover">
    <thead>
        <tr>
            <th scope="col">Name</th>
            <th scope="col">Organizes Department</th>
            <th scope="col">Organizes Unit</th>
            <th scope="col"></th>
        </tr>
    </thead>
    <tbody>
        {% for user in  users %}
        <tr>
            <td scope="row">
                <a href="{{ url_for('worker', worker_id=user.id) }}">
		    {%- if user.preferred_name %}
		    {{ user.preferred_name }} ({{ user.name }})
		    {% else %}
		    {{ user.name }}
		    {% endif %}
                </a>
            </td>
            <form class="form-inline" action="{{ url_for('users', user_id=user.id) }}" method="post">
                <td>
			{{ user.department_name }}
                </td>
                <td>
                    <select name="unit_chair_id" class="form-select">
                        <option value="">None</option>
			{% set user_unit = user.unit_chair_id %}
                        {% for unit in units %}
                        <option value="{{ unit.id }}" {% if user_unit == unit.id %}selected="selected" {% endif %}>
                            {{ unit.name }}
                        </option>
                        {% endfor %}
                    </select>
                </td>
                <td>
                    <button type="submit" class="btn btn-sm" title="Save changes"> 
                        <svg xmlns=" http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-save" viewBox="0 0 16 16">
                            <path d="M2 1a1 1 0 0 0-1 1v12a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1V2a1 1 0 0 0-1-1H9.5a1 1 0 0 0-1 1v7.293l2.646-2.647a.5.5 0 0 1 .708.708l-3.5 3.5a.5.5 0 0 1-.708 0l-3.5-3.5a.5.5 0 1 1 .708-.708L7.5 9.293V2a2 2 0 0 1 2-2H14a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V2a2 2 0 0 1 2-2h2.5a.5.5 0 0 1 0 1H2z" />
                        </svg>
                    </button>
                    <a class="btn btn-sm"
			    href="{{ url_for('user_delete', user_id=user.id) }}"
			    onclick="return confirm('Do you really want to remove {{ user.name }} from users');">
		          <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-x-lg" viewBox="0 0 16 16">
		            <path fill-rule="evenodd" clip-rule="evenodd" d="M13.854 2.146a.5.5 0 0 1 0 .708l-11 11a.5.5 0 0 1-.708-.708l11-11a.5.5 0 0 1 .708 0Z" fill="#000"/>
		            <path fill-rule="evenodd" clip-rule="evenodd" d="M2.146 2.146a.5.5 0 0 0 0 .708l11 11a.5.5 0 0 0 .708-.708l-11-11a.5.5 0 0 0-.708 0Z" fill="#000"/>
		          </svg>
		    </a>
                </td>
            </form>
        </tr>
        {% endfor %}
    </tbody>
</table>
{% endblock %}
