import {
  queryApproval,
  createApproval,
  updateApproval,
  deleteApproval,
  queryType,
  queryIndustry,
  queryFlow,
  authApproval,
  queryAuth,
  submitAudit,
  queryDepV2,
  addMember,
  queryMember,
  startExecution,
  startOperate,
  startQuality,
  deleteMember,
  queryBudget,
  setBudget,
  modifyManager,
} from '@/services/approval';
import { message } from 'antd';
import moment from 'moment';

function getDepUserTree(data) {
  data.title = `${data.Name}`;
  data.key = `dep-${data.ID}`;
  data.value = `dep-${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.key = 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: 'approval',
  state: {
    typeList: [],
    industryList: [],
    flowList: [],
    list: {
      list: [],
      pagination: {},
    },
    auth: {
      list: [],
      pagination: {},
    },
    filter: {},
    depUserTree: [],
    member: [],
    budget: [],
  },

  effects: {
    *fetchDepV2({ payload, callback }, { call, put }) {
      const response = yield call(queryDepV2, { pageSize: 999999 });
      if (response) {
        const depUserTree = response.data.list.map(item => {
          return getDepUserTree(item);
        });
        yield put({
          type: 'save',
          payload: { depUserTree },
        });
      }
    },

    *queryType({ payload }, { call, put, select }) {
      const { typeList } = yield select(s => s.approval);
      if (typeList.length > 0) return;
      const { data } = yield call(queryType);
      if (!data) return;
      yield put({
        type: 'save',
        payload: { typeList: data },
      });
    },
    *queryFlow({ payload }, { call, put, select }) {
      const { flowList } = yield select(s => s.approval);
      if (flowList.length > 0) return;
      const { data } = yield call(queryFlow);
      if (!data) return;
      yield put({
        type: 'save',
        payload: { flowList: data },
      });
    },
    *queryIndustry({ payload }, { call, put, select }) {
      const { industryList } = yield select(s => s.approval);
      if (industryList.length > 0) return;
      const { data } = yield call(queryIndustry);
      if (!data) return;
      yield put({
        type: 'save',
        payload: { industryList: data },
      });
    },
    *queryApproval({ payload = {}, callback }, { call, put, select }) {
      const { filter } = yield select(s => s.approval);
      const newFileter = {
        ...filter,
        ...payload,
      };
      const { data } = yield call(queryApproval, newFileter);
      if (!data) return;
      callback && callback(data);
      yield put({
        type: 'save',
        payload: { list: data, filter: newFileter },
      });
    },
    *queryAuth({ payload = {} }, { call, put }) {
      const { data } = yield call(queryAuth, payload);
      if (!data) return;
      yield put({
        type: 'save',
        payload: { auth: data },
      });
    },
    *queryMember({ payload }, { call, put }) {
      const { data } = yield call(queryMember, payload);
      if (!data) return;
      yield put({
        type: 'save',
        payload: { member: data.map(item => item.User) },
      });
    },
    *authApproval({ payload, callback }, { call, put }) {
      const { data } = yield call(authApproval, {
        id: payload.id,
        project_full_code: payload.project_full_code,
        flow_id: payload.flow_id,
        node_id: payload.node_id,
        audit_status: payload.audit_status,
        audit_comment: payload.audit_comment,
      });
      if (!data) return;
      callback && callback();
      message.success('审核成功');
      yield put({
        type: 'queryAuth',
      });
    },
    *createApproval({ payload, callback }, { call, put }) {
      const res = yield call(createApproval, payload);
      if (res) {
        const { data } = res;
        callback && callback();
        message.success('创建成功');
        yield put({
          type: 'queryApproval',
        });
      }
    },
    *updateApproval({ payload, callback }, { call, put }) {
      const { data } = yield call(updateApproval, payload);
      if (!data) return;
      callback && callback();
      message.success('更新成功');
      yield put({
        type: 'queryApproval',
      });
    },
    *deleteApproval({ payload, callback }, { call, put }) {
      const { data } = yield call(deleteApproval, payload);
      if (!data) return;
      callback && callback();
      message.success('删除成功');
      yield put({
        type: 'queryApproval',
      });
    },
    *submitAudit({ payload, callback }, { call, put }) {
      const { data } = yield call(submitAudit, payload);
      if (!data) return;
      callback && callback();
      message.success('提审成功');
      yield put({
        type: 'queryApproval',
      });
    },
    *addMember({ payload, callback }, { call, put }) {
      const res = yield call(addMember, payload);
      if (!res) return;
      callback && callback();
      message.success('添加成功');
      yield put({
        type: 'queryMember',
        payload: { project_code_id: payload.project_code_id, flow_id: payload.flow_id },
      });
    },
    *deleteMember({ payload, callback }, { call, put }) {
      const res = yield call(deleteMember, payload);
      if (!res) return;
      callback && callback();
      message.success('移除成功');
      yield put({
        type: 'queryMember',
        payload: { project_code_id: payload.project_code_id, flow_id: payload.flow_id },
      });
    },
    *startExecution({ payload, callback }, { call, put }) {
      const res = yield call(startExecution, payload);
      if (!res) return;
      callback && callback();
      message.success('转执行送审成功');
      yield put({
        type: 'queryApproval',
      });
    },
    *startOperate({ payload, callback }, { call, put }) {
      const res = yield call(startOperate, payload);
      if (!res) return;
      callback && callback();
      message.success('转运营送审成功');
      yield put({
        type: 'queryApproval',
      });
    },
    *startQuality({ payload, callback }, { call, put }) {
      const res = yield call(startQuality, payload);
      if (!res) return;
      callback && callback();
      message.success('转质保送审成功');
      yield put({
        type: 'queryApproval',
      });
    },

    *queryBudget({ payload, callback }, { call, put }) {
      const res = yield call(queryBudget, payload);
      if (!res) return;
      yield put({
        type: 'save',
        payload: { budget: res.data },
      });
      callback?.();
    },

    *setBudget({ payload, callback }, { call, put }) {
      const res = yield call(setBudget, payload);
      if (!res) return;
      message.success('项目预算设置成功');
      callback?.();
    },
    *modifyManager({ payload, callback }, { call, put }) {
      const res = yield call(modifyManager, payload);
      if (!res) return;
      callback?.();
      message.success('修改项目经理成功');
      yield put({
        type: 'queryApproval',
      });
    },
  },

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