import { defineStore } from "pinia";
import { request } from "../api";
const domain = import.meta.env.VITE_DOMAIN_URL;

export const useCollectStore = defineStore({
  id: "collect",
  state: () => {
    return {
      isLoadMoreLock: false,
      lists: [],
      entity: {},
      files: [],
      currentLevel: null,
      total: 0,
      pagination: {
        dictLevel: "",
        endTime: "",
        pageNum: 0,
        pages: null,
        pageSize: 20,
        searchKey: "",
        startTime: "",
      },
    };
  },
  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
      );
    },
    model() {
      const model = this.files.find((item) => item.type === "model");
      if (model) {
        // debugger
        return model.filePath;
      } else {
        return "";
      }
    },
    video() {
      const video = this.files.find((item) => item.type === "video");
      if (video) {
        const url = domain + video.filePath;
        return url;
      } else {
        return "";
      }
    },
    audio() {
      const audio = this.files.find((item) => item.type === "audio");
      if (audio) {
        const url = domain + audio.filePath;
        return url;
      } else {
        return "";
      }
    },
    gallery() {
      const gallery = this.files
        .filter((item) => item.type === "img")
        .map((item) => {
          const url = domain + item.filePath;
          return url;
        });
      if (gallery) {
        return gallery;
      } else {
        return [];
      }
    },
    firstTab() {
      if (this.entity.fileType) {
        if (this.entity.fileType.includes(",")) {
          return String(this.entity.fileType).split(",")[0];
        } else {
          return this.entity.fileType;
        }
      } else {
        return "model";
      }
    },
  },
  actions: {
    convertLevel(dictLevel) {
      switch (dictLevel) {
        case 1:
          return "一级";
          break;
        case 2:
          return "二级";
          break;
        case 3:
          return "三级";
          break;
        default:
          return "";
          break;
      }
    },
    async fetch() {
      const { data, status } = await request.post("/show/goods/pageList", {
        ...this.pagination,
        dictLevel: this.convertLevel(this.pagination.dictLevel),
      });
      if (data.code === 0) {
        if (this.pagination.pageNum === 1) {
          this.lists = [];
        }
        const { records, total, current, pages } = data.data;
        const templists = this.lists.concat(records);
        this.lists = templists;
        this.total = total;
        this.pagination.current = current;
        this.pagination.pages = pages;
        this.isLoadMoreLock = false;
        return Promise.resolve(0);
      } else {
        this.reset();
        return Promise.resolve(1);
      }
    },
    async getCollectList(page, level) {
      this.pagination.pageNum = page || 1;
      this.pagination.dictLevel = level === 0 ? "" : level;
      this.currentLevel = level;
      return await this.fetch();
    },

    async getDetail(id) {
      const { data, status } = await request.get(`show/goods/detail/${id}`);
      if (data.code === 0) {
        const { entity, file } = data.data;
        this.entity = entity;
        this.files = file;
      } else {
        this.entity = {};
        this.files = [];
      }
    },
    async search(searchKey, level) {
      this.pagination.searchKey = searchKey;
      this.pagination.pageNum = 1;
      this.pagination.dictLevel = level === 0 ? "" : level;
      return await this.fetch();
    },
    async clearSearch() {
      this.pagination.searchKey = "";
    },
    async reset() {
      this.lists = [];
      this.pagination = {
        dictLevel: "",
        endTime: "",
        pageNum: 0,
        pages: null,
        pageSize: 20,
        searchKey: "",
        startTime: "",
        current: null,
      };
      return Promise.resolve();
    },
    async loadMore(level) {
      if (!this.isLoadMoreLock && !this.isLast) {
        this.isLoadMoreLock = true;
        this.pagination.dictLevel = level;
        const page =
          this.pagination.pageNum < this.pagination.pages
            ? this.pagination.pageNum + 1
            : this.pagination.pages;
        this.pagination.pageNum = page;

        return await this.fetch();
      }
    },
  },
});
