'use strict';

/* Controllers */

var alertaControllers = angular.module('alertaControllers', []);

alertaControllers.controller('AlertListController', ['$scope', 'Environment', 'Service', 'Alert',
  function($scope, Environment, Service, Alert){

    $scope.status = 'open';
    $scope.options = ['open', 'ack', 'closed'];

    $scope.q = {};

    Environment.all(function(response) {
      $scope.environments = response.environments;
    });

    Service.all(function(response) {
      $scope.services = response.services;
    });

    $scope.getAlerts = function() {

      $scope.q['environment'] = $scope.environment;
      $scope.q['service'] = $scope.service;
      $scope.q['status'] = $scope.status;

      console.log('q=' + $scope.q)
      console.log('env=' + $scope.environment + ' svc=' + $scope.service);

      Alert.query($scope.q, function(response) {
        $scope.alerts = response.alerts;
      });
    };

    $scope.alertLimit = 10;
    $scope.getAlerts();
  }]);

alertaControllers.controller('AlertDetailController', ['$scope', '$routeParams', '$location', 'Alert',
  function($scope, $routeParams, $location, Alert){

    Alert.get({id: $routeParams.id}, function(response) {
      $scope.alert = response.alert;
    });

    $scope.openAlert = function(id) {
      console.log('Alert.open({id: ' + id + '});')
      Alert.status({id: id}, {status: 'open'});
    };

    $scope.ackAlert = function(id) {
      console.log('Alert.ack({id: ' + id + '});')
      Alert.status({id: id}, {status: 'ack'});
    };

    $scope.closeAlert = function(id) {
      console.log('Alert.close({id: ' + id + '});')
      Alert.status({id: id}, {status: 'closed'});
    };

    $scope.deleteAlert = function(id) {
      console.log('Alert.delete({id: ' + id + '});')
      Alert.delete({id: id}, {}, function(data) {
        $location.path('/');
      });
    };

  }]);

alertaControllers.controller('AlertLinkController', ['$scope', '$location',
  function($scope, $location) {

    $scope.getDetails = function(alert) {
      $location.url('/alert/' + alert.id);
    };
  }]);

@@ -1376,20 +1376,55 @@
 : 'open'
+, text: 'status change via console'
 %7D);%0A
-
     %7D;%0A%0A
@@ -1553,16 +1553,51 @@
 s: 'ack'
+, text: 'status change via console'
 %7D);%0A    
@@ -1683,32 +1683,32 @@
 ' + id + '%7D);')%0A
-
       Alert.stat
@@ -1737,16 +1737,51 @@
 'closed'
+, text: 'status change via console'
 %7D);%0A    

add text to status change