(function() {
	"use strict";

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

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

		vm.saving = false;
		vm.user = {};

		vm.statusMessage =  "Create a user";
		vm.errorMessages = [];

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