angular.module('bananaraceApp.controllers').controller('TeamCtrl', ['$scope', '$routeParams', 'TeamService', function($scope, $routeParams, TeamService) {
  //fetch all poules for the given championship
  var teams = [];

  //set the current championship
  $scope.currentChampionship = $routeParams.idchampionship;

  $scope.refreshTeams = function() {
    console.log('doing refreshTeams');
    var idchampionship = $routeParams.idchampionship;
    TeamService.getTeams(idchampionship).
    success(function(data, status, headers, config) {
      console.log('refreshTeams successful...');
      console.log(data);
      if(data['ok']==true) {
        $scope.teams = data.teams;
      } else {
      }
    }).
    error(function(data, status, headers, config) {
      console.log('refreshTeams failed...');
    });
  };

  $scope.addTeam = function(newteam) {
    var idchampionship = $routeParams.idchampionship;
    console.log('controller adding team... '+idchampionship);
    TeamService.addTeam(newteam, idchampionship).
    success(function(data, status, headers, config) {
      console.log(data);
      if(data['ok']==true) {
        $scope.refreshTeams();
        newteam.name = "";
      }
    }).
    error(function(data, status, headers, config) {
      console.log('addTeam failed...');
    }
  )};

  $scope.deleteTeam = function(team) {
    var idchampionship = $routeParams.idchampionship;
    console.log('controller deleting team...');
    TeamService.deleteTeam(team, idchampionship).
    success(function(data, status, headers, config) {
      console.log(data);
      if(data['ok']==true) {
        $scope.refreshTeams();
      }
    }).
    error(function(data, status, headers, config) {
      console.log('deleteTeam failed...');
    }
  );
};




$scope.refreshTeams();
}]);
