<!DOCTYPE html>
<html lang="de">
<head>
  <meta charset="UTF-8">
  <title><%= title %></title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <style>
    body { font-family: Arial, sans-serif; background:#f5f5f5; padding:20px; }
    .card { max-width: 560px; margin: 0 auto; background: white; padding: 20px; border-radius: 12px; box-shadow: 0 4px 16px rgba(0,0,0,.08); }
    label { display:block; margin-top: 12px; font-weight: 600; }
    input { width: 100%; padding: 10px; margin-top: 6px; border-radius: 8px; border: 1px solid #ddd; }
    .row { display:flex; gap: 12px; }
    .row > div { flex: 1; }
    button { margin-top: 16px; padding: 10px 14px; border: 0; border-radius: 10px; cursor:pointer; }
    .btn-primary { background:#2563eb; color:white; }
    .btn-secondary { background:#111827; color:white; }
    .msg { margin-top: 10px; padding:10px; border-radius: 10px; display:none; }
    .msg.ok { background:#dcfce7; color:#166534; }
    .msg.bad { background:#fee2e2; color:#991b1b; }
  </style>
</head>
<body>

  <div class="card">
    <h2>🛠️ Erstinstallation</h2>
    <p>Bitte DB Daten eingeben. Danach wird <code>config.enc</code> gespeichert.</p>

    <form method="POST" action="/setup">
        <label>DB Host</label>
        <input name="host" placeholder="192.168.0.86" required />

        <label>DB Port</label>
        <input name="port" placeholder="3306" value="3306" required />

        <label>DB Benutzer</label>
        <input name="user" placeholder="praxisuser" required />

        <label>DB Passwort</label>
        <input name="password" type="password" required />

        <label>DB Name</label>
        <input name="name" placeholder="praxissoftware" required />
        <label>Passwort</label>
        <input name="password" type="password" value="<%= defaults.password %>" />

        <button type="button" class="btn-secondary" onclick="testConnection()">🔍 Verbindung testen</button>
        <button type="submit" class="btn-primary">✅ Speichern & Setup abschließen</button>

      <div id="msg" class="msg"></div>
    </form>
  </div>

  <script>
    async function testConnection() {
      const form = document.querySelector("form");
      const data = new FormData(form);
      const body = Object.fromEntries(data.entries());

      const msg = document.getElementById("msg");
      msg.style.display = "block";
      msg.className = "msg";
      msg.textContent = "Teste Verbindung...";

      try {
        const res = await fetch("/setup/test", {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify(body)
        });

        const json = await res.json();
        msg.textContent = json.message;

        if (json.ok) msg.classList.add("ok");
        else msg.classList.add("bad");
      } catch (e) {
        msg.textContent = "❌ Fehler: " + e.message;
        msg.classList.add("bad");
      }
    }
  </script>
</body>
</html>
