

app.controller('JoinClassGroupController', ['$scope', '$http', 'auth', 'config', 'alertService','$location','$routeParams','rms','wx','common', function($scope, $http, auth, config, alertService, $location,$routeParams, rms,wx,common){
    var vm = $scope;

    var course_id = $routeParams.id;
    vm.alertService = alertService.create();
    vm.selectid = null;

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

    vm.selectGroup = function(id){
        vm.selectid = id;
    };


    vm.cancel = function(){
        rms.save('syllabuses',[]);
        $location.path("/class_list");
    };

    vm.confirm = function(){
        if(vm.selectid == null){
            vm.alertService.showMessage('提示', "请选择分班!");
            return;
        }

        if(vm.selectid == 0){
            rms.save('syllabuses',[]);
            $location.path("/class_list");
            return;
        }

        //加入分班
        $http.post(config.apiUrl+'courses/user_join_coursegroup', {
            token: auth.token(),
            id: course_id,
            course_group_id:vm.selectid
        }).then(function(response){
            console.log(response);
            if(response.data.status == 0){
                vm.alertService.showMessage('提示', "选择分班成功!",function(){
                    rms.save('syllabuses',[]);
                    $location.path("/class_list");
                });
            } else {
                vm.alertService.showMessage('提示', response.data.message,function(){
                    rms.save('syllabuses',[]);
                    $location.path("/class_list");
                });
            }
        });
    };

}] );