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

import { routerRedux } from 'dva/router'

import {Fromate} from '@/utils/method'
import { timeToMoment } from '@/utils/time';
export default {
  namespace: 'FacilityBookings',
  state: {
    Data: {data:[],total:0},
    Data2: {data:[],total:0},
    Data3: null,
    DataSave: null,
    DataSaveDetail: null,

    sourceData: null,
    curString: {
      subscribeDate: null,
      status: 0,
      communityNameList: null,
      tab:1,
      curPage: 1,
      curPage2: 1
    },
    
    Result:null,

    // 不知道前面是干嘛的，自增公告列表
    NoticeList:[]
  },

  reducers: {
    // 自增公告列表
    retuNotice(state,{ payload }:{payload:any}){
      let NoticeList = payload;
      return { ...state, NoticeList}
    },
    ///


    returnPage(state, { Data,DataPage,DataSave}) {
      return { ...state, Data,DataPage,DataSave };
    },
    returnPage2(state, { Data2 }) {
      return { ...state, Data2 };
    },
    returnPage3(state, { Data3 }) {
      return { ...state, Data3 };
    },    
    returnDataSave(state, { DataSave }) {
      return { ...state, DataSave};
    },
    DataSaveDetail(state, { DataSaveDetail,}) {
     return  { ...state, DataSaveDetail };
    },
    returnSource(state, { sourceData }) {
      return { ...state, sourceData };
    },
    returnCurString(state, { curString }) {
      return { ...state, curString };
    },
    returnResult(state, { Result }) {
      return { ...state, Result };
    }

  },

  effects: {
    
    //预订设施查询
    *RA({ playload }, { call, put }) {
      
      const resp = yield call(service.RA, playload);
     
      if (resp.code == 500||resp.error_code!="0000") {
        // window.location.href = '/500';
      }
      if (resp.error_code != "0000") {
        console.log("请求错误码:"+"("+playload.index+")" + resp.error_code)
        console.log(playload)
        var Result = null
        switch (playload.index) {
          case 1:
            // message.error("Could't not found the facility!")
            Result ={index:playload.index,msg:"Could't not found the facility!"};
            yield put({ type: 'returnResult', Result });
            break;     
          case 11:
            Result ={index:playload.index,msg:"Information Wrong!"};
            yield put({ type: 'returnResult', Result });
            break;                
        }
      }    
      else {    
        // console.log('===========================')  
        // console.log(playload)  
        switch (playload.index) {
          case 0:
          case 9:
            {
              let Data = {data:resp.data.list,total:resp.data.page} ; 
              yield put({ type: 'returnPage', Data });
            }
            break;
            case 1:
            {
                let Data3 = resp.data;               
                yield put({ type: 'returnPage3', Data3});
            }break;
            
          case 2:{
            let DataSaveDetail = resp.data; 
            yield put({ type: 'DataSaveDetail', DataSaveDetail });
          } break;
          case 3:
          case 4: 
          case 5:
          case 11:         
          case 6:
          case 17:{          
            message.success("Success Operation!", 3)
            // var Result=resp
            // yield put({ type: 'returnResult', Result })           
            history.push("/CommunityManagement/FacilityBookings")
          } break;
          case 7: {
            let tmp = resp.data; 
            const reg = [["url", "categoriesImageUrl"], ["name", "categoriesName"]]
            const reg2 = [["url", null]]
            let sourceData = {
              canReservationDay: tmp.canReservationDay,
              canReservationNum: tmp.canReservationNum,
              cancelReservationDay: tmp.cancelReservationDay,
              categoriesOpenTime: timeToMoment(tmp.categoriesOpenTime),
              communityManagerFee: tmp.communityManagerFee,
              communityMargin: tmp.communityMargin,
              communityName: tmp.communityName,
              facilityName: tmp.facilityName,
              nomarginCancelReservationDay: tmp.nomarginCancelReservationDay,
              periodType: tmp.periodType,
              reservationQuantumTime: tmp.reservationQuantumTime,
              categoriesName: Fromate(tmp.categoriesList, reg),
              categoriesDetailsImageName:Fromate(tmp.facilitiesImageList,reg2)
            } 
            console.log(sourceData)

            yield put({ type: 'returnSource', sourceData });
          } break;
          case 8: {
            let Data2 = {data:resp.data.list,total:resp.data.page}; 
            yield put({ type: 'returnPage2', Data2, });            
          } break;

          // 自增公告列表
          case 25:{
            let NoticeList = resp.data;
            yield put({ type: 'retuNotice',payload:NoticeList,  });  
          }
        }

      }
    },

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

    *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}) {      
      yield put({type: 'returnResult', null} )
    },   
    
  },

};
