import { Modal, message } from 'antd';
import { routerRedux } from 'dva/router';
import * as bpmService from '../../services/common/bpm';
import * as itemTrialService from '../../services/item/itemTrial';
import { stringUtils,dataUtils} from '../../utils';
export default {

  namespace: 'itemAccess',

  state: {
    list: [],
    total: 0,
    useOrg: '',
    cancelBtn: true,
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/item-mgmt/itemAccess' || location.pathname === '/item-mgmt/itemAccess/trial') {
          // dispatch({ type: 'clean' });
          if (location.pathname === '/item-mgmt/itemAccess/trial') {
            dispatch({ type: 'getUserDepartment' });
          }
        }
      });
    },
  },

  effects: {
    * query({ payload }, { call, select, put }) {
      let { cancelBtn } = yield select(({ itemAccess }) => itemAccess);
      const perms = yield select(({ app }) => app.perms);
      // 角色控制按钮权限
      if (perms) {
        for (const p of perms) {
          if (p.menuId === 'im_acc_cancel') {
            cancelBtn = false;
          }
        }
      }
      const { useOrg } = yield select(({ itemAccess }) => itemAccess);
      if (payload && payload.cccCode) {
        if (payload.cccCode.length > 0) {
          payload.cccCode.map((code, index) => {
            const key = 'c' + (index + 1) + 'Code';
            payload[key] = code;
          });
        } else {
          Object.keys(payload).map((key) => {
            if (key.indexOf('Code') === 2) {
              payload[key] = '';
            }
          });
        }
      }
      if (payload && !payload.bpmStatus) {
        payload.bpmStatus = 'NONE';
      }
      if (useOrg !== '') {
        payload.feedBack = '1';
      }
      const data = yield call(itemTrialService.query, payload);
      if (data.success) {
        yield put({
          type: 'updateState',
          payload: {
            list: data.data.rows,
            total: data.data.total,
            cancelBtn,
          },
        });
        yield put(routerRedux.replace({
          pathname: location.pathname,
          search: '?' + stringUtils.JSON2QueryString(payload),
        }));
      } else {
        throw data;
      }
    },

    * getSingle({ payload }, { put, call }) {
      const data = yield call(itemTrialService.getSingle, payload.id);
      if (data.success) {
        const status = yield call(bpmService.doCheckActived, data.data.prcId);
        if (status.success) {
          if (status.data) {
            if (payload && payload.trial === 'trial') {
              yield put(routerRedux.push(`/item-mgmt/itemTrial/trial?id=${payload.id}`));
            } else {
              yield put(routerRedux.push(`/item-mgmt/itemTrial/${payload.id}`));
            }
          } else {
            Modal.error({
              title: '您不是当前处理人',
              okText: '确认'
            });
          }
        }
      }
    },

    * getView({ payload }, { put, call }) {
      if (payload && payload.trial === 'trial') {
        yield put(routerRedux.push(`/item-mgmt/itemTrial/viewTrial?id=${payload.id}`));
      } else {
        yield put(routerRedux.push(`/item-mgmt/itemTrial/view?id=${payload.id}`));
      }
    },

    * getUserDepartment({ payload }, { put, call, select }) {
      const currentUser = yield select(({ app }) => app.user);
      const empId = currentUser.user.empId;
      const data = yield call(itemTrialService.getUserDepartment, empId);
      if (data.success) {
        yield put({
          type: 'updateState',
          payload: {
            useOrg: data.data || '0',
          },
        });
      } else {
        throw data;
      }
    },

    * delete({ payload }, { call, put, select }) {
      const list = yield select(({ itemAccess }) => itemAccess.list);
      // filter 返回一个新的数组
      const reuslt = list.filter((row) => {
        if (row.id === payload.id) {
          return true;
        }
      });
      if (reuslt[0].checkStatus !== '05') {
        message.warning('只有审核中的才能取消!');
        return;
      }
      const data = yield call(itemTrialService.cancel, payload.id);
      if (data.success) {
        yield put({ type: 'query',payload:dataUtils.queryStringToJSON(location.search)});
      } else {
        throw data;
      }
    },

    * clean({ payload }, { call, put }) {
      yield put({
        type: 'updateState',
        payload: {
          list: [],
          total: 0,
          useOrg: '',
          cancelBtn: true,
        },
      });
    },
  },

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

};
