

app.controller('EditProjectMemberController', ['$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 project_id = $routeParams.id;
    var user_id = $routeParams.user_id;

    vm.buttongrey = true;

    if(!vm.current_edit_member){
        $http.post(config.apiUrl+'projects/get_member_info', {
            token: auth.token(),
            id: project_id,
            user_id:user_id
        }).then(function(response){
            if(response.data.status!=0){
                vm.alertService.showMessage('提示', response.data.message,function(){
                    rms.save('project_activities_page',0);
                    rms.save("project_activities",[]);
                    rms.save("project_has_more",false);
                    rms.save("project",null);
                    rms.save("project_master_members",[]);
                    rms.save("project_develop_members",[]);
                    rms.save("project_report_members",[]);
                    rms.save("review_master_members",[]);
                    rms.save("review_develop_members",[]);
                    rms.save('tab_num',null);
//                    window.history.back();
                    $location.path("/project").search({id: project_id,tag:1});
                });
            } else {
                project_id = response.data.project_id;
                user_id = response.data.user_id;
                vm.current_edit_member = response.data.member_info;
                vm.current_edit_member.roles_id = vm.current_edit_member.roles_id[0];
                vm.current_roles_id = vm.current_edit_member.roles_id;
            }
        });
    }

    console.log(vm.current_edit_member);

    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 + "projects/" + project_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(){
                        //确定
                        rms.save('project_activities_page',0);
                        rms.save("project_activities",[]);
                        rms.save("project_has_more",false);
                        rms.save("project",null);
                        rms.save("project_master_members",[]);
                        rms.save("project_develop_members",[]);
                        rms.save("project_report_members",[]);
                        rms.save("review_master_members",[]);
                        rms.save("review_develop_members",[]);
                        rms.save('tab_num',null);
//                    window.history.back();
                        $location.path("/project").search({id: project_id,tag:1});

                    });
                }
                else{
                    vm.alertService.showMessage('提示', response.data.message);
                }

            });

        });
    };

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

        if(vm.current_edit_member.show){
            //删除成员
            vm.deleteMember(vm.current_edit_member.user.id);
        }
        else{
            if(vm.current_roles_id == vm.current_edit_member.roles_id){
                vm.alertService.showMessage('提示', "该用户当前已是该角色");
                return;
            }

            $http.post(config.apiUrl+'projects/edit_member_role', {
                token: auth.token(),
                id: project_id,
                user_id:vm.current_edit_member.user.id,
                role_id:vm.current_edit_member.roles_id
            }).then(function(response){
                if(response.data.status!=0){
                    vm.alertService.showMessage('提示', response.data.message);
                } else {
                    vm.alertService.showMessage('提示', '修改角色成功', function(){
                        rms.save('project_activities_page',0);
                        rms.save("project_activities",[]);
                        rms.save("project_has_more",false);
                        rms.save("project",null);
                        rms.save("project_master_members",[]);
                        rms.save("project_develop_members",[]);
                        rms.save("project_report_members",[]);
                        rms.save("review_master_members",[]);
                        rms.save("review_develop_members",[]);
                        rms.save('tab_num',null);
//                    window.history.back();
                        $location.path("/project").search({id: project_id,tag:1});
                    });
                }
            });
        }
    };

    vm.selectRole = function(role_id){
        vm.current_edit_member.roles_id = role_id;

        if(vm.current_roles_id == vm.current_edit_member.roles_id){
            vm.buttongrey = true;
        }
        else{
            vm.buttongrey = false;
        }
    };

    vm.clickChangeRole = function(){
        vm.current_edit_member.show = !vm.current_edit_member.show;
        if(vm.current_roles_id == vm.current_edit_member.roles_id){
            vm.buttongrey = true;
        }
        else{
            vm.buttongrey = false;
        }
    };

}] );