/**
 * archAuthCtrl Controller
 * Extracted from bookings module
 */

Bookings.controller('archAuthCtrl', ['$scope', 'archAuthService', '$http', 'ErrorsHandler', '$uibModalInstance', function ($scope, service, $http, ErrorsHandler, $uibModalInstance) {

	$scope.service = service;
	$scope.data = {};

	$scope.helper = {
		loading: false,
	};

	$scope.hideModal = function () {
		$uibModalInstance.dismiss();
	};

	$scope.submit = function () {

		if ($scope.helper.loading)
			return;

		$scope.helper.loading = true;

		$http.post('/auth/validate', $scope.data, { params: { login: true } })
			.success(function (response) {
				if (response.success === true) {
					service.respond(true, $scope.helper);
				}
				else {
					$scope.helper.loading = false;
					$scope.data.password = '';

					Messenger().post({
						message: 'Invalid credentials',
						hideAfter: 5,
						type: 'error'
					});
				}
			})
			.error(function (a, b) {
				$scope.helper.loading = false;
				ErrorsHandler(a, b);
			});
	};

}])
