angular.module("myApp").controller('IssuesShowController', function ($scope,$stateParams,$http,$state,$filter) {
    //lấy danh sách +phân trang+tìm kiếm
    $scope.ListIssues = [];
    $scope.currentPage = 1;
    $scope.numPerPage = 3;
    $scope.maxSize = 5;
    $scope.numberOfPage = numberOfPage;
    $scope.dataHasLoaded = false;
    //mới thêm
    $scope.makeEmployee = function () {
        $scope.ListIssues = [];
        for (i = 1; i <= 100; i++) {
            $scope.ListIssues.push({text: 'issue ' + i, done: false});
        }
    };
    $scope.keyword = "";
    $scope.getAll = getAll;
    $scope.getTotalItems = getTotalItems;

    getAll();

    function getTotalItems() {
        return $filter('filter')($scope.ListIssues, {'title': $scope.keyword}).length;
    }

    $scope.pageChangedIndex = function(){
        console.log($scope.currentPage);
    }

    function numberOfPage() {
        return Math.ceil($scope.ListIssues.length / $scope.numPerPage);

    };
    function getAll() {
        $http.get('http://localhost:8081/danh-sach-issues').then(successCallback, errorCallback);

        function successCallback(response) {
            console.log(response.data);
            {
                $scope.ListIssues = response.data;
            }
        }
    }
    function errorCallback(error) {
        //error code
        console.log("can't get data!!");
    }
    //end

    //update
    $scope.updateIssues = function (issue) {
        if (confirm("Bạn có muốn thay đổi trạng thái không ?")) {
            $http({
                method: 'PUT',
                url: 'http://localhost:8081/sua-issues',
                data: angular.toJson(issue),
                headers: {
                    'Content-Type': 'application/json'
                }
            }).then(successCallback, errorCallback);

            function successCallback(response) {
                $scope.view = response.data;
                if ($scope.view.status == 200) {
                    $state.reload();
                }
            }

            function errorCallback(error) {
                //error code
                console.log("can't update data!!");
            }
        }
    };
});