/** 恢复流程按钮 */


var angular = require('angular');
require('../modules');
require('../../flow/services/WorkflowTransfer');

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

/**
  <sino-flow-recovery-button
    flow-type=""
    record-id=""
    table-name=""
    table-id=""
    subflag="" 流程类型 默认'1'
    show="" 展示权限 默认false
    name="" 名称
  ></sino-flow-recovery-button>
 */
module.directive('sinoFlowRecoveryButton', function(WorkflowTransfer) {
  
  return {
    restrict: 'E',
    require: '^sinoFlowButtonBar',
    link: function(scope, element, attrs, ctrl) {

      function recoveryProcess() {
        var flowType = ctrl.getAttr(attrs, 'flowType'),
          recordId = ctrl.getAttr(attrs, 'recordId'),
          tableName = ctrl.getAttr(attrs, 'tableName'),
          tableId = ctrl.getAttr(attrs, 'tableId'),
          subflag = attrs.subflag || '1',
          promise = WorkflowTransfer.recoveryProcess(flowType, recordId, subflag, tableName, tableId).then(function() {
            ctrl.updateSubflag(scope, attrs, subflag);
          });
        return ctrl.successCallFnInAlert(promise, '恢复流程');
      };
      
      ctrl.button(function() {
        return {
          id: 'recoveryProcess',
          name: attrs.name,
          show: attrs.show ? scope.$eval(attrs.show) : true,
          onClick: recoveryProcess
        };
      });
    } 
  };
});
