{% extends 'base.html' %}

{% block content %}
<div class="container-fluid mt-0">
    <h1>{{ _("Interventions") }}</h1>
    <a href="{{ url_for('add_intervention') }}" class="btn btn-primary mb-3">{{ _("Register manually...") }}</a>
    <h4>{{ _("You can filter the results below with this form") }}</h4>
    <form method="GET" action="{{ url_for('view_interventions') }}">
        <select name="search_user_id" class="form-control">
            <option value="">{{ _("--Select User--") }}</option>
            {% for user in users %}
            <option value="{{ user.user_id }}" {% if search_user_id==user.user_id %}selected{% endif %}>
                {{ user.name }} {{ user.surname }}
            </option>
            {% endfor %}
        </select>
        <select name="search_machine_id" class="form-control">
            <option value="">{{ _("--Select Machine--") }}</option>
            {% for machine in machines %}
            <option value="{{ machine.machine_id }}" {% if search_machine_id==machine.machine_id %}selected{% endif %}>
                {{ machine.machine_name }}
            </option>
            {% endfor %}
        </select>
        <button type="submit" class="btn btn-info">{{ _("Filter") }}</button>
    </form>

    <!-- Your table here -->

    <table class="table table-striped">
        <thead>
            <tr>
                <th class="d-none d-lg-table-cell">Intervention ID</th>
                <th>{{ _("Maintenance") }}</th>
                <th>{{ _("Machine") }}</th>
                <th>{{ _("User") }}</th>
                <th class="d-none d-lg-table-cell">{{ _("Timestamp") }}</th>
                <th>{{ _("Action") }}</th>
            </tr>
        </thead>
        <tbody>
            {% for intervention in interventions %}
            <tr>
                <td class="d-none d-lg-table-cell">{{ intervention.intervention_id }}</td>
                <td>{{ intervention.maintenance.description }}</td>
                <td>{{ intervention.machine.machine_name }}</td>
                <td>{{ intervention.user.name }} {{ intervention.user.surname }}</td>
                <td class="d-none d-lg-table-cell">{{ intervention.timestamp | datetimeformat('%b %d, %Y %I:%M %p') }}
                    {% if intervention.replay %}
                    <i class="bi bi-arrow-counterclockwise"></i>
                    {% endif %}
                </td>
                <td>
                    <a href="{{ url_for('edit_intervention', intervention_id=intervention.intervention_id) }}"
                        class="btn btn-warning">{{ _("Edit") }}</a>
                    <a href="{{ url_for('delete_intervention', intervention_id=intervention.intervention_id) }}"
                        class="btn btn-danger">{{ _("Delete") }}</a>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>
{% if interventions|length >= 500 %}
<span class="small">{{ _("Only first 500 records are displayed. Use filters if needed.") }}</span>
{% endif %}
<a href="{{ url_for('interventions_export') }}" class="link-info"><i class="fas fa-file-excel"></i>Export table</a>
{% endblock %}