

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

    var vm = $scope;

    vm.alertService = alertService.create();

    vm.syllabus = {
        courses: [{name: ''}],
    };

    vm.addClass = function(){
        vm.syllabus.courses.push({});
    };

    vm.deleteClass = function(index){
        vm.syllabus.courses.splice(index, 1);
    }

    vm.newClass = function (frm, syllabus) {
        frm.$setSubmitted();
        console.log(syllabus);

        if(!frm.$valid){
            console.log(frm.$error);
            return;
        }

        var courses = [];
        for(var i in vm.syllabus.courses){
            courses .push(vm.syllabus.courses[i].name);
        }

        $http.post(config.apiUrl+"syllabuses", {
            token: auth.token(),
            title: syllabus.title,
            courses: courses
        }).then(function(response){
            if(response.data.status!=0){
                vm.alertService.showMessage('出错了', response.data.message);
            } else {
                vm.alertService.showMessage('提示', '新建课程成功', function(){
//                    window.history.back();
                    rms.save('syllabuses',[]);
                    $location.path("/class_list");
                });
            }
           console.log(response.data.data);
        });

    }

}] );