/** 流程图按钮 */


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

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

/**
  <sino-flow-img-button
    record-id=""
    flow-type=""
    show="" 展示权限 默认true
    name=""
    top="" 向上移动的距离
    left="" 向左移动的距离
  >
  </sino-flow-img-button>
 */
module.directive('sinoFlowImgButton', function(CommonService) {
  
  return {
    restrict: 'E',
    require: '^sinoFlowButtonBar',
    scope: true,
    link: function(scope, element, attrs, ctrl) {

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