(function() {
	"use strict";

	window.app.directive('inventoryRemove',
		function() {
			return {
				scope: { "inventory": "=" },
				templateUrl: '/inventory/template/inventoryRemove.tmpl.cshtml',
				controller: controller,
				controllerAs: 'vm'
			}
		});

	controller.$inject = ['$scope', 'inventorySvc'];
	function controller($scope, inventorySvc) {
		var vm = this;
		vm.inventory = $scope.inventory;

		vm.save = save;
		vm.saving = false;
		vm.removeForm = {
			inventoryId: vm.inventory.id,
			quantity: vm.inventory.quantity,
			transactionType:  vm.inventory.shredReadyDate ? "Shreded" : null
		};

		vm.statusMessage = "Enter details for the inventory removal below.";
		vm.errorMessages = [];

		function save() {
			vm.statusMessage = null;
			vm.saving = true;
			inventorySvc.remove(vm.removeForm)
				.success(function (data) {
					//Close the modal
					$scope.$parent.$close();
				})
				.error(function (data) {
					vm.errorMessages = angular.copy(data.errorMessages, vm.errorMessages);
				})
				.finally(function () {
					vm.saving = false;
				});
		}
	}
})();