<script>
  import { _ } from "svelte-i18n";
  import { UserService } from "@odit/lfk-client-js";
  const users_promise = UserService.userControllerGetAll();
  import store, { users as usersstore } from "../../store.js";
  import UsersEmptyState from "./UsersEmptyState.svelte";
  $: searchvalue = "";
  $: active_deletes = [];
  export let current_users = [];
  $: advanced_search = false;
  usersstore.subscribe((val) => {
    current_users = val;
  });
  users_promise.then((data) => {
    usersstore.set(data);
  });
</script>

{#if store.state.jwtinfo.userdetails.permissions.includes("USER:GET")}
  {#await users_promise}
    <div
      class="bg-teal-lightest border-t-4 border-teal rounded-b text-teal-darkest px-4 py-3 shadow-md my-2"
      role="alert"
    >
      <p class="font-bold">users are being loaded...</p>
      <p class="text-sm">{$_("this-might-take-a-moment")}</p>
    </div>
  {:then}
    {#if current_users.length === 0}
      <UsersEmptyState />
    {:else}
      <!-- {#if advanced_search}
        advanced search
      {:else} -->
      <input
        type="search"
        bind:value={searchvalue}
        placeholder={$_("datatable.search")}
        aria-label={$_("datatable.search")}
        class="mb-2 w-full sm:w-auto mt-1 sm:mt-0 p-2 rounded-md border"
      />
      <!-- {/if} -->
      <!-- <button
        on:click={() => {
          advanced_search = !advanced_search;
        }}
        type="button"
        class="w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-gray-600 text-base font-medium text-white hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-gray-500 sm:w-auto sm:text-sm">
        {#if advanced_search}
          toggle simple search
        {:else}toggle advanced search{/if}
      </button> -->
      <div
        class="shadow border-b border-gray-200 sm:rounded-lg overflow-x-scroll"
      >
        <table class="divide-y divide-gray-200 w-full">
          <thead class="bg-gray-50">
            <tr class="odd:bg-white even:bg-gray-100">
              <th
                scope="col"
                class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"
              >
                {$_("name")}
              </th>
              <th
                scope="col"
                class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"
              >
                {$_("status")}
              </th>
              <th
                scope="col"
                class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"
              >
                {$_("groups")}
              </th>
              <th scope="col" class="relative px-6 py-3">
                <span class="sr-only">{$_("action")}</span>
              </th>
            </tr>
          </thead>
          <tbody class="divide-y divide-gray-200">
            {#each current_users as u}
              {#if Object.values(u)
                .toString()
                .toLowerCase()
                .includes(searchvalue)}
                <tr
                  class="odd:bg-white even:bg-gray-100"
                  data-rowid="user_{u.id}"
                >
                  <td class="px-6 py-4 whitespace-nowrap">
                    <div class="flex items-center">
                      <div class="ml-4">
                        <div class="text-sm font-medium text-gray-900">
                          {u.firstname}
                          {u.lastname}
                        </div>
                        <div class="text-sm text-gray-500">
                          {u.email || u.username}
                        </div>
                      </div>
                    </div>
                  </td>
                  <td class="px-6 py-4 whitespace-nowrap">
                    {#if u.enabled}
                      <span
                        class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full border bg-green-100 text-green-800"
                        >{$_("active")}</span
                      >
                    {:else}
                      <span
                        class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full border bg-red-100 text-red-800"
                        >{$_("inactive")}</span
                      >
                    {/if}
                  </td>
                  <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 gap-0.5 flex flex-wrap">
                    {#each u.groups as g}
                      <a
                        href="../groups/{g.id}"
                        class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full border bg-gray-100 text-gray-800 border-current"
                        >{g.name}</a
                      >
                    {/each}
                  </td>
                  {#if active_deletes[u.id] === true}
                    <td
                      class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium"
                    >
                      <button
                        on:click={() => {
                          active_deletes[u.id] = false;
                        }}
                        tabindex="0"
                        class="ml-4 text-indigo-600 hover:text-indigo-900 cursor-pointer"
                        >{$_("cancel-delete")}</button
                      >
                      <button
                        on:click={() => {
                          UserService.userControllerRemove(u.id, true)
                            .then((resp) => {
                              current_users = current_users.filter(
                                (obj) => obj.id !== u.id
                              );
                            })
                            .catch((err) => {});
                        }}
                        tabindex="0"
                        class="ml-4 text-red-600 hover:text-red-900 cursor-pointer"
                        >{$_("confirm-delete")}</button
                      >
                    </td>
                  {:else}
                    <td
                      class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium"
                    >
                      <a
                        href="./{u.id}"
                        class="text-indigo-600 hover:text-indigo-900"
                        >{$_("details")}</a
                      >
                      {#if store.state.jwtinfo.userdetails.permissions.includes("USER:DELETE")}
                        <button
                          on:click={() => {
                            active_deletes[u.id] = true;
                          }}
                          tabindex="0"
                          class="ml-4 text-red-600 hover:text-red-900 cursor-pointer"
                          >{$_("delete")}</button
                        >
                      {/if}
                    </td>
                  {/if}
                </tr>
              {/if}
            {/each}
          </tbody>
        </table>
      </div>
    {/if}
  {:catch error}
    <div class="text-white px-6 py-4 border-0 rounded relative mb-4 bg-red-500">
      <span class="inline-block align-middle mr-8">
        <b>{$_("general_promise_error")}</b>
        {error}
      </span>
    </div>
  {/await}
{/if}
