(function() {
  'use strict';

  angular
    .module('kueJobs')
    .controller('ViewJobDetailsController', ViewJobDetailsController);

  ViewJobDetailsController.$inject = ['$scope', '$uibModal', 'JobsManager'];

  function ViewJobDetailsController($scope, $uibModal, JobsManager) {
    var scope = $scope;
    var vm = this;
    vm.message = 'Loading job...';
    vm.deleteJob = deleteJob;
    vm.onclose = onclose;
    vm.requeueJob = requeueJob;

	  /**
     * Job id watcher
     */
    var jobIdWatcher = scope.$watch('jobId', function(newValue) {
      if (newValue) {
        JobsManager.getJob(newValue).then(function(job) {
          vm.job = job;
        }, function(err) {
          vm.message = err;
        });
      } else {
        vm.message = 'Job ID is required';
      }
    });

    /**
     * On destroy handling
     */
    scope.$on('$destroy', function() {
      if (jobIdWatcher) {
        jobIdWatcher();
      }
    });

    /**
     * Delete current job
     */
    function deleteJob() {
      var dialogInstance = $uibModal.open({
        templateUrl: 'components/kue/views/job-confirmation-dialog.view.html',
        controller: 'JobConfirmationDialogController',
        controllerAs: 'jobConfirmationDialogCtrl',
        resolve: {
          confirmationText: function() {
            return 'This will permanently delete the job. Are you sure?';
          }
        }
      });

      dialogInstance.result.then(function(accept) {
        if (accept) {
          JobsManager.deleteJob(scope.jobId).then(function(message) {
            vm.job = null;
            vm.message = message;
          });
        }
      });
    }

	  /**
     * On close click
     */
    function onclose() {
      if (angular.isFunction(scope.onclose)) {
        scope.onclose({
          jobId: scope.jobId
        });
      }
    }

    /**
     * Refresh current job
     */
    // function refreshJob() {
    //   JobsManager.refreshJob(scope.jobId).then(function(job) {
    //     vm.job = job;
    //   });
    // }

	  /**
     * Requeue job i.e. create new job instance based on current job
     */
    function requeueJob() {
      var dialogInstance = $uibModal.open({
        templateUrl: 'components/kue/views/job-confirmation-dialog.view.html',
        controller: 'JobConfirmationDialogController',
        controllerAs: 'jobConfirmationDialogCtrl',
        resolve: {
          confirmationText: function() {
            return 'This will re-submit the job for execution. Are you sure?';
          }
        }
      });

      dialogInstance.result.then(function(accept) {
        if (accept) {
          var jobData = vm.job;
          JobsManager.createJob(jobData).then(function(job) {
            scope.jobId = job.id;
            vm.job = job;
          });
        }
      });
    }
  }
})();

@@ -176,16 +176,29 @@
 $scope',
+ '$interval',
  '$uibMo
@@ -263,16 +263,27 @@
 ($scope,
+ $interval,
  $uibMod
@@ -325,16 +325,81 @@
 $scope;%0A
+    var jobIdWatcher = null;%0A    var refreshDataInterval = null;%0A
     var 
@@ -533,23 +533,163 @@
 ueJob;%0A%0A
-%09
+    activate();%0A%0A    function activate() %7B%0A      if (!scope.autoRefreshInterval) %7B%0A        scope.autoRefreshInterval = 60000;%0A      %7D%0A%0A    
   /**%0A
+  
      * J
@@ -711,18 +711,18 @@
     
+  
 */%0A    
-var
+ 
  job
@@ -772,24 +772,121 @@
 newValue) %7B%0A
+        if (refreshDataInterval) %7B%0A          $interval.cancel(refreshDataInterval);%0A        %7D%0A%0A  
       if (ne
@@ -887,32 +887,34 @@
 if (newValue) %7B%0A
+  
         JobsMana
@@ -957,32 +957,34 @@
 ob) %7B%0A          
+  
 vm.job = job;%0A  
@@ -973,32 +973,34 @@
   vm.job = job;%0A
+  
         %7D, funct
@@ -1012,32 +1012,34 @@
 rr) %7B%0A          
+  
 vm.message = err
@@ -1044,28 +1044,156 @@
 rr;%0A        
+  
 %7D);%0A
+%0A          refreshDataInterval = $interval(function() %7B%0A            refreshJob();%0A          %7D, scope.autoRefreshInterval);%0A%0A  
       %7D else
@@ -1195,16 +1195,18 @@
  else %7B%0A
+  
         
@@ -1242,33 +1242,43 @@
 red';%0A      
-%7D%0A
+  %7D%0A  
     %7D);
+%0A    %7D
 %0A%0A    /**%0A  
@@ -1402,24 +1402,113 @@
 r();%0A      %7D
+%0A%0A      if (refreshDataInterval) %7B%0A        $interval.cancel(refreshDataInterval);%0A      %7D
 %0A    %7D);%0A%0A  
@@ -2451,19 +2451,16 @@
   */%0A   
- //
  functio
@@ -2479,19 +2479,16 @@
 () %7B%0A   
- //
    JobsM
@@ -2547,15 +2547,12 @@
     
-//
     
- 
 vm.j
@@ -2564,19 +2564,16 @@
 job;%0A   
- //
    %7D);%0A 
@@ -2578,11 +2578,8 @@
 %0A   
- //
  %7D%0A%0A

enable autoRefreshInterval. default to 60s