import Taro, { useState, useCallback } from '@tarojs/taro';
import { switchAccountLoginState } from '@/api/customer';

const useAccountLogin = (
  customerId: number,
  initState: string,
): [boolean, (state: boolean) => void] => {
  const [state, setState] = useState(initState === '1' ? true : false);
  const setStateHandle = useCallback(
    (state: boolean) => {
      setState(state);
      Taro.showLoading();
      switchAccountLoginState({
        customerId,
        state: state ? '1' : '0',
      })
        .then(res => {
          Taro.hideLoading();
          console.log(res);
          Taro.showToast({
            title: '修改成功',
          });
        })
        .catch(err => {
          Taro.hideLoading();
          console.log(err);
          setState(!state);
          Taro.showToast({
            title: err.msg || '修改失败',
            icon: 'none',
          });
        });
    },
    [customerId],
  );

  return [state, setStateHandle];
};

export default useAccountLogin;
