<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 gap-2 justify-between items-center">
      <EnvelopeIcon class="h-5 w-5 pr-1 box-content" v-if="communication.isNewsletterMain" />
      <p class="grow">{{ communication.type.type }} {{ communication.preferred ? "(bevorzugt)" : "" }}</p>
      <PencilIcon v-if="can('update', 'club', 'member')" class="w-5 h-5 cursor-pointer" @click="openEditModal" />
      <TrashIcon v-if="can('delete', 'club', 'member')" class="w-5 h-5 cursor-pointer" @click="openDeleteModal" />
    </div>
    <div class="p-2">
      <p v-for="field in communication.type.fields" :key="field">{{ field }}: {{ communication[field] || "--" }}</p>
    </div>
  </div>
</template>

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

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