{% extends "base.html" %}

{% block title %}Edit {{ device.hostname }} – {{ app_name }}{% endblock %}
{% block page_title %}Edit Device{% endblock %}

{% block content %}
<div class="row justify-content-center">
  <div class="col-lg-8">
    <div class="card">
      <div class="card-header">
        <i class="fas fa-edit me-2"></i>Edit: <strong>{{ device.hostname }}</strong>
      </div>
      <div class="card-body">
        <form method="post">

          <h6 class="text-muted mb-3">Monitoring Fields</h6>
          <div class="row g-3 mb-3">
            <div class="col-md-6">
              <label class="form-label fw-semibold">Hostname <span class="text-danger">*</span></label>
              <input type="text" name="hostname" class="form-control" required
                     value="{{ device.hostname }}">
            </div>
            <div class="col-md-6">
              <label class="form-label fw-semibold">IP Address <span class="text-danger">*</span></label>
              <input type="text" name="device_ip" class="form-control" required
                     value="{{ device.device_ip }}">
            </div>
            <div class="col-md-6">
              <label class="form-label fw-semibold">Work Place <span class="text-danger">*</span></label>
              <input type="text" name="nume_masa" class="form-control" required
                     value="{{ device.nume_masa or '' }}">
            </div>
            <div class="col-md-6">
              <label class="form-label fw-semibold">Status</label>
              <select name="status" class="form-select">
                <option value="active"      {% if device.status == 'active' %}selected{% endif %}>Active</option>
                <option value="inactive"    {% if device.status == 'inactive' %}selected{% endif %}>Inactive</option>
                <option value="maintenance" {% if device.status == 'maintenance' %}selected{% endif %}>Maintenance</option>
              </select>
            </div>
            <div class="col-md-6">
              <label class="form-label fw-semibold">Device Type</label>
              <input type="text" name="device_type" class="form-control"
                     value="{{ device.device_type or '' }}" placeholder="Raspberry Pi, PC, Server…">
            </div>
            <div class="col-md-6">
              <label class="form-label fw-semibold">Physical Location</label>
              <input type="text" name="location" class="form-control"
                     value="{{ device.location or '' }}" placeholder="Floor 2, Room 201">
            </div>
            <div class="col-md-6">
              <label class="form-label fw-semibold">OS Version</label>
              <input type="text" name="os_version" class="form-control"
                     value="{{ device.os_version or '' }}" placeholder="Raspberry Pi OS 11">
            </div>
            <div class="col-12">
              <label class="form-label fw-semibold">Description</label>
              <textarea name="description" class="form-control" rows="2">{{ device.description or '' }}</textarea>
            </div>
          </div>

          <hr>
          <h6 class="text-muted mb-3">WMT Client Fields</h6>
          <div class="row g-3 mb-3">
            <div class="col-md-6">
              <label class="form-label fw-semibold">MAC Address
                <small class="text-muted fw-normal">(WMT client identifier)</small>
              </label>
              <input type="text" name="mac_address" class="form-control"
                     value="{{ device.mac_address or '' }}"
                     placeholder="b8:27:eb:aa:bb:cc"
                     pattern="^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$">
              <div class="form-text">Leave empty if this is not a WMT client device.</div>
            </div>
            <div class="col-12">
              <label class="form-label fw-semibold">Custom Production URL
                <small class="text-muted fw-normal">(device-specific Chrome launch override)</small>
              </label>
              <input type="url" name="custom_chrome_url" class="form-control"
                     value="{{ device.custom_chrome_url or '' }}"
                     placeholder="Leave blank to use the global default from WMT Settings">
              {% if device.custom_chrome_url %}
              <div class="form-text text-warning">
                <i class="fas fa-exclamation-triangle me-1"></i>
                This device opens a custom URL instead of the global default. Clear the field to revert to the
                <a href="{{ url_for('wmt_web.settings') }}" target="_blank">global production URL</a>.
              </div>
              {% else %}
              <div class="form-text text-success">
                <i class="fas fa-check-circle me-1"></i>
                Using the global default production URL from
                <a href="{{ url_for('wmt_web.settings') }}" target="_blank">WMT Settings</a>.
              </div>
              {% endif %}
            </div>
          </div>

          {% if device.mac_address %}
          <div class="alert alert-light border small mb-4">
            <strong>Config last updated:</strong>
            {{ device.config_updated_at | local_dt('%Y-%m-%d %H:%M:%S') if device.config_updated_at else '—' }}<br>
            <strong>Info reviewed at:</strong>
            <span class="{% if device.info_reviewed_at and device.info_reviewed_at.year > 1970 %}text-success{% else %}text-muted{% endif %}">
              {{ device.info_reviewed_at | local_dt('%Y-%m-%d %H:%M:%S') if (device.info_reviewed_at and device.info_reviewed_at.year > 1970) else 'Never reviewed' }}
            </span><br>
            <strong>Last seen:</strong>
            {{ device.last_seen | local_dt('%Y-%m-%d %H:%M:%S') if device.last_seen else 'Never' }}
          </div>
          {% endif %}

          <div class="d-flex gap-2">
            <button type="submit" class="btn btn-primary">
              <i class="fas fa-save me-1"></i>Save Changes
            </button>
            <a href="{{ url_for('main.devices') }}" class="btn btn-outline-secondary">Cancel</a>
            <form method="post" action="{{ url_for('main.device_delete', device_id=device.id) }}"
                  class="ms-auto"
                  onsubmit="return confirm('Delete {{ device.hostname }}?  All logs will also be deleted.')">
              <button type="submit" class="btn btn-outline-danger">
                <i class="fas fa-trash me-1"></i>Delete Device
              </button>
            </form>
          </div>

        </form>
      </div>
    </div>
  </div>
</div>
{% endblock %}
