(function() {
	'use strict';

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

	controller.$inject = ['$scope'];
	function controller($scope) {
		var vm = this;
		vm.query = {};
		vm.submitting = false;
		vm.errorMessages = [];
		vm.submit = submit;

		function submit() {
			vm.submitting = true;
			$scope.queryFn(vm.query)
				.error(function (data) {
					vm.errorMessages = angular.copy(data.errorMessages, vm.errorMessages);
				})
				.finally(function () {
					vm.submitting = false;
				});
		}
	}
})();