<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: "کاوش با هوش مصنوعی در احادیث اسلامی" },
    {
      name: "msapplication-TileImage",
      content: "/img/haditha/fav-icons/ms-icon-144x144.png",
    },
    { name: "theme-color", content: "#ffffff" },
  ],
  bodyAttrs: {
    class: import.meta.env.VITE_HADITH_SYSTEM,
  },
  link: [
    {
      rel: "icon",
      type: "image/x-icon",
      href: "/img/haditha/fav-icons/favicon.ico",
    },
    { rel: "manifest", href: "/img/haditha/fav-icons/manifest.json" },
    // rel: icon
    {
      rel: "icon",
      type: "image/png",
      sizes: "16x16",
      href: "/img/haditha/fav-icons/favicon-16x16.png",
    },
    {
      rel: "icon",
      type: "image/png",
      sizes: "32x32",
      href: "/img/haditha/fav-icons/favicon-32x32.png",
    },
    {
      rel: "icon",
      type: "image/png",
      sizes: "96x96",
      href: "/img/haditha/fav-icons/favicon-96x96.png",
    },
    {
      rel: "icon",
      sizes: "192x192",
      type: "image/png",
      href: "/img/haditha/fav-icons/android-icon-192x192.png",
    },
    // rel: apple
    {
      rel: "apple-touch-icon",
      sizes: "57x57",
      href: "/img/haditha/fav-icons/apple-icon-57x57.png",
    },
    {
      rel: "apple-touch-icon",
      sizes: "60x60",
      href: "/img/haditha/fav-icons/android-icon-60x60.png",
    },
    {
      rel: "apple-touch-icon",
      sizes: "72x72",
      href: "/img/haditha/fav-icons/android-icon-72x72.png",
    },
    {
      rel: "apple-touch-icon",
      sizes: "76x76",
      href: "/img/haditha/fav-icons/android-icon-76x76.png",
    },
    {
      rel: "apple-touch-icon",
      sizes: "114x114",
      href: "/img/haditha/fav-icons/android-icon-114x114.png",
    },
    {
      rel: "apple-touch-icon",
      sizes: "120x120",
      href: "/img/haditha/fav-icons/android-icon-120x120.png",
    },
    {
      rel: "apple-touch-icon",
      sizes: "144x144",
      href: "/img/haditha/fav-icons/android-icon-144x144.png",
    },
    {
      rel: "apple-touch-icon",
      sizes: "152x152",
      href: "/img/haditha/fav-icons/android-icon-152x152.png",
    },
    {
      rel: "apple-touch-icon",
      sizes: "180x180",
      href: "/img/haditha/fav-icons/android-icon-180x180.png",
    },
  ],
});

// #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: [],
  pagination: {
    page: 1,
    pages: 1,
    offset: 0,
    limit: 10,
  },
});
// #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("@data_type", dataType);
  url = url.replace("@time_key", "all");
  url = url.replace("@source", "main");
  url = url.replace("@offset", state.pagination.offset);
  url = url.replace("@limit", state.pagination.limit);
  url = url.replace("@q", "none");

  return await httpService
    .getRequest(url)
    .then((res) => {
      state.list = res.hits.hits;
      state.totalCounts = res.hits.total.value;
      loading.value = false;
      // 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
            v-if="state.list?.length"
            class="search-box-container pb-0 flex justify-center"
          >
            <div class="search-list-contianer">
              <div class="total">
                <span>{{ total }}</span>
                نتیجه
              </div>

              <div ref="el" class="search-list firefox-scrollbar hadithaFavorites">
                <search-list
                  no-data-text="هنوز چیزی ذخیره نکرده‌اید!"
                  no-data-icon="/img/haditha/save.png"
                  :list="state.list"
                  :total="state.totalCounts"
                ></search-list>
              </div>
            </div>
          </div>
          <no-data
            v-else
            class="h-full w-full flex flex-col justify-center items-center"
          >
            <img fit="auto" quality="80" src="/img/haditha/save.png" />
            <p class="no-data-text">هنوز چیزی ذخیره نکرده‌اید!</p>
          </no-data>
        </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;
  }
}

.search-list-contianer {
  /*max-width: 41em; 656px*/
  max-width: 75em; /*1200px*/
  width: 100%;
  margin: 0 1em;

  .total {
    padding: 0.5em 1.8em;

    font-family: var(--font);
    font-weight: 400;
    font-size: 0.68rem; /*11px*/
    line-height: 1rem;
    letter-spacing: 0%;
    text-align: right;
    color: #b4c2cf;
  }
  .search-list {
    padding: 1em 1.3em;
    height: calc(100dvh - 16em);
    overflow-y: auto;

    &.hadithaFavorites {
      height: calc(100dvh - 8em);
    }

    &:not(:last-child) {
      border-bottom: 0.3px solid #d9d9d9;
    }
  }
}
</style>