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,

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

    returnAddAccount(state, result) {
      let SubResult = { ...state.SubResult, ...result.Data };
      return { ...state, SubResult };
    },

    returnDelccount(state, { SubResult }) {
      return { ...state, SubResult };
    },
  },

  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);
        message.error(`${resp.error_code}:${resp.error_msg}`);
      } else {
        switch (playload.index) {
          case 41:
            {
              // let Data = resp.data;
              // yield put({ type: 'returnPage', Data });
            }
            break;
          case 38:
            {
              let Data = {
                statr: 0,
              };
              yield put({ type: 'returnAddAccount', Data });
            }
            break;
          case 42:
            {
              let Data = {
                desc: 0,
              };
              yield put({ type: 'returnAddAccount', Data });
            }
            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 });
    },

    // 清除
    *AccountClear({}, { put }) {
      var SubResult = null;
      yield put({ type: 'returnDelccount', SubResult });
    },
  },
};
