import * as prepayService from '../../../../services/cooperate/order/prepayOrder';
import { routerRedux } from 'dva/router';
import { stringUtils, enums } from '@utils';
import { Modal } from 'antd';
const { entType, customMsg, payOrderStatus } = enums;

export default {
  namespace: 'prepayOrder',
  state: {
    list: [],
    total: 0,
    currentItem: {},
    mode: 'view',
    supplier: {},
    updateBtnFlag: false,
  },

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

  effects: {
    * query({ payload }, { call, select, put }) {
      let { updateBtnFlag } = yield select(({ prepayOrder }) => prepayOrder);
      const perms = yield select(({ app }) => app.perms);
      if (perms) {
        if (perms.find((x) => {
          return x.menuId === 'cp_order_ppo_update';
        })) {
          updateBtnFlag = true;
        }
      }
      yield put({
        type: 'updateState',
        payload: {
          updateBtnFlag,
        }
      });

      const data = yield call(prepayService.query, { ...payload });
      if (data.success) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.data.rows,
            total: data.data.total,
          },
        });
        yield put(routerRedux.replace({
          pathname: location.pathname,
          search: '?' + stringUtils.JSON2QueryString(payload),
        }));
      } else {
        throw data;
      }
    },

    * getSingle({ payload }, { put, call }) {
      const redirect = `/cooperate/order/ppo/${payload.id}?mode=${payload.mode}`;
      if (payload.mode === 'view') {
        yield put(routerRedux.push(redirect));
      } else if (payload.mode === 'update') {
        const data = yield call(prepayService.getSingle, payload.id);
        if (data.success && data.data) {
          const docStatus = data.data.docStatus;
          if (docStatus === payOrderStatus.DOC_STATUS_CANCEL) {
            Modal.error({ title: `${data.data.docStatusName} ${customMsg.PE_NOT_UPDATE}` });
          } else {
            yield put(routerRedux.push(redirect));
          }
        } else {
          throw data;
        }
      }
    },

    * pageDataClean({ payload }, { put, call, select, take }) {
      const { user } = yield select(({ app }) => app.user);
      yield put({
        type: 'updateState',
        payload: {
          supplier: {
            supplier: user.entType === entType.SUPP,
            suppName: user.name,
            docNo: stringUtils.queryURL('docNo'),
            docStatus: stringUtils.queryURL('docStatus'),
            suppCode: stringUtils.queryURL('suppCode'),
            contractCode: stringUtils.queryURL('contractCode'),
            docDateFrom: stringUtils.queryURL('docDateFrom'),
            docDateTo: stringUtils.queryURL('docDateTo'),
            payDateFrom: stringUtils.queryURL('payDateFrom'),
            payDateTo: stringUtils.queryURL('payDateTo'),
          },
        },
      });
    },

  },

  reducers: {
    querySuccess(state, action) {
      const { list, total } = action.payload;
      return {
        ...state,
        list,
        total,
      };
    },

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

  },
};
