import { ReduxAction, ReduxSagaEffects } from '@interfaces';
import { message } from 'antd';
import { routerRedux } from 'dva/router';
import * as usersService from '../../../services/contract-mgmt/contract-tmpls/contractTmpls';
import { stringUtils } from '../../../utils';

export default {
  namespace: 'contractTmpls',
  state: {
    list: [],
    total: 0,
    currentItem: {},
    modalVisible: false,
    mode: 'create',
    addBtnFlag: false,
    editBtnFlag: false,
    viewBtnFlag: false,
    deleteBtnFlag: false
  },

  subscriptions: {
    setup({ dispatch, history }: object) {
      history.listen((location) => {
        // if (location.pathname === '/contract-mgmt/contract-tmpls') {
        // }
      });
    },
  },

  effects: {
    * query({ payload }: ReduxAction, { call, put, select }: ReduxSagaEffects) {
      // 角色判断 ctm_ctt_add/ ctm_ctt_edit/ ctm_ctt_view/ ctm_ctt_delete
      let { addBtnFlag, editBtnFlag, viewBtnFlag,  deleteBtnFlag } = yield select(({ contractTmpls }) => contractTmpls);
      const perms = yield select(({ app }) => app.perms);
      // console.log('perms==>', perms)
      if (perms) {
        if (perms.find((x) => {
            return x.menuId === 'ctm_ctt_add';
          })) {
          addBtnFlag = true;
        }
        if (perms.find((x) => {
            return x.menuId === 'ctm_ctt_edit';
          })) {
          editBtnFlag = true;
        }
        if (perms.find((x) => {
            return x.menuId === 'ctm_ctt_view';
          })) {
          viewBtnFlag = true;
        }
        if (perms.find((x) => {
            return x.menuId === 'ctm_ctt_delete';
          })) {
          deleteBtnFlag = true;
        }
      }
      yield put(routerRedux.replace({
        pathname: location.pathname,
        search: '?' + stringUtils.JSON2QueryString(payload),
        // state: { ...payload },
      }));
      const data = yield call(usersService.query, payload);
      if (data.success) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.data.rows,
            total: data.data.total,
            addBtnFlag, editBtnFlag, viewBtnFlag,  deleteBtnFlag
          },
        });
      } else {
        throw data;
      }
    },

    * getSingle({ payload }: ReduxAction, { call, put }: ReduxSagaEffects) {
      yield put({
        type: 'updateState',
        payload: {
          mode: payload.mode,
        },
      });
      if (payload.mode === 'create') {

        yield put(routerRedux.push('/contract-mgmt/contract-tmpls/create'));
      } else {
        if (payload.mode === 'update') {
          const data = yield call(usersService.check, payload.id);
          if (data.success) {
            yield put(routerRedux.push(`/contract-mgmt/contract-tmpls/${payload.id}?mode=update`));
          } else {
            // throw data;;
            message.warn(data.message);
          }
        }
        if (payload.mode === 'view') {
          yield put(routerRedux.push(`/contract-mgmt/contract-tmpls/${payload.id}?mode=view`));
        }
      }
    },
    * delete({ payload }: ReduxAction, { call, put }: ReduxSagaEffects) {
      const data = yield call(usersService.remove, payload.ids[0]);
      // console.log("delete==>",data)
      if (data.success) {
        yield put({ type: 'query' });
      } else {
        // throw data;;
        message.warn(data.message);
      }
    },

    * copy({ payload }: ReduxAction, { put }: ReduxSagaEffects) {
      yield put(routerRedux.push(`/contract-mgmt/contract-tmpls/create?tmpl=${payload.id}`));
    },

  },

  reducers: {

    querySuccess(state: object, action: ReduxAction) {
      const { list, total, addBtnFlag, editBtnFlag, viewBtnFlag,  deleteBtnFlag } = action.payload;
      return {
        ...state,
        list,
        total,
        addBtnFlag, editBtnFlag, viewBtnFlag,  deleteBtnFlag
      };
    },

    updateState(state: object, { payload }: ReduxAction) {
      return {
        ...state,
        ...payload,
      };
    },

  },
};
