<script>
  import { ApiError, AuthService } from "@odit/lfk-client-js";
  import { _ } from "svelte-i18n";
  import Toastify from "toastify-js";
  import "toastify-js/src/toastify.css";
  import isEmail from "validator/es/lib/isEmail";

  let reset_mail_sent = false;
  let usersEmail = "";
  function reset() {
    if (isEmail(usersEmail)) {
      AuthService.authControllerGetResetToken("de", { email: usersEmail })
        .then((resp) => {
          Toastify({
            text: $_("mail-validation-in-progress"),
            duration: 3500,
          }).showToast();
          reset_mail_sent = true;
        })
        .catch((err) => {});
    } else {
      Toastify({
        text: $_("invalid-mail-reset"),
        duration: 3500,
      }).showToast();
    }
  }
</script>

{#if reset_mail_sent}
  <div class="min-h-screen flex items-center justify-center bg-gray-100">
    <div class="max-w-md w-full py-12 px-6">
      <img style="height:10rem;" class="mx-auto" src="/lfk-logo.png" alt="" />
      <p class="mt-6 text-lg text-center font-bold text-gray-900">
        {$_('application_name')}
      </p>
      <p class="mt-2 mb-2 text-sm text-center text-gray-900">
        {$_('password-reset-mail-sent', { values: { usersEmail: usersEmail } })}
      </p>
      <div class="mt-6">
        <div class="mt-6">
          <a
            href="/"
            class="block w-full text-center py-2 px-3 border border-gray-300 rounded-md text-gray-900 font-medium hover:border-gray-400 focus:outline-none focus:border-gray-400 sm:text-sm">
            {$_('goback')}
          </a>
        </div>
      </div>
    </div>
  </div>
{:else}
  <div class="min-h-screen flex items-center justify-center bg-gray-100">
    <div class="max-w-md w-full py-12 px-6">
      <img style="height:10rem;" class="mx-auto" src="/lfk-logo.png" alt="" />
      <p class="mt-6 text-lg text-center font-bold text-gray-900">
        {$_('application_name')}
      </p>
      <p class="mt-6 text-sm text-center text-gray-900">
        {$_('forgot_password')}
      </p>
      <p class="mt-2 mb-2 text-sm text-center text-gray-900">
        {$_('dont-panic-were-resetting-it')}
      </p>
      <div>
        <div class="rounded-md shadow-sm">
          <div>
            <input
              aria-label={$_('e-mail-adress')}
              name="email"
              type="email"
              required=""
              class="border-gray-300 placeholder-gray-500 appearance-none rounded-none relative block w-full px-3 py-2 border text-gray-900 rounded-t-md focus:outline-none focus:shadow-outline-blue focus:border-blue-300 focus:z-10 sm:text-sm"
              placeholder={$_('e-mail-adress')}
              bind:value={usersEmail} />
          </div>
        </div>

        <div class="mt-5">
          <button
            on:click={reset}
            type="submit"
            class="relative block w-full py-2 px-3 border border-transparent rounded-md text-white font-semibold bg-gray-800 hover:bg-gray-700 focus:bg-gray-900 focus:outline-none focus:shadow-outline sm:text-sm">
            <span class="absolute left-0 inset-y pl-3">
              <svg
                class="h-5 w-5 text-gray-500"
                fill="currentColor"
                viewBox="0 0 20 20">
                <path
                  fill-rule="evenodd"
                  d="M5 9V7a5 5 0 0110 0v2a2 2 0 012 2v5a2 2 0 01-2 2H5a2 2 0 01-2-2v-5a2 2 0 012-2zm8-2v2H7V7a3 3 0 016 0z"
                  clip-rule="evenodd" />
              </svg>
            </span>
            {$_('reset-my-password')}
          </button>
        </div>
        <div class="mt-6">
          <div class="relative">
            <div class="absolute inset-0 flex items-center">
              <div class="w-full border-t border-gray-300" />
            </div>
            <div class="relative flex justify-center text-sm">
              <span
                class="px-2 bg-gray-100 text-gray-500">{$_('dont-have-your-email-connected')}</span>
            </div>
          </div>
          <span
            class="mt-2 text-sm px-2 bg-gray-100 text-gray-500 justify-center relative flex">{$_('cannot-reset-your-password-directly')}</span>

          <div class="mt-6">
            <a
              href="mailto:lfk@odit.services"
              class="block w-full text-center py-2 px-3 border border-gray-300 rounded-md text-gray-900 font-medium hover:border-gray-400 focus:outline-none focus:border-gray-400 sm:text-sm">
              {$_('send-a-mail-to-lfk-odit-services')}
            </a>
          </div>
          <div class="mt-6">
            <a
              href="/"
              class="block w-full text-center py-2 px-3 border border-gray-300 rounded-md text-gray-900 font-medium hover:border-gray-400 focus:outline-none focus:border-gray-400 sm:text-sm">{$_('goback')}</a>
          </div>
        </div>
      </div>
    </div>
  </div>
{/if}
