<template>
  <MainTemplate>
    <template #headerInsert>
      <RouterLink to="../" class="text-primary">zurück zur Liste</RouterLink>
    </template>
    <template #topBar>
      <div class="flex flex-row items-center justify-between pt-5 pb-3 px-7">
        <h1 class="font-bold text-xl h-8">Nutzer {{ user?.username }} - Rollen bearbeiten</h1>
      </div>
    </template>
    <template #main>
      <Spinner v-if="loadingSingle == 'loading'" class="mx-auto" />
      <p v-else-if="loadingSingle == 'failed'">laden fehlgeschlagen</p>
      <div v-else class="flex flex-col grow gap-4">
        <div class="flex flex-col gap-2 grow">
          <p class="text-xl font-semibold">zugewiesene Rollen</p>
          <div class="flex flex-row flex-wrap gap-2 h-1/2 overflow-y-auto">
            <div
              v-for="role in assignedRoles"
              :key="role.id"
              class="flex flex-row gap-2 items-center px-2 p-1 border border-gray-300 rounded-md h-fit cursor-pointer"
              @click="removeAssigned(role.id)"
            >
              <p>{{ role.role }}</p>
              <XMarkIcon class="w-5 h-5" />
            </div>
          </div>
          <p class="text-xl font-semibold">verfügbare Rollen</p>
          <div class="flex flex-row flex-wrap gap-2 h-1/2 overflow-y-auto">
            <div
              v-for="role in availableRoles"
              :key="role.id"
              class="flex flex-row gap-2 items-center px-2 p-1 border border-gray-300 rounded-md h-fit cursor-pointer"
              @click="addAvailable(role.id)"
            >
              <p>{{ role.role }}</p>
              <PlusIcon class="w-5 h-5" />
            </div>
          </div>
        </div>
        <div class="flex flex-row justify-end gap-2">
          <RouterLink button primary-outline to="../" class="!w-fit">abbrechen</RouterLink>
          <button primary class="!w-fit" :disabled="updateStatus == 'loading'" @click="triggerRolesUpdate">
            speichern
          </button>
          <Spinner v-if="updateStatus == 'loading'" class="my-auto" />
          <SuccessCheckmark v-else-if="updateStatus?.status == 'success'" />
          <FailureXMark v-else-if="updateStatus?.status == 'failed'" />
        </div>
      </div>
    </template>
  </MainTemplate>
</template>

<script setup lang="ts">
import { defineComponent } from "vue";
import { RouterLink } from "vue-router";
import { mapState, mapActions } from "pinia";
import { useUserStore } from "@/stores/admin/user";
import { useRoleStore } from "@/stores/admin/role";
import type { PermissionObject } from "@/types/permissionTypes";
import MainTemplate from "@/templates/Main.vue";
import Permission from "@/components/admin/Permission.vue";
import Spinner from "@/components/Spinner.vue";
import SuccessCheckmark from "@/components/SuccessCheckmark.vue";
import FailureXMark from "@/components/FailureXMark.vue";
import { XMarkIcon, PlusIcon } from "@heroicons/vue/24/outline";
</script>

<script lang="ts">
export default defineComponent({
  props: {
    id: String,
  },
  watch: {
    user() {
      this.assigned = this.user?.roles.map((r) => r.id) ?? [];
    },
  },
  data() {
    return {
      assigned: [] as Array<number>,
    };
  },
  computed: {
    ...mapState(useUserStore, ["user", "loadingSingle", "updateStatus"]),
    ...mapState(useRoleStore, ["roles"]),
    assignedRoles() {
      return this.roles.filter((r) => this.assigned.includes(r.id));
    },
    availableRoles() {
      return this.roles.filter((r) => !this.assigned.includes(r.id));
    },
  },
  mounted() {
    this.resetStatus();
    this.fetchUserById(parseInt(this.id ?? ""));
    this.fetchRoles();
  },
  methods: {
    ...mapActions(useUserStore, ["fetchUserById", "updateActiveUserRoles", "resetStatus"]),
    ...mapActions(useRoleStore, ["fetchRoles"]),
    addAvailable(id: number) {
      this.assigned.push(id);
    },
    removeAssigned(id: number) {
      this.assigned = this.assigned.filter((roleId) => roleId !== id);
    },
    triggerRolesUpdate() {
      this.updateActiveUserRoles(this.assigned);
    },
  },
});
</script>
