var app = angular.module('myApp');
app.controller('loadGroupProjectByProjectId', loadGroupProjectByProjectId);
app.controller('insertEmployeeProject', insertEmployeeProject);
app.controller('deleteEmployeeProject', deleteEmployeeProject);
//	controlers tạo Get API
function loadGroupProjectByProjectId($scope,$stateParams,$http) {
    $http.get('http://localhost:8081/thong-tin-du-an/'+ $stateParams.ID).then(successCallback, errorCallback);
    function successCallback(response) {
        console.log(response.data);
        {
            $scope.listGruopProject = response.data
        }
    }

    function errorCallback(error) {
        //error code
        console.log("can't get data!!");
    }
}
// tạo controllers insert API
function insertEmployeeProject($scope, $http, $window) {
    $scope.insert_employeeproject = function () {
        $http({
            //khai báo type
            method: 'POST',
            //đường dẫn API
            url: 'http://localhost:8081/them-thanh-vien',
            //truyền dữ liệu nhập trên cline vào data

            data: angular.toJson($scope.employeeProject),
            //kiểu dữ liệu API
            headers: {
                'Content-Type': 'application/json'
            }
            //    kết quả trả về (THEN)
        }).then(successCallback, errorCallback);

        //tạo funtion nếu thành công!
        function successCallback(response) {
            $scope.view = response.data;
        }

        //tạo funtion kiểm tra nếu thất bại
        function errorCallback(error) {
            //error code
            console.log("can't insert data!!");
        }
    }
    //get Employee
    $http({
        method : 'GET',
        url : "http://localhost:8081/list_employee/"
    }).then(function successCallback(response) {
        console.log(response);

        $scope.eProject=response.data;
    }, function errorCallback(response) {
        console.log(response)
    });
//    get Project
    $http.get("http://localhost:8081/quan-tri/danh-sach-du-an").then(successCallback, errorCallback);

    function successCallback(response) {
        console.log(response.data);
        {
            $scope.pGroupProject = response.data
        }
    }

    function errorCallback(error) {
        //error code
        console.log("can't get data!!");
    }
};
function deleteEmployeeProject($scope, $window, $http) {
    $scope.deleteEmployeeProject = function (gruopProject) {
        if (confirm("Bạn có chắc chắn muốn xóa ?")) {
            $http({
                method: 'DELETE',
                url: 'http://localhost:8081/xoa-thanh-vien-du-an',
                data: $scope.gruopProject,
                headers: {
                    'Content-Type': 'application/json'
                }
            }).then(successCallback, errorCallback);

            //tạo funtion nếu thành công!
            function successCallback(response) {
                $window.location.reload();
            }

            //tạo funtion kiểm tra nếu thất bại
            function errorCallback(error) {
                //error code
                console.log("can't delete data!!");
            }
        }
    };
};