angular
    .module('universal')
    .controller('InstancesController', function ($scope, $http) {

        $scope.VIEW = {
            isLoading: false,
            isLoaded: false
        }

        //pagination settings
        $scope.maxSize = 5;
        $scope.totalRecords = 1;
        $scope.pageNum = 1;
        $scope.pageSize = 10;

        //$scope.processes = [];
        //$http.get('/processes/list/').then(function (response) {
          //  $scope.processes = response.data
        //});

        $scope.loadInstances = function () {
            $scope.VIEW.isLoading = true;
            $.post('/instances/Load/',
                {
                    pageNum: $scope.pageNum,
                    pageSize: $scope.pageSize
                },
                function (res) {

                    console.log(res);

                    $scope.VIEW.isLoading = false;
                    $scope.VIEW.isLoaded = true;

                    $scope.$apply(function () {
                        $scope.Items = res.instances;
                        $scope.totalRecords = res.totalRecords;

                        console.log($scope.totalRecords);
                    });
                }
            );
        };

        $scope.$watch('pageNum', function () {
            if ($scope.VIEW.isLoaded) {
                $scope.clearRepeater();
                $scope.loadInstances();
            }
        });

        $scope.clearRepeater = function () {
            $scope.Items.splice(0, $scope.Items.length);
        };

        $scope.loadInstances();
    });