import * as employeeService from '../../services/mdata/employee';

export default {

  namespace: 'emp',

  state: {
    list: [],
    total: 0,
    currentItem: {},
    modalVisible: false,
    mode: 'create',
    filterParams: {},
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if (location.pathname === '/mdata/emp') {
          // dispatch({ type: 'clean' });
          // dispatch({ type: 'query' });
        }
      });
    },
  },

  effects: {
    * query({ payload }, { call, put, select }) {
      const { filterParams } = yield select(({ emp }) => emp);
      const data = yield call(employeeService.query, { ...filterParams, ...payload });
      if (data.success) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.data.rows,
            total: data.data.total,
            filterParams: { ...filterParams, ...payload },
          },
        });
      } else {
        throw data;
      }
    },

    * querySingle({ payload }, { call, put }) {
      const data = yield call(employeeService.getSingle, payload.id);
      if (data.success) {
        yield put({
          type: 'showModal',
          payload: {
            currentItem: data.data,
            mode: payload.mode,
          },
        });
      } else {
        throw data;
      }
    },

    * getSingle({ payload }, { put }) {
      if (payload.mode === 'create') {
        yield put({
          type: 'showModal',
          payload: {
            mode: payload.mode,
          },
        });
      } else {
        yield put({ type: 'querySingle', payload });
      }
    },

    * update({ payload }, { select, call, put }) {
      const { id } = yield select(({ emp }) => emp.currentItem);
      // const { currentItem } = yield select(({ employee }) => employee);
      const { orgId } = payload;
      // console.log(orgId);
      const data = yield call(employeeService.doUpdateOrg, { orgId, empId: id });
      if (data.success) {
        yield put({ type: 'hideModal' });
        yield put({ type: 'query' });
      } else {
        throw data;
      }
    },

    * view({ payload }, { put }) {
      yield put({ type: 'hideModal' });
    },

    * hideModal({ payload }, { put }) {
      yield put({
        type: 'updateState',
        payload: {
          modalVisible: false,
          currentItem: {},
        },
      });
    },

    * clean({ payload }, { call, put }) {
      yield put({
        type: 'updateState',
        payload: {
          list: [],
          total: 0,
          currentItem: {},
          modalVisible: false,
          mode: 'create',
          filterParams: {},
        },
      });
    },

  },

  reducers: {

    querySuccess(state, action) {
      const { list, total, filterParams } = action.payload;
      return {
        ...state,
        list,
        total,
        filterParams,
      };
    },

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

    showModal(state, action) {
      return { ...state, ...action.payload, modalVisible: true };
    },
  },

};
