/**
 * BookingsOrderCtrl Controller
 * Extracted from bookings module
 */

Bookings.controller('BookingsOrderCtrl', ['$scope', '$route', '$routeParams', '$location', '_orderForm', '$ClientsManager', 'BookingsLocationManager', 'PartsService', function ($scope, $route, $routeParams, $location, _orderForm, $ClientsManager, BookingsLocationManager, PartsService) {

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

	$scope.data = _orderForm.data;
	$scope.data.parts = PartsService.parts;

	// working with the shared service managers
	$scope.clientManager = $ClientsManager;
	$scope.locationManager = BookingsLocationManager;

	$scope.form = _orderForm;
	$scope.routeParams = $routeParams;

	// added by nadeem 13.2.17 - if the user use the add task to project button
	// then this will pass the project id forward to the back-end
	// the crated task will then be correctly associated with the correct project
	if (typeof ($scope.$parent.projectId != 'undefined')) {
		// console.log($scope.$parent.projectId);
		$scope.data.project_id = $scope.$parent.projectId;
	}

	// ------------------------------------------------------------------------
	//
	//	WATCHERS AND EVENT LISTENERS BELOW -->
	//
	// ------------------------------------------------------------------------
	$scope.$watch('data.site_user_id', function () {
		$scope.data.direct_dial = BookingsLocationManager.getUserDDI($scope.data.site_user_id);
	});

	$scope.$watch('data.site_id', function () {
		$scope.data.direct_dial = BookingsLocationManager.getUserDDI($scope.data.site_user_id);
	});



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

	$scope.save = function () {

		if (!PartsService.hasPartsOnOrder()) {
			alert("To create an order, at least one part needs to be ordered!");
			return;
		}

		_orderForm.save();
	};


	$scope.addPart = function (event, data) {

		var part = _.find($scope.data.parts, function (el) {
			if (el.stock)
				return el.stock.id == data.id && !el.on_order;
			return false;
		});

		$scope.$apply(function () {

			if (part) {
				part.quantity++;
				$scope.updatePart(part);

			} else {
				part = {
					related_type: 'task',
					stock: data,
					stock_id: data.id,
					quantity: 1,
					price: data.price_excluding_vat

				};

				$scope.data.parts.push(part);

			}
		});
	};

	$scope.removePart = function (part) {

		var index = _.findIndex($scope.parts, function (el) {
			return el.stock_id === part.stock_id && el.on_order == part.on_order;
		});

		$scope.parts.splice(index, 1);

	}

	$scope.subtotal = function () {
		var subtotal = 0;
		_.each($scope.parts, function (part) {
			subtotal += part.price * part.quantity;
		});
		return subtotal;
	}

	$scope.total = function () {
		return $scope.subtotal() * (1 + $scope.vat / 100);
	}

	$scope.tax = function () {
		return $scope.subtotal() * ($scope.vat / 100);
	}

}]);
