<script lang="ts">
  import { postToServer } from "./utils";

  export let whereami: string;

  let pt: string;

  async function sendData() {
    const ids = ["server", "port", "database", "user", "password"];
    const body: any = {};
    for (const id of ids) {
      body[id] = (document.getElementById(id) as HTMLInputElement).value;
    }
    body["port"] = parseInt(body["port"]);

    const res = await postToServer("/check-db", body, true);
    if (res === "ok") {
      whereami = "login";
    } else {
      pt = "Error, your database credentials are wrong";
    }
  }
</script>

<main>
  <label for="server">Server:</label>
  <input type="text" id="server" value="127.0.0.1" /><br /><br />
  <label for="port">Port:</label>
  <input type="number" min="1" max="65535" id="port" value="3306" /><br /><br />
  <label for="database">Database:</label>
  <input type="text" id="database" value="aitp" /><br /><br />
  <label for="user">User:</label>
  <input type="text" id="user" value="admin" /><br /><br />
  <label for="password">Password:</label>
  <input type="password" id="password" value="qwerty" /><br /><br />
  <button type="button" on:click={sendData}>Connect</button>
  <p contenteditable="true" bind:innerHTML={pt} />
</main>

<style>
  :root {
    font-family: "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell;
  }

  main {
    text-align: center;
    padding: 1em;
    margin: 0 auto;
    font-size: 2em;
  }
  input,
  button {
    font-size: 1em;
    margin-left: 10px;
  }

  p {
    color: red;
  }
</style>
