(function() {
	"use strict";

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

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

		vm.add = add;
		vm.inventory = { };
		vm.programNames = [
			"Admin",
			"Aging",
			"CC",
			"CCFP",
			"CIS",
			"CSBG",
			"Homemaker",
			"HUD",
			"Misdemeanor",
			"Nutrition",
			"Title V",
			"Transportation",
			"WAP",
			"Workforce"
		];

		vm.saving = false;
		vm.statusMessage = "Enter details for the inventory addition below.";
		vm.errorMessages = [];

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