var angular = require('angular');
require('../modules');
require('../../notice/services/NoticeService');
require('../../intro/services/introService');
require('../../mulJobs/services/mulJobsService');
'use strict';

angular
  .module('setting')
  .controller('assistCtrl', assistCtrl);

/** @ngInject */
function assistCtrl($scope, AppConfig, LinkerService, $timeout, CacheService,
  NoticeService, introService, $toast, mulJobsService) {
  //2g/3g/4g网络下是否加载图片
  var module = AppConfig.getModuleByName('infoDisplay');
  $scope.imgLoadSet = false;
  if(module) {
    $scope.imgLoadSet = true;
    $scope.title = module.text;
  }
  //-----------------------

  var moduleNames = AppConfig.getModuleNames();

  var userId = LinkerService.currentUserId,
    key = 'notice-order-' + userId,
    noticeOrder = CacheService.get(key, 'noticeOrder_time');

  $scope.notice = {};
  $scope.notice.order = noticeOrder;
  $scope.introReset = introReset;

  $scope.$watch('notice.order', function() {
    var _order = CacheService.get(userId);
     if(!_.isEmpty($scope.notice.order) && _order != $scope.notice.order) {
      CacheService.put(key, $scope.notice.order);
      sortNotices($scope.notice.order, moduleNames);
    }
  }, true);

  function sortNotices(order, moduleNames) {
    if(order === 'noticeOrder_module') {
      NoticeService.appNotices.orderNotices(moduleNames);
    } else {
      NoticeService.appNotices.orderNotices();
    }
  }

  //使用向导
  function introReset() {
    introService.reset();
    $toast.showLongCenter('重启成功');
  }
  //---------------------
    initjobs();

    $scope.$watch('dept.deptId', function() {
      console.log('dept.deptId=:' + $scope.dept.deptId);
      // 采用键值对的方式将人-deptId进行保存
      CacheService.put(LinkerService.currentUserId, $scope.dept.deptId);
      //每次有改动时，需要去修改LinkerService中的当前登录人的默认登录部门
      LinkerService.setCurrentUser(LinkerService.currentUserId);
    });

    function initjobs() {
      $scope.showJobs = false;
      var user  = LinkerService.currentUser;
      $scope.dept = {};
      // 没有缓存时，设置为默认登录部门，如果没有默认登录部门，则取第一个单位。
      $scope.dept.deptId = CacheService.get(LinkerService.currentUserId) || user.noteDeptId || user.deptIds[0];
      
      $scope.showJobs = user.deptIds.length > 1;

      $scope.jobs = mulJobsService.getMulJobs();
    }
}
