import Taro, { useReducer, useEffect } from '@tarojs/taro';
import Actions from '@/types/Store/Actions';
import { getAppointmentDevice, AppointDeviceResponse } from '@/api/shower';

const reducer = (state: AppointDeviceResponse[], action: Actions) => {
  switch (action.type) {
    case 'updateDeviceList':
      return action.payload;
    default:
      return state;
  }
};
const useDeviceList = (
  campusId: number,
  userId: number,
  location: string,
): AppointDeviceResponse[] => {
  const [list, dispatch] = useReducer(reducer, []);
  // const fetchList = (pageNum: number) => {
  //   getAppointmentDevice({ location, campusId, userId, pageNum })
  //     .then(res => {
  //       Taro.hideLoading();
  //       dispatch({ type: 'updateDeviceList', payload: res.data.list });
  //     })
  //     .catch(err => {
  //       Taro.showToast({
  //         title: err.msg || '网络错误',
  //         icon: 'none',
  //       });
  //     });
  // };
  // const fetchNewList = () => {};
  // const fetchMoreList = () => {};

  useEffect(() => {
    Taro.showLoading();
    getAppointmentDevice(
      location ? { location, campusId, userId } : { campusId, userId },
    )
      .then(res => {
        Taro.hideLoading();
        dispatch({ type: 'updateDeviceList', payload: res.data.list });
      })
      .catch(err => {
        Taro.showToast({
          title: err.msg || '网络错误',
          icon: 'none',
        });
      });
  }, [campusId, userId, location]);
  return list;
};

export default useDeviceList;
