<script>
  import {
    GroupContactService,
    RunnerOrganizationService,
    RunnerTeamService,
  } from "@odit/lfk-client-js";
  import { getLocaleFromNavigator, _ } from "svelte-i18n";
  import Toastify from "toastify-js";
  import store from "../../store";
  import ImportRunnerModal from "../runners/ImportRunnerModal.svelte";
  import PromiseError from "../base/PromiseError.svelte";
  import ConfirmTeamDeletion from "./ConfirmTeamDeletion.svelte";
  let [teamdata, original, delete_team, orgs, contacts, modal_open] = [
    {},
    {},
    {},
    [],
    [],
    false,
  ];
  export let params;
  export let import_modal_open = false;
  $: delete_triggered = false;
  $: save_enabled = !data_changed;
  $: data_loaded = false;
  $: data_changed = JSON.stringify(teamdata) === JSON.stringify(original);
  $: sponsoring_contracts_download_open = false;
  //
  const promise = RunnerTeamService.runnerTeamControllerGetOne(
    params.teamid
  ).then((value) => {
    data_loaded = true;
    if (value.contact) {
      if (value.contact !== "null") {
        value.contact = value.contact.id;
      }
    }
    teamdata = Object.assign(teamdata, value);
    original = Object.assign(original, value);
  });
  RunnerOrganizationService.runnerOrganizationControllerGetAll().then((val) => {
    orgs = val;
  });
  GroupContactService.groupContactControllerGetAll().then((val) => {
    contacts = val;
  });
  function deleteTeam() {
    RunnerTeamService.runnerTeamControllerRemove(original.id, false)
      .then((resp) => {
        Toastify({
          text: "Organization deleted",
          duration: 500,
          backgroundColor: "linear-gradient(to right, #00b09b, #96c93d)",
        }).showToast();
        location.replace("./");
      })
      .catch((err) => {
        modal_open = true;
        delete_team = original;
      });
  }
  function submit() {
    if (data_loaded === true && save_enabled) {
      Toastify({
        text: "updating team",
        duration: 2500,
      }).showToast();
      teamdata.parentGroup = teamdata.parentGroup.id;
      let postdata = teamdata;
      postdata.contact = postdata.contact === "null" ? null : postdata.contact;
      RunnerTeamService.runnerTeamControllerPut(original.id, postdata)
        .then((resp) => {
          Object.assign(original, teamdata);
          original = teamdata;
          Object.assign(original, teamdata);
          //
          Toastify({
            text: "updated team",
            duration: 2500,
            backgroundColor: "linear-gradient(to right, #00b09b, #96c93d)",
          }).showToast();
        })
        .catch((err) => {});
    }
  }
  async function generateSponsoringContract(locale) {
    const runners = await RunnerTeamService.runnerTeamControllerGetRunners(
      teamdata.id
    );
    const toast = Toastify({
      text: $_("generating-pdf"),
      duration: -1,
    }).showToast();
    fetch(
      `https://dev.lauf-fuer-kaya.de/documents/contracts?locale=${locale}&download=true&key=${config.documentserver_key}`,
      {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify(runners),
      }
    )
      .then((response) => {
        if (response.status != "200") {
          toast.hideToast();
          Toastify({
            text: $_("pdf-generation-failed"),
            duration: 3500,
            backgroundColor:
              "linear-gradient(90deg, hsla(281, 37%, 45%, 1) 0%, hsla(1, 62%, 48%, 1) 100%)",
          }).showToast();
        } else {
          return response.blob();
        }
      })
      .then((blob) => {
        const url = window.URL.createObjectURL(blob);
        let a = document.createElement("a");
        a.href = url;
        a.download = "Sponsorings_" + teamdata.name + ".pdf";
        document.body.appendChild(a);
        a.click();
        a.remove();
        toast.hideToast();
        Toastify({
          text: $_("pdf-successfully-generated"),
          duration: 3500,
          backgroundColor: "linear-gradient(to right, #00b09b, #96c93d)",
        }).showToast();
      })
      .catch((err) => {});
  }
</script>

<ImportRunnerModal
  current_runners={[]}
  on:cancelDelete={(event) => {
    import_modal_open = false;
  }}
  passed_team={teamdata}
  passed_orgs={[]}
  passed_org={{}}
  opened_from="TeamDetail"
  bind:import_modal_open />
<ConfirmTeamDeletion bind:modal_open bind:delete_team />
{#if data_loaded}
  <section class="container p-5">
    <div class="mb-8 text-3xl font-extrabold leading-tight">
      {original.name}
      <span data-id="org_actions_${teamdata.id}">
        <div id="sponsoring:dropdown" class="relative inline-block">
          <div>
            <button
              on:click={() => {
                sponsoring_contracts_download_open = !sponsoring_contracts_download_open;
              }}
              type="button"
              class="w-full 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:ml-3 sm:w-auto sm:text-sm inline-flex"
              id="options-menu"
              aria-haspopup="true"
              aria-expanded="true">
              {$_('generate-sponsoring-contracts')}
              <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" class="-mr-1 ml-2 h-5 w-5"><path fill="none" d="M0 0h24v24H0z"/><path fill="currentColor" d="M3 19h18v2H3v-2zm10-5.83l6.07-6.07 1.42 1.41L12 17 3.52 8.52l1.4-1.42L11 13.17V2h2v11.17z"/></svg>
            </button>
          </div>
          {#if sponsoring_contracts_download_open}
            <div
              class="origin-top-right absolute right-0 mt-2 w-56 rounded-md shadow-lg bg-white ring-1 ring-black ring-opacity-5">
              <div
                class="py-1"
                role="menu"
                aria-orientation="vertical"
                aria-labelledby="options-menu">
                <span
                  class="block w-full text-left px-4 py-2 text-sm text-gray-700">{$_('select-language')}</span>
                <button
                  on:click={() => {
                    generateSponsoringContract('de');
                  }}
                  type="submit"
                  class="block w-full text-left px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 hover:text-gray-900 focus:outline-none focus:bg-gray-100 focus:text-gray-900 inline-flex"
                  role="menuitem">
                  {$_('german')}
                </button>
                <button
                  on:click={() => {
                    generateSponsoringContract('en');
                  }}
                  type="submit"
                  class="block w-full text-left px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 hover:text-gray-900 focus:outline-none focus:bg-gray-100 focus:text-gray-900 inline-flex"
                  role="menuitem">
                  {$_('english')}
                </button>
              </div>
            </div>
          {/if}
        </div>
        {#if store.state.jwtinfo.userdetails.permissions.includes('RUNNER:IMPORT')}
          <button
            on:click={() => {
              import_modal_open = true;
            }}
            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">
            {$_('import-runners')}
          </button>
        {/if}
        {#if store.state.jwtinfo.userdetails.permissions.includes('TEAM:DELETE')}
          {#if delete_triggered}
            <button
              on:click={deleteTeam}
              class="w-full justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-red-600 text-base font-medium text-white hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500 sm:ml-3 sm:w-auto sm:text-sm">{$_('confirm-delete')}</button>
            <button
              on:click={() => {
                delete_triggered = !delete_triggered;
              }}
              class="w-full justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-blue-400 text-base font-medium text-white sm:w-auto sm:text-sm">{$_('cancel')}</button>
          {/if}
          {#if !delete_triggered}
            <button
              on:click={() => {
                delete_triggered = true;
              }}
              type="button"
              class="w-full justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-red-600 text-base font-medium text-white hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500 sm:ml-3 sm:w-auto sm:text-sm">{$_('delete-team')}</button>
          {/if}
        {/if}
        {#if !delete_triggered}
          <button
            on:click={submit}
            disabled={!save_enabled}
            class:opacity-50={!save_enabled}
            type="button"
            class="w-full 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">{$_('save-changes')}</button>
        {/if}
      </span>
    </div>
    <div class="flex flex-row mb-4">
      <div class="w-full">
        <nav class="w-full flex">
          <ol class="list-none flex flex-row items-center justify-start">
            <li class="mr-2 flex items-center">
              <svg
                stroke="currentColor"
                fill="none"
                stroke-width="2"
                viewBox="0 0 24 24"
                stroke-linecap="round"
                stroke-linejoin="round"
                class="h-3 w-3 stroke-current"
                height="1em"
                width="1em"
                xmlns="http://www.w3.org/2000/svg"><path
                  d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z" />
                <polyline points="9 22 9 12 15 12 15 22" /></svg>
            </li>
            <li class="flex items-center">
              <a class="mr-2" href="/">Home</a><svg
                stroke="currentColor"
                fill="none"
                stroke-width="2"
                viewBox="0 0 24 24"
                stroke-linecap="round"
                stroke-linejoin="round"
                class="h-3 w-3 mr-2 stroke-current"
                height="1em"
                width="1em"
                xmlns="http://www.w3.org/2000/svg"><line
                  x1="5"
                  y1="12"
                  x2="19"
                  y2="12" />
                <polyline points="12 5 19 12 12 19" /></svg>
            </li>
            <li class="mr-2 flex items-center">
              <svg
                class="flex-shrink-0 w-5 h-5 mr-2"
                fill="currentColor"
                width="24"
                height="24"
                xmlns="http://www.w3.org/2000/svg"
                viewBox="0 0 640 512"><path
                  fill="currentColor"
                  d="M96 224c35.3 0 64-28.7 64-64s-28.7-64-64-64-64 28.7-64 64 28.7 64 64 64zm448 0c35.3 0 64-28.7 64-64s-28.7-64-64-64-64 28.7-64 64 28.7 64 64 64zm32 32h-64c-17.6 0-33.5 7.1-45.1 18.6 40.3 22.1 68.9 62 75.1 109.4h66c17.7 0 32-14.3 32-32v-32c0-35.3-28.7-64-64-64zm-256 0c61.9 0 112-50.1 112-112S381.9 32 320 32 208 82.1 208 144s50.1 112 112 112zm76.8 32h-8.3c-20.8 10-43.9 16-68.5 16s-47.6-6-68.5-16h-8.3C179.6 288 128 339.6 128 403.2V432c0 26.5 21.5 48 48 48h288c26.5 0 48-21.5 48-48v-28.8c0-63.6-51.6-115.2-115.2-115.2zm-223.7-13.4C161.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>
            </li>
            <li class="flex items-center">
              <a class="mr-2" href="./">Teams</a><svg
                stroke="currentColor"
                fill="none"
                stroke-width="2"
                viewBox="0 0 24 24"
                stroke-linecap="round"
                stroke-linejoin="round"
                class="h-3 w-3 mr-2 stroke-current"
                height="1em"
                width="1em"
                xmlns="http://www.w3.org/2000/svg"><line
                  x1="5"
                  y1="12"
                  x2="19"
                  y2="12" />
                <polyline points="12 5 19 12 12 19" /></svg>
            </li>
            <li class="flex items-center">
              <span class="mr-2">Team-Details #{params.teamid}</span>
            </li>
          </ol>
        </nav>
      </div>
    </div>
    <div class="text-sm w-full">
      <label for="name" class="font-medium text-gray-700">Name</label>
      <input
        autocomplete="off"
        placeholder="Name"
        type="text"
        bind:value={teamdata.name}
        name="name"
        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 class="text-sm w-full">
      <label
        for="contact"
        class="font-medium text-gray-700">{$_('contact')}</label>
      <select
        name="contact"
        bind:value={teamdata.contact}
        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">
        <option value="null">no contact</option>
        {#each contacts as c}
          <option value={c.id}>
            {c.firstname}
            {c.middlename || ''}
            {c.lastname}
          </option>
        {/each}
      </select>
    </div>
    <div class="text-sm w-full">
      <label
        for="org"
        class="font-medium text-gray-700">{$_('organization')}</label>
      <select
        name="org"
        bind:value={teamdata.parentGroup}
        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">
        {#each orgs as o}
          <option value={o.id}>{o.name}</option>
        {/each}
      </select>
    </div>
  </section>
{:else}
  {#await promise}
    {$_('team-detail-is-being-loaded')}
  {:catch error}
    <PromiseError />
  {/await}
{/if}
