app.controller('ClassPublishNoticeController', ['$scope', '$http', 'auth', 'config', 'alertService','$location','$routeParams','rms','common', function($scope, $http, auth, config, alertService, $location,$routeParams, rms,common){
//    common.checkLogin();

    var vm = $scope;
    vm.current_course = rms.get('current_course');

    var course_id = $routeParams.id;

    vm.alertService = alertService.create();

    vm.noticetitle = "";
    vm.notice = "";

    if(!vm.current_course){
        $http.get(config.apiUrl+ 'courses/'+course_id+"?token="+auth.token()).then(
            function(response) {
                console.log(response.data);
                if (response.data.status == 0){
                    vm.current_course = response.data.data;
                    console.log("courses");
                    console.log(response.data.data);
                }
                else{
                    vm.alertService.showMessage('提示', response.data.message);
                }
                if(!vm.current_course){
                    vm.tip_1 = "该班级不存在或已被删除";
                }

            }
        );
    }
    vm.cancel = function(){
        window.history.back();
    };

    //发布通知 只有老师能发布
    vm.publishNotice = function(){
        if(vm.noticetitle.length == 0)
        {
            vm.alertService.showMessage('提示', '标题不能为空');
            return;
        }

        if(vm.notice.length == 0)
        {
            vm.alertService.showMessage('提示', '内容不能为空');
            return;
        }

        var text = vm.notice.replace(/\n/g,'<br/>');

        $http.post(config.apiUrl + "courses/"+course_id+"/publishnotice",
            {token: auth.token(),title: vm.noticetitle, text: text}
        ).then(function(response){
                if(response.data.status == 0)
                {
                    vm.alertService.showMessage('提示', '发布成功',function(){
                        rms.save('course_activities_page',0);
                        rms.save("course_activities",[]);
                        rms.save("course_has_more",false);
                        $location.path("/class").search({id: course_id});
                    });
                }
                else{
                    vm.alertService.showMessage('提示', response.data.message);
                }
            });

    };

}] );