

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

    var vm = $scope;

//    vm.current_edit_member = rms.get('current_edit_member');
    vm.current_edit_member = null;
    var tag = $routeParams.tag;

    vm.alertService = alertService.create();

    vm.invite_code = "";
    vm.teacher = false;         //教师
    vm.assistant = false;       //教辅
    vm.student = false;         //学生.

    vm.cancel = function(){
//        vm.alertService.showMessage('提示', '您确定不对角色进行变更吗？', function(){
//            window.history.back();
//        });
        if(tag){
            rms.save('syllabuses',[]);
            $location.path("/class_list");
//            window.history.back();
        }
        else{
            wx.closeWindow();
        }

    };

    vm.joinClass = function(){
        if((vm.teacher == false)&& (vm.assistant == false)&&(vm.student == false)){
            vm.alertService.showMessage('提示', "请至少选择一个角色");
            return;
        }

        if(vm.invite_code.length == 0)
        {
            vm.alertService.showMessage('提示', '请输入5位邀请码');
            return;
        }
        if(vm.invite_code.length != 5)
        {
            vm.alertService.showMessage('提示', '邀请码格式不正确');
            return;
        }

        $http.post(config.apiUrl+'courses/join_class', {
            token: auth.token(),
            invite_code: vm.invite_code,
            teacher_flag:vm.teacher,
            assistant_flag:vm.assistant,
            student_flag:vm.student
        }).then(function(response){
            console.log(response);
            if(response.data.status == 0){
                vm.alertService.showMessage('提示', response.data.message,function(){
                    rms.save('syllabuses',[]);
                    if(response.data.go_coursegroup_flag == 0){
                        $location.path("/class_list");
                    }
                    else{
                        $location.path("/join_classgroup").search({id: response.data.course_id});
                    }
                });
            } else {
                vm.alertService.showMessage('提示', response.data.message,function(){
                    if(response.data.go_coursegroup_flag == 1) {
                        $location.path("/join_classgroup").search({id: response.data.course_id});
                    }
                });
            }
        });
    };

    vm.selectRole = function(role_id){
        if (role_id == 7){
            if(!vm.teacher){
                vm.assistant = !vm.assistant;
            }
        }
        else if (role_id == 9){
            if(!vm.assistant){
                vm.teacher = !vm.teacher;
            }
        }
        else if (role_id == 10){
            vm.student = !vm.student;
        }
    }

}] );