'use strict';
app.controller('signaturePoliciesAdminController', ['$scope', '$http', '$modal', '$timeout', '$translate', 'blockUI', 'lib', function ($scope, $http, $modal, $timeout, $translate, blockUI, lib) {

	$scope.policies = null;
	$scope.policyTypes = ['Cades', 'Pades', 'Xml'];
	$scope.newType = 'Cades';

	var init = function () {
		if (!lib.checkIsAdmin()) {
			return;
		}
		$http.get('/Api/SignaturePolicies').then(function (response) {
			$scope.policies = response.data;
		}, lib.handleRestError);
	};

	$scope.showPolicy = function (policy) {
		var modalInstance = $modal.open({
			templateUrl: views.signaturePolicyDetails,
			controller: 'signaturePolicyDetailsController',
			size: 'lg',
			resolve: {
				modelId: function () {
					return policy.id;
				},
				isEditorEnabled: function () {
					return true;
				}
			}
		});
		modalInstance.result.then(init, init);
	};

	$scope.create = function () {
		blockUI.start();
		$http.post('/Api/Admin/SignaturePolicies', {
			name: $scope.newName,
			type: $scope.newType
		}).then(function (response) {
			$scope.policies.push(response.data);
			$scope.newName = '';
			blockUI.stop();
		}, lib.handleRestError);
	};

	init();
}]);
