import { message, Modal } from 'antd';
import { routerRedux } from 'dva/router';
import * as bpmService from '../../../services/common/bpm';
import * as formaInvoiceService from '../../../services/cooperate/finance/formaInvoice';
import { stringUtils } from '../../../utils';

export default {

  namespace: 'proFormaInv',

  state: {
    list: [],
    total: 0,
    mode: 'create',
    editBtn: true,
    apprBtn: true,
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/cooperate/finance/s-invoice') {
        }
      });
    },
  },

  effects: {
    * query({ payload }, { call, put, select }) {
      let { editBtn, apprBtn } = yield select(({ proFormaInv }) => proFormaInv);
      const perms = yield select(({ app }) => app.perms);
      if (perms) {
        for (const p of perms) {
          if (p.menuId === 'cp_fn_si_edit') {
            editBtn = false;
          } else if (p.menuId === 'cp_fn_si_approval') {
            apprBtn = false;
          }
        }
      }
      if (payload && !payload.bpmStatus) {
        payload.bpmStatus = 'NONE';
      }
      const data = yield call(formaInvoiceService.queryBasic, payload);
      if (data.success) {
        yield put({
          type: 'updateState',
          payload: {
            list: data.data.rows,
            total: data.data.total,
            editBtn,
            apprBtn,
          },
        });
        yield put(routerRedux.replace({
          pathname: location.pathname,
          search: '?' + stringUtils.JSON2QueryString(payload),
        }));
      } else {
        throw data;
      }
    },
    * getSingle({ payload }, { put, call }) {
      const { mode, id, row } = payload;
      if (mode === 'update') {
        // 审批状态为待提交和驳回状态下可以编辑
        if (row.approveStatus === '01') {
          return message.warn('该汇总形式发票在审批中,不可编辑');
        } else if (row.approveStatus === '02') {
          return message.warn('该汇总形式发票已审批通过,不可编辑');
        }

      } else if (mode === 'approval') {
        const data = yield call(formaInvoiceService.getSingle, id);
        if (data.success) {
          const status = yield call(bpmService.doCheckActived, data.data.prcId);
          if (status.success) {
            if (!status.data) {
              Modal.error({
                title: '友情提示',
                content: '您不是当前处理人',
                okText: '确认'
              });
              return false;
            }
          }
        }
      }
      yield put({
        type: 'updateState',
        payload: { mode },
      });
      yield put(routerRedux.push(`/cooperate/finance/s-invoice/${id}?mode=${mode}`));
    },

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

  reducers: {

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

  },
};
