<%@ page language="java" contentType="application/x-javascript; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.qmth.qrzk.repository.utils.General"%>
<%@ taglib uri="/WEB-INF/taglib/c.tld" prefix="c"%>
<c:set var="ctx" value="<%=General.getBasePath(request)%>" scope="request"/>
var CurrentPage = 1;

function refreshTbl(){
	search('${ctx}',CurrentPage);
}

function search(url, currentPage) {
	var course = $("#course").val();
	//if (course == "0") {
		//$.messager.alert('提示','请选择课程');
		//alert('请选择课程');
		//return false;
	//} else {
		var book = $("#bookId").val();
		var gradeCode = $("#gradeCode").val();
		var estimateDifficultyCode = $("#estimateDifficultyCode").val();
		var assessmentDemand = $("#assessmentDemand").val();
		var type = $("#typeId").val();
		var chapter = $("#chapter").val();
		var section = $("#section").val();
		var item = $("#item").val();
		var page = $("#ye").val();
		var abilityCode = $("#abilityCode").val();
		var status = $("#status").val();
		var keyword = $("#keyword").val();
		var questionId;
		var obj;
		if(!isNaN(keyword)){  			//判断关键字输入栏是否是数字
			questionId=keyword;
			obj = {
					"id" :questionId,
					"courseId" : course,
					"typeId" : type,
					"bookId" : book,
					"estimateDifficultyCode" : estimateDifficultyCode,
					"gradeCode" : gradeCode,
					"abilityCode" : abilityCode,
					"assessmentDemand":assessmentDemand,
					"chapter" : chapter,
					"section" : section,
					"item" : item,
					"pageStr" : page,
					"statusCode" : status
				};
		}else{
			obj = {
					"courseId" : course,
					"typeId" : type,
					"bookId" : book,
					"estimateDifficultyCode" : estimateDifficultyCode,
					"gradeCode" : gradeCode,
					"abilityCode" : abilityCode,
					"assessmentDemand":assessmentDemand,
					"chapter" : chapter,
					"section" : section,
					"item" : item,
					"pageStr" : page,
					"statusCode" : status,
					"keyword" : keyword
				};
		}
		$(document).mask('正在加载中，请稍等...');
		var Question = JSON.stringify(obj);
		$.ajax({
					type : "POST",
					url : url + "/question/search?currentPage=" + currentPage,
					data : Question,
					contentType : "application/json; charset=utf-8",
					dataType : "json",
					success : function(callback) {
						$(document).unmask();
						var ReHtml = "";
						callback = eval(callback);
						var count = callback.count;
						CurrentPage = callback.currentPage;
						var list = callback.list;
						for (var i = 0; i < list.length; i++) {
							var questionIdTD = "<td vertical-align='top' width='80px'><div><a href='javascript:void(0)' onclick='toEditQues(" + list[i].courseId + "," + list[i].id + ")' id=\"questionId\">" + list[i].id + "</a></div>"
									+ "<div style='margin-top:10px;'><input type='button' value='编辑' onclick='toEditQues(" + list[i].courseId + "," + list[i].id + ")'/></div>"
									+ "</td>";
							
							var assessmentDemandText = codeMap['ASSESSMENT_DEMAND-' + list[i].assessmentDemand];
							var abilityText = codeMap['ABILITY-' + list[i].abilityCode];
							var estimateDifficultyText = codeMap['QUESTION_DIFFICULTY-' + list[i].estimateDifficultyCode];
							var questionBasicInfoTD = "<td style='text-align:left'>" 
								+ "<div>课程：" +list[i].courseName 
								+ "</div><div>题型：" +list[i].typeName 
								+ "</div><div>教材：" + list[i].bookName
								+ "</div><div>分值：" + list[i].score
								+ "</div><div>考核要求：" + assessmentDemandText
								+ "</div><div>能力要求：" + abilityText
								+ "</div><div>预计难度：" + estimateDifficultyText
								+ "</div><div>知识点：" + list[i].chapter + "章" + list[i].section + "节" + list[i].item + "目" + list[i].page+"页"
								+ "</div><div>状态：" + list[i].statusName
								+ "</div><div>命题时间：" + formatDate(list[i].setTime)
								+ "</div></td>";
							
							var questionContentTD = "<td width='70%' style='text-align: left;' onclick=\"openinfo("+list[i].id+")\" >"
							+ "<div id="+list[i].id+" title='点击显示答案和分析'>"
							+ "<div class='question-body'>" +
								"<div>"+list[i].body+"</div>";

							if(list[i].options != null && list[i].options.length > 0){
								questionContentTD += "<div style='text-align: left;'><table class='optionTable'>";
								for(var j = 0; j < list[i].options.length; j++){
									if(list[i].options[j].isCorrectOption == 1){
										questionContentTD += "<tr style='color:red;'><td>&nbsp;</td><td>";
									}else{
										questionContentTD += "<tr><td>&nbsp;</td><td>";
									}
									questionContentTD += list[i].options[j].optionNoString + "\.&nbsp;</td><td>" + list[i].options[j].contentHtml + "</td></tr>";
								}
								questionContentTD += "</table></div>";
							}else{
								questionContentTD += "<div style='border-top:1px dashed #cccccc;margin-top:5px;margin-bottom:5px;' id="+list[i].id+"_line></div>" +
										  "<div id="+list[i].id+"_question>" + list[i].answer + "</div>";
							}
							
							questionContentTD += "</div></div></td>";
							
							ReHtml += "<tr align='center'>"
								+ questionIdTD
								+ questionBasicInfoTD
								+ questionContentTD
								+ "</tr>";
							
							if (list.length != (i + 1)) {
								var intervalTR = "<tr class='interval'><td colspan='3' ></td></tr>";
								ReHtml += intervalTR;
							}

						}
						var size = "<span style=\"background-color: #bcb93a;padding-left: 35px;padding-right: 35px;\">满足条件试题数量："
								+ count + "</span>";
						$("#row").html(size);
						$(".info tbody").html(ReHtml);
						if (currentPage == "1")
							pageShow(1, count, "false");
					}
				});
	//}
}

function openinfo(id){
	if($("#"+id+"_question").is(":hidden")){
		$("#"+id+"_question").show();
		$("#"+id+"_line").css("border-top","1px dashed #cccccc");
		
		$("#"+id+"_line").show();
	}else{
		$("#"+id+"_question").hide();
		$("#"+id+"_line").hide();
	}
}

function courseChange() {
	resetSelect();
	var obj = {
		"id" : $("#course").val()
	};
	var Course = JSON.stringify(obj);
	$.ajax({
		type : "POST",
		url : "${ctx}/question/loadQuestionType",
		data : Course,
		contentType : "application/json; charset=utf-8",
		dataType : "json",
		success : function(callback) {
			var html = $("#typeId").html();
			callback = eval(callback);
			for (var i = 0; i < callback.length; i++) {
				html += "<option value=\"" + callback[i].id + "\" id=\""+callback[i].id+"_type\">"
						+ callback[i].name + "</option>";
			}
			$("#typeId").html(html);
		}
	});
	$.ajax({
		type : "POST",
		url : "${ctx}/question/loadBook",
		data : Course,
		contentType : "application/json; charset=utf-8",
		dataType : "json",
		success : function(callback) {
			var html = $("#bookId").html();
			callback = eval(callback);
			for (var i = 0; i < callback.length; i++) {
				html += "<option value=\"" + callback[i].id + "\"  id=\""+callback[i].id+"\" selected=\"selected\">"
						+ callback[i].name + "</option>";
			}
			$("#bookId").html(html);
		}
	});
}

function resetSelect(){
	$("#typeId option:gt(0)").remove();
	$("#bookId option:gt(0)").remove();
}

function pageShow(ThisPage, PageCount, flag) {
	if (flag == "false") {
	} else {
		search("${ctx}", ThisPage);
	}

	// ThisPage = 当前页
	// PageCount = 总条数
	// 获取当前页之后，可通过Ajax进行返值。
	$(function() {
		// 每页条数
		var pageText = 3;
		// 分页总数
		var pageNumber = Math.ceil(PageCount / pageText);
		// page分割数量
		var pageFor = 6;
		var pageSlipt = pageFor / 2;
		var pageHTML = new Array;
		
		// 首页
		if(PageCount!=0){
			pageHTML += "<a href=\"javascript:pageShow(1,'" + PageCount+ "');\" >第一页</a>";
		}else{
			pageHTML += "<a href=\"javascript:pageShow(1,'" + PageCount+ "','false');\" >第一页</a>";
		}
		
		if (pageNumber > pageFor) {
			if (ThisPage > pageSlipt) { // 如果当前页大于分割数量的一半就开始分页
				/*
				 * 最后几页的算法
				 */
				if (ThisPage >= (pageNumber - pageSlipt)) { // 如果当前页大于等于总页数减去分割数量的一半
					if (ThisPage == pageNumber) { // 如果当前页等于总页数不在往后加页数
						countPage = (((ThisPage - pageSlipt) + pageFor) - pageSlipt);
					} else {
						countPage = (((ThisPage - pageSlipt) + pageFor)- pageSlipt + 1);
					}
				} else {
					countPage = ((ThisPage - pageSlipt) + pageFor);
				}

				for (var i = (ThisPage - pageSlipt); i <= countPage; i++) {
					if (i == ThisPage) {
						pageHTML += "<a href=\"javascript:pageShow(" + i + ",'"+ PageCount + "');\" class=\"this\" >" + i+ "</a>";
					} else {
						pageHTML += "<a href=\"javascript:pageShow(" + i + ",'"+ PageCount + "');\"  >" + i + "</a>";
					}
				}
			} else {
				for (var i = 1; i < pageFor; i++) {
					if (i == ThisPage) {
						pageHTML += "<a href=\"javascript:pageShow(" + i + ",'"+ PageCount + "');\" class=\"this\" >" + i+ "</a>";
					} else {
						pageHTML += "<a href=\"javascript:pageShow(" + i + ",'"+ PageCount + "');\" >" + i + "</a>";
					}
				}
			}
		} else { //原始的那种分页
			for (var i = 1; i <= pageNumber; i++) {
				if (i == ThisPage) {
					pageHTML += "<a href=\"javascript:pageShow(" + i + ",'"+ PageCount + "');\" class=\"this\"  >" + i+ "</a>";
				} else {
					pageHTML += "<a href=\"javascript:pageShow(" + i + ",'"+ PageCount + "');\"  >" + i + "</a>";
				}
			}
		}
		//末页
		if(PageCount!=0){
			pageHTML += "<a href=\"javascript:pageShow(" + pageNumber + ",'"+ PageCount + "');\" >最后一页</a>";
		}else{
			pageHTML += "<a href=\"javascript:pageShow(" + pageNumber + ",'"+ PageCount + "','false');\" >最后一页</a>";
		}
		
		$(".pages").html(pageHTML);
	});
}
