'use strict';
angular.module('qrzk')

    .controller('CorrigendumListCtrl', function ($cookieStore, $scope, $http, appConst, $state, toaster, $modal) {
        $http.get('corrigendum/queryAllBatch').success(function (data) {
            $scope.batchs = data;
        }).error(function (data, code) {
            alert(code);
        });
        $http.get('course/queryAllCourses').success(function (data) {
            $scope.courses = data;
        }).error(function (data, code) {
            alert(code);
        });
        $scope.pageModel = {};
        $scope.pageModel.pageSize = 20;
        $scope.search = {};

        $scope.load = function () {
            _goPage(1);
        };
        $scope.load();

        $scope.goPage = function (pageNum) {
            _goPage(pageNum);
        }

        function _goPage(pageNum) {
            if (pageNum < 1 || (pageNum > $scope.pageModel.totalPage && $scope.pageModel.totalPage > 0)) {
                return;
            }

            $scope.pageModel.curPage = pageNum;

            var url = 'corrigendum/list';
            var params = {
                'page': pageNum
                , 'rows': $scope.pageModel.pageSize
                , 'keyword': $scope.search.keyword
                , 'batch': $scope.search.batch
            };
            $http.post(url, params).success(function (data) {
                $scope.pageModel.array = data.rows;
                $scope.pageModel.total = data.total;
                $scope.pageModel.totalPage = Math.ceil(data.total / $scope.pageModel.pageSize);
            })
                .error(function (data, code) {
                    alert(code);
                });
        }

        $scope.delete = function (id) {
            if (confirm('确认要删除吗？')) {
                $http.post('corrigendum/delete/' + id)
                    .success(function (data) {
                        toaster.pop('success', '删除成功!');
                        $scope.load();
                    }).error(function (data, code) {
                    alert(code);
                });
            }

        }

        var editModal = $modal({
            title: '编辑课程',
            scope: $scope,
            templateUrl: 'resources/app/corrigendum/add.html',
            show: false
        });
        $scope.edit = function (id) {
            //$state.go('course.edit', {'id':id});
            $scope.selectedCourseId = id;
            $scope.editModal = editModal;
            editModal.$promise.then(editModal.show);
        }
        var detailModal = $modal({
            title: '编辑课程',
            scope: $scope,
            templateUrl: 'resources/app/corrigendum/detail.html',
            show: false
        });
        $scope.detail = function (id) {
            //$state.go('course.edit', {'id':id});
            $scope.selectedCourseId = id;
            $scope.detailModal = detailModal;
            detailModal.$promise.then(detailModal.show);
        }
        $scope.$on('corrigendumModified', function (event, corrigendum) {
            $scope.load();
        });

    })

    .controller('CorrigendumAddCtrl', function ($scope, $http, $state, $stateParams, toaster, $rootScope) {

        setTimeout(() => {

            $('#select1').selectpicker({
                noneSelectedText: '-请选择-',
                noneResultsText: '没有查询到内容..'
            });

            CKEDITOR.replace("editor1")
            CKEDITOR.instances.editor1.setData($scope.model.content)
        }, 100);

        $scope.load = function () {

            if ($scope.selectedCourseId) {
                $stateParams.id = $scope.selectedCourseId;
                var url = 'corrigendum/get/' + $stateParams.id;
                $http.get(url).success(function (data) {
                    $scope.model = data;
                }).error(function (data, code) {
                    alert(code);
                });
            } else {
                $scope.model = {};
            }

        };
        $scope.load();

        $scope.submit = function () {
            if ($scope.form.$invalid) {
                toaster.pop('error', "校验失败，请检查表单中红框部分的内容！");
                $scope.submitted = true;
                return;
            }
            $scope.model.content = CKEDITOR.instances.editor1.getData();
            $scope.myPromise = $http.post('corrigendum/save', {
                id: $scope.model.id,
                batch: $scope.model.batch,
                course_id: $scope.model.course_id,
                content: $scope.model.content,
                num: $scope.model.num
            })
                .success(function (data) {
                    if (data.flag == 'failed') {
                        toaster.pop('error', data.msg);
                    } else {
                        toaster.pop('success', '操作成功！');
                        $rootScope.$broadcast('corrigendumModified', data.corrigendum);
                        $scope.editModal.hide();

                    }
                })
                .error(function (data, code) {
                    alert(code);
                });
        };

        $scope.closeModel = function () {
           if($scope.editModal) $scope.editModal.hide();
            if($scope.detailModal) $scope.detailModal.hide();
        }


    })

