

app.controller('EditClassGroupController', ['$scope', '$http', 'auth', 'config', 'alertService','$location','$routeParams','rms','common', function($scope, $http, auth, config, alertService, $location,$routeParams, rms,common){
    var vm = $scope;
    var courseid = $routeParams.id;
    vm.alertService = alertService.create();
    vm.alertService_2 = alertService.create();

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

    $http.get(config.apiUrl + 'courses/course_groups/'+courseid+'?token='+auth.token()).then(
        function(response) {
            console.log("groups=");
            console.log(response);
            if(response.data.status == 0) {
                vm.groups = response.data.data;
                for(var i in vm.groups){
                    vm.groups[i].tmpname = vm.groups[i].course_group.name;
                }
            }
            else{
                vm.groups = [];
            }
        });

    vm.addGroup = function(){
        vm.groups.push({tmpname:""});
    };

    vm.deleteGroup = function(index){
        var group = vm.groups[index];
        if(group.course_group){
            vm.alertService_2.showMessage('提示', '您确定要删除该分班吗？', function() {
                $http.post(config.apiUrl+'courses/delete_coursegroup', {
                    token: auth.token(),
                    id: courseid,
                    course_group_id:group.course_group.id
                }).then(function(response){
                    console.log(response);
                    if(response.data.status == 0){
                        vm.alertService.showMessage('提示', "删除成功!",function(){
                            vm.groups.splice(index, 1);
                        });
                    } else {
                        vm.alertService.showMessage('提示', response.data.message);
                    }
                });
            });

        } else {
            vm.groups.splice(index, 1);
        }

    };

    vm.newGroup = function (frm, groups) {
        frm.$setSubmitted();
        console.log(groups);

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

        if(vm.groups.length == 0){
            $location.path("/class").search({id: courseid,tag:1});
            return;
        }
        //不能有相同名称的分班
        for(var i=0;i< vm.groups.length-1;i++) {
            for (var j=i+1;j<vm.groups.length;j++){
                    if(vm.groups[i].tmpname == vm.groups[j].tmpname){
                        vm.alertService.showMessage('提示', "分班名称不能重复!");
                        return;
                    }
            }
        };

        var add_groups = [];
        var modify_groups = [];

        for(var i in vm.groups){
            var group = vm.groups[i];
            if(!group.course_group){
                add_groups.push(group.tmpname);
            }
            else if(group.tmpname != group.course_group.name){
                modify_groups.push({id:group.course_group.id,name:group.tmpname});
            }
        }

        if(add_groups.length == 0 && modify_groups.length == 0){
            vm.alertService.showMessage('提示', "保存分班成功!",function(){
                $location.path("/class").search({id: courseid,tag:1});
                return;
            });
        }

        //编辑分班
        $http.post(config.apiUrl+'courses/edit_coursegroup', {
            token: auth.token(),
            id: courseid,
            add_groups: add_groups,
            modify_groups: modify_groups
        }).then(function(response){
            console.log(response);
            if(response.data.status == 0){
                vm.alertService.showMessage('提示', "保存分班成功!",function(){
                    $location.path("/class").search({id: courseid,tag:1});
                });
            } else {
                vm.alertService.showMessage('提示', response.data.message,function(){
                    $location.path("/class").search({id: courseid,tag:1});
                });
            }
        });
    }

}] );