<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">Query Builder</h1>
      </div>
    </template>
    <template #diffMain>
      <div class="flex flex-col w-full h-full gap-2 justify-center px-7">
        <BuilderHost v-model="query" />
        <p>Ergebnisse:</p>
        <Pagination
          :items="data"
          :totalCount="totalLength"
          :indicateLoading="loadingData == 'loading'"
          @load-data="(offset, count) => sendQuery(offset, count)"
        >
          <template #pageRow="{ row }: { row: { id: number; [key: string]: any } }">
            <p>{{ row }}</p>
          </template>
        </Pagination>
      </div>
    </template>
  </MainTemplate>
</template>

<script setup lang="ts">
import { defineComponent } from "vue";
import { mapActions, mapState } from "pinia";
import MainTemplate from "@/templates/Main.vue";
import Pagination from "@/components/Pagination.vue";
import { useQueryBuilderStore } from "@/stores/admin/queryBuilder";
import BuilderHost from "../../../../components/queryBuilder/BuilderHost.vue";
import type { DynamicQueryStructure } from "@/types/dynamicQueries";
</script>

<script lang="ts">
export default defineComponent({
  data() {
    return {
      query: undefined as undefined | DynamicQueryStructure,
    };
  },
  computed: {
    ...mapState(useQueryBuilderStore, ["loading", "loadingData", "tableMetas", "data", "totalLength"]),
  },
  mounted() {
    this.fetchTableMetas();
  },
  methods: {
    ...mapActions(useQueryBuilderStore, ["fetchTableMetas", "sendQuery"]),
  },
});
</script>
