/*
 * @Author: your name
 * @Date: 2021-04-23 15:39:41
 * @LastEditTime: 2021-04-30 11:29:29
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /react-antd-admin-template/src/store/actions/auth.js
 */
import { setUserToken, resetUser } from "./user";
import { reqLogin, reqLogout } from "@/api/login";
import { setToken, removeToken } from "@/utils/auth";
export const login = (username, password,hospSign) => (dispatch) => {
  // console.log({username, password,hospSign});
  return new Promise((resolve, reject) => {
    reqLogin({ username: username.trim(), password: password,hospSign:hospSign})
      .then((response) => {
        const { data } = response;
        console.log({'登录返回':data});
        if (data.status === 200) {
          const token = data.data.token;
          // console.log({token});
          dispatch(setUserToken(token));
          setToken(token);
          resolve(data.data);
        } else {
          reject(data);
        }
      })
      .catch((error) => {
        reject(error);
      });
  });
};

export const logout = (token) => (dispatch) => {
  return new Promise((resolve, reject) => {

    dispatch(resetUser());
    removeToken();
    // resolve(data);
    localStorage.removeItem('userInfo');
    // reqLogout(token)
    //   .then((response) => {
    //     console.log({response});
    //     const { data } = response;
    //     console.log({data});
    //     if (data.status === 0) {
    //       dispatch(resetUser());
    //       removeToken();
    //       resolve(data);
    //       localStorage.removeItem('userInfo');
    //     } else {
    //       const msg = data.message;
    //       reject(msg);
    //     }
    //   })
    //   .catch((error) => {
    //     reject(error);
    //   });
  });
};
