

app.controller('JoinProjectController', ['$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.role_id = 0;
    vm.alertService = alertService.create();
    vm.invite_code = "";
    var tag = $routeParams.tag;

    vm.cancel = function(){
        if(tag){
//            window.history.back();
            rms.save('projects',[]);
            $location.path("/project_list");
        }
        else{
            wx.closeWindow();
        }
    };

    vm.join_project = function(){
        if(vm.role_id == 0){
            vm.alertService.showMessage('提示', "请至少选择一个角色");
            return;
        }

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


        $http.post(config.apiUrl+'projects/join_project', {
            token: auth.token(),
            invite_code: vm.invite_code,
            role_id:vm.role_id
        }).then(function(response){
            if(response.data.status == 0){
                vm.alertService.showMessage('提示', response.data.message,function(){
                    rms.save('projects',[]);
                    $location.path("/project_list");
                });
            } else {
                vm.alertService.showMessage('提示', response.data.message);
            }
        });
    };

    vm.selectRole = function(role_id){
        vm.role_id = role_id;
    }

}] );