import Common from '../common/Common.js';
import {languageList} from '../common/SupportedLanguage.js';

export default {

  /**
     * 设置LoginInfo
     * @param {Object} loginInfo
     * @author GuoZhiBo 20211026
     */
  setLoginInfo: function (loginInfo, router) {
    var _self = this;

    var memoryRouting = localStorage.getItem('memoryRouting');

    Common.clearLocalStorage(false);

    // path为指定路径，直接删除该路径下的cookie
    // $.removeCookie('token', { path: '/' });

    // bug fixed by jack
    // 清除掉/pcapp路径的cookie
    // $.removeCookie('token', { path: '/pcapp' });

    // bug fixed by jack
    // 把cookies的path属性设置成“/”。在指定路径的时候，凡是来自同一服务器，URL里有相同路径的所有WEB页面都可以共享cookies。
    // $.cookie('token', loginInfo.token, { expires: 7, path: '/', secure: true, sameSite: 'Strict' });

    _self.setLocalStorage(loginInfo);

    // asset.html 跳转到领用归还界面
    let type = Common.getRouteParam('type');

    let path = Common.getRouteParam('path');

    if (type == 'autoLoginRedirect') {
      var autoLoginData = {
        userName: _self.userName,
        password: _self.password,
      };
      localStorage.setItem('#AutoLoginInfo', JSON.stringify(autoLoginData));
      router.push('/' + path);
      return;
    }

    const redirectUrl = window.location.href.split('redirectUrl=')[1];

    if (redirectUrl) {
      window.location = decodeURIComponent(redirectUrl);
    } else {
      router.push('/desktop/dashboard');
    }

    // 设置记忆路由
    if (memoryRouting != null) {
      localStorage.setItem('memoryRouting', memoryRouting);
    }
  },


  //  设置localStorage
  setLocalStorage: function (loginInfo) {
    localStorage.setItem('#LoginInfo', JSON.stringify(loginInfo));
    localStorage.setItem('#token', loginInfo.token);
    localStorage.setItem('#accountId', loginInfo.accountId);
  },


    
  //  设置localStorage
  saveLocalStorage: function (loginData) {
    var _self = this;
    if (loginData.rememberPassword) {
      localStorage.setItem('#userName', loginData.userName);
      localStorage.setItem('#password', loginData.password);
      localStorage.setItem('#languageSelected', loginData.languageSelected);
      localStorage.setItem('#rememberPassword', loginData.rememberPassword);
    }else{
      localStorage.removeItem('#userName');
      localStorage.removeItem('#password');
      localStorage.removeItem('#languageSelected', loginData.languageSelected);
      localStorage.setItem('#rememberPassword', loginData.rememberPassword);
    }
  },



  /**
     * 从LocalStorage中恢复数据
     */
  restoreFromLocalStorage: function () {
    const result = {};


    var userName = localStorage.getItem('#userName');
    if (userName != null) {
      result.userName = userName;
    }else{
      result.userName = null;
    }

    var password = localStorage.getItem('#password');
    if (password != null) {
      result.password = password;
    }else{
      result.password = null;
    }

    var languageSelected = localStorage.getItem('#languageSelected');
    if (languageSelected != null && languageSelected != '') {
      result.languageSelected = languageSelected;
      if (languageList.indexOf(languageSelected) < 0) {
        result.languageSelected = languageList[0];
      }
    } else {
      result.languageSelected = languageList[0];
    }

    var rememberPassword = localStorage.getItem('#rememberPassword');
    if (rememberPassword === 'true') {
      result.rememberPassword = true;
    } else {
      result.rememberPassword = false;
    }

    return result;
  },
};