<template>
  <div class="flex flex-col border border-gray-300 rounded-md select-none">
    <div class="flex flex-row gap-2 border-b border-gray-300 p-2">
      <div
        class="p-1 border border-gray-400 bg-green-200 rounded-md"
        title="Abfrage starten"
        @click="$emit('query:run')"
      >
        <PlayIcon class="text-gray-500 h-6 w-6 cursor-pointer" />
      </div>
      <div
        class="p-1 border border-gray-400 bg-gray-100 rounded-md"
        title="Abfrage speichern"
        @click="$emit('query:save')"
      >
        <InboxArrowDownIcon class="text-gray-500 h-6 w-6 cursor-pointer" />
      </div>
      <div
        class="p-1 border border-gray-400 bg-gray-100 rounded-md"
        title="Ergebnisse exportieren"
        @click="$emit('results:export')"
      >
        <ArchiveBoxArrowDownIcon class="text-gray-500 h-6 w-6 cursor-pointer" />
      </div>
      <div
        class="p-1 border border-gray-400 bg-red-200 rounded-md"
        title="Ergebnisse leeren"
        @click="$emit('results:clear')"
      >
        <NoSymbolIcon class="text-gray-500 h-6 w-6 cursor-pointer" />
      </div>
      <div class="p-1 border border-gray-400 bg-red-200 rounded-md" title="Anfrage löschen" @click="clearQuery">
        <TrashIcon class="text-gray-500 h-6 w-6 cursor-pointer" />
      </div>
      <div class="grow"></div>
      <div class="flex flex-row overflow-hidden border border-gray-400 rounded-md">
        <div
          class="p-1"
          :class="queryMode == 'structure' ? 'bg-gray-200' : ''"
          title="Schema-Struktur"
          @click="queryMode = 'structure'"
        >
          <SparklesIcon class="text-gray-500 h-6 w-6 cursor-pointer" />
        </div>
        <div
          class="p-1"
          :class="queryMode == 'builder' ? 'bg-gray-200' : ''"
          title="Visual Builder"
          @click="queryMode = 'builder'"
        >
          <RectangleGroupIcon class="text-gray-500 h-6 w-6 cursor-pointer" />
        </div>
        <div
          class="p-1"
          :class="queryMode == 'editor' ? 'bg-gray-200' : ''"
          title="SQL Editor"
          @click="queryMode = 'editor'"
        >
          <CommandLineIcon class="text-gray-500 h-6 w-6 cursor-pointer" />
        </div>
      </div>
    </div>
    <div class="p-2 h-44 md:h-60 w-full overflow-y-auto">
      <div v-if="queryMode == 'structure'">
        <img src="/administration-db.png" class="h-full w-full cursor-pointer" @click="showStructure" />
      </div>
      <textarea v-else-if="typeof value == 'string'" v-model="value" placeholder="SQL Query" class="h-full w-full" />
      <Table v-else v-model="value" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineAsyncComponent, defineComponent, markRaw, type PropType } from "vue";
import { mapActions, mapState } from "pinia";
import type { DynamicQueryStructure } from "../../types/dynamicQueries";
import {
  ArchiveBoxArrowDownIcon,
  CommandLineIcon,
  InboxArrowDownIcon,
  NoSymbolIcon,
  PlayIcon,
  RectangleGroupIcon,
  TrashIcon,
  SparklesIcon,
} from "@heroicons/vue/24/outline";
import { useQueryBuilderStore } from "../../stores/admin/queryBuilder";
import { useModalStore } from "../../stores/modal";
import Table from "./Table.vue";
</script>

<script lang="ts">
export default defineComponent({
  props: {
    modelValue: {
      type: [Object, String] as PropType<DynamicQueryStructure | string>,
      default: {
        select: "*",
        table: "",
        where: [],
        join: [],
        orderBy: [],
      },
    },
  },
  emits: ["update:model-value", "query:run", "query:save", "results:export", "results:clear"],
  watch: {
    queryMode() {
      if (this.queryMode == "builder") {
        this.value = {
          select: "*",
          table: "",
          where: [],
          join: [],
          orderBy: [],
        };
      } else {
        this.value = "";
      }
    },
  },
  data() {
    return {
      queryMode: "builder" as "builder" | "editor" | "structure",
    };
  },
  computed: {
    ...mapState(useQueryBuilderStore, ["tableMetas", "loading"]),
    value: {
      get() {
        return this.modelValue;
      },
      set(val: DynamicQueryStructure) {
        this.$emit("update:model-value", val);
      },
    },
  },
  mounted() {
    this.fetchTableMetas();
  },
  methods: {
    ...mapActions(useModalStore, ["openModal"]),
    ...mapActions(useQueryBuilderStore, ["fetchTableMetas"]),
    clearQuery() {
      this.$emit("update:model-value", undefined);
      if (this.queryMode == "builder") {
        this.value = {
          select: "*",
          table: "",
          where: [],
          join: [],
          orderBy: [],
        };
      } else {
        this.value = "";
      }
    },
    showStructure() {
      this.openModal(markRaw(defineAsyncComponent(() => import("@/components/queryBuilder/StructureModal.vue"))));
    },
  },
});
</script>
