'use strict';
app.controller('billingController', ['$scope', '$http', '$translate', '$timeout', '$filter', 'blockUI', 'lib', 'NgTableParams', function ($scope, $http, $translate, $timeout, $filter, blockUI, lib, NgTableParams) {
	$scope.transactionTypes = [
		'PadesSignature',
		'CadesSignature',
		'XmlSignature',
		'CertAuthentication',
		'Timestamp',
		'OpenSignature'
	];

	var init = function () {
		if (!lib.checkIsAuthenticated()) {
			return;
		}

		blockUI.start();

		$scope.tableParams = new NgTableParams();

		$scope.fetchPage();

		blockUI.stop();
	};

	$scope.fetchPage = function () {
		$scope.loading = true;
		$http.get('/Api/Billing').then(function (response) {
			$scope.data = response.data;

			$scope.tableParams.settings({
				dataset: response.data,
				sortingIndicator: 'div'
			});

			$scope.tableParams.sorting('total.success', 'desc');

			$timeout(function () { $scope.loading = false; }, 500);
		}, lib.handleRestError);
	};

	$scope.sum = function (items, prop1, prop2) {
		if (items) {
			return items.reduce(function (total, item) {
				var property = item[prop1]; 
				return total + property[prop2];
			}, 0);
		}
		return 0;
	};

	// view
	init();

}]);
