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: 'Contract',
  state: {
    Data: { data: [], total: 0 },
    curString:{},
    Result: null,
    DataSave: null,
    DataSaveDetail:null,
    playload:{}
  },

  reducers: {
    returnPage(state, { Data}) {
      return { ...state, Data };
    },
    returnResult(state, { Result}) {
      return { ...state, Result};
    },
    returnDataSave(state, { DataSave }) {
      return {...state,DataSave}
    },
    returnDataSaveDetail(state, { DataSaveDetail }) {
      return {...state,DataSaveDetail}
    },
    returnPath(state, { playload}) {
      return { ...state, playload };
    },
  },

  effects: {
    
    //预订设施查询
    *RA({ playload }, { call, put }) {
      console.log("开始请求")
      const resp = yield call(service.RA, playload);
      console.log(resp)
      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)

      }    
      else {      
        switch (playload.index) {

          case 19:
            {
                let Data = resp.data;
                yield put({ type: 'returnPage', Data});
            } break;
          case 20:
            {
              let DataSaveDetail = resp.data;
              yield put({ type: 'returnDataSaveDetail', DataSaveDetail});
              
            }break;
          case 27: {
            message.success('save success !');
            var tmp=resp
            yield put({ type: 'returnResult', tmp })
            setTimeout(function(){
              history.goBack()
            },1000)
          } break;
        }

      }
    },

    // url 带参数
    *RA2({ playload }, { call, put }){
      console.log("开始请求")
      const resp = yield call(service.RA2, playload);
      console.log(resp)
    },

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

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

    *Paths({ playload }, { put }){
      yield put({type: 'returnPath', playload} )
    }

  },

  subscriptions: {
    setup({ dispatch, history }) {
        // 监听 路由
        return history.listen(({ pathname }) => {
          let path = {
            path : pathname
          }
            dispatch({
                type: 'Paths',    // 选择返回值
                playload:{
                  path
                },
            })
        });
    }
  }

};
