{% if logs %}
<div class="table-responsive">
  <table class="table table-modern">
    <thead>
      <tr>
        <th>{{ _('device_id') }}</th>
        <th>{{ _('user_name') }}</th>
        <th>{{ _('device_name') }}</th>
        <th>UUID</th>
        <th>{{ _('access_type') }}</th>
        <th>{{ _('status') }}</th>
        <th>{{ _('description') }}</th>
        <th>{{ _('date_time') }}</th>
        {% if request.endpoint == 'access_logs' %}
        <th>{{ _('actions') }}</th>
        {% endif %}
      </tr>
    </thead>
    <tbody>
      {% for log in logs %}
      <tr>
        <td><span class="device-id">{{ log.device_id }}</span></td>
        <td>
          {% if log.user_name %}
            <span class="user-info">{{ log.user_name }}</span>
          {% else %}
            <span class="text-muted">{{ _('unknown_user') }}</span>
          {% endif %}
        </td>
        <td>
          {% if log.device_name %}
            <span class="device-info">{{ log.device_name }}</span>
          {% else %}
            <span class="text-muted">{{ log.device_id }}</span>
          {% endif %}
        </td>
        <td><code>{{ log.card_uuid or 'N/A' }}</code></td>
        <td>
          {% set access_types = {1: ('NFC','access-nfc'), 2: ('BUTTON','access-button'), 3: ('TCP SERVER','access-tcpserver'), 4: ('TCP CLIENT','access-tcpclient'), 5: ('BLE','access-ble'), 6: ('RS485','access-rs485')} %}
          {% if log.access_type %}
            {% set type_info = access_types.get(log.access_type|int, ('UNKNOWN','access-unknown')) %}
            <div class="access-type-container">
              {% if log.access_type == 1 or log.access_type == '1' %}
                <img src="{{ url_for('static', filename='img/access_nfc.png') }}" alt="" aria-hidden="true" width="20" height="20">
              {% elif log.access_type == 2 or log.access_type == '2' %}
                <img src="{{ url_for('static', filename='img/access_button.png') }}" alt="" aria-hidden="true" width="20" height="20">
              {% elif log.access_type == 3 or log.access_type == '3' %}
                <img src="{{ url_for('static', filename='img/access_tcpserver.png') }}" alt="" aria-hidden="true" width="20" height="20">
              {% elif log.access_type == 4 or log.access_type == '4' %}
                <img src="{{ url_for('static', filename='img/access_tcpclient.png') }}" alt="" aria-hidden="true" width="20" height="20">
              {% elif log.access_type == 5 or log.access_type == '5' %}
                <img src="{{ url_for('static', filename='img/access_ble.png') }}" alt="" aria-hidden="true" width="20" height="20">
              {% elif log.access_type == 6 or log.access_type == '6' %}
                <img src="{{ url_for('static', filename='img/access_rs485.png') }}" alt="" aria-hidden="true" width="20" height="20">
              {% else %}
                <i class="bi bi-question-circle text-muted"></i>
              {% endif %}
              <span class="access-type {{ type_info[1] }}">
                {% if (log.access_type == 3 or log.access_type == '3') and (log.notes == 'Admin Panel') %}
                  {{ _('web_page_label') }}
                {% elif (log.access_type == 3 or log.access_type == '3') and (log.is_terminal == 2 or log.is_terminal == '2') %}
                  {{ _('mobile_page_label') }}
                {% else %}
                  {{ type_info[0] }}
                {% endif %}
              </span>
            </div>
          {% else %}
            <div class="d-flex align-items-center">
              <i class="bi bi-clock-history me-2 text-muted"></i>
              <span class="access-type fw-bold">{{ log.access_type or _('unknown') }}</span>
            </div>
          {% endif %}
        </td>
        <td>
          {% if log.is_granted %}
            <span class="badge badge-success"><i class="bi bi-check-circle me-1"></i>{{ _('IZIN_VERILDI') }}</span>
          {% else %}
            <span class="badge badge-danger"><i class="bi bi-x-circle me-1"></i>{{ _('IZIN_VERILMEDI') }}</span>
          {% endif %}
        </td>
        <td>
          {% if log.notes == 'Admin Panel' %}
            {{ _('admin_panel') }}
          {% elif (log.notes or '') == 'Cihaz Verisi' %}
            {{ _('device_data') }}
          {% elif (log.notes or '').lower() in ['mobil uygulama giriş','mobil uygulama giris','mobile app entry'] %}
            {{ _('mobile_app_entry') }}
          {% elif (log.notes or '').lower() in ['mesai giriş','mesai giris','shift in'] %}
            {{ _('shift_in_label') }}
          {% elif (log.notes or '').lower() in ['mesai çıkış','mesai cikis','shift out'] %}
            {{ _('shift_out_label') }}
          {% elif (log.notes or '').lower() in ['mola giriş','mola giris','break in'] %}
            {{ _('break_in_label') }}
          {% elif (log.notes or '').lower() in ['mola çıkış','mola cikis','break out'] %}
            {{ _('break_out_label') }}
          {% elif (log.notes or '').lower() in ['misafir giriş','misafir giris','guest in'] %}
            {{ _('guest_in_label') }}
          {% elif (log.notes or '').lower() in ['misafir çıkış','misafir cikis','guest out'] %}
            {{ _('guest_out_label') }}
          {% else %}
            {{ log.notes or '' }}
          {% endif %}
        </td>
        <td>
          <span class="timestamp">
            {% if log.source == 'new' %}
              {{ log.timestamp|datetime }}
            {% else %}
              {{ log.timestamp }}
            {% endif %}
          </span>
        </td>
        {% if request.endpoint == 'access_logs' %}
        <td>
          <div class="btn-group btn-group-sm">
            <button class="btn btn-outline-danger" data-log-id="{{ log.id }}" data-log-source="{{ log.source or '' }}" onclick="deleteLog(this.dataset.logId, this.dataset.logSource)" title="Bu kaydı sil">
              <i class="bi bi-trash"></i>
            </button>
          </div>
        </td>
        {% endif %}
      </tr>
      {% endfor %}
    </tbody>
  </table>
</div>

{% if total_pages and total_pages > 1 %}
<div class="row mt-4">
  <div class="col-12">
    <div class="d-flex justify-content-between align-items-center bg-light p-3 rounded">
      <div class="text-muted">
        {{ _('pagination_showing') }} {{ (page - 1) * per_page + 1 }} {{ _('pagination_to') }} {{ page * per_page if page * per_page < total_logs else total_logs }} {{ _('pagination_of_total') }} {{ total_logs }} {{ _('pagination_entries') }}
      </div>
      <nav aria-label="Sayfa navigasyonu">
        <ul class="pagination pagination-sm mb-0">
          {% if page > 1 %}
          <li class="page-item">
            <a class="page-link" href="{{ url_for('access_logs', page=1, per_page=per_page, adsoyad=request.args.get('adsoyad', '')) }}#logs" aria-label="&laquo;">
              <i class="fas fa-angle-double-left"></i>
            </a>
          </li>
          <li class="page-item">
            <a class="page-link" href="{{ url_for('access_logs', page=page-1, per_page=per_page, adsoyad=request.args.get('adsoyad', '')) }}#logs">
              <i class="fas fa-angle-left"></i>
            </a>
          </li>
          {% else %}
          <li class="page-item disabled"><span class="page-link"><i class="fas fa-angle-double-left"></i></span></li>
          <li class="page-item disabled"><span class="page-link"><i class="fas fa-angle-left"></i></span></li>
          {% endif %}

          {% for p in get_page_range(page, total_pages) %}
          <li class="page-item {% if p == page %}active{% endif %}">
            <a class="page-link" href="{{ url_for('access_logs', page=p, per_page=per_page, adsoyad=request.args.get('adsoyad', '')) }}#logs">{{ p }}</a>
          </li>
          {% endfor %}

          {% if page < total_pages %}
          <li class="page-item">
            <a class="page-link" href="{{ url_for('access_logs', page=page+1, per_page=per_page, adsoyad=request.args.get('adsoyad', '')) }}#logs">
              <i class="fas fa-angle-right"></i>
            </a>
          </li>
          <li class="page-item">
            <a class="page-link" href="{{ url_for('access_logs', page=total_pages, per_page=per_page, adsoyad=request.args.get('adsoyad', '')) }}#logs">
              <i class="fas fa-angle-double-right"></i>
            </a>
          </li>
          {% else %}
          <li class="page-item disabled"><span class="page-link"><i class="fas fa-angle-right"></i></span></li>
          <li class="page-item disabled"><span class="page-link"><i class="fas fa-angle-double-right"></i></span></li>
          {% endif %}
        </ul>
      </nav>
    </div>
  </div>
</div>
{% endif %}
{% else %}
<div class="d-flex align-items-center justify-content-center py-5" style="min-height: 220px;">
  <div class="text-center">
    <i class="bi bi-clock-history display-4 text-muted mb-3"></i>
    <h3 class="text-muted">{{ _('no_access_logs') }}</h3>
    <p class="text-muted">{{ _('no_access_logs_description') }}</p>
  </div>
</div>
{% endif %} 