/**
 * PromoteProjectCtrl Controller
 * Extracted from bookings module
 */

Bookings.controller('PromoteProjectCtrl', ['$scope', '$uibModalInstance', '$window', 'Tasks', 'ErrorsHandler', '$ClientsManager', function ($scope, $uibModalInstance, $window, Tasks, ErrorsHandler, ClientsManager) {

	$scope.data = {};

	$scope.save = function () {
		if (!$scope.data.description) {
			$scope.projectForm.$attempted = true;
			$scope.projectForm.description.$invalid = true;
			return false;
		}

		$scope.data.id = $scope.$parent.$$childHead.$$watchers[0].last; // nadeem 28.2.17 mini-hack - directive scope not passing taskId to controller
		$scope.data.client_id = ClientsManager.client.id;
		$scope.data.client_type = ClientsManager.getClientType();

		$scope.redirect_url = '/projects'; // nadeem 28.2.17 - defined this so the redirect went to the correct place

		Tasks.promote($scope.data, function (response) {
			$window.location.href = $scope.redirect_url + "/" + response.project_id;

		}, ErrorsHandler);

	};

	$scope.close = function () {
		$uibModalInstance.dismiss();
	};
}]);
