{% extends "base.html" %}

{% block title %}Update Requests – WMT – {{ app_name }}{% endblock %}

{% block extra_css %}
<style>
.badge-pending  { background-color: #f39c12; color: #fff; }
.badge-accepted { background-color: #2ecc71; color: #fff; }
.badge-rejected { background-color: #e74c3c; color: #fff; }
</style>
{% endblock %}

{% block page_title %}
  WMT Update Requests
  {% if pending_count > 0 %}
    <span class="badge bg-danger ms-2">{{ pending_count }} pending</span>
  {% endif %}
{% endblock %}

{% block content %}
<!-- Filter tabs -->
<ul class="nav nav-tabs mb-4">
  <li class="nav-item">
    <a class="nav-link {% if status_filter == 'pending' %}active{% endif %}"
       href="{{ url_for('wmt_web.update_requests', status='pending') }}">
      Pending
      {% if pending_count > 0 %}<span class="badge bg-danger ms-1">{{ pending_count }}</span>{% endif %}
    </a>
  </li>
  <li class="nav-item">
    <a class="nav-link {% if status_filter == 'accepted' %}active{% endif %}"
       href="{{ url_for('wmt_web.update_requests', status='accepted') }}">Accepted</a>
  </li>
  <li class="nav-item">
    <a class="nav-link {% if status_filter == 'rejected' %}active{% endif %}"
       href="{{ url_for('wmt_web.update_requests', status='rejected') }}">Rejected</a>
  </li>
  <li class="nav-item">
    <a class="nav-link {% if status_filter == 'all' %}active{% endif %}"
       href="{{ url_for('wmt_web.update_requests', status='all') }}">All</a>
  </li>
</ul>

{% if requests %}
<div class="card">
  <div class="card-body p-0">
    <div class="table-responsive">
      <table class="table table-hover align-middle mb-0">
        <thead class="table-light">
          <tr>
            <th>#</th>
            <th>MAC Address</th>
            <th>Proposed Work Place</th>
            <th>Proposed Hostname</th>
            <th>Proposed IP</th>
            <th>Submitted</th>
            <th>Client Config Time</th>
            <th>Status</th>
            {% if status_filter == 'pending' or status_filter == 'all' %}
            <th class="text-end">Actions</th>
            {% endif %}
          </tr>
        </thead>
        <tbody>
          {% for r in requests %}
          <tr>
            <td class="text-muted small">{{ r.id }}</td>
            <td><code>{{ r.mac_address }}</code></td>
            <td>{{ r.proposed_device_name or '—' }}</td>
            <td>{{ r.proposed_hostname or '—' }}</td>
            <td>{{ r.proposed_device_ip or '—' }}</td>
            <td class="text-muted small">{{ r.submitted_at | local_dt }}</td>
            <td class="text-muted small">{{ r.client_config_mtime or '—' }}</td>
            <td>
              <span class="badge badge-{{ r.status }} rounded-pill">{{ r.status }}</span>
              {% if r.admin_reviewed_at %}
              <br><small class="text-muted">{{ r.admin_reviewed_at | local_dt }}</small>
              {% endif %}
            </td>
            {% if status_filter == 'pending' or status_filter == 'all' %}
            <td class="text-end">
              {% if r.status == 'pending' %}
              <!-- Accept -->
              <form method="post" action="{{ url_for('wmt_web.accept_request', req_id=r.id) }}"
                    class="d-inline"
                    onsubmit="return confirm('Accept this request and update the device record?')">
                <button type="submit" class="btn btn-sm btn-success">
                  <i class="fas fa-check"></i> Accept
                </button>
              </form>
              <!-- Reject -->
              <form method="post" action="{{ url_for('wmt_web.reject_request', req_id=r.id) }}"
                    class="d-inline ms-1"
                    onsubmit="return confirm('Reject this request?')">
                <button type="submit" class="btn btn-sm btn-outline-danger">
                  <i class="fas fa-times"></i> Reject
                </button>
              </form>
              {% else %}
              <span class="text-muted small">—</span>
              {% endif %}
            </td>
            {% endif %}
          </tr>
          {% if r.admin_notes %}
          <tr class="table-light">
            <td colspan="9" class="small text-muted ps-4">
              <i class="fas fa-comment me-1"></i> Admin note: {{ r.admin_notes }}
            </td>
          </tr>
          {% endif %}
          {% endfor %}
        </tbody>
      </table>
    </div>
  </div>
</div>
{% else %}
<div class="text-center text-muted py-5">
  <i class="fas fa-inbox fa-3x mb-3 opacity-25"></i>
  <p>No {{ status_filter }} requests found.</p>
</div>
{% endif %}
{% endblock %}
