import * as returnOrderService from '../../../services/cooperate/order/returnOrder';
import { routerRedux } from 'dva/router';

export default {

  namespace: 'returnOrderForm',

  state: {
    list: [{
      productCode: 'RT1505426OH00489',
      productUnit: '2018-03-15',
      productClass1: '待确认',
      productModel: '2018-03-13',
      amount: '9536.00',
      requestDate: '2018-04-10',
      bidDeadline: '上海武林食品有限公司',
      productName: '上海埃林哲餐饮管理股份有限公司'
    }],
    total: 1,
    currentItem: {},
    modalVisible: false,
    mode: 'create',
    filterParams: {},
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if (location.pathname === '/cooperate/order/ro') {
          // dispatch({ type: 'clean' });
          // dispatch({ type: 'query' });
        }
      });
    },
  },

  effects: {
    * query({ payload }, { call, put, select }) {
      const { filterParams } = yield select(({ returnOrderForm }) => returnOrderForm);
      const data = yield call(returnOrderService.query, { ...filterParams, ...payload });
      if (data.success) {
        yield put({
          type: 'updateState',
          payload: {
            list: data.data.rows,
            total: data.data.total,
            filterParams: { ...filterParams, ...payload },
          },
        });
      } else {
        throw data;
      }
    },

    * getSingle({ payload }, { put }) {
      yield put({
        type: 'updateState',
        payload: {
          mode: payload.mode,
        },
      });
      yield put(routerRedux.push(`/cooperate/order/ro/${payload.id}?mode=view`));
    },

    * delete({ payload }, { call, put }) {
      const data = yield call(returnOrderService.remove, [...payload.ids]);
      if (data.success) {
        yield put({ type: 'query' });
      } else {
        throw data;
      }
    },

    * create({ payload }, { put, call }) {
      const data = yield call(returnOrderService.create, payload);
      if (data.success) {
        yield put({ type: 'hideModal' });
        yield put({ type: 'query' });
      } else {
        throw data;
      }
    },

    * update({ payload }, { put, call, select }) {
      const id = yield select(({ tenderForm }) => tenderForm.currentItem.id);
      const newData = { ...payload, id };
      const data = yield call(returnOrderService.update, newData);
      if (data.success) {
        yield put({ type: 'hideModal' });
        yield put({ type: 'query' });
      } else {
        throw data;
      }
    },

    * hideModal({ payload }, { call, put }) {
      yield put({
        type: 'updateState',
        payload: {
          modalVisible: false,
          currentItem: {},
        },
      });
    },

    * clean({ payload }, { call, put }) {
      yield put({
        type: 'updateState',
        payload: {
          list: [],
          total: 0,
          currentItem: {},
          modalVisible: false,
          mode: 'create',
          filterParams: {},
        },
      });
    },
  },

  reducers: {

    updateState(state, { payload }) {
      return { ...state, ...payload };
    },

    showModal(state, action) {
      return { ...state, ...action.payload, modalVisible: true };
    },

  },
}
