import { ReduxAction, ReduxSagaEffects } from '@interfaces';
import * as usersService from '../../../services/sys/users';

export default {
  namespace: 'dpowerModalModel',
  state: {
    mode: 'create',
    itemPowerFlag: false,
    regPowerFlag: false,
    regList: [],
    itemList: [],
    currentItem: null,
    modalVisible: false,
  },

  subscriptions: {},

  effects: {
    * showDpowerModal({ payload }, { put, call, select }: ReduxSagaEffects) {
      yield put({ type: 'queryPerms', });
      const data = yield call(usersService.getSingle, payload.id);
      if (data.success) {
        yield put({
          type: 'showModal',
          payload: {
            currentItem: data.data,
            mode: payload.mode,
          },
        });
      } else {
        throw data;
      }

      const powerData = yield call(usersService.getPower, payload.id);
      if (powerData.success) {
        const currentItem = yield select(({ dpowerModalModel }) => dpowerModalModel.currentItem);
        yield put({
          type: 'updateState',
          payload: {
            itemPowerFlag: powerData.data.itemPowerFlag,
            regPowerFlag: powerData.data.regPowerFlag,
            currentItem: {
              ...currentItem,
              itemCodes: powerData.data.itemCodes,
              regionCodes: powerData.data.regionCodes ? powerData.data.regionCodes.map(Number) : [],
              itemPowerFlag: powerData.data.itemPowerFlag,
              regPowerFlag: powerData.data.regPowerFlag,
            }
          },
        });
      } else {
        throw data;
      }
    },

    * updatePower({ payload }: ReduxAction, { select, call, put }: ReduxSagaEffects) {
      const { itemPowerFlag, regPowerFlag, currentItem } = yield select(({ dpowerModalModel }) => dpowerModalModel);
      const { itemCodes, regionCodes, id } = currentItem;

      const newData = {
        itemPowerFlag,
        regPowerFlag,
        itemCodes,
        regionCodes,
        userId: id,
      };

      const data = yield call(usersService.updatePower, newData);

      if (data.success) {
        yield put({ type: 'hideModal' });
        yield put({ type: 'query' });
      } else {
        throw data;
      }
    },

    * updateDpowerItem({ payload }: ReduxAction, { select, put }: ReduxSagaEffects) {
      const currentItem = yield select(({ dpowerModalModel }) => dpowerModalModel.currentItem);
      currentItem.itemCodes = payload;
      yield put({
        type: 'updateState',
        payload: {
          currentItem
        },
      });
    },

    * updateRegion({ payload }: ReduxAction, { select, put }: ReduxSagaEffects) {
      const currentItem = yield select(({ dpowerModalModel }) => dpowerModalModel.currentItem);
      currentItem.regionCodes = payload;
      yield put({
        type: 'updateState',
        payload: {
          currentItem
        },
      });
    },

    * queryPerms({ payload }: ReduxAction, { call, put }) {
      const regData = yield call(usersService.regQuery, {});
      if (regData.success) {
        yield put({
          type: 'updateState',
          payload: {
            regList: regData.data,
          },
        });
      } else {
        throw regData;
      }
      const itemData = yield call(usersService.itemQuery, {});
      if (itemData.success) {
        yield put({
          type: 'updateState',
          payload: {
            itemList: itemData.data,
          },
        });
      } else {
        throw itemData;
      }
    },

    // * regQuery({ payload }: ReduxAction, { call, put, select }: ReduxSagaEffects) {
    //   const { filterParams } = yield select(({ users }) => users);
    //   const data = yield call(usersService.regQuery, { ...filterParams, ...payload });
    //   if (data.success) {
    //     yield put({
    //       type: 'updateState',
    //       payload: {
    //         regList: data.data,
    //       },
    //     });
    //   } else {
    //     throw data;
    //   }
    // },
  },

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

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

    hideModal(state: object, action: ReduxAction) {
      return {
        ...state, ...action.payload, modalVisible: false, currentItem: {},
      };
    },
  },
};
