import { message } from 'antd';

import { ReduxAction, ReduxSagaEffects } from '@interfaces';
import * as usersService from '../../services/sys/users';

export default {
  namespace: 'users',
  state: {
    mode: 'create',
    currentItem: {},
    modalVisible: false,
    filterParams: {},
    pwModalVisible: false,
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/sys/users') {
          dispatch({ type: 'clean' });
          dispatch({ type: 'suppUserModel/clean' });
          dispatch({ type: 'entUserModel/clean' });
          dispatch({ type: 'entUserModel/query' });
        }
      });
    },
  },

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

    * create({ payload }: ReduxAction, { call, put }: ReduxSagaEffects) {
      const data = yield call(usersService.create, payload);
      if (data.success) {
        yield put({ type: 'hideModal' });
        yield put({ type: 'entUserModel/query' });
      } else {
        throw data;
      }
    },

    * update({ payload }: ReduxAction, { select, call, put }: ReduxSagaEffects) {
      const users = yield select(({ users }) => users.currentItem);
      const data = yield call(usersService.update, { id: users.id, ...payload });
      if (data.success) {
        yield put({ type: 'hideModal' });
        yield put({ type: 'entUserModel/query' });
      } else {
        throw data;
      }
    },

    * clean({ payload }: ReduxAction, { call, put }: ReduxSagaEffects) {
      yield put({
        type: 'updateState',
        payload: {
          itemPowerFlag: '',
          regPowerFlag: '',
          currentItem: {},
          modalVisible: false,
          mode: 'create',
          filterParams: {},
        },
      });
    },

    * matchRoles({ payload }: ReduxAction, { put, call, select }: ReduxSagaEffects) {
      payload.iamRoleIds = payload.iamRoleIds && payload.iamRoleIds.map(id => parseInt(id));
      payload.bpmRoleIds = payload.bpmRoleIds && payload.bpmRoleIds.map(id => parseInt(id));
      const data = yield call(usersService.checkRoles, payload);
      const currentItem = yield select(({ users }) => users.currentItem);
      if (data.success) {
        yield put({
          type: 'updateState',
          payload: {
            currentItem: {
              ...currentItem,
              iamRoles: data.data.iamRoleIds.map(id => '' + id),
              bpmRoles: data.data.bpmRoleIds.map(id => '' + id)
            }
          }
        });
      } else {
        throw data;
      }
    },

    * updatePW({ payload }: ReduxAction, { put, call, select }: ReduxSagaEffects) {
      const data = yield call(usersService.doUpdatePassword, { id: payload.id, p: payload.p });

      if (data.success) {
        message.success('操作成功');
        yield put({ type: 'hidePWModal' });
      } else {
        throw data;
      }
    }
  },

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

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

    updateItem(state: object, { payload }: ReduxAction) {
      const { currentItem } = state;
      // console.log(currentItem, payload);
      return {
        ...state,
        currentItem: { ...currentItem, ...payload },
      };
    },

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

    hideModal(state: object, action: ReduxAction) {
      return {
        ...state, ...action.payload, modalVisible: false, currentItem: {},
      };
    },


    showPWModal(state: object, action: ReduxAction) {
      return { ...state, pwModalVisible: action.payload.id };
    },

    hidePWModal(state: object, action: ReduxAction) {
      return {
        ...state, ...action.payload, pwModalVisible: false
      };
    },

  },
};
