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

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

module.directive('sinoViewFlowimgButton', function($toast, $ionicModal, CommonService) {
return {
  restrict: 'E',
  template: '<button>{{flowimgTitle}}</button>',
  replace: true,
  scope: true,
  link: function(scope, element, attrs) {
    scope.flowimgTitle = attrs.flowimgTitle || '查看流程图';
    var modal;

    var flowimgUrl = 'views/flow/workflowDrawing.tpl.html';
    function openModal() {
      if (modal) {
        modal.remove();
      }
      CommonService.createModal(flowimgUrl, 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();
      });
    });
  }
};

});
