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

var module = angular.module('commons.directives.sinoFooterGroup');

/* 页脚展示组件 
*
*  Item = {
*    id: 主键ID 
*    text: 显示内容
*  }
*  footerGroupConfig = {
*    getItem: {Function: return {Array.<Item>}} 获取展示信息  
*    updateItem: {Function} 修改ID
*   }
*/
module.directive('sinoFooterGroup', function() {
  return {
    restrict: 'EA',
    replace: true,
    scope: {
      footerGroupConfig: '='
    },
    templateUrl: 'views/commons/directives/sinoFooterGroup.tpl.html',
    link: function(scope) {
      scope.item = scope.footerGroupConfig.getItem();

      scope.hasNode = function() {
        return _.any(scope.item, function() {
          return true;
        });
      };

      /* 清空被选中的数据 */
      scope.clearItem = function() {
        _.each(scope.item, function(value, key) {
          delete scope.item[key];
        });
        scope.footerGroupConfig.updateItem(scope.item);
      };

      scope.removeItemById = function(id) {
        if (id) {
          delete scope.item[id];
        }
        scope.footerGroupConfig.updateItem(scope.item);
      };

      scope.$watch('footerGroupConfig', function() {
        scope.item = scope.footerGroupConfig.getItem();
      });
    }
  };
});
