

app.controller('EditClassController', ['$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.syllabus = rms.get('current_edit_syllobus');

    var syllabus_id = $routeParams.id;
    if(!vm.syllabus){
        $http.get(config.apiUrl+"syllabuses/"+syllabus_id+"?token="+auth.token()).then(function(response){
            console.log(response.data);
            vm.syllabus = response.data.data;
            if (vm.syllabus) {
                for (var i in vm.syllabus.courses) {
                    var course = vm.syllabus.courses[i];
                    course.tmpname = course.name;
                }
                vm.syllabus.tmptitle = vm.syllabus.title;
            }
        });
    }
    console.log(vm.syllabus);

    //名字用两份
    if (vm.syllabus) {
        for (var i in vm.syllabus.courses) {
            var course = vm.syllabus.courses[i];
            course.tmpname = course.name;
        }
        vm.syllabus.tmptitle = vm.syllabus.title;
    }
    //单弹框
    vm.alertService_1 = alertService.create();

    //双弹框
    vm.alertService_2 = alertService.create();

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

    vm.deleteClass = function(index){
        var course = vm.syllabus.courses[index];
        if(course.id >0){
            vm.alertService_2.showMessage('提示', '您确定要删除该班级吗？', function() {
                $http.post(config.apiUrl+'courses/'+course.id+'/del', {
                    token: auth.token()
                }).then(function(response){
                    if(response.data.status!=0){
                        vm.alertService_1.showMessage('提示', response.data.message);
                    } else {
                        vm.alertService_1.showMessage('提示', '删除班级成功', function(){
                            vm.syllabus.courses.splice(index, 1);
                        });
                    }
                    console.log(response.data.data);
                });
            });

        } else {
            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 add_courses = [];
        var modify_courses = [];

        for(var i in vm.syllabus.courses){
            var course = vm.syllabus.courses[i];
            if(!course.id){
                add_courses.push(course.tmpname);
            }
            else if(course.tmpname != course.name){
                modify_courses.push({id:course.id,name:course.tmpname});
            }
        }

        $http.post(config.apiUrl+'syllabuses/'+syllabus_id+'/edit', {
            token: auth.token(),
            id: syllabus_id,
            title:vm.syllabus.tmptitle,
            add_courses: add_courses,
            modify_courses: modify_courses
        }).then(function(response){
            if(response.data.status!=0){
                vm.alertService_1.showMessage('出错了', response.data.message);
            } else {
                vm.alertService_1.showMessage('提示', '保存课程成功', function(){
//                    window.history.back();
                    rms.save('syllabuses',[]);
                    $location.path("/class_list");

                });
            }
           console.log(response.data.data);
        });

    }

}] );