import { DownImgType, GoodsTableType } from "@/types";
import http from "@/utils/http";
import axios from "axios";
import store, { AppDispatch } from "..";
/**
 * 获取列表数据
 */
export const getGoodsListAPI = (data: any) => {
  return async (dispatch: AppDispatch) => {
    const res = await http.post("cms/goods/pageList", data);
    dispatch({
      type: "goods/getList",
      payload: { list: res.data.records, total: res.data.total },
    });
  };
};

/**
 * 点击导出的列表数据
 */
export const getGoodsListDeriveAPI = (data: any) => {
  return http.post("cms/goods/pageList", data);
};

/**
 * 内容-是否显示
 */
export const goodsDisplayAPI = (id: number, display: number) => {
  return http.get(`cms/goods/display/${id}/${display}`);
};

/**
 * 弹幕留言单个开启或者关闭
 */
export const goodsBarrageAPI = (id: number, display: number) => {
  return http.get(`cms/goods/updateBarrage/${id}/${display}`);
};

/**
 * 弹幕批量开关
 */
export const goodsBarrageAllAPI = (whether: number) => {
  return http.get(`cms/goods/batchUpdateBarrage/${whether}`);
};

/**
 * 删除藏品
 */
export const goodsRemoveAPI = (id: number) => {
  return http.get(`cms/goods/remove/${id}`);
};

// 上传附件的进度条
const UpAsyncLodingDom: any = document.querySelector("#UpAsyncLoding");
const progressDom: any = document.querySelector("#progress");

const CancelToken = axios.CancelToken;
/**
 * 上传封面图和附件
 */
export const goodsUploadAPI = (data: any) => {
  UpAsyncLodingDom.style.opacity = 1;
  UpAsyncLodingDom.style.pointerEvents = "auto";

  return http.post("cms/goods/upload", data, {
    timeout: 50000,
    // 显示进度条
    onUploadProgress: (e: any) => {
      const complete = (e.loaded / e.total) * 100 || 0;
      progressDom.style.width = complete + "%";
    },
    // 取消上传
    cancelToken: new CancelToken(function executor(c) {
      store.dispatch({
        type: "login/closeUpFile",
        payload: { fu: c, state: true },
      });
    }),
  });
};

/**
 * 新增|编辑
 */
export const getGoodsSaveAPI = (data: GoodsTableType) => {
  return http.post("cms/goods/save", data);
};

/**
 * 通过id获取详情
 */
export const getGoodsInfoByIdAPI = (id: number) => {
  return http.get(`cms/goods/detail/${id}`);
};

/**
 * 更新图片边框
 */
export const setGoodsImgBoderAPI = (id: number, whether: number) => {
  return http.get(`cms/goods/updateFrame/${id}/${whether}`);
};

/**
 * 下载图片--水印和压缩选择
 */
export const getDownImgAPI = (data: DownImgType) => {
  return http.post("cms/goods/download", data);
};
