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

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

module.directive('sinoViewFlowlogButton', function(CommonService) {
  return {
    restrict: 'E',
    template: '<button>{{flowlogTitle}}</button>',
    replace: true,
    scope: true,
    link: function(scope, element, attrs) {
      scope.flowlogTitle = attrs.flowlogTitle || '查看流程记录';
      var modal;

      var flowlogUrl = 'views/flow/flowLog.tpl.html';
      
      function openModal() {
        if (modal) {
          modal.remove();
        }
        CommonService.createModal(flowlogUrl, scope)
        .then(function(_modal) {
          modal = _modal;
          modal.show();
        });
      }

      scope.close = function() {
        modal.remove();
        modal = null;
      };

      scope.$on('$destroy', function() {
        if(modal) {
          modal.remove();
        }
      });

      element.on('click', function() {
        scope.flowType = attrs.flowType;
        scope.recordId = attrs.recordId;
        scope.$evalAsync(function() {
          openModal();
        });
      });
    }
  };

});
