<template>
  <div class="w-full md:max-w-md">
    <div class="flex flex-col items-center">
      <p class="text-xl font-medium">Abfrage aktualisieren</p>
    </div>
    <br />
    <div class="flex flex-col gap-4 py-2">
      <p>Abfrage mit dem Titel: "{{ activeQuery?.title }}" überschreiben</p>
      <div class="flex flex-row gap-2">
        <button primary :disabled="status == 'loading' || status?.status == 'success'" @click="changeToCreate">
          neu speichern
        </button>
        <button primary :disabled="status == 'loading' || status?.status == 'success'" @click="triggerUpdate">
          überschreiben
        </button>
        <Spinner v-if="status == 'loading'" class="my-auto" />
        <SuccessCheckmark v-else-if="status?.status == 'success'" />
        <FailureXMark v-else-if="status?.status == 'failed'" />
      </div>
    </div>

    <div class="flex flex-row justify-end">
      <div class="flex flex-row gap-4 py-2">
        <button primary-outline @click="closeModal" :disabled="status == 'loading' || status?.status == 'success'">
          abbrechen
        </button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineAsyncComponent, defineComponent, markRaw } from "vue";
import { mapState, mapActions } from "pinia";
import { useModalStore } from "@/stores/modal";
import Spinner from "@/components/Spinner.vue";
import SuccessCheckmark from "@/components/SuccessCheckmark.vue";
import FailureXMark from "@/components/FailureXMark.vue";
import { useQueryStoreStore } from "@/stores/admin/settings/queryStore";
import type { CreateQueryViewModel, UpdateQueryViewModel } from "@/viewmodels/admin/settings/query.models";
import { useQueryBuilderStore } from "@/stores/admin/club/queryBuilder";
</script>

<script lang="ts">
export default defineComponent({
  data() {
    return {
      status: null as null | "loading" | { status: "success" | "failed"; reason?: string },
      timeout: undefined as any,
    };
  },
  computed: {
    ...mapState(useModalStore, ["data"]),
    ...mapState(useQueryBuilderStore, ["query"]),
    ...mapState(useQueryStoreStore, ["queries"]),
    activeQuery() {
      return this.queries.find((t) => t.id == this.data);
    },
  },
  beforeUnmount() {
    try {
      clearTimeout(this.timeout);
    } catch (error) {}
  },
  methods: {
    ...mapActions(useModalStore, ["closeModal", "openModal"]),
    ...mapActions(useQueryStoreStore, ["updateActiveQueryStore"]),
    changeToCreate() {
      this.openModal(
        markRaw(defineAsyncComponent(() => import("@/components/admin/settings/queryStore/CreateQueryStoreModal.vue")))
      );
    },
    triggerUpdate() {
      let updateQuery: UpdateQueryViewModel = {
        id: this.data,
        query: this.query ?? "",
      };
      this.status = "loading";
      this.updateActiveQueryStore(updateQuery)
        .then(() => {
          this.status = { status: "success" };
          this.timeout = setTimeout(() => {
            this.closeModal();
          }, 1500);
        })
        .catch(() => {
          this.status = { status: "failed" };
        });
    },
  },
});
</script>
