import * as service from '../services/tos';
import { message } from 'antd';

import { routerRedux } from 'dva/router';

export default {
  namespace: 'ServiceProvider',
  state: {
    Data: null,
    CurDataFollow: null,
    SaveChooseData: null,
  },

  reducers: {
    returnPage(state, { Data }) {
      console.log(state);
      console.log(Data);
      return { ...state, Data };
    },

    returnCurDataFollow(state, { CurDataFollow }) {
      return { ...state, CurDataFollow };
    },

    returnCurDataFollowDetail(state, { CurDataFollowDetail }) {
      return { ...state, CurDataFollowDetail };
    },
    returnSaveChooseData(state, { SaveChooseData }) {
      return { ...state, SaveChooseData };
    },
  },

  effects: {
    // 获取服务商
    *TosTosServiceProviderGet({ playload }, { call, put }) {
      const resp = yield call(service.TosTosServiceProviderGet, playload);
      console.log(resp);
      if (resp.error_code == '0000') {
        let Data = resp.data;
        yield put({ type: 'returnPage', Data });
      } else {
        console.log('请求错误码:' + resp.error_code);
        let Data = null;
        console.log(playload);
        yield put({ type: 'returnPage', Data });
      }
    },
    // 新建服务商
    *TosTosServiceProviderSave({ playload }, { call, put }) {
      console.log(playload);
      const resp = yield call(service.TosTosServiceProviderSave, playload);
      console.log(resp);
      if (resp.code == 500) {
        window.location.href = '/500';
      } else {
        message.success('operator success!', 2);
        window.location.href = '/UserManagement/ServiceProviderManagement';
      }
    },

    //获取服务商保安根据服务商名
    *TosSecurityGuarderGet({ playload }, { call, put }) {
      console.log(playload);
      const resp = yield call(service.TosSecurityGuarderGet, playload);
      console.log(resp);
      let CurDataFollow = resp.data.rows;
      yield put({ type: 'returnCurDataFollow', CurDataFollow });
    },

    //获取服务商保安详情根据服务商名
    *GuarderById({ playload }, { call, put }) {
      let CurDataFollowDetail = playload;
      yield put({ type: 'returnCurDataFollowDetail', CurDataFollowDetail });
    },
    *SaveChooseData({ playload }, { call, put }) {
      let SaveChooseData = playload;
      yield put({ type: 'returnSaveChooseData', SaveChooseData });
    },
  },
};
