import http from "@/utils/http";
import store, { AppDispatch } from "..";
import { A2R_fSaveType, A2SelectType, A2getGoodsDataType } from "@/types";

/**
 * 获文物列表数据
 */
export const A2_APIgetGoodsList = (data: A2getGoodsDataType) => {
  return async (dispatch: AppDispatch) => {
    const res = await http.post("show/goods/pageList", data);

    if (res.code === 0) {
      const obj = {
        list: res.data.records,
        total: res.data.total,
      };

      dispatch({
        type: "main/goodsList",
        payload: obj,
      });
    }
  };
};

/**
 * 获取下拉框列表
 */
export const A2_APIgetSelectData = (type: "age" | "texture") => {
  return async (dispatch: AppDispatch) => {
    const res = await http.get(`/show/dict/list?type=${type}`);
    if (res.code === 0) {
      const obj = store.getState().A2Main.selectData;
      let newObj = {} as A2SelectType;
      const data = res.data.map((v: any) => ({ value: v.name, label: v.name }));
      data.unshift({
        value: "",
        label: type === "age" ? "全部年代" : "全部",
      });

      if (type === "age") {
        newObj = {
          ...obj,
          age: data,
        };
      } else {
        newObj = {
          ...obj,
          texture: data,
        };
      }

      dispatch({
        type: "main/goodsSelect",
        payload: newObj,
      });
    }
  };
};

/**
 * 获取知识列表
 */
export const A2_APIgetKnowData = (data: any) => {
  return async (dispatch: AppDispatch) => {
    const res = await http.post("show/getKnowledge", data);

    if (res.code === 0) {
      const obj = {
        list: res.data.records,
        total: res.data.total,
      };

      dispatch({
        type: "main/tab5List",
        payload: obj,
      });
    }
  };
};

/**
 * 通过id获取详情(知识)
 */
export const A2_APItab5Info = (channelId: string) => {
  return http.get(`show/knowledge/cms/getTree/${channelId}`);
};

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

/**
 * 获取问答列表
 */
export const A2_APIgetQuestion = (id: number) => {
  return http.get(`show/goods/getQuestion/${id}`);
};

/**
 * 回答问题--对错统计
 */
export const A2_APIgoodsSaveAnswer = (questionId: number, has: 0 | 1) => {
  return http.get(`show/goods/saveAnswer/${questionId}/${has}`);
};

/**
 * 获取留言列表
 */
export const A2_APIgetBarrage = (id: number) => {
  return http.get(`show/goods/getBarrage/${id}`);
};

/**
 * 获取弹幕列表随机 20条
 */
export const A2_APIgetBarrageAll = () => {
  return http.get(`show/barrage/limitList/20`);
};

/**
 * 获取弹幕总开关
 */
export const A2_APIgetConfigBarrage = () => {
  return http.get("show/getConfig/barrage");
};

/**
 * 文物点赞
 */
export const A2_APIgoodsaddStar = (id: number) => {
  return http.get(`show/goods/addStar/${id}`);
};

//------------left2
/**
 * 获取知识列表
 */
export const A2_APIgetKnowledge = (type: string) => {
  return http.get(`show/goods/getKnowledge/${type}`);
};

//------------R_FORM
/**
 * 获取验证码
 */
export const A2_APIgetRandCode = () => {
  return http.get("show/getRandCode", { responseType: "blob" });
};

/**
 * 新增留言（弹幕）
 */
export const A2_APIsaveBarrage = (data: A2R_fSaveType) => {
  return http.post("show/goods/saveBarrage", data);
};
