<script>
  import { _, json } from "svelte-i18n";
  import Toastify from "toastify-js";
  import TracksEmptyState from "./TracksEmptyState.svelte";
  import { TrackService, UserService } from "@odit/lfk-client-js";
  const users_promise = UserService.userControllerGetAll();
  import { getlang } from "./datatable_i18n";
  import { Grid, html } from "gridjs";
  import "gridjs/dist/theme/mermaid.css";
  import { tracks as tracksstore } from "../store.js";
  import UsersEmptyState from "./UsersEmptyState.svelte";
  $: userscache = [];
  $: blocked = [];
  let table;
  let datatable;
  let datatable_inited = false;
  tracksstore.subscribe((val) => {
    userscache = val;
  });
  users_promise.then((data) => {
    console.log(data);
    tracksstore.set(data);
  });
</script>

{#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 users}
  {#if userscache.length === 0}
    <UsersEmptyState />
  {:else}
    <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>
            <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">
              Title
            </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">
              Role
            </th>
            <th scope="col" class="relative px-6 py-3">
              <span class="sr-only">Edit</span>
            </th>
          </tr>
        </thead>
        <tbody class="bg-white divide-y divide-gray-200">
          {#each users as u}
            <tr>
              <td class="px-6 py-4 whitespace-nowrap">
                <div class="flex items-center">
                  {#if u.profilePic}
                    <div class="flex-shrink-0 h-10 w-10">
                      <img
                        class="h-10 w-10 rounded-full"
                        src={u.profilePic}
                        alt="" />
                    </div>
                  {/if}
                  <div class="ml-4">
                    <div class="text-sm font-medium text-gray-900">
                      {u.firstname}
                    </div>
                    <div class="text-sm text-gray-500">
                      {u.email || u.username}
                    </div>
                  </div>
                </div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <div class="text-sm text-gray-900">
                  Regional Paradigm Technician
                </div>
                <div class="text-sm text-gray-500">Optimization</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 bg-green-100 text-green-800">Active</span>
                {:else}
                  <span
                    class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-red-100 text-red-800">Inactive</span>
                {/if}
              </td>
              <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                {#each u.groups as g}
                  <a href="../groups/{g.id}"
                    class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-gray-100 text-gray-800">{g.name}</a>
                {/each}
              </td>
              <td
                class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                <a
                  href="#"
                  class="text-indigo-600 hover:text-indigo-900">Edit</a>
                <a href="#" class="text-red-600 hover:text-red-900">Delete</a>
              </td>
            </tr>
          {/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 class="capitalize">{$_('general_promise_error')}</b>
      {error}
    </span>
  </div>
{/await}
