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

    var vm = $scope;
//    vm.menus = ['课件', '作业', '测验'];

    vm.g_localhost = window.g_localhost;

    vm.menus = ['课件', '作业'];

    vm.resources = [];
    vm.homeworks = [];
    vm.exercise  = [];

    vm.resources_tag = false;
    vm.homeworks_tag = false;
    vm.exercises_tag = false;

    //课件
    vm.page = rms.get('page') || 0;
    vm.resources = rms.get("resources") || [];
    vm.has_more = rms.get("has_more");

    //作业
    vm.homework_page = rms.get('homework_page') || 0;
    vm.homeworks = rms.get("homeworks") || [];
    vm.homework_has_more = rms.get("homework_has_more");

    //测验
    vm.exercise_page = rms.get('exercise_page') || 0;
    vm.exercise = rms.get("exercise") || [];
    vm.exercise_has_more = rms.get("exercise_has_more");

    vm.searchText = "";

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

    vm.sendFile = function(r,index){
        vm.myresource_sendIndex = index;

        rms.save("has_more",vm.has_more);
        rms.save("homework_has_more",vm.homework_has_more);
        rms.save('myresource_sendIndex',index);

//        $location.path("/send_class_list").search({id: r.id});
        $location.path("/send_class_list").search({id: r.id,course_id: r.course_id});
    };

    vm.loadResourceData = function (index,page){
        if(index == 1){
            vm.page = page;
            $http({
                method: 'POST',
                url: apiUrl+ "resources",
                data: {token: auth.token(), page: page}
            }).then(function successCallback(response) {
                vm.resources_tag = true
                if(response.data.page >0) {
                    vm.resources = vm.resources.concat(response.data.data);
                } else {
                    vm.resources = response.data.data;
                }

                rms.save("resources", vm.resources);
                vm.has_more = (response.data.count + response.data.page * 10) < response.data.all_count;
                rms.save('has_more', vm.has_more);
                rms.save('page', response.data.page);

                console.log(response.data);

            }, function errorCallback(response) {
            });

        }
        else if(index==2 ){
            vm.homework_page = page;
            $http({
                method: 'POST',
                url: apiUrl+ "resources/homeworks",
                data: {token: auth.token(), page: page}
            }).then(function successCallback(response) {
                vm.homeworks_tag = true
                if(response.data.page >0) {
                    vm.homeworks = vm.homeworks.concat(response.data.data);
                } else {
                    vm.homeworks = response.data.data;
                }

                rms.save("homeworks", vm.homeworks);
                vm.homework_has_more = (response.data.count + response.data.page * 10) < response.data.all_count;
                rms.save('homework_has_more', vm.has_more);
                rms.save('homework_page', response.data.page);

                console.log(response.data);

            }, function errorCallback(response) {
            });
        }
        else if(index==3 ){
            vm.exercise_page = page;
            $http({
                method: 'POST',
                url: apiUrl+ "resources/exercises",
                data: {token: auth.token(), page: page}
            }).then(function successCallback(response) {
                vm.exercises_tag = true;
                if(response.data.page >0) {
                    vm.exercise = vm.exercise.concat(response.data.data);
                } else {
                    vm.exercise = response.data.data;
                }

                rms.save("exercise", vm.exercise);
                vm.exercise_has_more = (response.data.count + response.data.page * 10) < response.data.all_count;
                rms.save('exercise_has_more', vm.exercise_has_more);
                rms.save('exercise_page', response.data.page);

                console.log(response.data);

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

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

        switch(index){
            //ALL
            case 1:
                if(vm.resources.length<=0){
                    vm.loadResourceData(index,0);
                }
                break;
            //homework
            case 2:
                if(vm.homeworks.length<=0){
                    vm.loadResourceData(index,0);
                }
                break;
            //exercise
            case 3:
                if(vm.exercise.length<=0){
                    vm.loadResourceData(index,0);
                }
                break;
        }
        rms.save("resource_tab_num",vm.currentTab);
    }

    var currentTab = rms.get('resource_tab_num') || 1;

    vm.tab(currentTab);

    vm.goHomeworkDetail = function(id){
        rms.save("has_more",vm.has_more);
        rms.save("homework_has_more",vm.homework_has_more);
        rms.save("yoffset", document.documentElement.scrollTop || document.body.scrollTop);
        $location.path("/homework").search({id: id});
    }

}] );
