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

  namespace: 'priceList',

  state: {
    list: [],
    total: 0,
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/price-mgmt/adjust') {
          // dispatch({ type: 'clean' });
        }
      });
    },
  },

  effects: {
    * query({ payload }, { call, select, put }) {
      if (payload && !payload.bpmStatus) {
        payload.bpmStatus = 'NONE';
      }
      const data = yield call(adjustService.query, payload);
      if (data.success) {
        yield put({
          type: 'updateState',
          payload: {
            list: data.data.rows,
            total: data.data.total,
          },
        });
        yield put(routerRedux.replace({
          pathname: location.pathname,
          search: '?' + stringUtils.JSON2QueryString(payload),
        }));
      } else {
        throw data;
      }
    },

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

    * delete({ payload }, { call, put, select }) {
      const list = yield select(({ priceList }) => priceList.list);
      const currentUser = yield select(({ app }) => app.user); 
      // filter 返回一个新的数组
      const reuslt = list.filter((row) => {
        if (row.id === payload.id) {
          return true;
        }
      });
      if (reuslt[0].createUserId !== currentUser.user.id) {
        message.warning('只有调整人才能取消!');
        return;
      }
      if (reuslt[0].status !== '1') {
        message.warning('只有审批中的才能取消!');
        return;
      }
      const data = yield call(adjustService.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,
      };
    },
  },

};
