<script setup>
import hadithaApi from "@haditha/apis/hadithaApi";

definePageMeta({
  layout: false,
  name: "hadithaFavorites",
});
useHead({
  name: "hadithaFavorites",
  title: `${import.meta.env.VITE_HADITH_PAGE_TITLE} | ذخیره ها`,
  meta: [
    { name: "description", content: "کاوش با هوش مصنوعی در احادیث اسلامی" },
  ],
  bodyAttrs: {
    class: import.meta.env.VITE_HADITH_SYSTEM,
  },
});

// #region refs
const loading = ref(false);
const httpService = useNuxtApp()["$http"];

// #endregion refs

// #region reactive
const state = reactive({
  // list: new Array(5).fill(0),
  list: [],
  counts: [],
  totalCounts: [],
});
// #endregion reactive

// #region methods

const getCategories = async (dataType = "bookmark") => {
  if (loading.value) return;

  loading.value = true;

  let url = repoUrl() + hadithaApi.favorite.getList;
  url = url.replace("@field_collapsed", "book_title");
  url = url.replace("@offset", 0);
  url = url.replace("@limit", 20);
  url = url.replace("@q", "none");

  return await httpService
    .getRequest(url)
    .then((res) => {
      state.list = res.data;
      getCounts();
    })
    .catch((err) => {
      console.info(err);
      loading.value = false;
    });
};

const getCounts = async () => {
  let url = repoUrl() + hadithaApi.favorite.getCounts;
  url = url.replace("@data_type", "bookmark");

  await httpService
    .getRequest(url)
    .then((res) => {
      state.totalCounts = res.hits.total.value;
      state.counts = res.aggregations.result.buckets;
      loading.value = false;
    })
    .catch((err) => {
      console.info(err);
    })
    .finally(() => {
      loading.value = false;
    });
};
// #endregion methods

// #region hooks
onMounted(() => {
  getCategories();
});
// #endregion methods

const HadithaLayout = defineAsyncComponent(() =>
  import("@haditha/layouts/HadithaLayout.vue")
);
const NavigationMenu = defineAsyncComponent(() =>
  import("@haditha/components/haditha/NavigationMenu.vue")
);

const SearchList = defineAsyncComponent(() =>
  import("@haditha/components/haditha/search-page/SearchList.vue")
);
</script>

<template>
  <HadithaLayout>
    <div class="h-full flex flex-col justify-center">
      <div class="bg-container h-full">
        <navigation-menu></navigation-menu>

        <div class="text-logo">
          <div class="search-box-container pb-0 flex justify-center">
            <search-list
              no-data-text="هنوز چیزی ذخیره نکرده‌اید!"
              no-data-icon="/img/haditha/save.png"
              :list="state.list"
            ></search-list>
          </div>
        </div>
      </div>
    </div>
  </HadithaLayout>
</template>

<style scoped>
.bg-container {
  min-height: 100%;
  /* height: 100dvh; */
  background-size: cover;
  background-repeat: no-repeat;
  background: #f7fffd;
}

.text-logo {
  padding-top: 4.5em;
  position: relative;
}

.search-box-container {
  padding-top: 0.7em;
  padding-bottom: 4em; /*64px */

  &.pb-0 {
    padding-bottom: 0 !important;
  }
}
</style>
