myApp.controller("openPoolController", function($scope, $http, myFactory, $mdDialog, appConfig, exportUiGridService) {
	$scope.records = [];
	var today = new Date();
	$scope.gridOptions = {
			paginationPageSizes : [ 10, 20, 30, 40, 50, 100],
			paginationPageSize : 10,
		    pageNumber: 1,
			pageSize:10,
			enableFiltering:true,
			columnDefs : [ 
				{field : 'employeeId',displayName: 'Employee ID', enableColumnMenu: true, enableSorting: true, enableFiltering: true,width:'*'},
				{field : 'employeeName',displayName: 'Employee Name', enableColumnMenu: false, enableSorting: false, enableFiltering: true,width:'*'},
				{field : 'resourceRole',displayName: 'Role', enableColumnMenu: false, enableSorting: false,enableFiltering: true,width:'*'}, 
				{field : 'designation',displayName: 'Designation', enableColumnMenu: false,enableFiltering: false,width:'*'},
				{field : 'billableStatus',displayName: 'Billability', enableColumnMenu: false,enableSorting: false,enableFiltering: true , cellFilter: 'date:"dd-MMM-yyyy"',width:'*'}, 
				{field : 'billingStartDate',displayName: 'Start Date', enableColumnMenu: false, enableSorting: false, enableFiltering: true,width:'*',cellFilter: 'date:"dd-MMM-yyyy"'}
			],
		enableGridMenu: true,
	    enableSelectAll: true,
	    exporterMenuExcel:false,
	    exporterMenuCsv:false,
	    exporterCsvFilename: 'openPoolReport.csv',
	    exporterExcelFilename:'OpenPoolReport',
	    exporterPdfDefaultStyle: {fontSize: 9},
	    exporterPdfTableStyle: {margin: [15, 15, 15, 15]},
	    exporterPdfTableHeaderStyle: {fontSize: 10, bold: true, italics: true, color: 'red'},
	    exporterPdfHeader: { text: "Open Pool  Report", style: 'headerStyle' },
	    exporterPdfFooter: function ( currentPage, pageCount ) {
	      return { text: currentPage.toString() + ' of ' + pageCount.toString(), style: 'footerStyle' };
	    },
	    exporterPdfCustomFormatter: function ( docDefinition ) {
	      docDefinition.styles.headerStyle = { fontSize: 22, bold: true };
	      docDefinition.styles.footerStyle = { fontSize: 10, bold: true };
	      return docDefinition;
	    },
	    exporterPdfOrientation: 'portrait',
	    exporterPdfPageSize: 'LETTER',
	    exporterPdfMaxGridWidth: 400,
	    exporterCsvLinkElement: angular.element(document.querySelectorAll(".custom-csv-link-location")),
	    onRegisterApi: function(gridApi){
	      $scope.gridApi = gridApi;
	    },
	            gridMenuCustomItems: [{
	                    title: 'Export all data as EXCEL',
	                    action: function ($event) {
	                        exportUiGridService.exportToExcel('sheet 1', $scope.gridApi, 'all', 'all');
	                    },
	                    order: 110
	                },
	                {
	                    title: 'Export visible data as EXCEL',
	                    action: function ($event) {
	                        exportUiGridService.exportToExcel('sheet 1', $scope.gridApi, 'visible', 'visible');
	                    },
	                    order: 111
	                }
	            ]
		};
	$scope.gridOptions.data = [];

	$scope.getOpenPoolRecords = function(type){
		$mdDialog.hide();
		if(type == "onload"){
			showProgressDialog("Fetching data please wait...");
		}
		$http({
	        method : "GET",
	        url : appConfig.appUri + 'resources/project/Nisum0000?status=Active'
	    }).then(function mySuccess(response) {
	    	$mdDialog.hide();
	    	today.setHours(0, 0, 0, 0);
	    	var openPoolRecords = response.data.records.filter(function (employee) {
    		    return employee.billingEndDate >= today;
    		});
	        $scope.gridOptions.data = openPoolRecords;
	        if(response.data.records.length > 10){
	    		$scope.gridOptions.enablePaginationControls = true;
	    	}
	    	else{
	    		$scope.gridOptions.enablePaginationControls = false;
	    	}
	    }, function myError(response) {
	    	showAlert("Something went wrong while fetching data!!!");
	    	$scope.gridOptions.data = [];
	    });
	};
	function showProgressDialog(msg){
		$('#home').addClass('md-scroll-mask');
		$mdDialog.show({
	      templateUrl: 'templates/progressDialog.html',
	      controller: ProgressController,
	      parent: angular.element(document.body),
	      clickOutsideToClose:false,
	      locals: {dataToPass:msg}
	    });
	}
	function ProgressController($scope, dataToPass) {
		$scope.progressText = dataToPass;
	}
});