{% 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; }
.badge-to_be_configured{ background-color: #6f42c1; 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 %}
  {% if unconfigured_count > 0 %}
    <span class="badge bg-purple ms-1" style="background:#6f42c1">{{ unconfigured_count }} to configure</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 == 'to_be_configured' %}active{% endif %}"
       href="{{ url_for('wmt_web.update_requests', status='to_be_configured') }}">
      <i class="fas fa-tools me-1"></i>To Be Configured
      {% if unconfigured_count > 0 %}<span class="badge ms-1" style="background:#6f42c1;color:#fff">{{ unconfigured_count }}</span>{% endif %}
    </a>
  </li>
  <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 status_filter == 'to_be_configured' and requests %}
<div class="alert alert-info d-flex align-items-start gap-2 mb-3">
  <i class="fas fa-info-circle mt-1"></i>
  <div>
    These devices are online but have <strong>no work_place assigned</strong> yet.
    They are <em>not</em> registered in the devices table. Accept a request to create
    the device record and push a configuration to the client.
  </div>
</div>
{% endif %}

{% 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>Last Seen</th>
            <th>Status</th>
            {% if status_filter in ('pending', 'to_be_configured', 'all') %}
            <th class="text-end">Actions</th>
            {% endif %}
          </tr>
        </thead>
        <tbody>
          {% for r in requests %}
          <tr {% if r.status == 'to_be_configured' %}class="table-warning"{% endif %}>
            <td class="text-muted small">{{ r.id }}</td>
            <td><code>{{ r.mac_address or '—' }}</code></td>
            <td>
              {% if r.status == 'to_be_configured' %}
                <span class="text-muted fst-italic">not assigned</span>
              {% else %}
                {{ r.proposed_device_name or '—' }}
              {% endif %}
            </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>
              <span class="badge badge-{{ r.status }} rounded-pill">{{ r.status.replace('_', ' ') }}</span>
              {% if r.admin_reviewed_at %}
              <br><small class="text-muted">{{ r.admin_reviewed_at | local_dt }}</small>
              {% endif %}
            </td>
            {% if status_filter in ('pending', 'to_be_configured', 'all') %}
            <td class="text-end">
              {% if r.status in ('pending', 'to_be_configured') %}
              <!-- Accept / Assign -->
              <form method="post" action="{{ url_for('wmt_web.accept_request', req_id=r.id) }}"
                    class="d-inline"
                    onsubmit="return confirm('Accept this request and create/update the device record?')">
                <button type="submit" class="btn btn-sm btn-success">
                  <i class="fas fa-check"></i> {% if r.status == 'to_be_configured' %}Assign{% else %}Accept{% endif %}
                </button>
              </form>
              <!-- Reject / Dismiss -->
              <form method="post" action="{{ url_for('wmt_web.reject_request', req_id=r.id) }}"
                    class="d-inline ms-1"
                    onsubmit="return confirm('Dismiss this device?')">
                <button type="submit" class="btn btn-sm btn-outline-danger">
                  <i class="fas fa-times"></i> Dismiss
                </button>
              </form>
              {% else %}
              <span class="text-muted small">—</span>
              {% endif %}
            </td>
            {% endif %}
          </tr>
          {% if r.admin_notes %}
          <tr class="table-light">
            <td colspan="8" class="small text-muted ps-4">
              <i class="fas fa-comment me-1"></i>{{ 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.replace('_', ' ') }} requests found.</p>
</div>
{% endif %}
{% endblock %}
