import { useEffect, useReducer } from '@tarojs/taro';
import { getUserAppointableData, UserAppointableData } from '@/api/shower';
import Actions from '@/types/Store/Actions';

const reducer = (state: UserAppointableData, action: Actions) => {
  switch (action.type) {
    case 'getAppointData':
      return action.payload;
    default:
      return state;
  }
};
const initState: UserAppointableData = {
  appointMode: 0,
  defaultType: 0,
  duration: 0,
  message: '',
  punishment: 0,
  useType: 0,
  userPunishment: 0,
};
const useCheckAppointable = (
  userId: number,
  campusId: number,
): UserAppointableData => {
  const [data, dispatch] = useReducer(reducer, initState);
  useEffect(() => {
    getUserAppointableData({
      userId,
      campusId,
    })
      .then(res => {
        console.log(res);
        dispatch({ type: 'getAppointData', payload: res.data });
      })
      .catch(err => {
        console.log(err);
      });
  }, [userId, campusId]);

  return data;
};

export default useCheckAppointable;
