(function() {
	"use strict";

	window.app.directive('editInventoryType', editInventory);

	function editInventory() {
		return {
			scope: {
				inventoryType: "="
			},
			templateUrl: '/inventoryType/template/inventoryTypeEdit.tmpl.cshtml',
			controller: controller,
			controllerAs: 'vm'
		}
	}

	controller.$inject = ['$scope', 'inventoryTypeSvc'];
	function controller($scope, inventoryTypeSvc) {
		var vm = this;
		vm.inventoryType = angular.copy($scope.inventoryType);
		vm.save = save;

		vm.saving = false;
		vm.inventoryTypeSvc = inventoryTypeSvc;
		vm.errorMessages = [];

		function save() {
			vm.saving = true;
			inventoryTypeSvc.update($scope.inventoryType, vm.inventoryType)
				.success(function () {
					//Close the modal
					$scope.$parent.$close();
				})
				.error(function(data) {
					vm.errorMessages = angular.copy(data.errorMessages, vm.errorMessages);
				})
				.finally(function() {
					vm.saving = false;
				});
		}
	}
})();