<%@ 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;
		{
			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/searchForMng?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;
						var role=$("#role").val();
						var userId=$("#userId").val();
						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].questionNo + "</a></div>"
									+ "<div style='margin-top:10px;'><input type='button' class='bigBtn' value='编辑' onclick='toEditQues(" + list[i].courseId + "," + list[i].id + ")'/></div>"
									+ "<div style='margin-top:30px;'><input type='button' class='bigBtn' value='删除' onclick='toDelQues(" + list[i].id + ")'/></div>"
									+ "<div style='margin-top:30px;'><input type='button' class='bigBtn' value='审核' onclick='audit(" + list[i].id + ")'/></div>"
									+ "<div style='margin-top:30px;'><input type='button' class='bigBtn' value='日志' onclick='getAuditLogs(" + 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>知识点：" + trimStr(list[i].chapter) + "章" + trimStr(list[i].section) + "节" + trimStr(list[i].item) + "目" + trimStr(list[i].page)+"页"
								+ "</div><div>能力要求：" + trimStr(abilityText)
								+ "</div><div>考核要求：" + trimStr(assessmentDemandText)
								+ "</div><div>预计难度：" + trimStr(estimateDifficultyText)
								+ "</div><div>状态：" + trimStr(list[i].statusName)
								+ "</div><div>命题教师：" + trimStr(list[i].setTeacherName)
								+ "</div><div>命题时间：" + formatDate(list[i].setTime)
								+ "</div><div>启用时间：" + trimStr(list[i].openTimesStr)
								+ "</div><div>创建人：" + trimStr(list[i].createdByBean.name)
								+ "</div><div>创建时间：" + longFormatDate(list[i].createdTime)
								+ "</div><div>最近修改人：" + trimStr(list[i].updatedByBean.name)
								+ "</div><div>最近修改时间：" + longFormatDate(list[i].updatedTime)
								+ "</div>";
							if(list[i].paperTasks.length == 0){
								questionBasicInfoTD = questionBasicInfoTD + '<div>此试题未被使用</div>';
							}else{
								var content = '<div style="color:#000">此题已被<span style="font-weight:bold;">' + list[i].paperTasks.length + '</span>个试卷使用：</div>';
             					content = content + '<table cellpadding="0" cellspacing="0" border="0" style="width:270px;border-collapse:collapse;border:none;">';
             					content = content + '<tr><td class="myTd3" nowrap="nowrap">试卷名称</td><td class="myTd3" nowrap="nowrap">状态</td></tr>'
             					for(var j=0;j<list[i].paperTasks.length;j++){
             						var task = list[i].paperTasks[j];
             						content = content + '<tr><td class="myTd4" width="50%"><a href="' + url + '/zujuan/manualAdd?taskId=' + task.id + '" target="viewPaper_' + task.id + '">' + task.paperName + '</a></td><td class="myTd4">' + task.statusName + '</td></tr>';
             					}
             					content = content + '</table>';
								questionBasicInfoTD = questionBasicInfoTD + content;
							}
							questionBasicInfoTD = questionBasicInfoTD + "</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>";
							var answer = '';
							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>";
										answer = answer + list[i].options[j].optionNoString + " ";
									}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{
								answer = list[i].answer;
							}
							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>" + 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 audit(id){
	alert(id);
}

function courseChange(url,role) {
	if($("#course").val() == ""){
		$("#statBtn").css("visibility","hidden");
	}else{
		$("#statBtn").css("visibility","visible");
	}
	resetSelect();
	var obj = {
		"id" : $("#course").val()
	};
	var Course = JSON.stringify(obj);
	$.ajax({
		type : "POST",
		url : url + "/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 : url + "/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);
	});
}

function trimStr(str,defaultStr){
	if(defaultStr == null){
		defaultStr = '';
	}
	if(str == null || str == '' || typeof(str) == "undefined"){
		return defaultStr;
	}else{
		return str;
	}
}