{% extends "base.html" %}

{% block title %}WMT Management – {{ app_name }}{% endblock %}

{% block extra_css %}
<style>
.stat-card { border-left: 4px solid; }
.stat-card.blue  { border-color: #3498db; }
.stat-card.green { border-color: #2ecc71; }
.stat-card.orange{ border-color: #f39c12; }
.stat-card.red   { border-color: #e74c3c; }
.badge-pending  { background-color: #f39c12; }
.badge-accepted { background-color: #2ecc71; }
.badge-rejected { background-color: #e74c3c; }
</style>
{% endblock %}

{% block page_title %}WMT Management{% endblock %}

{% block content %}
<div class="row mb-3">
  <div class="col">
    <a href="{{ url_for('wmt_web.settings') }}" class="btn btn-primary me-2">
      <i class="fas fa-cog"></i> Global Settings
    </a>
    <a href="{{ url_for('main.devices') }}" class="btn btn-outline-primary me-2">
      <i class="fas fa-desktop"></i> Devices
    </a>
    <a href="{{ url_for('wmt_web.update_requests') }}" class="btn btn-outline-warning">
      <i class="fas fa-inbox"></i> Update Requests
      {% if pending_count > 0 %}
        <span class="badge bg-danger ms-1">{{ pending_count }}</span>
      {% endif %}
    </a>
  </div>
</div>

<!-- Stats row -->
<div class="row g-3 mb-4">
  <div class="col-sm-6 col-lg-3">
    <div class="card stat-card blue h-100">
      <div class="card-body">
        <div class="d-flex justify-content-between align-items-center">
          <div>
            <p class="text-muted mb-1 small">Registered Devices</p>
            <h4 class="mb-0">{{ devices | length }}</h4>
          </div>
          <i class="fas fa-desktop fa-2x text-primary opacity-50"></i>
        </div>
      </div>
    </div>
  </div>
  <div class="col-sm-6 col-lg-3">
    <div class="card stat-card orange h-100">
      <div class="card-body">
        <div class="d-flex justify-content-between align-items-center">
          <div>
            <p class="text-muted mb-1 small">Pending Requests</p>
            <h4 class="mb-0">{{ pending_count }}</h4>
          </div>
          <i class="fas fa-clock fa-2x text-warning opacity-50"></i>
        </div>
      </div>
    </div>
  </div>
  <div class="col-sm-6 col-lg-3">
    <div class="card stat-card green h-100">
      <div class="card-body">
        <div class="d-flex justify-content-between align-items-center">
          <div>
            <p class="text-muted mb-1 small">Config Last Updated</p>
            <h6 class="mb-0">
              {% if global_cfg and global_cfg.updated_at %}
                {{ global_cfg.updated_at | local_dt }}
              {% else %}
                Never
              {% endif %}
            </h6>
          </div>
          <i class="fas fa-sync fa-2x text-success opacity-50"></i>
        </div>
      </div>
    </div>
  </div>
  <div class="col-sm-6 col-lg-3">
    <div class="card stat-card red h-100">
      <div class="card-body">
        <div class="d-flex justify-content-between align-items-center">
          <div>
            <p class="text-muted mb-1 small">Chrome URL</p>
            <small class="text-truncate d-block" style="max-width:160px">
              {{ global_cfg.chrome_url if global_cfg else '—' }}
            </small>
          </div>
          <i class="fas fa-globe fa-2x text-danger opacity-50"></i>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="row g-3">
  <!-- Device list -->
  <div class="col-lg-7">
    <div class="card">
      <div class="card-header d-flex justify-content-between align-items-center">
        <strong><i class="fas fa-desktop me-2"></i>WMT Client Devices</strong>
        <a href="{{ url_for('main.devices') }}" class="btn btn-sm btn-success">
          <i class="fas fa-plus"></i> Add
        </a>
      </div>
      <div class="card-body p-0">
        {% if devices %}
        <div class="table-responsive">
          <table class="table table-sm table-hover mb-0">
            <thead class="table-light">
              <tr>
                <th>Work Place</th>
                <th>Client Name</th>
                <th>MAC</th>
                <th>IP</th>
                <th>Last Seen</th>
                <th></th>
              </tr>
            </thead>
            <tbody>
              {% for d in devices %}
              <tr>
                <td><strong>{{ d.device_name or '—' }}</strong></td>
                <td>{{ d.hostname or '—' }}</td>
                <td><code>{{ d.mac_address }}</code></td>
                <td>{{ d.device_ip or '—' }}</td>
                <td class="text-muted small">
                  {{ d.last_seen | local_dt if d.last_seen else 'Never' }}
                </td>
                <td>
                  <a href="{{ url_for('main.device_edit', device_id=d.id) }}"
                     class="btn btn-xs btn-outline-primary btn-sm py-0">Edit</a>
                </td>
              </tr>
              {% endfor %}
            </tbody>
          </table>
        </div>
        {% else %}
        <p class="text-muted p-3 mb-0">No WMT client devices registered yet.
          <a href="{{ url_for('main.devices') }}">Manage devices</a>.
        </p>
        {% endif %}
      </div>
    </div>
  </div>

  <!-- Recent update requests -->
  <div class="col-lg-5">
    <div class="card">
      <div class="card-header d-flex justify-content-between align-items-center">
        <strong><i class="fas fa-inbox me-2"></i>Recent Requests</strong>
        <a href="{{ url_for('wmt_web.update_requests') }}" class="btn btn-sm btn-outline-secondary">
          View All
        </a>
      </div>
      <div class="card-body p-0">
        {% if recent_requests %}
        <ul class="list-group list-group-flush">
          {% for r in recent_requests %}
          <li class="list-group-item d-flex justify-content-between align-items-start py-2">
            <div>
              <code class="small">{{ r.mac_address }}</code><br>
              <small class="text-muted">{{ r.submitted_at | local_dt }}</small>
            </div>
            <span class="badge badge-{{ r.status }} rounded-pill">{{ r.status }}</span>
          </li>
          {% endfor %}
        </ul>
        {% else %}
        <p class="text-muted p-3 mb-0">No recent requests.</p>
        {% endif %}
      </div>
    </div>
  </div>
</div>
{% endblock %}
