/* eslint-disable no-param-reassign,no-undef */
import { history } from 'umi';

export default {
  namespace: 'login',
  state: {
    loginLoading: false,
  },
  reducers: {
    showLoginLoading(state) {
      return { ...state, loginLoading: true };
    },
    hideLoginLoading(state) {
      return { ...state, loginLoading: false };
    },
  },
  effects: {
    *login({ payload }, { put }) {
      const {
        token, userinfo, commonConst, systemData, decimals, dateFormat, dispatch, rxtx, footer, logoImageInfo, auxiliaryQty,
        isInitPassword,
      } = payload;
      const reStart = false;
      yield put({ type: 'app/saveToken', payload: token });
      yield put({ type: 'app/saveRxtx', payload: rxtx });
      yield put({ type: 'app/saveCommonConst', payload: commonConst });
      yield put({ type: 'app/saveUserinfo', payload: userinfo });
      yield put({ type: 'app/createWebSocket', payload: { reStart, dispatch } });
      yield put({ type: 'app/saveSystemData', payload: systemData });
      yield put({ type: 'app/saveDecimals', payload: decimals });
      yield put({ type: 'app/saveDateFormat', payload: dateFormat });
      yield put({ type: 'app/saveFooter', payload: footer });
      yield put({ type: 'app/saveLogoImageInfo', payload: logoImageInfo });
      yield put({ type: 'app/saveAuxiliaryQty', payload: auxiliaryQty });
      yield put({ type: 'app/saveIsInitPassword', payload: isInitPassword });

      // yield put(routerRedux.push('/indexPage'));
      yield history.push('/indexPage');
      // yield put({ type: 'showLoginLoading' });
      // const { value, url } = payload;
      // const { data } = yield call(loginService.postValueService, null, value, url);
      // if (data.code === 1) {
      //   const {
      //     token, gdslogininfo: userinfo, systemData, commonConst,
      //   } = data.dataset.rows[0];
      //   if (systemData !== undefined) {
      //     const dNetPrice = systemData.filter(item => (item.sName === 'NetPrice'))[0];
      //     const dNetMoney = systemData.filter(item => (item.sName === 'NetMoney'))[0];
      //     const decimals = {
      //       dNetPrice: dNetPrice.sValue !== undefined ? (dNetPrice.sValue) * 1 : 6,
      //       dNetMoney: dNetMoney.sValue !== undefined ? (dNetMoney.sValue) * 1 : 6,
      //     };
      //
      //   }
      //   // yield put({ type: 'app/savePanes', payload: { panes: [] } });
      //
      //   yield put({ type: 'app/saveDecimals', payload: decimals });
      //   yield put({ type: 'app/saveToken', payload: token });
      //   yield put({ type: 'app/saveUserinfo', payload: userinfo });
      //   yield put({ type: 'app/saveSystemData', payload: systemData });
      //   yield put({ type: 'app/saveCommonConst', payload: commonConst });
      //   yield put(routerRedux.push('indexPage'));
      // } else {
      //   yield put({ type: 'app/throwError', payload: data });
      // }
      // yield put({ type: 'hideLoginLoading' });
    },
  },
  subscriptions: {
  },
};
