import { ReduxAction, ReduxSagaEffects } from '@interfaces';
import { dataUtils, enums, formUtils } from '@utils';
import { routerRedux } from 'dva/router';
import * as bidGroupService from '../../../services/source/bidGroup';

export default {

  namespace: 'bidGroup',

  state: {
    formData: {},
    list: [],
    total: 1,
    mode: 'create',
    from: '',
    readOnly: false,
    addBtnFlag: false,
    editBtnFlag: false,
    viewBtnFlag: false,
    deleteBtnFlag: false
  },

  subscriptions: {
    setup({ dispatch, history }: object) {
      history.listen(location => {
        if (location.pathname === '/source-mgmt/bid-mgmt/bidGroup') {
          // dispatch({ type: 'clean' });
          // dispatch({ type: 'query' });
        }
      });
    },
  },

  effects: {
    * query({ payload }, { call, put, select }) {
      // console.log('payload', payload)
      // 角色判断
      /*soum_bm_eg_add/soum_bm_eg_edit/soum_bm_eg_view/soum_bm_eg_delete*/
      let {
        addBtnFlag, editBtnFlag, viewBtnFlag,
        deleteBtnFlag
      } = yield select(({ bidGroup }) => bidGroup);
      const perms = yield select(({ app }) => app.perms);
      // console.log('perms==>', perms)
      if (perms) {
        if (perms.find((x) => {
            return x.menuId === 'soum_bm_eg_add';
          })) {
          addBtnFlag = true;
        }
        if (perms.find((x) => {
            return x.menuId === 'soum_bm_eg_edit';
          })) {
          editBtnFlag = true;
        }
        if (perms.find((x) => {
            return x.menuId === 'soum_bm_eg_view';
          })) {
          viewBtnFlag = true;
        }
        if (perms.find((x) => {
            return x.menuId === 'soum_bm_eg_delete';
          })) {
          deleteBtnFlag = true;
        }
      }
      const data = yield call(bidGroupService.query, payload);
      if (data.success) {
        yield put({
          type: 'updateState',
          payload: {
            list: data.data.rows,
            total: data.data.total,
            addBtnFlag,
            editBtnFlag,
            viewBtnFlag,
            deleteBtnFlag
          },
        });
      } else {
        throw data;
      }
    },

    * getSingle({ payload }, { put }) {
      yield put({
        type: 'updateState',
        payload: {
          mode: payload.mode,
        },
      });
      if (payload.mode === 'create') {
        yield put(routerRedux.push('/source-mgmt/bid-mgmt/bidGroup/create'));
      } else {
        if (payload.mode === 'update') {
          yield put(routerRedux.push(`/source-mgmt/bid-mgmt/bidGroup/${payload.id}?mode=update`));
        }
        if (payload.mode === 'view') {
          yield put(routerRedux.push(`/source-mgmt/bid-mgmt/bidGroup/${payload.id}?mode=view`));
        }
      }
    },

    * delete({ payload }, { call, put }) {
      const data = yield call(bidGroupService.remove, [...payload.ids]);
      if (data.success) {
        yield put({ type: 'query' });
      } else {
        throw data;
      }
    },


    * showPop({ payload }, { put, call }) {
      yield put({
        type: 'showModal',
        payload: {
          mode: payload.mode,
        }
      });
    },

    * hideModal({ payload }, { call, 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',
        },
      });
    },
  },

  reducers: {

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

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

  },
}
