{% extends 'base.html' %}

{% block content %}
<div class="container-fluid mt-0">
  <h1>{{ _("Usage History") }}</h1>
  <h3>{{ _("Search for machine usage history") }}</h3>
  <a href="{{ url_for('add_use') }}" class="btn btn-primary mb-3">{{ _("Manual registration") }}</a>
  <form method="GET" action="{{ url_for('view_uses') }}" class="mb-4">
    <div class="row">
      <div class="col-md-4">
        <div class="form-group">
          <label for="user_id">User:</label>
          <select id="user_id" name="user_id" class="form-control">
            <option value="">{{ _("All Users") }}</option>
            {% for user in all_users %}
            <option value="{{ user.user_id }}" {% if user.user_id==selected_user_id %}selected{% endif %}>
              {{ user.name}} {{ user.surname}}</option>
            {% endfor %}
          </select>
        </div>
      </div>
      <div class="col-md-4">
        <div class="form-group">
          <label for="machine_id">{{ _("Machine") }}:</label>
          <select id="machine_id" name="machine_id" class="form-control">
            <option value="">{{ _("All Machines") }}</option>
            {% for machine in all_machines %}
            <option value="{{ machine.machine_id }}" {% if machine.machine_id==selected_machine_id %}selected{% endif
              %}>{{ machine.machine_name }}</option>
            {% endfor %}
          </select>
        </div>
      </div>
      <div class="col-md-4">
        <div class="form-group">
          <label for="start_time">{{ _("Start Time") }}:</label>
          <input type="datetime-local" id="start_time" name="start_time" class="form-control"
            value="{{ selected_start_time }}">
        </div>
      </div>
    </div>
    <input type="submit" value="{{ _("Filter") }}" class="btn btn-info">
  </form>

  <table class="table table-striped">
    <thead>
      <tr>
        <th>{{ _("Machine") }}</th>
        <th>{{ _("User") }}</th>
        <th>{{ _("Start Time") }}
        </th>
        <th>{{ _("End Time") }}</th>
        <th class="d-none d-lg-table-cell">{{ _("Duration (minutes)") }}</th>
        <th class="d-none d-lg-table-cell">{{ _("Last update") }}</th>
        <th>{{ _("Action") }}</th>
      </tr>
    </thead>
    <tbody>
      {% for use in uses %}
      <tr class="{% if use.end_timestamp is none %}table-warning{% endif %}">
        <td>{{ use.machine.machine_name }}</td>
        <td>
          {% if use.user.deleted %}
          {{ _("deleted user") }}
          {% else %}
          {{ use.user.name }} {{ use.user.surname }}
          {% endif %}
        </td>
        <td>{{ use.start_timestamp|datetimeformat('%b %d, %Y %I:%M %p') }}
          {% if use.replay %}
          <i class="bi bi-arrow-counterclockwise"></i>
          {% endif %}
        </td>
        <td>
          {% if use.end_timestamp is none %}
          {{ _("Still in use") }}
          {% else %}
          {{ use.end_timestamp|datetimeformat('%b %d, %Y %I:%M %p') }}
          {% endif %}
        </td>
        <td class="d-none d-lg-table-cell">
          {% if use.end_timestamp is not none %}
          {{ ((use.end_timestamp - use.start_timestamp) // 60) | round(0, 'ceil') }}
          {% else %}
          {{ _("In use") }}
          {% endif %}
        </td>
        <td class="d-none d-lg-table-cell">{{ use.last_seen|time_since }}</td>
        <td>
          <a href="{{ url_for('delete_use', use_id=use.use_id) }}" class="btn btn-danger">{{ _("Delete") }}</a>
        </td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
  {% if uses|length >= 500 %}
  <span class="small">{{ _("Only first 500 records are displayed. Use filters if needed.") }}</span>
  {% endif %}
  <a href="{{ url_for('uses_export') }}" class="link-info"><i class="fas fa-file-excel"></i>{{ _("Export table") }}</a>
</div>
{% endblock %}