import http from "@/utils/http";
import { AppDispatch } from "..";
import { A2Tab1Type, A2Tab1_1 } from "@/types/api/A2Dict";
import { A5TableType } from "@/types";
/**
 * 获取项目属性：职能，状态，签订主体，业务部门，业务类型，项目范围，客户端
 */
export const A2_APIgetList1 = () => {
  return async (dispatch: AppDispatch) => {
    const res = await http.get("cms/dict/getList");

    if (res.code === 0) {
      const arrTemp: A2Tab1_1[] = res.data;

      const arr1: A2Tab1_1[] = [];
      const arr2: A2Tab1_1[] = [];
      const arrMain: A2Tab1_1[] = []; // 签订主体
      const arrDept: A2Tab1_1[] = []; // 业务部门
      const arrProjectBusiness: A2Tab1_1[] = []; // 业务类型
      const arrProjectScope: A2Tab1_1[] = []; // 项目范围
      const arrProjectApp: A2Tab1_1[] = []; // 客户端

      arrTemp.forEach((v) => {
        if (v.type === "job") arr1.push(v);
        else if (v.type === "status") arr2.push(v);
        else if (v.type === "main") arrMain.push(v);
        else if (v.type === "dept") arrDept.push(v);
        else if (v.type === "projectBusiness") arrProjectBusiness.push(v);
        else if (v.type === "projectScope") arrProjectScope.push(v);
        else if (v.type === "projectApp") arrProjectApp.push(v);
      });

      const obj: A2Tab1Type = {
        job: arr1,
        status: arr2,
        main: arrMain,
        dept: arrDept,
        projectBusiness: arrProjectBusiness,
        projectScope: arrProjectScope,
        projectApp: arrProjectApp,
      };
      dispatch({ type: "A2/getInfo1_1", payload: obj });
    }
  };
};

export const A2_APIgetList2 = () => {
  return async (dispatch: AppDispatch) => {
    // 获取阶段
    const res = await http.get(`cms/dict/getTree?type=stage`);
    if (res.code === 0) {
      const arr3: A5TableType[] = res.data;
      arr3.forEach((v) => {
        v.level = 1;
        if (v.children && v.children.length) {
          v.children.forEach((v2) => {
            v2.level = 2;
            if (v2.children && v2.children.length) {
              v2.children.forEach((v3) => {
                v3.level = 3;
                if (v3.children && v3.children.length) {
                  v3.children.forEach((v4) => {
                    v4.level = 4;
                  });
                }
              });
            }
          });
        }
      });
      dispatch({ type: "A2/getInfo1_2", payload: arr3 });
    }
  };
};

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

/**
 * 删除 项目属性
 */
export const A2_APIdel = (id: number | string) => {
  return http.get(`cms/dict/remove/${id}`);
};

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

/**
 * 获取 内控文件属性列表
 */
export const A2_APIgetListFile = () => {
  return async (dispatch: AppDispatch) => {
    const res = await http.get("cms/attribute/getList");

    if (res.code === 0) {
      dispatch({ type: "A2/getInfo2", payload: res.data });
    }
  };
};

/**
 * 删除 内控文件属性
 */
export const A2_APIdelFile = (id: number) => {
  return http.get(`cms/attribute/remove/${id}`);
};

/**
 * 新增/修改
 */
export const A2_APIFileAdd = (data: any) => {
  return http.post("cms/attribute/save", data);
};
