export class LeftMenuController {
  constructor($state, $window, toastr, globalVar) {
    'ngInject';

    this.$state = $state;
    this.$window = $window;
    this.toastr = toastr;
    this.globalVar = globalVar;
    // this.userInfo = userInfo;

    this.init();
  }

  init() {
    setTimeout(() => {
      console.log('this.bg = ' + this.bg);
      console.log('this.logo = ' + this.logo);
      angular.element('nav.navbar-default.navbar-static-side').css('background', 'url("' + this.bg + '") no-repeat');
      angular.element('nav.navbar-default.navbar-static-side').css('background-size', '100% 100%');
    }, 0);
  }

  getName() {
    let name = '';
    const params = this.userInfo;
    if (params && params.name) {
      name = params.name;
    }
    return name;
  }

  goTo(e, target) {
    e.stopPropagation();
    e.preventDefault();
    if (!this.userInfo.shopId) {
      this.toastr.warning('请先选择所需操作的门店！');
      return;
    }
    this.$state.go(target);
  }

  hidenUserName(userName) {
    let regExp = /^.*[\u2E80-\u9FFF].*$/;
    if (!userName) {
      return;
    }
    // 如果长度小于7或者存在中文则不隐藏
    if (userName.length < 7 || regExp.test(userName)) {
      return userName;
    }
    let left = userName.substr(0, 3);
    let right = userName.substring(userName.length - 4);
    // let middle = '*'.padStart(userName.length - 7, '*');
    // return left.concat(middle, right);

    // Only show 4 '*' in the middle, make it simple and consistence
    return left.concat('****', right);
  }
}

export const leftMenuComponent = {
  bindings: {
    sessions: '=',
    userInfo: '=',
    logout: '&',
    logo: '=',
    bg: '='
  },
  templateUrl: 'app/controls/components/leftMenu/leftMenu.component.html',
  controller: LeftMenuController
};