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';

export default {
  namespace: 'Contract',
  state: {
    Data: null,
    curString: {},
    Result: null,
    DataSave: null,
    DataSaveDetail: null,
    playload: {},
    // 图片列表
    FileImg: null,
  },

  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 };
    },

    // 详情
    FileListImg(state, { FileImg }) {
      return { ...state, FileImg };
    },
  },

  effects: {
    //预订设施查询
    *RA({ playload }, { call, put }) {
      console.log('开始请求');
      const resp = yield call(service.RA, playload);
      if (resp.error_code != '0000') {
        printf(playload, resp);
        message.error(`${resp.error_code}:${resp.error_msg}`);
      } 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 !');
              let tmp = resp;
              let Data = null;
              yield put({ type: 'returnResult', tmp });
              // 保存成功 清空列表 以便列表拿取最新数据
              yield put({ type: 'returnPage', Data });

              setTimeout(function () {
                history.goBack();
              }, 500);
            }
            break;
          case 47:
            {
              var FileImg = resp.data;
              yield put({ type: 'FileListImg', FileImg });
            }
            break;
        }
      }
    },

    *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 });
    },

    // 详情
    *DataSaveDetail({ playload }, { put }) {
      let DataSaveDetail = playload.record;
      yield put({ type: 'returnDataSaveDetail', DataSaveDetail });
    },

    // 清掉图片列表
    *FileImgs({ playload }, { put }) {
      let FileImg = null;
      yield put({ type: 'FileListImg', FileImg });
    },
  },

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