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

    $scope.replaceUrl = function(url){
        return url;
    };

    var vm = $scope;
    var projectid = $routeParams.id;
    var tag = $routeParams.tag;

    vm.project_activities_page = rms.get('project_activities_page') || 0;
    vm.project_activities = rms.get("project_activities") || [];
    vm.project_has_more = rms.get("project_has_more");

    vm.project = rms.get("project") || null;


//    vm.project_members_page = rms.get('project_members_page') || 0;
    vm.project_master_members = rms.get("project_master_members") || [];
    vm.project_develop_members = rms.get("project_develop_members") || [];
    vm.project_report_members = rms.get("project_report_members") || [];

//    vm.review_master_members = rms.get("review_master_members") || [];
//    vm.review_develop_members = rms.get("review_master_members") || [];

    vm.review_master_members = rms.get("review_master_members") || [];
    vm.review_develop_members = rms.get("review_develop_members") || [];

//    vm.project_members_has_more = rms.get("project_members_has_more");

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

    //跳入邀请界面
    vm.invite = function(){
//        vm.alertService.showMessage('提示', "该功能将在近日开放，敬请期待！");

        rms.save('project_activities_page',vm.project_activities_page);
        rms.save("project_activities",vm.project_activities);
        rms.save("project_has_more",vm.project_has_more);
        rms.save("project",vm.project);
        rms.save("project_master_members",vm.project_master_members);
        rms.save("project_develop_members",vm.project_develop_members);
        rms.save("project_report_members",vm.project_report_members);
        rms.save("review_master_members",vm.review_master_members);
        rms.save("review_develop_members",vm.review_develop_members);

        $location.path("/project_invite_code").search({id: projectid});
    };

    //获取项目动态
    vm.getActivities = function(page){
        $http({
            method: 'POST',
            url: apiUrl + "projects/activities?id=" + projectid,
            data:{token:auth.token(),page:page}
        }).then(function successCallback(response) {
            console.log(response.data);
            if(response.data.status == 0){
                vm.project_activities_page = response.data.page;
                if(response.data.page > 0)
                {
                    vm.project_activities = vm.project_activities.concat(response.data.data);
                    vm.project_has_more = (response.data.count + response.data.page * 10) < response.data.all_count;
                }
                else{
                    vm.project_activities = response.data.data;
                    vm.project_activities_page = 0;
                    vm.project_has_more = (response.data.count + response.data.page * 10) < response.data.all_count;
                }
            }
            else{
                vm.alertService.showMessage('提示', response.data.message);
            }


        }, function errorCallback(response) {
        });
    }

    vm.getMembers = function(page){
        $http({
            method: 'POST',
            url: apiUrl + "projects/members?id=" + projectid,
            data:{token:auth.token(),page:page}
        }).then(function successCallback(response) {
            console.log(response.data);
            if(response.data.status == 0){
//                vm.project_members_page = response.data.page;
//                if(response.data.page > 0)
//                {
//                    vm.project_members = vm.project_members.concat(response.data.data);
//                }
//                else{
//                    vm.project_members = response.data.data;
//                    vm.project_members_page = 0;
//                    vm.project_members_has_more = (response.data.count + response.data.page * 10) < response.data.all_count;
//                }
                vm.project_master_members = response.data.master_members;
                vm.project_develop_members = response.data.develop_members;
                vm.project_report_members = response.data.report_members;

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


        }, function errorCallback(response) {
        });
    }

    //跳到详情页
    vm.goDetail = function(type, act_id,id){
        rms.save("yoffset", document.documentElement.scrollTop || document.body.scrollTop);
        rms.save('project_activities_page',vm.project_activities_page);
        rms.save("project_activities",vm.project_activities);
        rms.save('project_has_more', vm.project_has_more);
        rms.save("project",vm.project);
//        $location.path('/'+type+'/'+act_id);
        $location.path("/"+type).search({id: act_id});
    };

    //切换 按钮
    vm.tab = function(index){
        vm.currentTab = index;
        vm.searchText = '';

        if(index == 1 && vm.project_activities.length <= 0 ){
            vm.getActivities(0);
        }
        else if(index == 2 && vm.project_master_members.length <= 0){
            vm.getMembers(0);
        }

        rms.save("tab_num",vm.currentTab);
    };

    //初始化TAB按钮
    var resetMenu = function(can_setting){
        if(can_setting){
            vm.menus = ["项目动态", "成员管理"];
        } else {
            vm.menus = ['项目动态', "我的伙伴"];
        }
    }
    if(vm.project){
        resetMenu(vm.project.can_setting);
    }

    vm.currentTab = rms.get('tab_num');

    //
    if(!vm.currentTab || !vm.project){
        $http.get(config.apiUrl+ 'projects/'+projectid+"?token="+auth.token()).then(
            function(response) {
                console.log(response.data);

                if (response.data.status == 0){
                    vm.project = response.data.data;
                    resetMenu(vm.project.can_setting);
                    if(tag){
                        vm.tab(2);
                        tag = null;
                        vm.currentTab = 2;
                    }
                    else{
                        vm.tab(1);
                        vm.currentTab = 1;
                    }
                }
                else{
                    vm.alertService.showMessage('提示', response.data.message,function(){
                        window.history.back();
                    });

                }
            }
        );
    } else {
        $timeout(function(){
            window.scrollTo(0, rms.get("yoffset"));
        });
    }

    vm.addPraise = function(act){
        for(var i in vm.project_activities){
            if(vm.project_activities[i].act_id == act.act_id){
                vm.project_activities[i].praise_count += 1;
                vm.project_activities[i].has_praise = true;
                break;
            }
        }

        common.addCommonPraise(act);
    };

    vm.decreasePraise = function(act){
        for(var i in vm.project_activities){
            if(vm.project_activities[i].act_id == act.act_id){
                vm.project_activities[i].praise_count -= 1;
                vm.project_activities[i].has_praise = false;
                break;
            }
        }

        common.decreaseCommonPraise(act);
    };

    vm.onSetting = function(data){
        rms.save('current_edit_member', data);

        rms.save('project_activities_page',vm.project_activities_page);
        rms.save("project_activities",vm.project_activities);
        rms.save("project_has_more",vm.project_has_more);

        rms.save("project",vm.project);
        rms.save("project_master_members",vm.project_master_members);
        rms.save("project_develop_members",vm.project_develop_members);
        rms.save("project_report_members",vm.project_report_members);
        rms.save("review_master_members",vm.review_master_members);
        rms.save("review_develop_members",vm.review_develop_members);

        $location.path("/edit_project_member").search({id: projectid,user_id: data.user.id});
    };

    vm.review = function(user){
        rms.save('current_review_member', user);
        rms.save('current_project', vm.project);
        rms.save("project",vm.project);
        rms.save("project_master_members",vm.project_master_members);
        rms.save("project_develop_members",vm.project_develop_members);
        rms.save("project_report_members",vm.project_report_members);
        rms.save("review_master_members",vm.review_master_members);
        rms.save("review_develop_members",vm.review_develop_members);

        $location.path("/review_project_member").search({id: projectid,user_id: user.id});
    }

    vm.goPublishNote = function(){
        rms.save('project_activities_page',vm.project_activities_page);
        rms.save("project_activities",vm.project_activities);
        rms.save("project_has_more",vm.project_has_more);

        rms.save("project",vm.project);
        rms.save("project_master_members",vm.project_master_members);
        rms.save("project_develop_members",vm.project_develop_members);
        rms.save("project_report_members",vm.project_report_members);
        rms.save("review_master_members",vm.review_master_members);
        rms.save("review_develop_members",vm.review_develop_members);
        rms.save('current_project', vm.project);
        $location.path("/project_publishnote").search({id:projectid});
    };

    vm.quit = function(){
        vm.alertService_2.showMessage('提示', "是否确认退出项目",function(){
            $http({
                method: 'POST',
                url: apiUrl + "projects/" + projectid+"/quit",
                data:{token:auth.token()}
            }).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);
                        rms.save('projects',[]);
                        $location.path("/project_list");
                    });
                }
                else{
                    vm.alertService.showMessage('提示', response.data.message);
                }
            });
        });

    };

}]);