import * as edService from '../../../services/quality/ed/ed'
import * as bpmService from '../../../services/common/bpm';
import { routerRedux } from 'dva/router';
import { message} from 'antd';
import { stringUtils, enums } from '@utils';

const { entType } = enums;

export default {
  namespace: 'ed',
  state: {
    list: [],
    total: 0,
    currentItem: {},
    filterParams: {},
    mode: 'create',
    createBtn: true, // 是否隐藏按钮 true隐藏 false不隐藏
    editBtn: true,
    approvalBtn: true,
    disableBtn: true,
    showUploadModal: false,
    supplier: false,
  },

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

  effects: {
    * query({ payload }, { call, select, put}) {
      let { createBtn, editBtn, approvalBtn, disableBtn } = yield select(({ ed }) => ed);
      const perms = yield select(({ app }) => app.perms);

      if (perms) {
        for (const p of perms) {
          if (p.menuId === 'qa_edo_add') {
            createBtn = false;
          } else if (p.menuId === 'qa_edo_update') {
            editBtn = false;
          } else if (p.menuId === 'qa_edo_approval') {
            approvalBtn = false;
          } else if (p.menuId === 'qa_edo_disable') {
            disableBtn = false;
          }
        }
      }
      const currentItem = yield select(({ ed }) => ed.currentItem);
      const data = yield call(edService.query, { ...currentItem, ...payload });

      if (data.success) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.data.rows,
            total: data.data.total,
            createBtn,
            editBtn,
            approvalBtn,
            disableBtn,
          },
        })
        yield put(routerRedux.replace({
          pathname: location.pathname,
          search: '?' + stringUtils.JSON2QueryString(payload),
        }));
      } else {
        throw data
      }
    },

    * getSingle({ payload }, { put,call ,select}) {
      yield put({
        type: 'updateState',
        payload: {
          mode: payload.mode,
        },
      });
      if (payload.mode === 'create') {
        yield put(routerRedux.push('/quality/edo/create'));
      } else {
        const edData = yield call(edService.getSingle, payload.id);
        if (edData.success) {
          if (payload.mode === 'update') {
            if (edData.data.reportStatus === '01') {
              if (edData.data.approveStatus === '00') {
                yield put(routerRedux.push(`/quality/edo/${payload.id}?mode=update`));
              } else {
                message.error('该报告审批状态不能修改');
              }
            } else {
              message.error('该报告状态不能修改');
            }
          }

          if (payload.mode === 'approval') {
            if (edData.data.approveStatus !== '01') {
              message.error('该报告状态不能进行审批');
            } else {
              const status = yield call(bpmService.doCheckActived, edData.data.prcId);
              if (status.success) {
                if (status.data) {
                  yield put(routerRedux.push(`/quality/edo/${payload.id}?mode=approval`));
                } else {
                  message.error('您不是当前处理人，不能进行审批');
                }
              }
            }
          }
        }

        if (payload.mode === 'view') {
          yield put(routerRedux.push(`/quality/edo/${payload.id}?mode=view`));
        }
      }
    },

    * pageDataClean({ payload }, { put, call, select, take }) {
      const { user } = yield select(({ app }) => app.user);
      yield put({
        type: 'updateState',
        payload: {
          supplier: user.entType === entType.SUPP,
          currentItem: {
            reportCode: stringUtils.queryURL('reportCode'),
            dutyType: stringUtils.queryURL('dutyType'),
            reportDateFrom: stringUtils.queryURL('reportDateFrom'),
            reportDateTo: stringUtils.queryURL('reportDateTo'),
            approveStatus: stringUtils.queryURL('approveStatus'),
            activeTaskKey: stringUtils.queryURL('activeTaskKey'),
            bpmStatus: stringUtils.queryURL('bpmStatus'),
          },
        },
        showUploadModal: false,
      });
    },

    * delete({ payload }, { call, put }) {
      const suppData = yield call(edService.getSingle, payload.id);
      if (suppData.success) {
        if (suppData.data.reportStatus === '03') {
          return message.error('该报告状态不能作废');
        }else{
          payload.function();
        }
      }
    },

    * disable({ payload }, { call, put }) {
      const data = yield call(edService.disable, payload.id);
      if (data.success) {
        yield put({ type: 'query' });
      } else {
        throw data;
      }
    },


  },

  reducers: {

    querySuccess(state, action) {
      const { list, total ,createBtn, editBtn,approvalBtn,disableBtn, } = action.payload
      return {
        ...state,
        list,
        total,
        createBtn,
        editBtn,
        approvalBtn,
        disableBtn,
      }
    },

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

    showUploadModal(state, action) {
      return {...state, showUploadModal: action.payload};
    },

  },
}
