var angular = require('angular');
var _ = require('jsUtil');
require('../modules');

var module = angular.module('flow.directives');

module.directive('sinoNotionList', function(NotionService, $q) {


  return {
    restrict: 'E',
    replace: true,
    scope: {
      _notions: '=?notions',
      recordId: '@',
      flowType: '@',
      sortType: '='
    },
    templateUrl: 'views/flow/directives/sinoNotionList.tpl.html',
    link: function(scope) {
      /**
       * 初始化方法
       */
      (function() {
        initNotion();
      })();

      scope.$watch('sortType', function(value) {
        orderBy(value);
      });

      function initNotion() {
        scope.loadingState = 1;
        $q.all([initFlowNotion(), initDyNotion()]).then(function(notionArray) {
          scope._notions = notionArray[0] || {};
          _.each(notionArray[1], function(value, key) {
            scope._notions[key] = value;
          });
        }).then(function() {
          scope.backNotions = scope._notions;
          scope.loadingState = !_.isEmpty(scope._notions) ? 2 : 4;
        }, function() {
          scope.loadingState = 3;
        });
      }

      function initFlowNotion() {
        if (scope._notions) {
          return $q.when(scope._notions);
        } else {
          return NotionService.queryNotion(scope.flowType, scope.recordId).then(function(items) {
            var NotionList = {};
            _.each(items, function(item) {
              if (!_.isArray(NotionList[item.ideaFieldTitle])) {
                NotionList[item.ideaFieldTitle] = [];
              }
              if(item.content === 'TRUE' || item.content === 'FALSE') {item.content = '无';}
              NotionList[item.ideaFieldTitle].push(item);
            });
            return NotionList;
          });
        }
      }

      function initDyNotion() {
        return NotionService.getDyNotion(scope.flowType, scope.recordId).then(function(data) {
          if(!_.isEmpty(data)) {
            var list = [],
              notions = {};
            for (var i = 0; i < data.length; i++) {
              if(_.isEmpty(data[i].userNotion)) {
                data[i].userNotion = '无';
              }
              list.push({
                "content":data[i].userNotion,
                "dateStr":data[i].notionDate,
                "userName":data[i].inUserName,
                "deptName":data[i].inDeptName
              });
            };
            if (list.length > 0) {
              notions['阅毕意见'] = list;
            }
            return notions;
          }
        });
      }



      function orderBy(orderBy) {
        var key = '';
        if (orderBy === 'time') {
          if (!scope._notions[key]) {
            scope._notions = getNotionsOrderByDate(key);
          }
        } else if (orderBy === 'node'){
          scope._notions = scope.backNotions;
        }
      };

      // 获取按时间排序的意见
      function getNotionsOrderByDate(key) {
        var notions = {};
        notions[key] = [];
        _.each(scope._notions, function(value) {
          _.each(value, function(value) {
            notions[key].push(value);
          });
        });
        return notions;
      }

    }
  };
});
