
angular
    .module('universal')
    .controller('ModalCtrl',
        function ($scope, $modal, $http) {
            $scope.source = {
                name: '',
                id: undefined
            };
            $scope.process = { id: undefined };
            $scope.rule = { id: undefined };

            $scope.archiveRule = function () {
                return $modal.open({
                    templateUrl: 'archiveRuleModalContent.html', // loads the template
                    backdrop: true, // setting backdrop allows us to close the modal window on clicking outside the modal window
                    windowClass: 'modal', // windowClass - additional CSS class(es) to be added to a modal window template
                    controller: function ($scope, $modalInstance, rule) {
                        $scope.rule = rule;
                        $scope.archive = function () {
                            if (rule && rule.id) {
                                $.get("/rules/archive/" + rule.id, function (data) {
                                    $modalInstance.dismiss('cancel');
                                });
                            }
                        }
                        $scope.cancel = function () {
                            $modalInstance.dismiss('cancel');
                        };
                    },
                    resolve: {
                        rule: function () {
                            return $scope.rule;
                        }
                    }
                });//end of modal.open
            }
            $scope.archiveProcess = function () {
                return $modal.open({
                    templateUrl: 'archiveProcessModalContent.html', // loads the template
                    backdrop: true, // setting backdrop allows us to close the modal window on clicking outside the modal window
                    windowClass: 'modal', // windowClass - additional CSS class(es) to be added to a modal window template
                    controller: function ($scope, $modalInstance, process) {
                        $scope.process = process;
                        $scope.archive = function () {
                            if (process && process.id) {
                                $.get("/processes/delete/" + process.id, function (data) {
                                    $modalInstance.dismiss('cancel');
                                });
                            }
                        }
                        $scope.cancel = function () {
                            $modalInstance.dismiss('cancel');
                        };
                    },
                    resolve: {
                        process: function () {
                            return $scope.process;
                        }
                    }
                });//end of modal.open
            }
            $scope.open = function () {
                return $modal.open({
                    templateUrl: 'modalContent.html', // loads the template
                    backdrop: true, // setting backdrop allows us to close the modal window on clicking outside the modal window
                    windowClass: 'modal', // windowClass - additional CSS class(es) to be added to a modal window template
                    controller: function ($scope, $modalInstance, source) {
                        $scope.source = source;
                        $scope.create = function () {
                            $.post(
                                '/sources/createsource/',
                                source,
                                function (data) {
                                    $modalInstance.dismiss('cancel');
                                });
                            //$modalInstance.dismiss('cancel'); // dismiss(reason) - a method that can be used to dismiss a modal, passing a reason
                        }
                        $scope.cancel = function () {
                            $modalInstance.dismiss('cancel');
                        };
                    },
                    resolve: {
                        source: function () {
                            return $scope.source;
                        }
                    }
                });//end of modal.open
            }; // end of scope.open function
        });