{% extends "base.html" %}
{% block title %}Moje urządzenia - RouterOS Update{% endblock %}
{% block content %}
<h2>Moje urządzenia</h2>
<div class="row mb-3">
  <div class="col text-end">
    <button type="button" class="btn btn-success" onclick="window.location.href='{{ url_for('add_device') }}'">
      Dodaj nowe urządzenie
    </button>
  </div>
</div>
<form method="POST" action="{{ url_for('update_selected_devices') }}">
  <button type="submit" class="btn btn-primary mb-3">Aktualizuj wybrane</button>
  <table class="table table-bordered table-hover">
    <thead class="table-dark">
      <tr>
        <th><input type="checkbox" id="select-all"></th>
        <th>Nazwa / Adres IP</th>
        <th>Ostatnie sprawdzenie</th>
        <th>Status</th>
        <th>System / Firmware</th>
        <th>Akcje</th>
      </tr>
    </thead>
    <tbody>
      {% for device in devices %}
      <tr>
        <td>
          <input type="checkbox" name="selected_devices" value="{{ device.id }}">
        </td>
        <td>
          {% if device.name %}
            {{ device.name }} | 
            <code><small class="text-muted">{{ device.ip }}</small></code>
          {% else %}
            {{ device.ip }}
          {% endif %}
        </td>
        <td>{{ device.last_check.strftime('%Y-%m-%d %H:%M:%S') if device.last_check else 'Brak' }}</td>
        <td>
          {% if device.update_required %}
            <span class="badge bg-danger">Wymaga aktualizacji</span>
          {% else %}
            <span class="badge bg-success">Aktualny</span>
          {% endif %}
        </td>
        <td>
          <small>
            <strong>System:</strong> {{ device.current_version or 'Brak' }}<br>
            <strong>Firmware:</strong> {{ device.current_firmware or 'Brak' }}
          </small>
        </td>
        <td>
          <button type="button" class="btn btn-info btn-sm" onclick="window.location.href='{{ url_for('device_detail', device_id=device.id) }}'">Szczegóły</button>
          <button type="button" class="btn btn-secondary btn-sm" onclick="window.location.href='{{ url_for('force_check', device_id=device.id) }}'">Wymuś sprawdzenie</button>
          <button type="button" class="btn btn-warning btn-sm" onclick="window.location.href='{{ url_for('edit_device', device_id=device.id) }}'">Edytuj</button>
        </td>
      </tr>
      {% else %}
      <tr>
        <td colspan="6" class="text-center">Brak dodanych urządzeń.</td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
</form>
<script>
  // Funkcja "Select all" – zaznacza lub odznacza wszystkie checkboxy
  document.getElementById('select-all').addEventListener('change', function() {
    var checkboxes = document.querySelectorAll('input[name="selected_devices"]');
    for (var checkbox of checkboxes) {
      checkbox.checked = this.checked;
    }
  });
</script>
{% endblock %}