var angular = require('angular');
require('../modules');
require('../services/FlowButtonService');
require('../../flow/services/WorkflowTransfer');
require('../../../commons/services/CommonService');
require('../../../commons/directives/$toast');

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

/**
* 按钮组件
*    
* 使用方式: <sino-flow-button button-confg="buttonConfig"></sino-flow-button>
*
* 参数 buttonConfig: {
*   flowType: 流程类型
*   recordId: 流程主键ID
*   tableName: 主表名
*   tableId: 主表主键ID
*   subflag: 流程状态
*   refresh: 刷新配置方法
*   offsetPositionConfig: {object} 设置流程图位置
*     left: XXX 相对于默认位置左移XXXpx
*     top: XXX 相对于默认的位置上移XXXpx
*   commonButtons: [{
*       id: 按钮id
*       show: 展示权限
*       onClick: 点击功能 
*     },...] 
* }
*
* 按钮id: 
*   edit: 详情, save:保存, send: 发送
*   notion: 意见, delete: 删除， approve: 审核通过
*   notApprove: 审核不通过, flowLog: 流程记录, flowDraw: 流程图
*   transfer: 转办, recycle: 收回重办, finish: 结束流程
*   recoveryProcess: 恢复流程, cancelDo: 撤办, reuseProcess: 重启流程
*/
module.directive('sinoFlowButton', function(FlowButtonService, $window, 
  CommonService, WorkflowTransfer, $toast) {
  return {
    restrict: 'EA',
    replace: true,
    templateUrl: 'views/flow/flowButtonModule.tpl.html',
    scope: {
      buttonConfig: '=buttonConfig'
    },
    link: function(scope) {

      scope.selectMore = function() {
        if (scope.moreFlag) {
          scope.moreFlag = false;
        } else {
          scope.moreFlag = true;
        }
      };

      scope.openFlowLog = function() {
        if (!scope.buttonConfig.recordId) {
          $toast.showLongCenter('请保存文件。');
          return;
        }
        var flowLogUrl = 'views/flow/flowLog.tpl.html';
        CommonService.createModal(flowLogUrl, scope).then(function(modal) {
          scope.flowLogModal = modal;
          scope.flowLogModal.show();
        });   
      };

      scope.closeFlowLog = function() {
          scope.flowLogModal.remove();
      };       

      scope.openWorkflowDrawing = function() {
        if (!scope.buttonConfig.recordId) {
          $toast.showLongCenter('请保存文件。');
          return;
        }
        var workflowDrawingUrl = 'views/flow/workflowDrawing.tpl.html';
        CommonService.createModal(workflowDrawingUrl, scope).then(function(modal) {
          scope.modalDraw = modal;
          scope.modalDraw.show();
        });
      };     

      scope.close = function() {
        scope.modalDraw.remove();
      };    

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

      scope.recycle = function() {
        var promise = WorkflowTransfer.recycle(scope.buttonConfig.flowType, scope.buttonConfig.recordId);
        return promise.then(function(data) {
          if(data.success) {
            return scope.buttonConfig.refresh().then(function() {
              $toast.showLongCenter('收回重办成功。');
            });           
          } else {
            $toast.showLongCenter('收回重办失败，请检查网络连接。');
          }       
        }, function() {
          $toast.showLongCenter('收回重办失败，请检查网络连接。');
        });          
      };

      scope.reuseProcess = function() {
        var promise = WorkflowTransfer.reuseProcess(scope.buttonConfig.flowType, scope.buttonConfig.recordId);
        return promise.then(function(data) {
          if(data.success) {
            return scope.buttonConfig.refresh().then(function() {
              $toast.showLongCenter('重启流程成功。');
            });
          } else {
            $toast.showLongCenter('重启流程失败，请检查网络连接。');
          }
        }, function() {
          $toast.showLongCenter('重启流程失败，请检查网络连接。');
        });
      };

      scope.recoveryProcess = function() {
        var promise = WorkflowTransfer.recoveryProcess(scope.buttonConfig.flowType, scope.buttonConfig.recordId, 
          scope.buttonConfig.subflag, scope.buttonConfig.tableName, scope.buttonConfig.tableId);
        return promise.then(function(data) {
          if(data.success) {
            return scope.buttonConfig.refresh().then(function() {
              $toast.showLongCenter('恢复流程成功。');
            });
          } else {
            $toast.showLongCenter('恢复流程失败，请检查网络连接。');
          }
          
        }, function() {
          $toast.showLongCenter('恢复流程失败，请检查网络连接。');
        });            
      };

      scope.cancelDo = function() {
        var promise = WorkflowTransfer.cancelDo(scope.buttonConfig.flowType, scope.buttonConfig.recordId);
        return promise.then(function(data) {
          if(data.success) {
            return scope.buttonConfig.refresh().then(function() {
              $toast.showLongCenter('撤办成功。');
            });
          } else {
            $toast.showLongCenter('撤办失败，请检查网络连接。');
          }
        }, function() {
          $toast.showLongCenter('撤办失败，请检查网络连接。');
        });
      };

      $window.document.body.addEventListener('click', function() {
        scope.$evalAsync(function() {
          scope.moreFlag = false;
        });
      }, false);

      var flowButtonService = new FlowButtonService(scope.buttonConfig, scope);
      scope.$watch('buttonConfig', function(buttonConfig) {
        if (!buttonConfig) { return; }
        initScope();
        flowButtonService.refresh(buttonConfig);
        scope.buttons = flowButtonService.showButtons;
        scope.moreButton = flowButtonService.moreButton;
      }, true);


      function initScope() {
        scope.recordId = scope.buttonConfig.recordId;
        scope.flowType = scope.buttonConfig.flowType;
        scope.offsetPositionConfig = scope.buttonConfig.offsetPositionConfig;
      }
    }
  };  
});
