import { ListController } from "@/controllers/ListController";
import { defineStore } from "pinia";
import { request } from "../../objects";
import loading from "@/components/Provider/Loading";
import { message } from "ant-design-vue";
import Modal from "@/components/Provider/Modal";
import { https } from "./https";
export const useArticle = defineStore("article", {
  state: () => ({
    listController: new ListController(request),
    bannerList: new ListController(request),
  }),
  getters: {},
  actions: {
    initArticle() {
      this.listController.setCrudPrefix("/article");
      this.bannerList.setCrudPrefix("/banner");
    },
    async addOrUpdateArticle(item: any) {
      loading.show("保存中");
      let res = {} as any;
      if (item._id) {
        res = await this.listController.saveItem(item);
      } else {
        res = await this.listController.addItem(item);
      }
      loading.hidden();
      if (res.errorNo != 200) {
        message.success("保存失败");
        return;
      }
      message.success("保存成功");
    },
    async getArticleDetail(item: any) {
      const res = await this.listController.itemDetail(item._id);
      if (res.errorNo != 200) {
        message.warn("未查询到数据！");
        return;
      }
      return res.result;
    },
    async addOrUpdateBanner(item: any) {
      loading.show("保存中");
      let res = {} as any;
      if (item._id) {
        res = await this.bannerList.saveItem(item);
      } else {
        res = await this.bannerList.addItem(item);
      }
      loading.hidden();
      if (res.errorNo != 200) {
        message.success("保存失败");
        return;
      }
      message.success("保存成功");
    },
    async deleteBanner(item: any) {
      const ok = await Modal.confirm({
        title: "删除确认",
        content: `删除后数据无法恢复，确认删除：${item?.name}？`,
        type: "danger",
      });
      if (ok) {
        loading.show("删除中");
        await this.bannerList.deleteItem(item._id);
        loading.hidden();
        message.success("删除成功");
      }
    },
    async deleteArticle(item: any) {
      const ok = await Modal.confirm({
        title: "删除确认",
        content: `删除后数据无法恢复，确认删除：${item?.title}？`,
        type: "danger",
      });
      if (ok) {
        loading.show("删除中");
        await this.listController.deleteItem(item._id);
        loading.hidden();
        message.success("删除成功");
      }
    },
    getContentFirstImage(content: string) {
      if (!content) {
        return "";
      }
      const html = content;
      const reg = /<img.*?(?:>|\/>)/gi;
      const images = html.match(reg);
      const firstImage = images ? images[0] : "";
      if (!firstImage) {
        return "";
      }
      const srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;
      const src = firstImage.match(srcReg);
      return src ? src[1] : "";
    },
    async getArticleList(query: any) {
      const res = await https.getArticleList(query);
      if (res.errorNo != 200) {
        return false;
      }
      return res.result;
    },
  },
});
