<template>
  <MainTemplate>
    <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">Templates</h1>
        <RouterLink :to="{ name: 'admin-settings-template-info' }">
          <InformationCircleIcon class="text-gray-500 h-5 w-5" />
        </RouterLink>
      </div>
    </template>
    <template #diffMain>
      <div class="flex flex-col gap-4 grow pl-7">
        <div class="flex flex-col gap-2 grow overflow-y-scroll pr-7">
          <TemplateListItem v-for="template in templates" :key="template.id" :template="template" />
        </div>
        <div class="flex flex-row gap-4">
          <button v-if="can('create', 'settings', 'template')" primary class="!w-fit" @click="openCreateModal">
            Template erstellen
          </button>
        </div>
      </div>
    </template>
  </MainTemplate>
</template>

<script setup lang="ts">
import { defineAsyncComponent, defineComponent, markRaw } from "vue";
import { mapState, mapActions } from "pinia";
import MainTemplate from "@/templates/Main.vue";
import TemplateListItem from "@/components/admin/settings/template/TemplateListItem.vue";
import { useTemplateStore } from "@/stores/admin/template";
import { useAbilityStore } from "@/stores/ability";
import { useModalStore } from "@/stores/modal";
import { RouterLink } from "vue-router";
import { InformationCircleIcon } from "@heroicons/vue/24/outline";
</script>

<script lang="ts">
export default defineComponent({
  computed: {
    ...mapState(useTemplateStore, ["templates"]),
    ...mapState(useAbilityStore, ["can"]),
  },
  mounted() {
    this.fetchTemplates();
  },
  methods: {
    ...mapActions(useTemplateStore, ["fetchTemplates"]),
    ...mapActions(useModalStore, ["openModal"]),
    openCreateModal() {
      this.openModal(
        markRaw(defineAsyncComponent(() => import("@/components/admin/settings/template/CreateTemplateModal.vue")))
      );
    },
  },
});
</script>
