

app.controller('EditClassMemberController', ['$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_edit_member = rms.get('current_edit_member');
    vm.current_edit_member = null;

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

    var course_id = $routeParams.id;
    var user_id = $routeParams.user_id;

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

    vm.tmpteacher = false;         //教师
    vm.tmpassistant = false;       //教辅
    vm.tmpstudent = false;         //学生

    vm.buttongrey = true;
    vm.selectid = 0;

    vm.show_1 = true;
    vm.show_2 = false;
    vm.show_3 = false;


    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;
                user_id = response.data.user_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] == 9){
                        vm.teacher = true;
                        vm.tmpteacher = true;
                    }
                    else if(vm.current_roles_id[i] == 7){
                        vm.assistant = true;
                        vm.tmpassistant = true;
                    }
                    else if(vm.current_roles_id[i] == 10){
                        vm.student = true;
                        vm.tmpstudent = true;
                    }
                }

                $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(){
//        vm.alertService.showMessage('提示', '您确定不对角色进行变更吗？', function(){
//            window.history.back();
//        });
        window.history.back();
    };

    vm.deleteMember = function(user_id){
        vm.alertService_2.showMessage('提示', "是否确认删除成员",function(){
            $http({
                method: 'POST',
                url: apiUrl + "courses/" + course_id+"/deletemember",
                data:{token:auth.token(),user_id:user_id}
            }).then(function successCallback(response) {
                console.log(response.data);

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

            });
        });
    };

    vm.edit_member_role = function(){
        if(vm.buttongrey == true){
            return;
        }

        if(vm.show_2){
            //删除成员
            vm.deleteMember(vm.current_edit_member.user.id);
        }
        else if(vm.show_3){
            //加入分班
            $http.post(config.apiUrl+'courses/user_edit_coursegroup', {
                token: auth.token(),
                id: course_id,
                course_group_id:vm.selectid,
                user_id:vm.current_edit_member.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);
                }
            });

        }
        else{
            if((vm.teacher == vm.tmpteacher)&& (vm.assistant == vm.tmpassistant)&&(vm.student == vm.tmpstudent)){
                vm.alertService.showMessage('提示', "该用户当前已是该角色");
                return;
            }

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

            $http.post(config.apiUrl+'courses/edit_member_role', {
                token: auth.token(),
                id: course_id,
                user_id:vm.current_edit_member.user.id,
                teacher_flag:vm.teacher,
                assistant_flag:vm.assistant,
                student_flag:vm.student
            }).then(function(response){
                if(response.data.status!=0){
                    vm.alertService.showMessage('提示', response.data.message);
                } else {
                    vm.alertService.showMessage('提示', '修改角色成功', function(){
//                    window.history.back();
                        $location.path("/class").search({id: course_id,tag:1});
                    });
                }
            });
        }
    };

    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;
        }

        if((vm.teacher == vm.tmpteacher)&& (vm.assistant == vm.tmpassistant)&&(vm.student == vm.tmpstudent)){
            vm.buttongrey = true;
        }
        else{
            vm.buttongrey = false;
        }

        if((vm.teacher == false)&& (vm.assistant == false)&&(vm.student == false)){
            vm.buttongrey = true;
        }

    };

    vm.clickDeleteMember = function(){
        vm.show_2 = !vm.show_2;
        if(vm.show_2){
            vm.show_1 = false;
            vm.show_3 = false;
            vm.buttongrey = false;
        }
    };

    vm.clickChangeRole = function(){
        vm.show_1 = !vm.show_1;

        if(vm.show_1){
            vm.show_2 = false;
            vm.show_3 = false;
            if((vm.teacher == vm.tmpteacher)&& (vm.assistant == vm.tmpassistant)&&(vm.student == vm.tmpstudent)){
                vm.buttongrey = true;
            }
            else{
                vm.buttongrey = false;
            }
        }
    };

    vm.clickSelectGroup = function(){
        vm.show_3 = !vm.show_3;

        if(vm.show_3){
            vm.show_1 = false;
            vm.show_2 = false;
            if(vm.selectid != vm.course_group_id){
                vm.buttongrey = false;
            }
            else{
                vm.buttongrey = true;
            }
        }
    };

    vm.selectGroup = function(id){
        vm.selectid = id;
        if(vm.course_group_id != vm.selectid){
            vm.buttongrey = false;
        }
        else{
            vm.buttongrey = true;
        }
    };

}] );