/**
 * SiteUserModalCtrl Controller
 * Extracted from clients module
 */

ClientsModule.controller('SiteUserModalCtrl', ['$scope', '$uibModalInstance', 'data', 'DoNotSave', 'SiteUser', 'ErrorsHandler', 'site', function ($scope, $uibModalInstance, data, DoNotSave, SiteUser, ErrorsHandler, site) {

	$scope.data = data || { authority: 0 };
	$scope.data.site_id = site.id;

	$scope.submit = function () {

		if (!DoNotSave) {
			if ($scope.data.id) {
				SiteUser.update($scope.data, function (response) {
					$uibModalInstance.close(response);
				}, ErrorsHandler);
			}
			else {
				SiteUser.save($scope.data, function (response) {
					$uibModalInstance.close(response);
				}, ErrorsHandler);
			}
		}
		else {
			$uibModalInstance.close($scope.data);
		}
	};

	$scope.cancel = function () {
		$uibModalInstance.dismiss('cancel');
	};
}]);
