app.controller('StudentSupportController',['$scope','$http','subsidizedStudentFactory','courseFactory',
    function ($scope, $http, subsidizedStudentFactory, courseFactory) {
        $scope.availableTerms = {}; // Terms available to select
        $scope.selectedTerm = "";
        $scope.terms = [];

        $scope.sortType = 'inumber'; // set the default sort type
        $scope.sortReverse = false;  // set the default sort order
        $scope.baseUrl = appUrl;

        $scope.initialize = function () {
            $scope.GetTerms();
        };

        $scope.searchStudents = function (query, term) {
            subsidizedStudentFactory.getStudent(query, term)
                .then(function(response) {
                    $scope.subsidizedStudentList = response.data;
                }, function(error) {
                    $scope.status = "Unable to find any matching students";
                });
        };

        $scope.changeTerm = function (term) {
            // Term was changed, update what we have on the screen            
         
        };

        $scope.GetTerms = function () {
            return courseFactory.getTerms()
                .then(function (response)
                {
                    var data = response.data;
                    $scope.availableTerms = data;
                    // Split out our terms object
                    if ($scope.availableTerms.activeSemester) {
                        $scope.selectedTerm = data.activeSemester;
                        $scope.terms.push(data.activeSemester);
                    }
                    if ($scope.availableTerms.openList) {
                        $scope.availableTerms.openList.forEach(function (element, index) {
                            if (data.activeSemester && data.activeSemester.termToUse === element.termToUse) {
                            } else {
                                $scope.terms.push(element);
                            }
                        });
                    }
                },
                function () {
                    $scope.availableTerms = {};
                });
        };
    }]);