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

	//set scope vars
	$scope.currentChampionship = $routeParams.idchampionship;

	$scope.refreshPoules = function() {
		var idchampionship = $routeParams.idchampionship;
		console.log('doing refreshPoules');
		PouleService.getPoules(idchampionship).
		success(function(data, status, headers, config) {
			console.log('refreshPoules successful...');
			console.log(data);
			if(data['ok']==true) {
				$scope.poules = data.poules;
			} else {
			}
		}).
		error(function(data, status, headers, config) {
			console.log('refreshChampionships failed...');
		});
	};

	$scope.addPoule = function(newpoule) {
		PouleService.addPoule(newpoule, 	$scope.currentChampionship).
			success(function(data, status, headers, config) {
				console.log('addPoule successful...');
				console.log(data);
				if(data['ok']==true) {
						$scope.refreshPoules();
						newpoule.tier = "";
						newpoule.poule = "";
				}
			}).
			error(function(data, status, headers, config) {
				console.log('addPoule failed...');
			}
		);;
	};

	$scope.deletePoule = function(tier, poule, idchampionship) {
		PouleService.deletePoule(tier, poule, idchampionship).
			success(function(data, status, headers, config) {
				console.log('deletePoule successful...');
				console.log(data);
				if(data['ok']==true) {
						$scope.refreshPoules();
				}
			}).
			error(function(data, status, headers, config) {
				console.log('deletePoule failed...');
			}
		);
	};

	//do initial refresh of poules
	$scope.refreshPoules();
}]);
