import { statusObjCK } from "@/utils/dataChange";
import http from "@/utils/http";
import { AppDispatch } from "..";

/**
 * 获取藏品登记列表信息
 */
export const getObject4List = (data: any) => {
  return async (dispatch: AppDispatch) => {
    // 获取列表数据
    const res: any = await http.post("cms/out/pageList", data);
    const list = res.data.records;
    list.forEach((v: any) => {
      v.statusTxt = statusObjCK[v.status];
    });
    const obj = {
      list,
      total: res.data.total,
    };
    dispatch({ type: "object4/getList", payload: obj });
  };
};

/**
 * 获取藏品登记列表顶部数字信息
 */
export const getObject4ListNum = () => {
  return async (dispatch: AppDispatch) => {
    // 获取统计数据
    const res: any = await http.get("cms/out/countByStatus");

    const data = [
      { id: null, name: "全部", num: res.data.all ? res.data.all : 0 },
      // { id: 0, name: "待办理", num: res.data[0] ? res.data[0] : 0 },
      { id: 1, name: "待审核", num: res.data[1] ? res.data[1] : 0 },
      { id: 2, name: "审核不通过", num: res.data[2] ? res.data[2] : 0 },
      { id: 3, name: "待归还", num: res.data[3] ? res.data[3] : 0 },
      { id: 4, name: "已归还", num: res.data[4] ? res.data[4] : 0 },
    ];
    dispatch({ type: "object4/getListNum", payload: data });
  };
};

/**
 * 删除外层表格数据
 */
export const object4DelAPI = (id: number) => {
  return http.get(`cms/out/remove/${id}`);
};

/**
 * 通过id获取信息
 */
export const object4infoOutAPI = (id: number) => {
  return http.get(`cms/out/detail/${id}`);
};

/**
 * 进新增页面发送请求
 */
export const object4AddAPI = (data?: any) => {
  return http.post("cms/out/save", { ...data });
};

/**
 * 点击新增或者编辑里面的添加按钮的所有藏品列表
 */
export const getGoodsListAllAPI = (val: any) => {
  return async (dispatch: AppDispatch) => {
    // 获取统计数据
    const res: any = await http.get(`cms/out/goods/list?searchKey=${val}`);
    dispatch({ type: "object4/setGoodsListAll", payload: res.data });
  };
};

/**
 * 审核入库信息
 */
export const auditObject4API = (data: any) => {
  return http.post("cms/out/audit", data);
};

/**
 * 出库查看页面的归还
 */
export const returnObject4API = (data: any) => {
  return http.post("cms/out/goodsReturn", data);
};