$(document).ready(function(){

	(function($) {
		$.fn.cp_get_lessons = function(data) {
			var cp_class = $(this).data("class");
			var term = $(this).data("term");
			var course = $(this).data("course");
			var btn = $(this).data("btn");
			var e = $(this);
			$.ajax({
				type: "post",
				url: "contents/plugins/classroom_plus/ajax.php",
				data: {"action": "cp_get_lessons", "class": cp_class, "term": term, "course": course}
			}).done(function(data){
				try {
					data = JSON.parse(data);
					$(e).html("<h1>Lessons</h1>"+data.lesson_table);
					$("#"+btn).html("Get Contents").attr("disabled", false);
					$("#display-quiz").cp_get_quiz({
						"class": cp_class, 
						"term": term, 
						"course": course
					});
				}
				catch(err) {
					console.log(err);
					console.log(data);
				}
				finally {}
			})
		}
	})(jQuery);

	(function($) {
		$.fn.cp_get_quiz = function(data) {
			data.action = "cp_get_quiz"
			var e = $(this);
			$.ajax({
				type: "post",
				url: "contents/plugins/classroom_plus/ajax.php",
				data: data
			}).done(function(data){
				try {
					data = JSON.parse(data);
					$(e).html("<h1>Quiz</h1>"+data.quiz_table);
				}
				catch(err) {
					console.log(err);
					console.log(data);
				}
				finally {}
			}).fail(function(){
				$("#display-quiz").cp_get_quiz(data);
			})
		}
	})(jQuery);

  $.extend({
	    getClassCourses : function(options) {
	       return $.ajax({
                type: 'POST',
                url: 'contents/plugins/eedu_portal/ajax.php',
                data: {action: 'get_class_courses', class_id: options.class_id, category_id: options.category_id, fulloption: true},
			    success: function(data) {
			    	data = JSON.parse(data);
			    	if(data.error == 0) {
			    		$("#in_course").empty();
			    		$("#in_course").append('<option value="">Select...</option>');
			    		var html = "";
			    		for(var course_id in data.courses) {
			    			html += '<option value="'+course_id+'">'+data.courses[course_id]+'</option>';
			    		}
			    		$("#in_course").append(html).trigger("chosen:updated");
			    	} else if(data.error == 1) {
			    		$("#in_course").empty();
			    		var html = "<option value=''>Select...</option>";
			    		$("#in_course").append(html);
			    	}						
			    },
			    error: function(xhr, status, error) {
			    	$("#course").empty();
			    	$("#course").append('<option value="">Select...</option>');
			    }
	        });
	    }
	})

  	if($("#in_category").val() !== '' && $("#in_class").val() !== '' && $("#in_course").val() == '') {
  		var class_id = $("#in_class").val();
  		var category_id = $("#in_category").val();
  		$.getClassCourses({class_id: class_id, category_id: category_id});
  	}

});

$(document).on("change", "#in_class, #in_category", function(){
	$("#in_course").val("");
  	if($("#in_category").val() !== '' && $("#in_class").val() !== "") {
  		var class_id = $("#in_class").val();
  		var category_id = $("#in_category").val();
  		$.getClassCourses({class_id: class_id, category_id: category_id});
  	}
})

$(document).on("change", "#in_class, #in_category, #in_term, #in_course", function(){
	$("#create-new").css("display", "none");
})

$(document).on("click", "#search-query-btn", function(e){
	e.preventDefault();
	var btn = $(this);
	if($("#search-form").validateForm()) {
		$("#create-new").css("display", "block");
		var _class = $("#in_class").val();
		var _course = $("#in_course").val();
		var _term = $("#in_term").val();
		// $(".meta").data("class", _class).data("course", _course).data("term", _term);
		$("#add-lesson").attr("href", "cp-add-lesson?class="+_class+"&course="+_course+"&term="+_term);
		$("#add-quiz").attr("href", "cp-add-quiz?class="+_class+"&course="+_course+"&term="+_term);
		$("#add-assignment").attr("href", "cp-add-assignment?class="+_class+"&course="+_course+"&term="+_term);
		$("#display-lessons").data("class", _class).data("course", _course).data("term", _term).data("btn", $(btn).attr("id"));
		$(btn).html("<em>Loading... Please wait</em>").attr("disabled", true);
		$("#display-lessons").cp_get_lessons();
	}
	
})