<template>
  <div class="flex flex-col h-fit w-full border border-primary rounded-md">
    <div class="bg-primary p-2 text-white flex flex-row justify-between items-center">
      <p>{{ communicationType.type }}</p>
      <div class="flex flex-row">
        <RouterLink
          v-if="can('update', 'settings', 'communication_type')"
          :to="{ name: 'admin-settings-communication_type-edit', params: { id: communicationType.id } }"
        >
          <PencilIcon class="w-5 h-5 p-1 box-content cursor-pointer" />
        </RouterLink>
        <div v-if="can('delete', 'settings', 'communication_type')" @click="openDeleteModal">
          <TrashIcon class="w-5 h-5 p-1 box-content cursor-pointer" />
        </div>
      </div>
    </div>
    <div class="flex flex-col p-2">
      <div class="flex flex-row gap-2">
        <p class="min-w-16">Felder:</p>
        <div class="flex flex-row gap-2 flex-wrap grow">
          <p v-for="field in communicationType.fields" :key="field" class="px-1 border border-gray-300 rounded-md">
            {{ field }}
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineComponent, defineAsyncComponent, markRaw, type PropType } from "vue";
import { mapState, mapActions } from "pinia";
import { PencilIcon, TrashIcon } from "@heroicons/vue/24/outline";
import { useAbilityStore } from "@/stores/ability";
import { useModalStore } from "@/stores/modal";
import type { CommunicationTypeViewModel } from "@/viewmodels/admin/settings/communicationType.models";
</script>

<script lang="ts">
export default defineComponent({
  props: {
    communicationType: { type: Object as PropType<CommunicationTypeViewModel>, default: {} },
  },
  computed: {
    ...mapState(useAbilityStore, ["can"]),
  },
  methods: {
    ...mapActions(useModalStore, ["openModal"]),
    openDeleteModal() {
      this.openModal(
        markRaw(
          defineAsyncComponent(
            () => import("@/components/admin/settings/communicationType/DeleteCommunicationTypeModal.vue")
          )
        ),
        this.communicationType.id
      );
    },
  },
});
</script>
