import {
  queryAuditList,
  addAudit,
  queryProcessFlows,
  saveAuditFlowInfo,
  queryClassify,
  // addAuditNode,
  // addFlow,
  // queryBoomFlowDetail,
  // updateNode,
  // queryFlowList,
  // queryDingTemplateList,
  // queryDepV2,
  // queryDefaultBindClassify,
} from '@/services/boom';
// import { queryApproval } from '@/services/approval';
// import { queryRole } from '@/services/SysAdmin';
import { message } from 'antd';

// function getDepUserTree(data) {
//   data.title = `${data.Name}`;
//   data.id = data.ID;
//   data.value = data.ID;
//   // data.selectable = false;
//   if (!data.children) data.children = new Array();

//   if (data.children) {
//     data.children.forEach((item) => {
//       getDepUserTree(item);
//     });
//   }

//   if (data.Users && data.Users.length !== 0) {
//     data.Users.forEach((item) => {
//       item.title = item.CName;
//       item.id = item.ID + '||' + data.ID;
//       item.value = item.ID + '||' + data.ID;
//       // item.selectable = true;
//       item.DepId = data.ID;
//       data.children.push(item);
//     });
//   }
//   return data;
// }

export default {
  namespace: 'flow',
  state: {
    flowDetail: { nodes: [], edges: [] },
    formData: {},
    auditList: [],
    flowList: [],
    projectList: [],
    current: {},
    roleList: [],
    templateList: [],
    depUserTree: [],
    simpleFlowDteail: '',
    classify: [],
  },

  effects: {
    *queryAuditList({ payload }, { call, put }) {
      try {
        const response = yield call(queryAuditList, payload);
        yield put({
          type: 'save',
          payload: {
            auditList: response.data,
          },
        });
      } catch (error) {}
    },
    *queryClassify({ payload }, { call, put }) {
      try {
        const response = yield call(queryClassify, payload);

        yield put({
          type: 'save',
          payload: {
            classify: response.data,
          },
        });
      } catch (error) {}
    },
    *addAudit({ payload, callback }, { call, put }) {
      try {
        const response = yield call(addAudit, payload);
        callback && callback(response);
        yield put({
          type: 'queryAuditList',
          payload: {
            flow_type: 1,
          },
        });
      } catch (error) {
        console.log(error);
      }
    },
    *queryProcessFlows({ payload }, { call, put }) {
      const { data } = yield call(queryProcessFlows, payload);
      yield put({
        type: 'save',
        payload: {
          flowDetail: data.flowDetail,
          formData: data.formData,
          simpleFlowDteail: data.simpleFlowDteail,
        },
      });
    },
    *saveAuditFlowInfo({ payload, callback }, { call, put }) {
      try {
        yield call(saveAuditFlowInfo, payload);
        message.success('保存成功');
        callback && callback();
        yield put({
          type: 'flow/queryProcessFlows',
          payload: { ids: Number(payload.id) },
        });
      } catch (error) {}
    },
    // *addFlow({ payload, callback }, { call, put }) {
    //   const res = yield call(addFlow, payload);
    //   if (res) {
    //     message.success('添加成功');
    //     callback && callback();
    //     yield put({
    //       type: 'queryFlowList',
    //     });
    //   }
    // },
    // *getRoleList({ payload }, { call, put }) {
    //   const response = yield call(queryRole, payload);
    //   if (response) {
    //     yield put({
    //       type: 'save',
    //       payload: { roleList: response.data.list },
    //     });
    //   }
    // },
    // *queryBoomFlowDetail({ payload }, { call, put }) {
    //   const data = yield call(queryBoomFlowDetail, payload);
    //   console.log(data);
    //   yield put({
    //     type: 'save',
    //     payload: { flowDetail: data },
    //   });
    // },
    // *queryProject({ callback }, { call, put }) {
    //   const response = yield call(queryApproval, { pageSize: 99999 });
    //   if (response) {
    //     yield put({
    //       type: 'save',
    //       payload: {
    //         projectList: response.data.list,
    //       },
    //     });
    //   }
    // },
    // *queryFlowList({ payload }, { call, put }) {
    //   const res = yield call(queryFlowList, payload);
    //   yield put({
    //     type: 'save',
    //     payload: { flowList: res.data },
    //   });
    // },
    // *updateNode({ payload, callback }, { call, put }) {
    //   const data = yield call(updateNode, payload);
    //   console.log(data);
    //   message.success('修改成功');
    //   callback && callback();
    // },
    // *addAuditNode({ payload, callback }, { call, put }) {
    //   const response = yield call(addAuditNode, payload);
    //   if (response) {
    //     message.success('操作成功');
    //     callback && callback();
    //   }
    // },
    // *queryDingTemplateList({ payload }, { call, put }) {
    //   const response = yield call(queryDingTemplateList, payload);
    //   if (response) {
    //     yield put({
    //       type: 'save',
    //       payload: { templateList: response.data.result },
    //     });
    //   }
    // },
    // *fetchDepV2({ payload, callback }, { call, put }) {
    //   const response = yield call(queryDepV2, { pageSize: 999999 });
    //   if (response) {
    //     // const depUserTree = response.data?.list;
    //     const depUserTree = response.data.list.map((item) => {
    //       return getDepUserTree(item);
    //     });
    //     yield put({
    //       type: 'save',
    //       payload: { depUserTree },
    //     });
    //   }
    // },
    // *queryDefaultBindClassify({ payload }, { call, put }) {
    //   const data = yield call(queryDefaultBindClassify, payload);
    //   if (data) {
    //   }
    // },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
