'use strict';

// Courses controller
angular.module('courses').controller('CoursesController', ['$scope', '$stateParams', '$location', 'Authentication', 'Courses', '$modal', '$log',
	function($scope, $stateParams, $location, Authentication, Courses, $modal, $log) {
		$scope.authentication = Authentication;

		// Blocking non admins from creating/editing/deleting courses..
		
		$scope.open = function (size) {

			var modalInstance = $modal.open({
				templateUrl: 'modules/courses/views/create-course.client.view.html',
				controller: function ($scope, $modalInstance){

					$scope.ok = function () {
						//can only close if the form is valid!
						//eg if the "required" field is filled

						//if (createCustomerForm.$valid){
							$modalInstance.close();
						//}
					};

					$scope.cancel = function () {
						$modalInstance.dismiss('cancel');
					};
				},
				size: size
			});

			modalInstance.result.then(function (selectedItem) {
				$scope.selected = selectedItem;
			}, function () {
				$log.info('Modal dismissed at: ' + new Date());
			});
		};
	

		// Create new Course
		$scope.create = function() {
			// Create new Course object
			var course = new Courses ({
				name: this.name,
				courseCode: this.courseCode,
				owner: $scope.authentication.user._id
			});

				// Redirect after save
				course.$save(function(response) {
					$location.path('courses/' + response._id);

					// Clear form fields
					$scope.name = '';
				}, function(errorResponse) {
					$scope.error = errorResponse.data.message;
				});
			};


	// Remove existing Course
		$scope.remove = function(course) {
			if ($scope.authentication.user.roles[0] === 'admin'){
				if ( course ) { 
					course.$remove();

					for (var i in $scope.courses) {
						if ($scope.courses [i] === course) {
							$scope.courses.splice(i, 1);
						}
					}
				} else {
					$scope.course.$remove(function() {
						$location.path('courses');
					});
				}
			}
		};

		// Update existing Course
		$scope.update = function() {
			if ($scope.authentication.user.roles[0] === 'admin') {
				var course = $scope.course;

				course.$update(function () {
					$location.path('courses/' + course._id);
				}, function (errorResponse) {
					$scope.error = errorResponse.data.message;
				});
			}
		};

		// Find a list of Courses
		$scope.find = function() {
			$scope.courses = Courses.query();
		};

		// Find existing Course
		$scope.findOne = function() {
			$scope.course = Courses.get({
				courseId: $stateParams.courseId
			});
		};

	}
]);

@@ -1156,16 +1156,75 @@
  object%0A
+%09%09%09if ($scope.authentication.user.roles%5B0%5D === 'admin')%7B%0A%0A%09
 %09%09%09var c
@@ -1250,16 +1250,17 @@
  (%7B%0A%09%09%09%09
+%09
 name: th
@@ -1268,16 +1268,17 @@
 s.name,%0A
+%09
 %09%09%09%09cour
@@ -1306,16 +1306,17 @@
 de,%0A%09%09%09%09
+%09
 owner: $
@@ -1345,16 +1345,17 @@
 ser._id%0A
+%09
 %09%09%09%7D);%0A%0A
@@ -1358,16 +1358,17 @@
 );%0A%0A%09%09%09%09
+%09
 // Redir
@@ -1382,16 +1382,17 @@
 er save%0A
+%09
 %09%09%09%09cour
@@ -1417,24 +1417,25 @@
 response) %7B%0A
+%09
 %09%09%09%09%09$locati
@@ -1476,16 +1476,17 @@
 ;%0A%0A%09%09%09%09%09
+%09
 // Clear
@@ -1498,16 +1498,17 @@
  fields%0A
+%09
 %09%09%09%09%09$sc
@@ -1522,24 +1522,25 @@
 e = '';%0A%09%09%09%09
+%09
 %7D, function(
@@ -1548,32 +1548,33 @@
 rrorResponse) %7B%0A
+%09
 %09%09%09%09%09$scope.erro
@@ -1605,28 +1605,35 @@
 essage;%0A%09%09%09%09
+%09
 %7D);%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D;%0A%0A%0A%09// 

Add safeguard on front-end controller to create course