import * as service from '../../services/tos';
import { message } from 'antd';
import { Link, useIntl, connect, Dispatch, history } from 'umi';

import { routerRedux } from 'dva/router';

export default {
  namespace: 'User',
  state: {
    Data: null,
    DataPage: { totalRow: 0 },
    Data3Error: null,
    DataSave: null,
    DataSaveDetail: null,

    sourceData: null,
    curString: { subscribeDate: null, status: null, curPage: 1, curPage2: 1 },

    communityInfo: null,
    pageDate: null,

    returnValue: null,
    memberResult: null,

    DataServices: null,
  },

  reducers: {
    returnPage(state, { Data, DataPage, DataSave }) {
      return { ...state, Data, DataPage, DataSave };
    },
    returnData3Error(state, { Data3Error }) {
      return { ...state, Data3Error };
    },
    returnDataSave(state, { DataSave }) {
      return { ...state, DataSave };
    },
    DataSaveDetail(state, { DataSaveDetail, returnValue }) {
      return { ...state, DataSaveDetail, returnValue };
    },
    returnSource(state, { sourceData }) {
      return { ...state, sourceData };
    },
    returnCurString(state, { curString }) {
      return { ...state, curString };
    },
    returnResult(state, { Result }) {
      return { ...state, Result };
    },
    returnCommunityInfo(state, { communityInfo }) {
      return { ...state, communityInfo };
    },
    PageDate(state, { pageDate }) {
      return { ...state, pageDate };
    },
    ReturnValue(state, { returnValue }) {
      return { ...state, returnValue };
    },
    returnMemberResult(state, { memberResult }) {
      return { ...state, memberResult };
    },
    returnDataServices(state, { DataServices }) {
      return { ...state, DataServices };
    },
  },

  effects: {
    *RA({ playload }, { call, put }) {
      switch (playload.index) {
        case 12:
          {
            let Data = null;
            yield put({ type: 'returnPage', Data });
          }
          break;
        case 13:
          {
            let DataSaveDetail = null;
            let returnValue = null;
            yield put({ type: 'DataSaveDetail', DataSaveDetail, returnValue });
          }
          break;
        case 43:
          {
            let DataServices = null;
            yield put({ type: 'returnDataServices', DataServices });
          }
          break;
      }

      const resp = yield call(service.RA, playload);
      console.log('resp', resp);
      if (resp.code == 500 || resp.error_code != '0000') {
        // message.error("Server Error,try again,error code:500",3)
        // window.location.href = '/500';
      }

      if (resp.error_code != '0000') {
        console.log('请求错误码:' + '(' + playload.index + ')' + resp.error_code);
        console.log(playload);
        var Data3Error = null;
        var Result = null;
        switch (playload.index) {
          case 11:
            {
              Data3Error = { msg: 'Information Wrong!' };
              yield put({ type: 'returnData3Error', Data3Error });
            }
            break;
          case 13:
            {
              //
              history.push('/UserManagement/LIFEUserManagement');
            }
            break;
          // 删除失败的案例
          // case 16:{
          //   Result = resp;
          //   yield put({ type: 'returnResult', Result });
          // } break;
        }
      } else {
        switch (playload.index) {
          case 12:
            {
              let Data = resp;
              yield put({ type: 'returnPage', Data });
            }
            break;
          case 18:
          case 14:
            {
              message.success('Success Operation!', 3);
              // window.location.href = '/CommunityManagement/FacilityBookings';
              history.push('/UserManagement/LIFEUserManagement');
            }
            break;
          case 13:
            {
              let DataSaveDetail = resp.data;
              yield put({ type: 'DataSaveDetail', DataSaveDetail });
            }
            break;
          case 16:
            {
              console.log('等待删除完成');
              var returnValue = resp;
              yield put({ type: 'ReturnValue', returnValue });
            }
            break;
          case 32:
            {
              let communityInfo = resp;
              yield put({ type: 'returnCommunityInfo', communityInfo });
            }
            break;
          case 39:
            {
              let memberResult = resp;
              yield put({ type: 'returnMemberResult', memberResult });
            }
            break;

          case 43:
            {
              let DataServices = resp;
              yield put({ type: 'returnDataServices', DataServices });
            }
            break;
        }
      }
    },

    *IA({ playload }, { call, put }) {
      var Data3 = null;
      yield put({ type: 'returnPage3', Data3 });
    },
    *IA2({ playload }, { call, put }) {
      var Data3Error = null;
      yield put({ type: 'returnData3Error', Data3Error });
    },

    *SA({ playload }, { call, put }) {
      var DataSave = playload;
      yield put({ type: 'returnDataSave', DataSave });
    },

    *CA({ playload }, { call, put }) {
      var sourceData = playload;
      yield put({ type: 'returnSource', sourceData });
    },

    *QA({ playload }, { call, put }) {
      var curString = playload;
      yield put({ type: 'returnCurString', curString });
    },

    *ResultClear({}, { put }) {
      var tmp = null;
      yield put({ type: 'returnResult', tmp });
    },

    *PAGE({ playload }, { put }) {
      var pageDate = playload;
      yield put({ type: 'PageDate', pageDate });
    },

    *DataClear({}, { put }) {
      var pageDate = null;
      yield put({ type: 'returnPage', pageDate });
    },

    *MemberResultClear({}, { put }) {
      let memberResult = null;
      yield put({ type: 'returnMemberResult', memberResult });
    },
  },
};
