<template>
  <div class="main">
    <div class="content">
      <sub-header />
      <div class="left">
        <n-tabs type="line" pane-class="tab-content" v-model:value="currentTab">
          <template #prefix>
            <span class="meta-title">
              <img src="@/assets/subtitle_3.png" />
            </span>
          </template>
          <template #suffix>
            <span class="input-container">
              <n-input-group round>
                <n-input
                  round
                  clearable
                  v-model:value="inputValue"
                  placeholder="请输入要搜索的藏品"
                  @clear="collectStore.clearSearch"
                />
                <n-button round type="primary" @click="handleSearch">
                  搜索
                </n-button>
              </n-input-group>
            </span>
          </template>
          <n-tab-pane name="all" tab="全部" v-infinite-scroll="onLoadMore">
            <n-grid :x-gap="XGap" :y-gap="YGap" :cols="3" class="tab-grid">
              <template v-for="item in collectLists">
                <n-gi>
                  <collect-box
                    v-if="Number(item.display) === 1"
                    :id="item.id"
                    :title="item.name"
                    :cover="domain + item.thumb"
                    :time="item.publishDate"
                  />
                </n-gi>
              </template>
            </n-grid>
            <empty :show="collectLists.length === 0" :height="500" />
          </n-tab-pane>
          <n-tab-pane name="1" tab="一级" v-infinite-scroll="onLoadMore">
            <n-grid :x-gap="XGap" :y-gap="YGap" :cols="3" class="tab-grid">
              <template v-for="item in collectLists">
                <n-gi>
                  <collect-box
                    v-if="Number(item.display) === 1"
                    :id="item.id"
                    :title="item.name"
                    :cover="domain + item.thumb"
                    :time="item.publishDate"
                  />
                </n-gi>
              </template>
            </n-grid>
            <empty :show="collectLists.length === 0" :height="500" />
          </n-tab-pane>
          <n-tab-pane name="2" tab="二级" v-infinite-scroll="onLoadMore">
            <n-grid :x-gap="XGap" :y-gap="YGap" :cols="3" class="tab-grid">
              <template v-for="item in collectLists">
                <n-gi>
                  <collect-box
                    v-if="Number(item.display) === 1"
                    :id="item.id"
                    :title="item.name"
                    :cover="domain + item.thumb"
                    :time="item.publishDate"
                  />
                </n-gi>
              </template>
            </n-grid>
            <empty :show="collectLists.length === 0" :height="500" />
          </n-tab-pane>
          <n-tab-pane name="3" tab="三级" v-infinite-scroll="onLoadMore">
            <n-grid :x-gap="XGap" :y-gap="YGap" :cols="3" class="tab-grid">
              <template v-for="item in collectLists">
                <n-gi>
                  <collect-box
                    v-if="Number(item.display) === 1"
                    :id="item.id"
                    :title="item.name"
                    :cover="domain + item.thumb"
                    :time="item.publishDate"
                  />
                </n-gi>
              </template>
            </n-grid>
            <empty :show="collectLists.length === 0" :height="500" />
          </n-tab-pane>
        </n-tabs>
      </div>
      <side-menu />
    </div>
  </div>
</template>

<script setup>
import { computed, ref, unref, watch } from "vue";
import { vInfiniteScroll } from "@vueuse/components";
import { useLoadingBar } from "naive-ui";
// import { useFullscreen } from "@vueuse/core";
import collectBox from "../components/collectBox";
import subHeader from "../components/subHeader";
import sideMenu from "../components/sideMenu";
import empty from "../components/empty.vue";
import { useCollectStore } from "../store/collect";
import { useThrottleFn } from "@vueuse/core";

const loadingBar = useLoadingBar();
const collectStore = useCollectStore();
const domain = ref(import.meta.env.VITE_DOMAIN_URL);
const collectLists = computed(() => collectStore.lists);
const XGap = ref(50);
const YGap = ref(50);
const inputValue = ref("");
const currentTab = ref("all");

watch(
  currentTab,
  async (val) => {
    loadingBar.start();
    const level = isNaN(Number(val)) ? 0 : Number(val);
    await collectStore.getCollectList(1, level);
    loadingBar.finish();
  },
  {
    immediate: true,
  }
);
const handleSearch = () => {
  if (unref(inputValue).length > 0) {
    console.log("search");
    const level = isNaN(Number(currentTab.value))
      ? 0
      : Number(currentTab.value);
    collectStore.search(unref(inputValue), level);
  } else {
    collectStore.fetch();
  }
};

const onLoadMore = useThrottleFn(async () => {
  if (collectStore.isLoad) {
    console.log("canLoadMore", collectStore.canLoadMore);
    collectStore.canLoadMore && loadingBar.start();
    const level = isNaN(Number(currentTab.value))
      ? 0
      : Number(currentTab.value);
    const code = await collectStore.loadMore(level);
    console.log("code", code);
    loadingBar.finish();
  }
}, 1000);
</script>

<style lang="scss" scoped>
:deep(.n-tabs-nav__suffix) {
  min-width: 100px;
  flex: 1;
  .input-container {
    width: 100%;
    max-width: 515px;
    padding: 0 20px;
  }
}
</style>
