/**
 * QuotesNewCtrl Controller
 * Extracted from invoices module
 */

Invoices.controller('QuotesNewCtrl', ['$scope', 'Quote', 'Tasks', 'ErrorsHandler', '$routeParams', '$location', 'InvoiceService', function ($scope, Quote, Tasks, ErrorsHandler, $routeParams, $location, InvoiceService) {

	// ------------------------------------------------------------------------
	//
	//	SCOPE VARIABLES BELOW -->
	//
	// ------------------------------------------------------------------------

	$scope.title = 'Quotes';
	$scope.type = 'quote';
	$scope.service = InvoiceService.create(Quote);

	// ------------------------------------------------------------------------
	//
	//	WATCHERS AND EVENT LISTENERS BELOW -->
	//
	// ------------------------------------------------------------------------

	$scope.$on('$routeChangeSuccess', function (ev, currentRoute, previousRoute) {
		if ($routeParams.taskId) {
			$scope.loadTask($routeParams.taskId);
		}
	});

	$scope.$watch('service.data.items', function () {
		$scope.service.calculateTotals();
	}, true);

	$scope.$watch('service.data.include_vat', function () {
		$scope.service.calculateTotals();
	});

	// ------------------------------------------------------------------------
	//
	//	FUNCTIONS BELOW THIS LINE -->
	//
	// ------------------------------------------------------------------------

	/**
	 * Clears the form
	 */
	$scope.reset = function () {
		$scope.service.data.items = [{ type: $scope.service.types[0].value }];
		$scope.service.data.id = null;
	};

}]);
