<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>{{ queryItem.title }}</p>
      <div class="flex flex-row">
        <div @click="loadUpdate">
          <EyeIcon class="w-5 h-5 p-1 box-content cursor-pointer" />
        </div>
        <div v-if="can('update', 'settings', 'query_store')" @click="loadUpdate">
          <PencilIcon class="w-5 h-5 p-1 box-content cursor-pointer" />
        </div>
        <div v-if="can('delete', 'settings', 'query_store')" @click="openDeleteModal">
          <TrashIcon class="w-5 h-5 p-1 box-content cursor-pointer" />
        </div>
      </div>
    </div>
  </div>
</template>

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

<script lang="ts">
export default defineComponent({
  props: {
    queryItem: { type: Object as PropType<QueryViewModel>, default: {} },
  },
  computed: {
    ...mapState(useAbilityStore, ["can"]),
    ...mapWritableState(useQueryBuilderStore, ["query", "activeQueryId"]),
  },
  methods: {
    ...mapActions(useModalStore, ["openModal"]),
    loadUpdate() {
      this.activeQueryId = this.queryItem.id;
      this.query = this.queryItem.query;
      this.$router.push({ name: "admin-club-query_builder" });
    },
    openDeleteModal() {
      this.openModal(
        markRaw(defineAsyncComponent(() => import("@/components/admin/settings/queryStore/DeleteQueryStoreModal.vue"))),
        this.queryItem.id
      );
    },
  },
});
</script>
