'use strict';

angular.module('app')
    .controller('rgiDocumentTableCtrl', function (
        _,
        $scope,
        $rootScope,
        rgiDialogFactory,
        rgiAssessmentSrvc,
        rgiDocumentSrvc,
        rgiHttpResponseProcessorSrvc,
        rgiIdentitySrvc,
        rgiNotifier
    ) {
        var limit = 100,
            currentPage = 0,
            totalPages = 0;

        $scope.current_user = rgiIdentitySrvc.currentUser;
        $scope.busy = false;

        $scope.assessment_filter_options = [
            {value: 'all', text: 'Show all documents'}
            // {value: 'type', text: 'Sort by document type'},
            // {value: 'assessments', text: 'Sort by attached assessments'}
        ];

        $scope.assessment_filter = $scope.assessment_filter_options[0].value;

        rgiAssessmentSrvc.query({}, function (assessments) {
            if(assessments.reason) {
                rgiNotifier.error('No assessments');
            } else {
                assessments.forEach(function(assessment) {
                    console.log(assessment);
                    $scope.assessment_filter_options.push({
                        value: assessment.assessment_ID,
                        text: assessment.country + ' ' + assessment.year + ' ' + assessment.version
                    });
                });
            }
        });

        rgiDocumentSrvc.query({skip: currentPage, limit: limit}, function (response) {
            if(response.reason) {
                rgiNotifier.error('Load document data failure');
            } else {
                $scope.count = response.count;
                $scope.documents = response.data;
                totalPages = Math.ceil(response.count / limit);
                currentPage = currentPage + 1;
            }
        }, rgiHttpResponseProcessorSrvc.getDefaultHandler('Load document data failure'));

        $scope.loadMoreDocs = function() {
            if ($scope.busy) {
                return;
            }

            $scope.busy = true;

            if(currentPage < totalPages) {
                rgiDocumentSrvc.query({skip: currentPage, limit: limit}, function (response) {
                    if(response.reason) {
                        rgiNotifier.error('Documents loading failure');
                    } else {
                        $scope.documents = _.union($scope.documents, response.data);
                        currentPage = currentPage + 1;
                        $scope.busy = false;
                    }
                }, rgiHttpResponseProcessorSrvc.getDefaultHandler('Load document data failure'));
            }
        };

        $scope.deleteDocument = function(doc) {
            rgiDialogFactory.deleteDocument($scope, doc);
        };
    });

@@ -248,33 +248,8 @@
 vc,%0A
-        rgiIdentitySrvc,%0A
     
@@ -288,61 +288,20 @@
 var 
-limit = 100,%0A            currentPage = 0,%0A           
+currentPage,
  tot
@@ -311,74 +311,24 @@
 ages
+, limit
  = 
+10
 0;%0A%0A
-        $scope.current_user = rgiIdentitySrvc.currentUser;%0A
     
@@ -352,17 +352,16 @@
  false;%0A
-%0A
         
@@ -400,215 +400,11 @@
  = %5B
-%0A            %7Bvalue: 'all', text: 'Show all documents'%7D%0A            // %7Bvalue: 'type', text: 'Sort by document type'%7D,%0A            // %7Bvalue: 'assessments', text: 'Sort by attached assessments'%7D%0A        
 %5D;%0A
-%0A
     
@@ -438,49 +438,10 @@
 r = 
-$scope.assessment_filter_options%5B0%5D.value
+''
 ;%0A%0A 
@@ -674,53 +674,8 @@
 ) %7B%0A
-                    console.log(assessment);%0A
     
@@ -970,63 +970,344 @@
     
-rgiDocumentSrvc.query(%7Bskip: currentPage, limit: limit%7D
+$scope.$watch('assessment_filter', function(assessment) %7B%0A            currentPage = 0;%0A            totalPages = 0;%0A            var searchOptions = %7Bskip: currentPage, limit: limit%7D;%0A%0A            if(assessment) %7B%0A                searchOptions.assessments = assessment;%0A            %7D%0A%0A            rgiDocumentSrvc.queryCached(searchOptions
 , fu
@@ -1318,32 +1318,36 @@
 on (response) %7B%0A
+    
             if(r
@@ -1372,32 +1372,36 @@
                 
+    
 rgiNotifier.erro
@@ -1437,32 +1437,36 @@
 ');%0A            
+  
+  
 %7D else %7B%0A       
@@ -1450,32 +1450,36 @@
        %7D else %7B%0A
+    
                 
@@ -1517,32 +1517,36 @@
                 
+    
 $scope.documents
@@ -1563,16 +1563,168 @@
 e.data;%0A
+%0A
+                    if($scope.documents.length === 0) %7B%0A                        rgiNotifier.error('No documents uploaded');%0A                    %7D%0A%0A    
         
@@ -1791,37 +1791,27 @@
             
-currentPage =
+   
  currentPage
@@ -1803,33 +1803,33 @@
     currentPage 
-+
+=
  1;%0A            
@@ -1820,34 +1820,42 @@
  1;%0A            
+  
-%7D%0A
+  %7D%0A    
         %7D, rgiHt
@@ -1927,16 +1927,28 @@
 lure'));
+%0A        %7D);
 %0A%0A      
@@ -2150,30 +2150,28 @@
     
-rgiDocumentSrvc.query(
+var searchOptions = 
 %7Bski
@@ -2199,16 +2199,210 @@
 : limit%7D
+;%0A%0A                if($scope.assessment_filter) %7B%0A                    searchOptions.assessments = $scope.assessment_filter;%0A                %7D%0A%0A                rgiDocumentSrvc.query(searchOptions
 , functi

fix the 'Document Table' directive controller