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: 'Account',
  state: {
    Data: null,
    Result: null,
    DataSave: null,
    DataSaveDetail: null,
    Permission: 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 };
    },
    returnPermission(state, { Permission }) {
      return { ...state, Permission };
    },
  },

  effects: {
    //标准请求
    *RA({ playload }, { call, put }) {
      const resp = yield call(service.RA, playload);

      if (resp.code == 500 || resp.error_code != '0000') {
      }
      if (resp.error_code != '0000') {
        printf(playload, resp);
      } else {
        switch (playload.index) {
          case 41:
            {
              // let Data = resp.data;
              // yield put({ type: 'returnPage', Data });
            }
            break;
          case 38:
            {
              console.log(resp);
            }
            break;
          case 42:
            {
              // 保存成功， 清掉原来数据 并且跳转
              message.success('Submitted Successfully !');
              let Data = null;
              yield put({ type: 'returnPage', Data });
              history.go(-1);
            }
            break;
          case 45:
            {
              let Data = resp.data;
              yield put({ type: 'returnPage', Data });
            }
            break;
          case 51: // 获取后台所有账号
            {
              let Data = resp.data;
              yield put({ type: 'returnPage', Data });
            }
            break;
          case 52: // 修改账号状态
            {
              let Result = resp.error_code;
              yield put({ type: 'returnResult', Result });
            }
            break;
          case 54: // 账号权限
            {
              let Permission = resp.data;
              yield put({ type: 'returnPermission', Permission });
              // let Result = resp.error_code;
              // yield put({ type: 'returnResult', Result });
            }
            break;
        }
      }
    },

    *ReData({}, { put }) {
      var Data = null;
      yield put({ type: 'returnPage', Data });
    },

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

    *SA({ DataSave }, { call, put }) {
      console.log(DataSave);
      yield put({ type: 'returnDataSave', DataSave });
    },
  },
};
