{% extends 'base.html' %}
{% block content %}
<div class="container-fluid mt-0">
    <h1>{{ _("Machines") }}</h1>
    <h3>{{ _("Use this page to view and configure FabLab machines.") }}</h3>
    <a href="{{ url_for('add_machine') }}" class="btn btn-primary mb-3">{{ _("Add Machine") }}</a>
    <table class="table table-striped">
        <thead>
            <tr>
                <th class="d-none d-lg-table-cell">ID</th>
                <th>{{ _("Name") }}</th>
                <th class="d-none d-lg-table-cell">{{ _("Machine type") }}</th>
                <th>{{ _("Cumulated usage") }}</th>
                <th>{{ _("Blocked for all?") }}</th>
                <th>{{ _("Maintenance plan") }}</th>
                <th class="d-none d-lg-table-cell">{{ _("Last seen") }}</th>
                <th>{{ _("Actions") }}</th>
            </tr>
        </thead>
        <tbody>
            {% for machine in machines %}
            <tr>
                <td class="d-none d-lg-table-cell">{{ machine.machine_id }}</td>
                <td>{{ machine.machine_name }}</td>
                <td class="d-none d-lg-table-cell"><span class="badge bg-info">{{ machine.machine_type.type_name }}</span></td>
                <td>{{ machine.machine_hours | format_hours }}</td>
                <td>
                    {% if machine.blocked %}
                    <i class="fa-solid fa-lock"></i> <!-- Locked Icon -->
                    {% else %}
                    <i class="fa-solid fa-lock-open"></i> <!-- Unlocked Icon -->
                    {% endif %}
                </td>
                <td>                    
                    {% for m in machine.maintenances %} 
                        {% if maint_stats[m.maintenance_id]["expired"] %}
                            <span class="badge bg-danger">{{ _("EXPIRED") }}</span>
                        {% else %}
                            <span class="badge bg-success">{{ _("OK") }}</span>
                        {% endif %}
                        {{ m.description }}
                        <div class="d-none d-lg-table-cell">({{ maint_stats[m.maintenance_id]["elapsed"] }} / {{ m.hours_between }} {{ _("hours") }})</div>
                        <br/>
                    {% endfor %}
                </td>
                <td class="d-none d-lg-table-cell">{{ machine.last_seen | datetimeformat('%b %d, %Y %I:%M %p') }}</td>
                <td>
                    <a href="{{ url_for('view_machine_use_history', machine_id=machine.machine_id) }}"
                        class="btn btn-info">{{ _("View history") }}</a>
                    <a href="{{ url_for('edit_machine', machine_id=machine.machine_id) }}"
                        class="btn btn-warning">{{ _("Edit") }}</a>
                    <a href="{{ url_for('delete_machine', machine_id=machine.machine_id) }}"
                        class="btn btn-danger">{{ _("Delete") }}</a>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>
{% endblock %}