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

export const useInfoStore = defineStore({
  id: "info",
  state: () => {
    return {
      exhibitions: [],
      activates: [],
      news: [],
      notices: [],
      detail: {
        createTime: "",
        creatorId: "",
        creatorName: "",
        display: null,
        id: "",
        name: "",
        publishDate: "",
        richText: "",
        thumb: "",
        type: "",
        video: "",
      },
      pagination: {
        pageNum: 1,
        pageSize: 20,
        searchKey: "",
        type: "",
        total: 0,
      },
    };
  },
  getters: {},
  actions: {
    async getExhibition(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, page } = data.data;
        this.exhibition = records;
        this.pagination.total = total;
        this.pagination.current = current;
        this.pagination.page = page;
      }
    },
    async getActivity(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, page } = data.data;
        this.activates = records;
        this.pagination.total = total;
        this.pagination.current = current;
        this.pagination.page = page;
      }
    },
    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, page } = data.data;
        this.news = records;
        this.pagination.total = total;
        this.pagination.current = current;
        this.pagination.page = page;
      }
    },
    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, page } = data.data;
        this.notices = records;
        this.pagination.total = total;
        this.pagination.current = current;
        this.pagination.page = page;
      }
    },

    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;
      }
    },
  },
});
