import { defineStore } from "pinia";
import { request } from "../api";

export const useInfoStore = defineStore({
  id: "info",
  state: () => {
    return {
      isLoadMoreLock: false,
      tabs: ["exhibitions", "activates", "news", "notices"],
      exhibitions: [],
      activates: [],
      news: [],
      notices: [],
      detail: {
        createTime: "",
        creatorId: "",
        creatorName: "",
        display: null,
        id: "",
        name: "",
        publishDate: "",
        richText: "",
        thumb: "",
        type: "",
        video: "",
      },
      total: 0,
      pagination: {
        pageNum: 1,
        pageSize: 20,
        searchKey: "",
        type: "",
        pages: 0,
        current: 0,
      },
    };
  },
  getters: {
    canLoadMore() {
      return this.isLoad && !this.isLast && !this.isLoadMoreLock;
    },
    isLoad() {
      return this.total > 0;
    },
    isLast() {
      return (
        this.pagination.current &&
        this.pagination.pages &&
        this.pagination.pages === this.pagination.current
      );
    },
  },
  actions: {
    async getExhibitions(page) {
      this.pagination.type = "exhibition";
      this.pagination.pageNum = page || 1;
      const { data, status } = await request.post("/show/news/pageList", {
        ...this.pagination,
      });
      if (data.code === 0) {
        const { records, total, current, pages } = data.data;
        const lists = this.exhibitions.concat(records);
        this.exhibitions = lists;
        this.total = total;
        this.pagination.current = current;
        this.pagination.pages = pages;
        this.isLoadMoreLock = false;
        return Promise.resolve(0);
      } else {
        this.resetPage();
        this.exhibitions = [];
        return Promise.resolve(1);
      }
    },
    async getActivates(page) {
      this.pagination.pageNum = page || 1;
      this.pagination.type = "activity";
      const { data, status } = await request.post("/show/news/pageList", {
        ...this.pagination,
      });
      if (data.code === 0) {
        const { records, total, current, pages } = data.data;
        const lists = this.activates.concat(records);
        this.activates = lists;
        this.total = total;
        this.pagination.current = current;
        this.pagination.pages = pages;
        return Promise.resolve(0);
      } else {
        this.resetPage();
        this.activates = [];
        return Promise.resolve(1);
      }
    },
    async getNews(page) {
      this.pagination.type = "news";
      this.pagination.pageNum = page || 1;
      const { data, status } = await request.post("/show/news/pageList", {
        ...this.pagination,
      });
      if (data.code === 0) {
        const { records, total, current, pages } = data.data;
        const lists = this.news.concat(records);
        this.news = lists;
        this.total = total;
        this.pagination.current = current;
        this.pagination.pages = pages;
        return Promise.resolve(0);
      } else {
        this.resetPage();
        this.news = [];
        return Promise.resolve(1);
      }
    },
    async getNotices(page) {
      this.pagination.type = "notice";
      this.pagination.pageNum = page || 1;
      const { data, status } = await request.post("/show/news/pageList", {
        ...this.pagination,
      });
      if (data.code === 0) {
        const { records, total, current, pages } = data.data;
        const lists = this.notices.concat(records);
        this.notices = lists;
        this.total = total;
        this.pagination.current = current;
        this.pagination.pages = pages;
        return Promise.resolve(0);
      } else {
        this.resetPage();
        this.notices = [];
        return Promise.resolve(1);
      }
    },

    async getDetail(id) {
      const { data, status } = await request.get(`/show/news/detail/${id}`);
      if (data.code === 0) {
        this.detail = data.data;
      } else {
        this.detail = null;
      }
    },
    resetPage() {
      this.total = 0;
      this.pagination = {
        pageNum: 1,
        pageSize: 20,
        searchKey: "",
        type: "",
        current: null,
        pages: null,
      };
    },
    loadMore(currentTab) {
      if (!this.isLoadMoreLock && !this.isLast) {
        console.log("loadMore", currentTab);
        this.isLoadMoreLock = true;
        const page =
          this.pagination.pageNum < this.pagination.pages
            ? this.pagination.pageNum + 1
            : this.pagination.pages;

        switch (currentTab) {
          case "exhibitions":
            this.getExhibitions(page);
            break;
          case "activates":
            this.getActivates(page);
            break;
          case "news":
            this.getNews(page);
            break;
          case "notices":
            this.getNotices(page);
            break;
        }
      }
    },
    async switchTab(tab) {
      this.resetPage();
      switch (tab) {
        case "exhibitions":
          this.exhibitions = [];
          return this.getExhibitions();

        case "activates":
          this.activates = [];
          return this.getActivates();

        case "news":
          this.news = [];
          return this.getNews();

        case "notices":
          this.notices = [];
          return this.getNotices();
      }
    },
  },
});
