{% extends "base.html" %}
{% block title %}{{ 'Éditer' if dest else 'Nouvelle destination' }}{% endblock %}

{% block content %}
<div class="max-w-xl">
  <div class="mb-6">
    <a href="{{ url_for('cfg.settings') + '?tab=destinations' }}" class="text-gray-400 hover:text-gray-600 text-sm">← Destinations</a>
  </div>
  <h1 class="text-xl font-bold text-gray-900 mb-6">
    {{ 'Éditer « ' + dest.name + ' »' if dest else 'Nouvelle destination rsync SSH' }}
  </h1>

  <form method="post" class="space-y-5">

    <div class="bg-white rounded-xl border border-gray-200 p-6 space-y-4">
      <h2 class="text-sm font-semibold text-gray-700 uppercase tracking-wide">Connexion SSH</h2>

      <div>
        <label class="block text-sm font-medium text-gray-700 mb-1">Nom</label>
        <input type="text" name="name" required
               value="{{ dest.name if dest else '' }}"
               placeholder="ex: VPS Hetzner"
               class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
      </div>

      <div class="grid grid-cols-3 gap-3">
        <div class="col-span-2">
          <label class="block text-sm font-medium text-gray-700 mb-1">Hôte</label>
          <input type="text" name="host" required
                 value="{{ dest.host if dest else '' }}"
                 placeholder="backup.exemple.fr"
                 class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm font-mono focus:outline-none focus:ring-2 focus:ring-blue-500">
        </div>
        <div>
          <label class="block text-sm font-medium text-gray-700 mb-1">Port</label>
          <input type="number" name="port" min="1" max="65535"
                 value="{{ dest.port if dest else 22 }}"
                 class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm font-mono focus:outline-none focus:ring-2 focus:ring-blue-500">
        </div>
      </div>

      <div>
        <label class="block text-sm font-medium text-gray-700 mb-1">Utilisateur SSH</label>
        <input type="text" name="user"
               value="{{ dest.user if dest else 'root' }}"
               placeholder="root"
               class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm font-mono focus:outline-none focus:ring-2 focus:ring-blue-500">
      </div>

      <div>
        <label class="block text-sm font-medium text-gray-700 mb-1">Chemin distant</label>
        <input type="text" name="remote_path"
               value="{{ dest.remote_path if dest else '/home/yunohost.backup/archives' }}"
               placeholder="/home/yunohost.backup/archives"
               class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm font-mono focus:outline-none focus:ring-2 focus:ring-blue-500">
        <p class="text-xs text-gray-400 mt-1">
          Répertoire de destination sur le serveur distant. Doit exister et être accessible par l'utilisateur SSH.
        </p>
      </div>

      <div class="flex items-center gap-2">
        <input type="checkbox" name="enabled" value="1" id="dest_enabled"
               {% if not dest or dest.enabled %}checked{% endif %}
               class="rounded border-gray-300 text-blue-600">
        <label for="dest_enabled" class="text-sm font-medium text-gray-700">Destination activée</label>
      </div>
    </div>

    {% if pub_key %}
    <div class="bg-white rounded-xl border border-gray-200 p-6 space-y-3">
      <h2 class="text-sm font-semibold text-gray-700 uppercase tracking-wide">Clé publique SSH</h2>
      <p class="text-xs text-gray-500">
        Copiez cette clé dans <code class="bg-gray-100 px-1 rounded">~{{ dest.user }}/.ssh/authorized_keys</code>
        sur <strong>{{ dest.host }}</strong> pour autoriser le transfert.
      </p>
      <div class="relative">
        <pre id="pubkey" class="bg-gray-900 text-green-400 text-xs p-3 rounded-lg overflow-x-auto whitespace-pre-wrap break-all select-all">{{ pub_key }}</pre>
        <button type="button" onclick="navigator.clipboard.writeText(document.getElementById('pubkey').textContent)"
                class="absolute top-2 right-2 bg-gray-700 hover:bg-gray-600 text-gray-300 text-xs px-2 py-1 rounded transition">
          Copier
        </button>
      </div>
    </div>
    {% elif dest %}
    <div class="bg-amber-50 border border-amber-200 rounded-xl p-4 text-sm text-amber-800">
      ⚠ La clé SSH sera générée lors de l'enregistrement.
    </div>
    {% endif %}

    <div class="flex gap-3">
      <button type="submit" class="btn-primary btn-md">
        {{ 'Enregistrer' if dest else 'Créer la destination' }}
      </button>
      <a href="{{ url_for('cfg.settings') + '?tab=destinations' }}" class="btn-secondary btn-md">Annuler</a>
    </div>
  </form>
</div>
{% endblock %}
