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

import { routerRedux } from 'dva/router';

import { timestampToTime, timestampToTime4 } from '@/utils/time';
import { picFromate, Fromate } from '@/utils/method';

export default {
  namespace: 'CommunityService',
  state: {
    Data: null,
    DataSave: null,
    CurData: null,
    CurDataDetail: null,
    Data7: null,
    Data8: null,
    Result: null,
    DataSaveDetail: null,
  },

  reducers: {
    returnPage(state: object, { Data }: any) {
      return { ...state, Data };
    },
    returnDataSave(state: object, { DataSave }: any) {
      return { ...state, DataSave };
    },
    returnCurData(state: object, { CurData }: any) {
      return { ...state, CurData };
    },
    returnCurDataDetail(state: object, { CurDataDetail }: any) {
      console.log(state);
      return { ...state, CurDataDetail };
    },
    returnPage7(state: object, { Data7 }: any) {
      return { ...state, Data7 };
    },
    returnPage8(state: object, { Data8 }: any) {
      return { ...state, Data8 };
    },
    returnResult(state: object, { Result }: any) {
      return { ...state, Result };
    },
    returnDataSaveDetail(state: object, { DataSaveDetail }: any) {
      return { ...state, DataSaveDetail };
    },
  },

  effects: {
    //预订设施查询
    *RA({ playload }: any, { call, put }: any) {
      switch (playload.index) {
        case 40:
          {
            var Data = null;
            yield put({ type: 'returnPage', Data });
          }
          break;
      }
      const resp = yield call(service.RA, playload);
      console.log(resp);
      if (resp.code == 500 || resp.error_code != '0000') {
        // window.location.href = '/500';
        message.error(`${resp.error_code}:${resp.error_msg}`);
      }
      if (resp.error_code != '0000') {
        message.error(`${resp.error_code}:${resp.error_msg}`);
      } else {
        switch (playload.index) {
          case 21:
            {
              var tmp = resp.data.tosOwerModel;
              var tmp2 = resp.data;
              console.log(resp);
              if (!tmp) {
                message.error('Data loading error!');
                history.go(-1);
                return;
              }
              var CurDataDetail: {
                community: string;
                address: string;
                home: string;
                name: string;
                phone: string;
                email: string;
                content: string;
                replyContent: string;
                replyImgUrl: string[];
                status: number;
                pictrues: string[];
                time: string;
              } = {
                community: tmp.communityName,
                address: tmp.addressAndpostalCode,
                home: tmp.buildingNumber + ' #' + tmp.floorNumber + '-' + tmp.roomNumber,
                name: tmp.owerName,
                phone: tmp.owerPhone,
                email: tmp.owerEmail,
                content: tmp2.serviceContent,
                replyContent: tmp2.replyContent,
                replyImgUrl: Fromate(tmp2.replyImgUrl, [['url', null]]),
                status: tmp2.handleStatus,
                pictrues: picFromate(tmp2.imgUrl),
                time: timestampToTime4(tmp2.createTime.time),
              };
              yield put({ type: 'returnCurDataDetail', CurDataDetail });
            }
            break;
          case 33:
          case 34:
          case 22:
            {
              message.success('Success Operation!', 3);
              history.go(-1);
              // history.push("/CommercialService/CommunityMaintenance")
            }
            break;
          case 23:
            {
              let Data8 = resp;
              yield put({ type: 'returnPage8', Data8 });
            }
            break;
          case 31:
            {
              let Data7 = resp;
              yield put({ type: 'returnPage7', Data7 });
            }
            break;
          case 35:
            {
              let Result = resp;
              yield put({ type: 'returnResult', Result });
            }
            break;
          case 37:
            {
              let DataSaveDetail = resp;
              yield put({ type: 'returnDataSaveDetail', DataSaveDetail });
            }
            break;
          case 36:
            {
              let DataSaveDetail = resp;
              yield put({ type: 'returnDataSaveDetail', DataSaveDetail });
            }
            break;
          case 40:
            {
              let Data = resp;
              yield put({ type: 'returnPage', Data });
            }
            break;
        }
      }
    },

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

    *setCurData({ playload }: any, { call, put }: any) {
      var CurData = playload;
      yield put({ type: 'returnCurData', CurData });
    },

    *setCurDataDetail({ playload }: any, { call, put }: any) {
      var CurDataDetail = null;
      yield put({ type: 'returnCurDataDetail', CurDataDetail });
    },

    *ResultClear({ }, { put }: any) {
      var Result = null;
      console.log('清除');
      yield put({ type: 'returnResult', Result });
    },

    *DataSaveDetailClear({ }, { put }: any) {
      console.log('清楚数据');
      let DataSaveDetail = null;
      yield put({ type: 'returnDataSaveDetail', DataSaveDetail });
    },

    *DataSaveClear({ }, { put }: any) {
      let DataSave = null;
      yield put({ type: 'returnDataSave', DataSave });
    },

    *Data7Clear({ }, { put }: any) {
      let Data7 = null;
      yield put({ type: 'returnPage7', Data7 });
    },
  },
};
