import { WallSaveAPIType } from "@/types";
import { domShowFu, progressDomFu } from "@/utils/domShow";
import http from "@/utils/http";
import axios from "axios";
import store, { AppDispatch } from "..";

/**
 * 获取万物墙自动播放数据
 */
export const getWallAutoApi = () => {
  return http.get("cms/wall/getConfig");
};

/**
 * 修改万物墙自动播放数据
 */
export const editWallAutoApi = (data: { content: string }) => {
  return http.post("cms/wall/setConfig", data);
};

/**
 * 获取列表数据
 */
export const getWallTableListAPI = () => {
  return async (dispatch: AppDispatch) => {
    const res = await http.get("cms/wall/list");
    if (res.code === 0) dispatch({ type: "wall/getList", payload: res.data });
  };
};

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

/**
 * 内容-删除
 */
export const wallRemoveAPI = (id: number) => {
  return http.get(`cms/wall/remove/${id}`);
};

/**
 * 内容-排序
 */
export const wallSortAPI = (id1: number, id2: number) => {
  return http.get(`cms/wall/sort/${id1}/${id2}`);
};

/**
 * 内容-新增|编辑
 */
export const setWallSave = (data: WallSaveAPIType) => {
  return http.post("cms/wall/save", data);
};

const CancelToken = axios.CancelToken;
/**
 * 上传封面图和附件
 */
export const goodsUploadAPI = (data: any) => {
  domShowFu("#UpAsyncLoding", true);

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

/**
 * 内容-新增|编辑
 */
export const getWallDetailAPI = (id: number) => {
  return http.get(`cms/wall/detail/${id}`);
};
