/** 流程日志按钮 */


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

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

/**
  <sino-flow-log-button
   record-id=""
   flow-type=""
   show="" 展示权限 默认true
  ></sino-flow-log-button>
 */
module.directive('sinoFlowLogButton', function(CommonService) {
  
  return {
    restrict: 'E',
    require: '^sinoFlowButtonBar',
    scope: true,
    link: function(scope, element, attrs, ctrl) {

      scope.openFlowLog = function() {
        scope.recordId = ctrl.getAttr(attrs, 'recordId');
        scope.flowType = ctrl.getAttr(attrs, 'flowType');
        if (!scope.modal) {
          CommonService.createModal('views/flow/flowLog.tpl.html', scope).then(function(modal) {
            scope.modal = modal;
            scope.modal.show();
          });
        } else {
          scope.modal.show();
        }
      };
  
      scope.close = function() {
        if (scope.modal) {
          scope.modal.remove();
          scope.modal = null;
        }
      };
      
      scope.$on('$destroy', function() {
        scope.close();
      });
      
      ctrl.button(function() {
        return {
          id: 'flowLog',
          name: attrs.name,
          show: attrs.show ? scope.$eval(attrs.show) : true,
          onClick: scope.openFlowLog  
        }
      });       
    }
  };
});
