/**
 * RequestExistingPartCtrl Controller
 * Extracted from bookings module
 */

Bookings.controller('RequestExistingPartCtrl', ['$scope', 'Part', 'Stock', 'ErrorsHandler', 'PartsService', '$location', 'apiUrl', '$uibModal', function ($scope, Part, Stock, ErrorsHandler, PartsService, $location, apiUrl, $uibModal) {

	/**
	 * Form data.
	 *
	 * @type Object
	 */
	$scope.data = {};


	$scope.service = {

		select2Options: {
			allowClear: true,
			minimumInputLength: 1,
			placeholder: 'Select a part to order',
			query: function (options) {
				var discontinued = typeof $scope.data.discontinued !== 'undefined' ? $scope.data.discontinued : 0;
				options.discontinued = discontinued;
				var response = function (response) {

					options.callback({
						more: response.current_page < response.last_page,
						results: response.data
					});

				};

				Stock.get({ query: options.term, page: options.page, show_discontinued: options.discontinued }, response, ErrorsHandler);
			},
			formatResult: function (obj, container, query) {
				return obj.name + ' - £' + obj.retail_price + ' (In stock: ' + obj.quantity + ')';
			},
			formatSelection: function (obj) {
				return obj.name;
			}
		}

	};

	/**
	 * A link to parts array
	 *
	 * @type Array
	 */
	$scope.parts = PartsService.parts;


	$scope.selectPart = function () {
		// console.log('selectPart() | $scope.data -> ', $scope.data);
		$scope.errors = "";
		$scope.data.stock_id = $scope.data.part_search.id;
		$scope.data.stock = $scope.data.part_search;
	}

	/**
	 * Brings up the modal form.
	 *
	 * @return void
	 */
	$scope.showModal = function () {
		$scope.data = {};
		$scope.errors = null;

		$scope.modal = $uibModal.open({
			templateUrl: 'modals/request-existing-part-modal.html',
			scope: $scope
		});
	};

	/**
	 * Hides the modal
	 *
	 * @return void
	 */
	$scope.hideModal = function () {
		$scope.modal.dismiss();
	};

	/**
	 * Form submission event, on success
	 * we clear up the form.
	 *
	 * @return void
	 */
	$scope.save = function () {

		$scope.loading = true;

		$scope.data.related_id = $scope.related.id;
		$scope.data.related_type = $scope.related_type;

		//Do not send the part, just add it to the temporal parts array
		if ($scope.virtualMode) {
			var part = {
				stock: $scope.data.stock,
				stock_id: $scope.data.stock.id,
				quantity: $scope.data.quantity,
				price: $scope.data.stock.price_excluding_vat,
				on_order: 1
			};

			$scope.parts.push(part);
			$scope.hideModal();
			$scope.data = {
				quantity: 1,
				include_vat: 1
			};
			$scope.data.part_search = undefined;
			$scope.loading = false;
		}
		else {
			Part.requestPart($scope.data, function (response) {

				$scope.hideModal();
				$scope.data = {
					quantity: 1,
					include_vat: 1
				};
				$scope.parts.push(response);
				$scope.data.part_search = undefined;
				$scope.loading = false;

			}, function (response) {
				$scope.loading = false;
				$scope.errors = _.isObject(response.data.error) ? response.data.error.message : response.data.error;
			});
		}
	};

}])
