/**
 * SubscriptionsCtrl Controller
 * Extracted from clients module
 */

ClientsModule.controller('SubscriptionsCtrl', ['$scope', '$http', 'apiUrl', 'uiGridConstants', function ($scope, $http, apiUrl, uiGridConstants) {

	var myAwesomeSortFn = function (a, b) {
		if (a.severity_id == b.severity_id) return 0;
		if (a.severity_id < b.severity_id) return -1;
		return 1;
	};

	$scope.subscriptions = [];
	$scope.load = function () {
		$http.get(apiUrl('clients/subscriptions/'))
			.success(function (response) {
				$scope.gridOptions.data = response;
			});
	};

	$scope.columns = [
		{
			name: 'ID',
			field: 'id',
			cellTemplate: '<div class="ui-grid-cell-contents"><a href="/tasks/{[{ row.entity | taskUrl }]}">{[{grid.getCellValue(row,col)}]}</a></div>'
		},
		{
			field: 'title'
		},
		{
			name: 'client',
			field: 'client.name'
		},
		{
			name: 'contractType',
			field: 'client.contract.name'
		},
		{
			name: 'status',
			field: 'status.name'
		},
		{
			name: 'severity',
			field: 'severity.name',
			sort: { direction: uiGridConstants.ASC, priority: 0 }
		},
		{
			name: 'Type',
			field: 'type'
		},
		{
			name: 'assignedTo',
			field: 'user.first_name'
		},
		{
			name: 'created',
			field: 'created_at',
			cellTemplate: '<div class="ui-grid-cell-contents"> {[{ grid.getCellValue(row, col) | fromNow }]} </div>',
			sort: { direction: uiGridConstants.ASC, priority: 1 }
		}];
	$scope.gridOptions = {
		enableHorizontalScrollbar: 0,
		enableVerticalScrollbar: 2,
		enableSorting: true,
		columnDefs: $scope.columns,
		onRegisterApi: function (gridApi) {
			$scope.gridApi = gridApi;
		}
	};

	$scope.load();

}]);
