<script>
  import { _ } from "svelte-i18n";
  import { clickOutside } from "../base/outsideclick";
  import { UserGroupService } from "@odit/lfk-client-js";
  import toast from "svelte-french-toast";
  export let modal_open;
  export let current_groups;
  let description_input_value;
  function focus(el) {
    el.focus();
  }
  $: description_input_value = "";
  $: name_input_value = "";
  $: processed_last_submit = true;
  $: isNameValid = name_input_value.trim().length !== 0;
  $: createbtnenabled = isNameValid;
  (() => {
    document.onkeydown = (e) => {
      e = e || window.event;
      if (e.key === "Escape") {
        modal_open = false;
      }
      if (e.keyCode === 13) {
        if (createbtnenabled === true) {
          createbtnenabled = false;
          submit();
        }
      }
    };
  })();
  function submit() {
    if (processed_last_submit === true) {
      processed_last_submit = false;
      toast.loading($_("group-is-being-added"));
      let postdata = {
        name: name_input_value,
        description: description_input_value,
      };
      UserGroupService.userGroupControllerPost(postdata)
        .then((result) => {
          name_input_value = "";
          description_input_value = "";
          modal_open = false;
          //
          toast.dismiss();
          toast.success($_("group-added"));
          current_groups.push(result);
          current_groups = current_groups;
        })
        .catch((err) => {
          //
        })
        .finally(() => {
          processed_last_submit = true;
        });
    }
  }
</script>

{#if modal_open}
  <div
    class="fixed z-10 inset-0 overflow-y-auto"
    use:clickOutside
    on:click_outside={() => {
      modal_open = false;
    }}
  >
    <div
      class="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0"
    >
      <div class="fixed inset-0 transition-opacity" aria-hidden="true">
        <div
          class="absolute inset-0 bg-gray-500 opacity-75"
          data-id="modal_backdrop"
        />
      </div>
      <span
        class="hidden sm:inline-block sm:align-middle sm:h-screen"
        aria-hidden="true">&#8203;</span
      >
      <div
        class="inline-block align-bottom bg-white rounded-lg text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full"
        role="dialog"
        aria-modal="true"
        aria-labelledby="modal-headline"
      >
        <div class="bg-white px-4 pt-5 pb-4 sm:p-6 sm:pb-4">
          <div class="sm:flex sm:items-start">
            <div
              class="mx-auto flex-shrink-0 flex items-center justify-center h-12 w-12 rounded-full bg-blue-100 sm:mx-0 sm:h-10 sm:w-10"
            >
              <svg
                xmlns="http://www.w3.org/2000/svg"
                viewBox="0 0 640 512"
                class="h-6 w-6 text-blue-600"
                fill="currentColor"
                width="24"
                height="24"
                ><path fill="none" d="M0 0h24v24H0z" />
                <path
                  d="M610.5 341.3c2.6-14.1 2.6-28.5 0-42.6l25.8-14.9c3-1.7 4.3-5.2 3.3-8.5-6.7-21.6-18.2-41.2-33.2-57.4-2.3-2.5-6-3.1-9-1.4l-25.8 14.9c-10.9-9.3-23.4-16.5-36.9-21.3v-29.8c0-3.4-2.4-6.4-5.7-7.1-22.3-5-45-4.8-66.2 0-3.3.7-5.7 3.7-5.7 7.1v29.8c-13.5 4.8-26 12-36.9 21.3l-25.8-14.9c-2.9-1.7-6.7-1.1-9 1.4-15 16.2-26.5 35.8-33.2 57.4-1 3.3.4 6.8 3.3 8.5l25.8 14.9c-2.6 14.1-2.6 28.5 0 42.6l-25.8 14.9c-3 1.7-4.3 5.2-3.3 8.5 6.7 21.6 18.2 41.1 33.2 57.4 2.3 2.5 6 3.1 9 1.4l25.8-14.9c10.9 9.3 23.4 16.5 36.9 21.3v29.8c0 3.4 2.4 6.4 5.7 7.1 22.3 5 45 4.8 66.2 0 3.3-.7 5.7-3.7 5.7-7.1v-29.8c13.5-4.8 26-12 36.9-21.3l25.8 14.9c2.9 1.7 6.7 1.1 9-1.4 15-16.2 26.5-35.8 33.2-57.4 1-3.3-.4-6.8-3.3-8.5l-25.8-14.9zM496 368.5c-26.8 0-48.5-21.8-48.5-48.5s21.8-48.5 48.5-48.5 48.5 21.8 48.5 48.5-21.7 48.5-48.5 48.5zM96 224c35.3 0 64-28.7 64-64s-28.7-64-64-64-64 28.7-64 64 28.7 64 64 64zm224 32c1.9 0 3.7-.5 5.6-.6 8.3-21.7 20.5-42.1 36.3-59.2 7.4-8 17.9-12.6 28.9-12.6 6.9 0 13.7 1.8 19.6 5.3l7.9 4.6c.8-.5 1.6-.9 2.4-1.4 7-14.6 11.2-30.8 11.2-48 0-61.9-50.1-112-112-112S208 82.1 208 144c0 61.9 50.1 112 112 112zm105.2 194.5c-2.3-1.2-4.6-2.6-6.8-3.9-8.2 4.8-15.3 9.8-27.5 9.8-10.9 0-21.4-4.6-28.9-12.6-18.3-19.8-32.3-43.9-40.2-69.6-10.7-34.5 24.9-49.7 25.8-50.3-.1-2.6-.1-5.2 0-7.8l-7.9-4.6c-3.8-2.2-7-5-9.8-8.1-3.3.2-6.5.6-9.8.6-24.6 0-47.6-6-68.5-16h-8.3C179.6 288 128 339.6 128 403.2V432c0 26.5 21.5 48 48 48h255.4c-3.7-6-6.2-12.8-6.2-20.3v-9.2zM173.1 274.6C161.5 263.1 145.6 256 128 256H64c-35.3 0-64 28.7-64 64v32c0 17.7 14.3 32 32 32h65.9c6.3-47.4 34.9-87.3 75.2-109.4z"
                /></svg
              >
            </div>
            <div class="mt-3 text-center sm:mt-0 sm:ml-4 sm:text-left">
              <h3 class="text-lg leading-6 font-medium text-gray-900">
                {$_("create-a-new-user-group")}
              </h3>
              <div class="mt-2 mb-6">
                <p class="text-sm text-gray-500">
                  {$_(
                    "please-provide-the-required-information-for-creating-a-new-user-group"
                  )}
                </p>
              </div>
              <div class="grid grid-cols-6 gap-6">
                <div class="col-span-6">
                  <label
                    for="firstname"
                    class="block text-sm font-medium text-gray-700"
                    >{$_("name")}</label
                  >
                  <input
                    use:focus
                    autocomplete="off"
                    placeholder={$_("name")}
                    class:border-red-500={!isNameValid}
                    class:focus:border-red-500={!isNameValid}
                    class:focus:ring-red-500={!isNameValid}
                    bind:value={name_input_value}
                    type="text"
                    name="firstname"
                    class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm rounded-l-md sm:text-sm border-gray-300 border bg-gray-50 text-gray-500 rounded-md p-2"
                  />
                  {#if !isNameValid}
                    <span
                      class="flex items-center font-medium tracking-wide text-red-500 text-xs mt-1 ml-1"
                    >
                      {$_("name-is-required")}
                    </span>
                  {/if}
                </div>
                <div class="col-span-6">
                  <label
                    for="trackname"
                    class="block text-sm font-medium text-gray-700"
                    >{$_("description-optional")}</label
                  >
                  <input
                    autocomplete="off"
                    placeholder={$_("something-about-the-group")}
                    bind:value={description_input_value}
                    type="text"
                    name="trackname"
                    class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm rounded-l-md sm:text-sm border-gray-300 border bg-gray-50 text-gray-500 rounded-md p-2"
                  />
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse">
          <button
            disabled={!createbtnenabled}
            class:opacity-50={!createbtnenabled}
            on:click={submit}
            type="button"
            class="w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-blue-600 text-base font-medium text-white hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 sm:ml-3 sm:w-auto sm:text-sm"
          >
            {$_("create")}
          </button>
          <button
            on:click={() => {
              modal_open = false;
            }}
            type="button"
            class="mt-3 w-full inline-flex justify-center rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-base font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 sm:mt-0 sm:ml-3 sm:w-auto sm:text-sm"
          >
            {$_("cancel")}
          </button>
        </div>
      </div>
    </div>
  </div>
{/if}
