import http from "@/utils/http";
import { AppDispatch } from "..";
import { Z0tableType } from "@/types";

/**
 * 获取栏目列表
 */

const Z0listRes = (item: Z0tableType) => {
  return item.name === "其他" ? { ...item, rtf: "-" } : item;
};

export const Z0_APIgetList = (): any => {
  return async (dispatch: AppDispatch) => {
    const res = await http.get("cms/dict/getList");
    if (res.code === 0) {
      const obj = {
        dynamic: [] as Z0tableType[],
        goods: [] as Z0tableType[],
        activity: [] as Z0tableType[],
      };

      const list: Z0tableType[] = res.data;

      list.forEach((v) => {
        if (v.type === "dict-news") obj.dynamic.push(Z0listRes(v));
        else if (v.type === "dict-goods") obj.goods.push(Z0listRes(v));
        else if (v.type === "dict-activity") obj.activity.push(Z0listRes(v));
      });

      dispatch({ type: "Z0/getList", payload: obj });
    }
  };
};

/**
 * 删除栏目
 */
export const Z0_APIdel = (id: number) => {
  return http.get(`cms/dict/remove/${id}`);
};

/**
 * 栏目排序
 */
export const Z0_APIsort = (id1: number, id2: number) => {
  return http.get(`cms/dict/sort/${id1}/${id2}`);
};

/**
 * 栏目 新增/编辑
 */
export const Z0_APIadd = (data: any) => {
  return http.post("cms/dict/save", data);
};
