'use strict';

angular.module('app')
    .controller('rgiAssignAssessmentSupervisorDialogCtrl', function (
        $scope,
        $location,
        $route,
        ngDialog,
        rgiNotifier,
        rgiAssessmentSrvc,
        rgiAssessmentMethodSrvc,
        rgiIdentitySrvc,
        rgiUserSrvc
    ) {
        $scope.current_user = rgiIdentitySrvc.currentUser;

        rgiAssessmentSrvc.get({assessment_ID: $scope.$parent.assessment_update_ID}, function(assessment) {
            $scope.assessment = assessment;

            for(var supervisorIndex in $scope.assessment.supervisor_ID) {
                if($scope.assessment.supervisor_ID.hasOwnProperty(supervisorIndex)) {
                    $scope.assessment.supervisor_ID[supervisorIndex] = $scope.assessment.supervisor_ID[supervisorIndex]._id;
                }
            }

            if($scope.assessment.supervisor_ID.length === 0) {
                $scope.assessment.supervisor_ID.push({});
            }

            rgiUserSrvc.query({role: 'supervisor'}, function(users) {
                $scope.availableUsers = users;
            });
        });

        $scope.addAssignee = function () {
            $scope.assessment.supervisor_ID.push({});
        };

        $scope.removeAssignee = function (index) {
            $scope.assessment.supervisor_ID.splice(index, 1);
        };

        $scope.assignAssessmentSupervisor = function () {
            if ($scope.assessment.supervisor_ID.length < 1) {
                rgiNotifier.error('You must select a supervisor!');
            } else {
                rgiAssessmentMethodSrvc.updateAssessment($scope.assessment)
                    .then(function () {
                        rgiNotifier.notify('Assessment assigned!');
                        $scope.closeThisDialog();
                        $route.reload();
                    }, function (reason) {
                        if (reason) {
                            rgiNotifier.error(reason);
                        } else {
                            rgiNotifier.error('Validation error');
                            $scope.closeThisDialog();
                            $route.reload();
                        }
                    });
            }
        };

        $scope.closeDialog = function () {
            ngDialog.close();
        };
    });

@@ -924,39 +924,19 @@
 pe.a
-ssessment.supervisor_ID.push(%7B%7D
+ddAssignee(
 );%0A 
@@ -1191,10 +1191,17 @@
 ush(
-%7B%7D
+undefined
 );%0A 
@@ -1355,34 +1355,27 @@
 ope.
-a
+isA
 ssign
-AssessmentSupervisor
+eeListEmpty
  = f
@@ -1373,38 +1373,74 @@
 Empty = function
- 
 () %7B%0A
+            var assigneeNumber = 0;%0A%0A
             if (
@@ -1437,17 +1437,16 @@
       if
- 
 ($scope.
@@ -1459,33 +1459,366 @@
 ment
-.supervisor_ID.length %3C 1
+) %7B%0A                $scope.assessment.supervisor_ID.forEach(function(assignee) %7B%0A                    if(assignee) %7B%0A                        assigneeNumber++;%0A                    %7D%0A                %7D);%0A            %7D%0A%0A            return assigneeNumber %3C 1;%0A        %7D;%0A%0A        $scope.saveAssigneeList = function () %7B%0A            if ($scope.isAssigneeListEmpty()
 ) %7B%0A

fix the validation of empty users