import * as bidService from '../../../services/source/bids';
import { routerRedux } from 'dva/router';
import { message } from 'antd';
import { enums, stringUtils } from '@utils';
const { bidStatus,bidInfoStatus,entType} = enums;
export default {

  namespace: 'bidForm',

  state: {
    list: [],
    total: 1,
    currentItem: {},
    modalVisible: false,
    mode: 'create',
    filterParams: {},
    suppFlag: false,
    editBtnFlag: false,
    viewBtnFlag: false
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if (location.pathname === '/source-mgmt/bid-mgmt/bids/bids') {
          // dispatch({ type: 'clean' });
          // dispatch({ type: 'query' });
        }
      });
    },
  },

  effects: {
    * query({ payload }, { call, put, select }) {
      // 角色判断
      /*soum_bm_bs_view/soum_bm_bs_update*/
      let {  editBtnFlag, viewBtnFlag} = yield select(({ bidForm }) => bidForm);
      const perms = yield select(({ app }) => app.perms);
      // console.log('perms==>', perms)
      if (perms) {
        if (perms.find((x) => {
            return x.menuId === 'soum_bm_bs_update';
          })) {
          editBtnFlag = true;
        }
        if (perms.find((x) => {
            return x.menuId === 'soum_bm_bs_view';
          })) {
          viewBtnFlag = true;
        }
      }
      const currentUser = yield select(({app}) => app.user);
      const suppFlag = (entType.SUPP === currentUser.user.entType) ? true : false;
      const queryparam = suppFlag?{...payload, ...{suppCodeName:currentUser.user.name}}:payload;
      // console.log('queryparam=>',queryparam)
      const data = yield call(bidService.query, queryparam );
      if (data.success) {
        yield put({
          type: 'updateState',
          payload: {
            list: data.data.rows,
            total: data.data.total,
            suppFlag,
            editBtnFlag,
            viewBtnFlag
          },
        });
        yield put(routerRedux.replace({
          pathname: location.pathname,
          search: '?' + stringUtils.JSON2QueryString(queryparam),
          // state: { ...payload },
        }));
      } else {
        throw data;
      }
    },
    * querySingle({ payload }, { call, put }) {
      const data = yield call(bidService.getSingle, payload.id);
      if (data.success) {
        yield put({
          type: 'updateState',
          payload: {
            mode: payload.mode,
          },
        });
      } else {
        throw data;
      }
    },
    * getSingle({ payload }, { select, put, call }) {
      yield put({
        type: 'updateState',
        payload: {
          mode: payload.mode,
        },
      });
      const formData =  yield call(bidService.getSingle, payload.id);
      if (formData.success) {
        // console.log('formdata==>',formData)
        if (payload.mode === 'view') {
          const currentUser = yield select(({ app }) => app.user);
          if (entType.SUPP === currentUser.user.entType) {
            yield put(routerRedux.push(`/source-mgmt/bid-mgmt/bids/bids/${payload.id}?mode=view`));
          } else {
            if (bidStatus.TENDER === formData.data.tenderStatus) {
              message.warn('本标尚未开标，不能查看投标信息');
            } else {
              yield put(routerRedux.push(`/source-mgmt/bid-mgmt/bids/bids/${payload.id}?mode=view`));
            }
          }
        } else if (payload.mode === 'update') {
          // console.log('update==>',formData)
          const currentUser = yield select(({ app }) => app.user);
          if (entType.SUPP === currentUser.user.entType) {
            if (formData.data.docStatus === bidInfoStatus.WAITING ) {
              if(formData.data.createUserId === currentUser.user.id){
                yield put(routerRedux.push(`/source-mgmt/bid-mgmt/bids/bids/${payload.id}?mode=update`));
              }else{
                message.warn('非创建人不可做修改');
              }
            } else if (formData.data.docStatus === bidInfoStatus.SUBMITTED ) {
              message.warn('该投标单已提交，不可再修改');
            }else if (formData.data.docStatus === bidInfoStatus.CLOSED ) {
              message.warn('该投标单已关闭，不可再修改');
            }
          }else{
            message.error('只能供应商修改自己的投标信息')
          }
        }
      } else {
        message.error(formData.message);
      }
    },
    * delete({ payload }, { call, put }) {
      const data = yield call(bidService.remove, [...payload.ids]);
      if (data.success) {
        yield put({ type: 'query' });
      } else {
        throw data;
      }
    },
    * create({ payload }, { put, call }) {
      const data = yield call(bidService.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(({ bidForm }) => bidForm.currentItem.id);
      const newData = { ...payload, id };
      const data = yield call(bidService.update, newData);
      if (data.success) {
        yield put({ type: 'hideModal' });
        yield put({ type: 'query' });
      } else {
        throw data;
      }
    },
    * showPop({ payload }, { put, call }) {
      yield put({
        type: 'showModal',
        payload: {
          mode: payload.mode,
        }
      });
    },
    * 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 };
    },

  },
}
