$(function() {
	document.oncontextmenu = function(){return false;};    
	SendMsg(568,362,"创建答题卡","this");
	bindEvents();
	$(".cardSend").bind("click", function() {
		var exerciseType;
		$("#exercise_type li").each(function() {
			if($(this).text() == $("#exercise_type #type").text()) {
				exerciseType = $(this).val();
			}
		});
		switch($("#exercise_type #type").text()){
			case "单选题":{
				exerciseType = "single_selection";
				break;
			}
			case "多选题":{
				exerciseType = "multiple_selection";
				break;
			}
			case "判断题":{
				exerciseType = "boolean_selection";
				break;
			}
			case "主观题":{
				exerciseType = "subjective";
				break;
			}
		}
		var exerciseCount;
		$("#exercise_value li").each(function() {
			if($(this).text() == $("#exercise_value #type").text()) {
				exerciseCount = $(this).val();
			}
		});
		var question_time;
		$("#exercise_time li").each(function() {
			if($(this).text() == $("#exercise_time #type").text()) {
				question_time = $(this).val();
			}
		});
		fetch("/classroomControl",{
			method:"POST",
			body:JSON.stringify({
				type: 'Quiz',
				on: true,
				id: '' + Math.random(),
				lifeSpan: question_time==0?-1:parseInt(question_time) * 60 *1000,
				questions:[{
					type:exerciseType,
					optionsCount:exerciseCount
				}]
			}),
		}).then(function(res) {
			if (res.ok) {
				SendMsg(612,46,"null","next");
			} else {
				console.log("发送题目出错");
			}
		}, function(err){
			console.log("发送题目出错");
		})
	});
});

function bindEvents() {
	$(".select-box").unbind().bind({
		"click": function(event) {
			event.stopPropagation();
			if($(this).find("#type").text()!="" && $(this).find("ul").is(":hidden")) {
				$(".select-type").removeClass("select-active");
				$(this).find(".select-type").addClass("select-active");
				$(".upload-item").find("ul").hide();
				var chooseValue = $(this).find("#type").text();
				$(this).find("ul").show();
			} else {
				$(this).find("ul").hide();
				$(this).find(".select-type").removeClass("select-active");
			}
		}
	});
	$(".select-box ul li").unbind("click").bind({
		"click": function(event) {
			event.stopPropagation();
			var newSerachText = $(this).text();
			$(".select-type").removeClass("select-active");
			$(this).parent().hide();
			if(newSerachText !== $(this).parents(".select-box").find("#type").text()) {
				$(this).parents(".select-box").find("#type").text(newSerachText);
				$(this).parents(".select-box").trigger("change");
			}
		}
	});
	$("#exercise_type").unbind("change").bind("change", function() {
		typeChanged();
	});
	$(document).on("click", function(e) {
		var e = e || window.event;
		var tar = e.target || e.srcElement;
		$(".select-box ul").hide();
	});
	//阻止冒泡
	$(".select-box").click(function(e) {
		e.stopPropagation();
	});
}

function typeChanged() {
	if($("#exercise_type #type").text() == "判断题" || $("#exercise_type #type").text() == "主观题") {
		$(".upload-item:nth-of-type(2)").hide();
		$(".upload-first").css({"margin-top":"100px"});
	} else {
		$(".upload-item:nth-of-type(2)").show();
		$(".upload-first").css({"margin-top":"60px"});
	}
	
	if($("#exercise_type #type").text() == "单选题") {
		$("#exercise_value .types li[value=2]").show();
		$("#exercise_value #type").text("A B C D");
	} 
	if($("#exercise_type #type").text() == "多选题") {
		$("#exercise_value .types li[value=2]").hide();
		$("#exercise_value #type").text("A B C D");
	}
}