var angular = require('angular');
require('jsUtil');
require('../modules');
require('../services/FlowLogService');

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

/**
 * <div sino-flow-log flow-type="{{}}" record-id="{{}}"></div> ->A
 * <sino-flow-log flow-type="{{}}" record-id="{{}}"></sino-flow-log> ->E
 */
module.directive('sinoFlowLog', function(FlowLogService) {
  return {
    restrict: 'EA',
    replace: true,
    scope: {
      flowType: '@flowType',
      recordId: '@recordId'
    },
    templateUrl: 'views/flow/flowLogModule.tpl.html',
    link: function(scope) {
      
      scope.onRefresh = onRefresh;
      
      onRefresh();
      
      function onRefresh() {
        scope.loadingState = 1;
        FlowLogService.getViewFlowLog(scope.flowType, scope.recordId)
          .then(function(data) {
            scope.flowLogs = data;
            scope.loadingState = 2;
          }, function() {
            scope.loadingState = 3;
          });
      };
      
    }
  };
});
