import Store from './store/index';
import Http from './http/index';
import Error from './untils/error/index';
import Config from './config/index';
import Router from './untils/router/index'
class UserLogin {
  constructor () {
    this.state = {};
    this._hasShowLoginTip = false;
    this.instance = null;
  }

  // 静态方法作为广为人知的接口
  static getInstance () {
    if (!this.instance) {
      this.instance = new UserLogin();
    }
    return this.instance;
  }

  /**
   * 登录
   * @param {object} objs { mobile: '', password: '' }
   * @returns {boolean} 如果登录成功返回true，登录失败返回false
   *
   * @example login({mobile: '13412341234', password: '123456'})
   */
  async login (objs = { account: '', password: '' }) {
    // ------------------------------------------------发送登录请求
    let res = await Http.post({
      url: Http.urlMap.login,
      data: {
        account: objs.account,
        password: objs.password
      }
    });
    // 根据登录结果判断是否登录成功
    // ①：如果成功
    if (res.code === 1) {
      // ------------------------------------------------将用户保存到vuex中
      Store.commit('setUserInfo', { userinfo: res.data.userinfo });
      // ------------------------------------------------保存该用户的token数据
      // 返回true
      return true;
    } else {
      // ②：如果失败
      // 返回false
      return false;
    }
  }

  /**
   * 退出登录
   * @param {object} objs
   * @returns {boolean} 如果退出登录成功返回true，否则返回false
   */
  async logout (objs = {}) {

    Store.commit('setUserInfo', { userinfo: {} });
    return true;
  }


  /**
   * 检查是否登录
   * @param {object} objs
   * @returns {boolean} 如果登录状态正常，如果正常，返回true，否则返回false
   */
  checkIfLogin (objs = {}) {
    // 检查是否登录
    // ①：检查用户数据是否存在
    // ②：检查token是否过期
    if (this.getToken()) {
      return true; // 登录态正常
    }
    // 退出登录
    this.logout();
    // this.showLoginTips();
    return false; // 登录失效
  }

  /**
   * 显示登录提示
   * @param {object} objs
   */
  showLoginTips (objs = {}) {
    if (!this._hasShowLoginTip) {
      this._hasShowLoginTip = true;
      Error.errorNotLoggedIn(
        // success
        () => {
          this._hasShowLoginTip = false;
        },
        // fail
        () => {
          this._hasShowLoginTip = false;
        }
      );
    }
  }

  /**
   * 获取token
   * @returns {boolean} 如果登录状态正常返回token的值，否则返回false
   */
  getToken () {
    let userinfo = this.getUserInfo();
    if (userinfo.token) {
      return userinfo.token;
    }
    return false;
  }

  /**
   * 获取当前用户信息
   * @returns {object} 如果登录状态正常返回用户信息，否则返回空对象{}
   */
  getUserInfo () {
	  // console.log(Store.state.userinfo)
    let userinfo = Store.state.userinfo;
    if (userinfo) {
      if (userinfo.token) {
        return userinfo;
      }
    }
    // 退出登录
    this.logout();
    return {};
  }
  //是否是vip
  async getSubvip(id){
  	let res = await Http.post({
  		url:Http.urlMap.checkvip,
  		data:{
  			subject_id:id
  		},
  		needLogin:true,
  	})
  	if(res.code == 1){
  		let is_vip = res.data.is_vip
  		Store.commit('setSubjectVipInfo', is_vip)
  		return is_vip
  	}
  }
  async getUnitVipInfo(type,unit_id){
	  // type // 1：章节练习  2历年真题
	  let res = await Http.post({
	  	url:type == 1 ? Http.urlMap.unitVip : Http.urlMap.zhentiVip,
	  	data:{
	  		id:unit_id
	  	},
	  	needLogin:true
	  })
	  if(res.code == 1){
		  return res.data
	  }
  }
  async getExamVipInfo(exam_id){
	  console.log(exam_id)
  	  let res = await Http.post({
  	  	url:Http.urlMap.examVip,
  	  	data:{
  	  		id:exam_id
  	  	},
  	  	needLogin:true
  	  })
  	  if(res.code == 1){
  		  return res.data
  	  }
  }
  
  
  
}

let _userLogin = new UserLogin();
export default _userLogin;
