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

import { routerRedux } from 'dva/router';

import { printf } from '@/utils/log';
import { getObjectInfo } from '@/utils/method';

export default {
  namespace: 'CellList',
  state: {
    Data: null,
    curString: {},
    Result: null,
    DataSave: null,
    detailData: {}, // 详情
    imgUrl: null, // 预览地址
    village: null, // 小区列表
    overCom: null,
  },

  reducers: {
    returnPage(state, { Data }) {
      return { ...state, Data };
    },
    returnResult(state, { Result }) {
      return { ...state, Result };
    },
    returnDataSave(state, { DataSave }) {
      return { ...state, DataSave };
    },
    returnDataSaveDetail(state, { detailData }) {
      return { ...state, detailData };
    },
    returnImgurl(state, { imgUrl }) {
      return { ...state, imgUrl };
    },
    returnVillage(state, { village }) {
      return { ...state, village };
    },
    overComList(state, { overCom }) {
      return { ...state, overCom };
    },
  },

  effects: {
    //标准请求
    *RA({ playload }, { call, put }) {
      const resp = yield call(service.RA, playload);
      // if (resp.code == 500||resp.error_code!="0000") {
      // }
      if (resp.code == 500 && resp.error_code != '0000') {
        getObjectInfo(playload.body);
        printf(playload, resp);
        message.error(`${resp.code}:${resp.msg}`);
      } else if (resp.error_code == '0001') {
        getObjectInfo(playload.body);
        printf(playload, resp);
        message.error(`${resp.error_code}:${resp.error_msg}`);
      } else {
        switch (playload.index) {
          case 24:
            {
              let CommunityList = null;
              yield put({ type: 'Init/returnCommunityList', CommunityList });
              let Data = resp.data;
              CommunityList = resp.data.communityList;
              yield put({ type: 'returnPage', Data });
              yield put({ type: 'Init/returnCommunityList', CommunityList });
            }
            break;
          case 29: // 小区新增编辑
            {
              let Result = resp;
              yield put({ type: 'returnResult', Result });
              // 新增成功清空Data 用来刷新最新数据
              let Data = null;
              let village = null;
              yield put({ type: 'returnPage', Data });
              yield put({ type: 'returnVillage', village });
              // 提示跳转
              message.success(`Information saved successfully!`);
              history.push('/CommunityManagement/CellList');
            }
            break;
          case 32: // 获取小区详情
            {
              let detailData = resp.data;
              yield put({ type: 'returnDataSaveDetail', detailData });
            }
            break;
          case 47: // 图片
            {
              let imgUrl = {
                url: resp.data,
                name: playload.body.fileName,
              };
              yield put({ type: 'returnImgurl', imgUrl });
            }
            break;
          case 48: {
            // 小区列表
            let village = resp.data;
            yield put({ type: 'returnVillage', village });
            break;
          }
          case 49: {
            // 关闭小区
            let overCom = resp.data;
            yield put({ type: 'overComList', overCom });
            // 关闭成功清空Data 用来刷新最新数据
            let Data = null;
            let village = null;
            yield put({ type: 'returnPage', Data });
            yield put({ type: 'returnVillage', village });
            break;
          }
        }
      }
    },

    *urlRemove({}, { put }) {
      let imgUrl = null;
      yield put({ type: 'returnImgurl', imgUrl });
    },

    *comRemove({}, { put }) {
      let nus = null;
      yield put({ type: 'overComList', nus });
    },

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

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