import * as publicService from '../../services/message/public';
import { formUtils } from '../../utils';
import format from 'date-fns/format';

export default {
  namespace: 'publicMsg',
  state: {
    list: [],
    total: 0,
    limit: 10,
    currentItem: {},
    modalVisible: false,
    mode: 'create',
    menuBtns: ['nm_pb_save', 'nm_pb_del', 'cp_order_co_approve', 'cp_order_co_remove'],
    showMenu: [false, false],
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/messages/public') {
          dispatch({ type: 'clean' });
          dispatch({ type: 'query' });
        }
      });
    },
  },

  effects: {
    * query({ payload }, { call, select, put }) {
      const { showMenu, menuBtns } = yield select(({ publicMsg }) => publicMsg);
      const perms = yield select(({ app }) => app.perms);
      menuBtns.forEach((i) => {
        const menuId = perms.map((m) => m.menuId).find((n) => n === i);
        showMenu[menuBtns.indexOf(i)] = menuId ? true : false;
      });

      const { limit } = yield select(({ publicMsg }) => publicMsg);
      const data = yield call(publicService.query, { ...payload, limit, sortBy: 'publishTime', sortDirection: 'DESC' });
      if (data.success) {
        yield put({
          type: 'querySuccess',
          payload: {
            showMenu,
            list: data.data.rows,
            total: data.data.total,
          },
        });
      } else {
        throw data;
      }
    },

    * querySingle({ payload }, { call, put }) {
      const data = yield call(publicService.getSingle, payload.id);
      if (data.success) {
        const { anncType, anncClass } = data.data;
        let { outerAnncType, innerAnncType } = data.data;
        if (anncClass === '1') {
          outerAnncType = anncType;
        } else {
          innerAnncType = anncType;
        }
        yield put({
          type: 'showModal',
          payload: {
            currentItem: { ...data.data, outerAnncType, innerAnncType },
            mode: payload.mode,
          },
        });
      } else {
        throw data;
      }

      yield put({
        type: 'showModal',
        payload: {
          mode: payload.mode,
        },
      });
    },

    * getSingle({ payload }, { put }) {
      if (payload.mode === 'create') {
        const attachRepo = format(new Date(), 'YYYYMM');
        yield put({
          type: 'updateState',
          payload: {
            currentItem: { attachRepo },
          },
        });
        yield put({
          type: 'showModal',
          payload: {
            mode: payload.mode,
          },
        });
      } else {
        yield put({ type: 'querySingle', payload });
      }
    },

    * pagination({ payload }, { put, select }) {
      const { page } = payload;
      const { limit } = yield select(({ publicMsg }) => publicMsg);
      const offset = (page - 1) * limit;
      yield put({ type: 'query', payload: { offset } });
    },

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

    * create({ payload }, { call, put, select }) {
      const { anncClass, outerAnncType, innerAnncType } = payload;
      const anncType = anncClass === '1' ? outerAnncType : innerAnncType;
      const data = yield call(publicService.update, { ...formUtils.standardiseFormData(payload), anncType });
      if (data.success) {
        yield put({ type: 'hideModal' });
        yield put({ type: 'query' });
      } else {
        throw data;
      }
    },

    * update({ payload }, { select, call, put }) {
      const id = yield select(({ publicMsg }) => publicMsg.currentItem.id);
      const { anncClass, outerAnncType, innerAnncType } = payload;
      const anncType = anncClass === '1' ? outerAnncType : innerAnncType;

      const newData = { ...formUtils.standardiseFormData(payload), id, anncType };
      const data = yield call(publicService.update, newData);
      if (data.success) {
        yield put({ type: 'hideModal' });
        yield put({ type: 'query' });
      } else {
        throw data;
      }
    },

    * view({ payload }, { put }) {
      yield put({ type: 'hideModal' });
    },

    * hideModal({ payload }, { put }) {
      yield put({
        type: 'updateState',
        payload: {
          modalVisible: false,
          currentItem: {},
        },
      });
    },

    * clean({ payload }, { call, put }) {
      yield put({
        type: 'updateState',
        payload: {
          list: [],
          total: 0,
          limit: 10,
          currentItem: {},
          modalVisible: false,
          mode: 'create',
          showMenu: [false, false, false, false],
        },
      });
    },
  },

  reducers: {

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

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

    showModal(state, action) {
      return { ...state, ...action.payload, modalVisible: true };
    },

  },

};
