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

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

    var vm = $scope;

    vm.g_localhost = window.g_localhost;

    var courseid = $routeParams.id;
    var tag = $routeParams.tag;

    vm.course_activities_page = rms.get('course_activities_page') || 0;
    vm.course_activities = rms.get("course_activities") || [];
    vm.course_has_more = rms.get("course_has_more");

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

    vm.getClassActivities = function(page){
        $http({
            method: 'POST',
            url: apiUrl + "courses/activities?id=" + courseid,
            data:{token:auth.token(),page:page}
        }).then(function successCallback(response) {
            console.log(response.data);
            if(response.data.status == 0){
                vm.course_activities_page = response.data.page;
                if(response.data.page > 0)
                {
                    vm.course_activities = vm.course_activities.concat(response.data.data);
                    vm.course_has_more = (response.data.count + response.data.page * 10) < response.data.all_count;
                }
                else{
                    vm.course_activities = response.data.data;
                    vm.course_activities_page = 0;
                    vm.course_has_more = (response.data.count + response.data.page * 10) < response.data.all_count;
                }
            }
            else{
                vm.alertService.showMessage('提示', response.data.message);
            }


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

    var getUsers = function(){
        if(vm.teachers.length<=0){
            $http.get(config.apiUrl + 'courses/teachers?token='+auth.token()+'&course_id='+courseid).then(
                function(response) {
                    console.log(response.data);
                    vm.teachers = response.data.data;
                }
            )
        }

        if(vm.course.is_creator){
            if(vm.reviewers.length<=0){
                $http.get(config.apiUrl + 'courses/reviewers?token='+auth.token()+'&course_id='+courseid).then(
                    function(response) {
                        console.log(response.data);
                        vm.reviewers = response.data.data;
                    }
                )
            }
        }

        if(vm.students.length<=0){
            $http.get(config.apiUrl + 'courses/students?token='+auth.token()+'&course_id='+courseid).then(
                function(response) {
                    console.log(response.data);
                    vm.students = response.data.data;
                }
            )
        }
    };

    var getResources = function(){
        if(vm.resources.length<=0){
            $http.post(config.apiUrl + "courses/"+courseid+"/attachments",
                {token: auth.token(), name: ''}
            ).then(function(response){
                vm.resources = response.data.data;
                vm.resources_tag = true;
            });
        }
    };

    var getHomeworks = function(){
        if(vm.homeworks.length <=0){
            $http.get(config.apiUrl + "courses/homeworks/"+courseid+"?token="+auth.token()).then(function(response){
                vm.homeworks = response.data.data;
                console.log(response.data);
                vm.homeworks_tag = true;
            });
        }
    };

    var getExercises = function(){
        if(vm.exercises.length <=0){
            $http.get(config.apiUrl + "courses/"+courseid+"/exercises?token="+auth.token()).then(function(response){
                vm.exercises = response.data.data;
                console.log(response.data);
                vm.exercises_tag = true;
            });
        }
    };


    vm.isTeacher = false;
//    vm.currentTab = 1;

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

    vm.tab = function(index){
       vm.currentTab = index;
       vm.searchText = '';

        vm.showActivities = false;
        vm.showClassMate = false;
        vm.showResources = false;
        vm.showHomework = false;
        vm.showTestcase = false;
        vm.resources_tag = false;
        vm.homeworks_tag = false;
        vm.exercises_tag = false;

        if(vm.isTeacher){
            if(index == 1){
                if(vm.course_activities.length <= 0){
                    vm.getClassActivities(0);
                }
                vm.showActivities = true;
            }
            else if(index == 2){ //课件
                getResources();
                vm.showResources = true;
            }
            else if(index==3){ //作业
                getHomeworks();
                vm.showHomework = true;
            }
            else if(index==4){ //学生管理
                getUsers();
                vm.showClassMate = true;
            }

        } else {
            if(index == 1){
                if(vm.course_activities.length <= 0){
                    vm.getClassActivities(0);
                }
                vm.showActivities = true;
            }
            else if(index == 3){
                getUsers();
                vm.showClassMate = true;
            }
            else if(index==2){
                getResources();
                vm.showResources = true;
            }
        }
        rms.save("tab_num",vm.currentTab);
    };

//    vm.tabRecord = rms.get('tab_num') || 1;

//    vm.course = {};
    vm.students = [];
    vm.teachers = [];
    vm.reviewers = [];   //待审批
    vm.resources = [];
    vm.homeworks = [];
    vm.exercises = [];

    //发送类别 1课件 2作业 3测验
    vm.myresource_sendIndex = rms.get('myresource_sendIndex') || 1;

    vm.alertService = alertService.create();
    //双弹框
    vm.alertService_2 = alertService.create();
    
    vm.invite = function(){
       rms.save('course_activities_page',vm.course_activities_page);
       rms.save("course_activities",vm.course_activities);
       rms.save("course_has_more",vm.course_has_more);
       rms.save("course",vm.course);

       $location.path("/invite_code").search({id: courseid}); 
    };

    vm.sendFile = function(r,index){
        vm.myresource_sendIndex = index;
        rms.save('course_activities_page',vm.course_activities_page);
        rms.save("course_activities",vm.course_activities);
        rms.save("course_has_more",vm.course_has_more);
        rms.save("course",vm.course);
        rms.save('myresource_sendIndex',index);
        $location.path("/send_class_list").search({id: r.id,course_id: courseid});
    };

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

                if (response.data.status == 0){
                    vm.course = response.data.data;
                    resetMenu(vm.course.current_user_is_teacher,vm.currentTab);
                    if(tag){
                        vm.tab(4);
                        tag = null;
                        vm.currentTab = 4;
                    }
                    else{
                        vm.currentTab = 1;
                        vm.tab(vm.currentTab);
                    }
                }
                else{
                    vm.alertService.showMessage('提示', response.data.message);
                }

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

    var resetMenu = function(is_teacher,index){
        vm.isTeacher = is_teacher;
        if(is_teacher){
            vm.menus = ["动态", "课件", "作业", "成员管理"];
        } else {
            vm.menus = ['动态','课件', "我的同学"];
        }
        vm.tab(index);
    };

    if(vm.course){
        resetMenu(vm.course.current_user_is_teacher,vm.currentTab);
    }

    vm.onSetting = function(user){
        rms.save('current_edit_member', user);
        rms.save("course",vm.course);
        rms.save("tab_num",vm.currentTab);
        $location.path("/edit_class_member").search({id: courseid,user_id: user.id});
    };

    vm.onSetting_1 = function(user){
        rms.save('current_edit_member', user);
        rms.save("course",vm.course);
        rms.save("tab_num",vm.currentTab);
        $location.path("/select_my_coursegroup").search({id: courseid,user_id: user.id});
    };

    vm.review = function(user){
        rms.save('current_review_member', user);
        rms.save('current_course', vm.course);
        rms.save("course",vm.course);
        rms.save("tab_num",vm.currentTab);
        $location.path("/review_class_member").search({id: courseid,user_id: user.id});
    };

    //跳到详情页
    vm.goDetail = function(type, act_id,id){
        rms.save("yoffset", document.documentElement.scrollTop || document.body.scrollTop);
        rms.save('course_activities_page',vm.course_activities_page);
        rms.save("course_activities",vm.course_activities);
        rms.save('course_has_more', vm.course_has_more);
        rms.save("tab_num",vm.currentTab);
        rms.save("course",vm.course);
//        $location.path('/'+type+'/'+act_id);
        $location.path("/"+type).search({id: act_id});
    };

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

        common.addCommonPraise(act);
    };

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

        common.decreaseCommonPraise(act);
    };

    vm.goPublishNotice = function(){
        if(!vm.isTeacher){
            return;
        }
        rms.save('course_activities_page',vm.course_activities_page);
        rms.save("course_activities",vm.course_activities);
        rms.save('course_has_more', vm.course_has_more);
        rms.save("tab_num",vm.currentTab);
        rms.save("course",vm.course);
        rms.save('current_course', vm.course);
        $location.path("/class_publishnotice").search({id:courseid});
    };

    vm.goPublishIssue = function(){
        rms.save('course_activities_page',vm.course_activities_page);
        rms.save("course_activities",vm.course_activities);
        rms.save('course_has_more', vm.course_has_more);
        rms.save("tab_num",vm.currentTab);
        rms.save("course",vm.course);
        rms.save('current_course', vm.course);
        $location.path("/class_publishissue").search({id:courseid});
    };

    vm.goHomeworkDetail = function(id){
        rms.save("yoffset", document.documentElement.scrollTop || document.body.scrollTop);
        rms.save('course_activities_page',vm.course_activities_page);
        rms.save("course_activities",vm.course_activities);
        rms.save('course_has_more', vm.course_has_more);
        rms.save("tab_num",vm.currentTab);
        rms.save("course",vm.course);
//        $location.path('/'+type+'/'+act_id);
        $location.path("/homework").search({id: id});
    };

    vm.quit = function(){
        vm.alertService_2.showMessage('提示', "是否确认退出班级",function(){
            $http({
                method: 'POST',
                url: apiUrl + "courses/" + courseid+"/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('course_activities_page',0);
                        rms.save("course_activities",[]);
                        rms.save("course_has_more",false);
                        rms.save("course",null);
                        rms.save("tab_num",null);
                        rms.save('syllabuses',[]);
                        $location.path("/class_list");
                    });
                }
                else{
                    vm.alertService.showMessage('提示', response.data.message);
                }
            });
        });

    };

    vm.goEditGroup = function(){
        if(!vm.isTeacher){
            return;
        }

        rms.save('course_activities_page',vm.course_activities_page);
        rms.save("course_activities",vm.course_activities);
        rms.save('course_has_more', vm.course_has_more);
        rms.save("tab_num",vm.currentTab);
        rms.save("course",vm.course);
        rms.save('current_course', vm.course);

        if(vm.course.groupnum == 0){
            $location.path("/edit_class_group").search({id: courseid});
        }
        else{
            $location.path("/class_group").search({id: courseid});
        }
    }

}]);