import * as prepayService from '../../../../services/cooperate/order/prepayOrder';
import { routerRedux } from 'dva/router';
import pathToRegexp from 'path-to-regexp';
import { dataUtils, enums } from '@utils';
import { Modal } from 'antd';
const { customMsg, payOrderStatus, viewMode } = enums;

export default {
  namespace: 'prepayOrderDetail',
  state: {
    formData: {},
    mode: 'view',
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen((location) => {
        const match = pathToRegexp('/cooperate/order/ppo/:id').exec(location.pathname);
        if (match) {
          dispatch({ type: 'updateState', payload: dataUtils.queryStringToJSON(location.search) || { from: '' } });
          dispatch({ type: 'querySingle', payload: { id: match[1] } });
        }
      });
    },
  },

  effects: {
    * querySingle({ payload }, { call, put, select }) {
      let { mode } = yield select(({ prepayOrderDetail }) => prepayOrderDetail);
      const data = yield call(prepayService.getSingle, payload.id);
      if (data.success) {
        const { contractId, contractName, contractCode } = data.data;
        const contract = { id: contractId, name: contractName, code: contractCode };
        data.data.contract = contract;

        const formatter = (value) => {
          return value ? `${parseFloat(value).toFixed(2)}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',') : '';
        };
        data.data.payAmt = formatter(data.data.payAmt);
        data.data.netAmt = formatter(data.data.netAmt);

        const docStatus = data.data.docStatus;
        if (mode === viewMode.UPDATE && docStatus === payOrderStatus.DOC_STATUS_CANCEL) {
          Modal.error({ title: `${data.data.docStatusName} ${customMsg.PE_NOT_UPDATE}` });
          mode = viewMode.VIEW;
        }

        yield put({
          type: 'updateState',
          payload: {
            mode,
            formData: data.data,
          },
        });
      } else {
        throw data;
      }
    },

    * update({ payload }, { select, call, put }) {
      const { formData } = yield select(({ prepayOrderDetail }) => prepayOrderDetail);
      const { contract } = formData;
      formData.contractId = contract.id;
      const formatter = (value) => {
        return value.replace(/\$\s?|(,*)/g, '');
      };
      formData.payAmt = formatter(formData.payAmt);
      formData.netAmt = formatter(formData.netAmt);
      const data = yield call(prepayService.update, formData);
      if (data.success) {
        yield put({ type: 'cancel' });
      } else {
        throw data;
      }
    },

    * cancel({ payload }, { put }) {
      yield put(routerRedux.goBack());
    },

    * clean({ payload }, { call, put }) {
      yield put({
        type: 'updateState',
        payload: {
          formData: {},
          mode: 'view',
        },
      });
    },
  },

  reducers: {
    updateForm(state, { payload }) {
      const { key, value } = payload;
      const formData = state.formData;
      const newFormData = Object.assign({}, formData);
      newFormData[key] = value;
      return {
        ...state,
        formData: newFormData,
      };
    },

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

  },
};
