export function ToggleThirdMenuDirective() {
  const directive = {
    restrict: 'A',
    scope: {
      autoExpand: '@'
    },
    link: linkFunc
  };

  return directive;

  function linkFunc(scope, element) {
    let flag = false;

    // 自动展开
    if (scope.autoExpand && location.href.indexOf('#/home') > -1) {
      let obj = element.next('ul'); // 下级菜单ul列表
      let parentObj = element.parent(); // ul列表上级li
      let oUl = parentObj.parent(); // li上级ul
      oUl.find('>li').each((i, one) => {
        if (parentObj.is(one)) {
          return;
        }
      });
      angular.element('.nav-third-level').css('display', 'none');
      angular.element(obj).find('a').css('display', 'inline-block');
      angular.element(obj).find('a').css('z-index', '100');
      obj.fadeIn(0, () => {
        flag = false;
        angular.element('.nav-second-level>li').removeClass('active').addClass('inActive');
        parentObj.removeClass('inActive').addClass('active');
        // parentObj.next('a').css('color', '#707070');
        // parentObj.removeClass('active').addClass('inActive');
        // obj.addClass('mini-navbar-second-menu').css('top', index * 50 + 11 + 'px');
      });
      element.css('cssText', 'color:#707070 !important');
    }

    element.off('click').on('click', e => {
      if (flag) {
        return false;
      }
      flag = true;
      e.stopPropagation();
      let obj = element.next('ul');
      let parentObj = element.parent();
      let oUl = parentObj.parent();
      let index = 0;
      oUl.find('>li').each((i, one) => {
        if (parentObj.is(one)) {
          index = i;
          return;
        }
      });
      if (obj.css('display') === 'none') {
        angular.element('.nav-third-level').css('display', 'none');
        angular.element(obj).find('a').css('display', 'inline-block');
        angular.element(obj).find('a').css('z-index', '100');

        obj.fadeIn(400, () => {
          flag = false;
          angular.element('.nav-second-level>li').removeClass('active').addClass('inActive');
          parentObj.removeClass('inActive').addClass('active');
          obj.addClass('mini-navbar-third-menu').css('top', index * 50 + 161 + 'px');
        });
      } else if (obj.css('display') === 'block') {
        obj.fadeOut(400, () => {
          flag = false;
          obj.removeClass('active').addClass('inActive');
          parentObj.removeClass('active').addClass('inActive');
        });
      }
    });
  }
}