

app.controller('SelectMyCourseGroupController', ['$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_review_member =  rms.get('current_review_member');

    vm.alertService = alertService.create();

    vm.tip_1 = "";
    vm.tip_2 = "";

    var course_id = $routeParams.id;
    var user_id = $routeParams.user_id;
    vm.selectid = null;
    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 = "该班级不存在或已被删除";
                }

            }
        );
    }

    if(!vm.current_edit_member){
        $http.post(config.apiUrl+'courses/get_member_info', {
            token: auth.token(),
            id: course_id,
            user_id:user_id
        }).then(function(response){
            if(response.data.status!=0){
                vm.alertService.showMessage('提示', response.data.message,function(){
                    $location.path("/class").search({id: course_id,tag:1});
                });
            } else {
                console.log(response);
                course_id = response.data.course_id;
                vm.current_edit_member = response.data.member_info;
                vm.current_roles_id = vm.current_edit_member.roles_id;
                vm.selectid = response.data.course_group_id;
                vm.course_group_id = vm.selectid;

                for(var i in vm.current_roles_id){
                    if(vm.current_roles_id[i] == 10){
                        vm.student = true;
                    }
                }

                if(!vm.student){
                    vm.tip_2 = "您不是该班级的学生!";
                }
                else{
                    $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 = [];
                            }
                    });
                }
            }
        });
    }

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

    vm.confirm = function(){
        if(vm.course_group_id == vm.selectid){
            $location.path("/class").search({id: course_id,tag:1});
            return;
        }

        //加入分班
        $http.post(config.apiUrl+'courses/user_edit_coursegroup', {
            token: auth.token(),
            id: course_id,
            course_group_id:vm.selectid,
            user_id:user_id
        }).then(function(response){
            console.log(response);
            if(response.data.status == 0){
                vm.alertService.showMessage('提示', "修改分班成功!",function(){
                    $location.path("/class").search({id: course_id,tag:1});

                });
            } else {
                vm.alertService.showMessage('提示', response.data.message,function(){
                    $location.path("/class").search({id: course_id,tag:1});
                });
            }
        });
    };

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

}] );