{% extends "base.html" %}

{% block title %}Global Settings – WMT – {{ app_name }}{% endblock %}

{% block page_title %}WMT Global Settings{% endblock %}

{% block content %}
<div class="row justify-content-center">
  <div class="col-lg-9">
    <div class="card">
      <div class="card-header">
        <i class="fas fa-cog me-2"></i>
        Global Configuration
        <small class="text-muted ms-3">
          Applied to all WMT devices on next sync.
          {% if cfg and cfg.updated_at %}
            Last saved: {{ cfg.updated_at | local_dt('%Y-%m-%d %H:%M:%S') }} by {{ cfg.updated_by or 'admin' }}
          {% endif %}
        </small>
      </div>
      <div class="card-body">
        <form method="post">

          <h6 class="text-uppercase text-muted mb-3 mt-2">
            <i class="fas fa-globe me-1"></i> Chrome Launch
          </h6>
          <div class="mb-3">
            <label class="form-label fw-semibold">Production URL
              <small class="text-muted fw-normal">(kiosk mode at startup)</small>
            </label>
            <input type="url" name="chrome_url" class="form-control"
                   value="{{ cfg.chrome_url if cfg else '' }}" required>
          </div>
          <div class="mb-3">
            <label class="form-label fw-semibold">Local / Fallback URL
              <small class="text-muted fw-normal">(optional)</small>
            </label>
            <input type="url" name="chrome_local_url" class="form-control"
                   value="{{ cfg.chrome_local_url or '' }}">
          </div>
          <div class="mb-4">
            <label class="form-label fw-semibold">Insecure Origin to Trust
              <small class="text-muted fw-normal">(--unsafely-treat-insecure-origin-as-secure)</small>
            </label>
            <input type="text" name="chrome_insecure_origin" class="form-control"
                   value="{{ cfg.chrome_insecure_origin if cfg else '' }}">
          </div>

          <hr>
          <h6 class="text-uppercase text-muted mb-3">
            <i class="fas fa-id-card me-1"></i> Card API
          </h6>
          <div class="mb-4">
            <label class="form-label fw-semibold">Base URL
              <small class="text-muted fw-normal">Format: {base_url}/{device_name}/{card_id}/{0or1}/{timestamp}</small>
            </label>
            <input type="url" name="card_api_base_url" class="form-control"
                   value="{{ cfg.card_api_base_url if cfg else '' }}" required>
          </div>

          <hr>
          <h6 class="text-uppercase text-muted mb-3">
            <i class="fas fa-server me-1"></i> Server / Network
          </h6>
          <div class="row g-3 mb-3">
            <div class="col-md-6">
              <label class="form-label fw-semibold">Log Server URL</label>
              <input type="url" name="server_log_url" class="form-control"
                     value="{{ cfg.server_log_url if cfg else '' }}">
            </div>
            <div class="col-md-6">
              <label class="form-label fw-semibold">Internet Check Host
                <small class="text-muted fw-normal">(ping target)</small>
              </label>
              <input type="text" name="internet_check_host" class="form-control"
                     value="{{ cfg.internet_check_host if cfg else '' }}">
            </div>
          </div>
          <div class="row g-3 mb-4">
            <div class="col-md-6">
              <label class="form-label fw-semibold">Auto-Update Host</label>
              <input type="text" name="update_host" class="form-control"
                     value="{{ cfg.update_host if cfg else '' }}">
            </div>
            <div class="col-md-6">
              <label class="form-label fw-semibold">Auto-Update SSH User</label>
              <input type="text" name="update_user" class="form-control"
                     value="{{ cfg.update_user if cfg else '' }}">
            </div>
          </div>

          <hr>
          <div class="mb-3">
            <label class="form-label fw-semibold">Admin Notes</label>
            <textarea name="notes" class="form-control" rows="2">{{ cfg.notes or '' }}</textarea>
          </div>

          <div class="d-flex gap-2 mt-3">
            <button type="submit" class="btn btn-primary">
              <i class="fas fa-save me-1"></i> Save Settings
            </button>
            <a href="{{ url_for('main.devices') }}" class="btn btn-outline-secondary">Cancel</a>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>
{% endblock %}
