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

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

  reducers: {
    returnPage(state: object, { Data }: any) {
      return { ...state, Data };
    },

    returnCurDataFollow(state: object, { CurDataFollow }: any) {
      return { ...state, CurDataFollow };
    },

    returnCurDataFollowDetail(state: object, { CurDataFollowDetail }: any) {
      return { ...state, CurDataFollowDetail };
    },
    returnSaveChooseData(state: object, { SaveChooseData }: any) {
      return { ...state, SaveChooseData };
    },
    returnCurData(state: object, { CurData }: any) {
      console.log(CurData)
      return { ...state, CurData };
    },
    returnResult(state: object, { Result }: any) {
      return { ...state, Result };
    },
  },

  effects: {
    // 获取服务商
    *TosTosServiceProviderGet({ playload }: any, { call, put }: any) {
      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);
        console.log('请求错误码:' + resp.error_code);
        message.error(resp.error_msg);
        let Data = undefined;
        console.log(playload);
        yield put({ type: 'returnPage', Data });
      }
    },
    // 新建服务商
    *TosTosServiceProviderSave({ playload }: any, { call, put }: any) {
      console.log(playload);
      const resp = yield call(service.TosTosServiceProviderSave, playload);
      console.log(resp);

      // 服务范围选择错误
      if (resp.error_code === "0002") {
        let CurData: Boolean = false;
        yield put({ type: "returnCurData", CurData });
        // message.error("Service scope selection error!!!")
        Modal.error({
          title: 'Service scope selection error',
          content: 'Accounting service / security  service provider is a single option!!!',
        });
        return;
      }

      // 小区选择错误
      if (resp.error_code === "0003") {
        let CurData: Boolean = false;
        yield put({ type: "returnCurData", CurData });
        Modal.error({
          title: 'Community selection error',
          content: 'You cannot select a community that already has a scope of service!!!',
        });
        return;
      }

      // 提交失败
      if (resp.error_code === "0001") {
        let CurData: Boolean = false;
        yield put({ type: 'returnCurData', CurData });
        message.error('Creation failed. Please try again!');
        return;
      }

      // 创建成功
      if (resp.error_code === "0000") {
        message.success('Operator Success!', 1.5, () => {
          window.location.href = '/UserManagement/ServiceProviderManagement';
        });
        let CurData: Boolean = true;
        yield put({ type: 'returnCurData', CurData });
        return;
      }
    },

    //获取服务商保安根据服务商名
    *TosSecurityGuarderGet({ playload }: any, { call, put }: any) {
      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 }: any, { call, put }: any) {
      let CurDataFollowDetail = playload;
      yield put({ type: 'returnCurDataFollowDetail', CurDataFollowDetail });
    },

    *SaveChooseData({ playload }: any, { call, put }: any) {
      let SaveChooseData = playload;
      yield put({ type: 'returnSaveChooseData', SaveChooseData });
    },

    // 注销服务商
    *DelServiceProvider({ playload }: any, { call, put }: any) {
      const resp = yield call(service.TosTosServiceProviderDel, playload);
      if (resp.error_code === '0000') {
        let Result = true;
        yield put({ type: 'returnResult', Result });
      } else {
        let Result = false;
        yield put({ type: 'returnResult', Result });
      }
    },

    *ResultClear({ playload }: any, { call, put }: any) {
      console.log('清除数据');
      let Result = null;
      yield put({ type: 'returnResult', Result });
    },

    *DelGuarder({ playload }: any, { call, put }: any) {
      console.log('删除保安');
      console.log(playload);
      const res = yield call(service.TosGuarderDel, playload);
      console.log(res);
      if (res.error_code === '0000') {
        message.success('Safer information deleted successfully!!!');
        history.go(-1);
      } else {
        message.error('Deletion failed,Please try again');
      }
    },

    *CurDataClear({ playload }: any, { call, put }: any) {
      console.log('清除数据');
      let CurData = null;
      yield put({ type: 'returnCurData', CurData });
    },
  },
};
