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,

    Data4Error: null,
  },

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

  effects: {
    *RA({ playload }: { playload: { index: number } }, { call, put }: any) {
      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';
        // message.error(resp.msg);
      }

      if (resp.error_code != '0000') {
        message.error(`${resp.error_code}:${resp.error_msg}`);
        var Data3Error = null;
        var Result = null;
        let Data4Error = null;
        switch (playload.index) {
          case 11:
            {
              Data3Error = { msg: 'Information Wrong!' };
              yield put({ type: 'returnData3Error', Data3Error });
            }
            break;
          case 13:
            {
              // message.error('Request exception please retry!!!');
              history.push('/UserManagement/LIFEUserManagement');
            }
            break;
          // 删除失败的案例
          case 16:
            {
              Result = resp;
              yield put({ type: 'returnResult', Result });
            }
            break;
          case 14:
            {
              Data4Error = { msg: 'The community or unit is already registered!' };
            }
            yield put({ type: 'returnData4Error', Data4Error });
            break;
          case 18:
          case 53:
            {
              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!');
              // 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;
          case 44: {
            let Result = resp;
            yield put({ type: 'returnResult', Result });
          }
        }
      }
    },

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

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

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

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

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

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

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

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

    *Data4ErrorClear({ }, { put }: any) {
      let Data4Error = null;
      yield put({ type: 'returnData4Error', Data4Error });
    },

    *ClearResult({ }, { put }: any) {
      let Result = null;
      yield put({ type: 'returnResult', Result });
    },

    *ClearReturnValue({ }, { put }: any) {
      let returnValue = null;
      yield put({ type: "ReturnValue", returnValue })
    }
  },
};
