<%@ page contentType="text/html;charset=gbk" %>
<%@ page import="java.util.*" %>
<%@ page import="net.sz600.tool.SzDateHelp"%>
<%@ page import="net.sz600.basicdata.*" %>
<%@ page import="net.sz600.tool.SzConvert" %>
<%@ page import="net.sz600.database.SzPageInfo" %>
<%@ page import="net.sz600.system.*" %>
<%@ page import="net.sz600.drivingSchool.*" %>
<%@ include file="/sessionInfo.jsp"%>

<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<link href="<%=strSkin_SESSION%>/css2007.css" rel="stylesheet" type="text/css">
<script language="javascript" src="../../share/js/commonTool.js"></script>
</head>
<%
	//考试科目
	//科目顺序
	String sSubjectOrder = SzConvert.strWriteData(request.getParameter("subjectOrder"));	 //科目
	
	//科目信息
	Subject subjectClass = new Subject();
	String subject[] = (String[])subjectClass.getSubjectInfoByOrder(sSubjectOrder);
	int iSelectSubject = 0;
	String sSubjectName = "";
	if (subject != null) {
		iSelectSubject = SzConvert.toInt(subject[0]);
		sSubjectName = subject[1];
	}	


	//查询时间段
	String sSearchBegin = SzConvert.strWriteData(request.getParameter("txtSearchBegin"));
	String sSearchEnd = SzConvert.strWriteData(request.getParameter("txtSearchEnd"));
	//获取选择的部门信息
	String BMID = SzConvert.toZeroStr(request.getParameter("BMID"));
	String BMMC = SzConvert.strWriteData(request.getParameter("BMMC"));
	//学号
	String XH = SzConvert.strWriteData(request.getParameter("XH"));
	//流水号
	String LSH = SzConvert.strWriteData(request.getParameter("LSH"));
	//条件sCond
	StringBuffer sCond = new StringBuffer("");
	//如果开始时间和结束时间都为空，就默认为当前月
	if(sSearchBegin.equals("")){
		sSearchBegin = SzDateHelp.dateToYMD(SzDateHelp.getFirstDateMonth());
	}
	if (sSearchEnd.equals("")){
		sSearchEnd = SzDateHelp.dateToYMD(SzDateHelp.getLastDateMonth());
	}
	//查询的结束时间
	String searchEnd = SzDateHelp.dateToYMD(SzDateHelp.nextDay(SzDateHelp.stringToDate(sSearchEnd)));
	
	if (!sSearchBegin.equals("")) {
        sCond.append(" AND XXMC24>='" + sSearchBegin + "'");
  }
    if (!searchEnd.equals("")) {
        sCond.append(" AND XXMC24<'" + searchEnd + "'");
    }
	
	
	if (!BMMC.equals("")){
		sCond.append(" AND XXMC6='"+BMMC+"'");
	}
	//学号
	if (!XH.equals("")){
		sCond.append(" AND XXMC3='"+ XH +"'");
	}
	//流水号
	if (!LSH.equals("")){
		sCond.append(" AND XXMC32='"+ LSH +"'");
	}
	//排除已分配教练的学员
	sCond.append(" AND XXMC1 NOT IN (SELECT XXMC14 FROM BD_DRIVINGSCHOOL_TOSETUPCOACH WHERE XXMC8='" + subject[1] + "' )");
	//增加条件：文考通过,
	//教练分配删除后,状态就不是文考及格
	//sCond.append(" AND ( XXMC37='文考及格' or  XXMC37='桩考教练已分配' or XXMC37='桩考练车') ");
	Exam exam = new Exam();
	sCond.append(exam.getCond("分配教练", sSubjectName));	
	
	String sSortField = SzConvert.toSpaceStr(request.getParameter("txtSortField")); 
  int iSortOrder    = Integer.parseInt(SzConvert.toZeroStr(request.getParameter("txtSortOrder"))); 
  if (sSortField.equals("")) { 
    	sSortField = "XXMC34";
    	iSortOrder = SzProperties.I_SortOrder_DESC;
  }
	sCond.append(" ORDER BY "+ sSortField + (iSortOrder==SzProperties.I_SortOrder_DESC? " DESC":" ASC"));
	
	
	//获取场地信息
	String[] tpFields = new String[]{"XXMC1","XXMC2","XXMC3","XXMC4","XXMC5"};
	TrainingPlace tPlace = new TrainingPlace();
	ArrayList<String[]> tpList = tPlace.getTrainingPlaceList(tpFields,"");
	tpList.add(new String[]{"","其他","","","不详"});
	int tpSize = tpList.size();
	String[] tpInfo = null;
	
	//获取Vehicle对象
    Student student = new Student();
	//场地ID,学员ID,学号,姓名,流水号,班别,班别ID
	String[] fields = new String[]{"XXMC41","XXMC1","XXMC3","XXMC2","XXMC32","XXMC16","XXMC19"};
	//根据学员的就近训练场分组学员信息
	Map<String,ArrayList<String[]>> studentMap = student.getXYListMap(fields,1,sCond.toString());
	
	//获取学员信息集合
	ArrayList<String[]> studentList = null;
	int iSize = 0;
	//学员
	String[] studentInfo = null;
	

	
%>
<!-----上面的工具栏------>
<%@ include file="/docEngine/title.jsp"%>
<table width="100%">
  <form name="formBar" method="post">
  <input type="hidden" name="txtSortField" value="<%=sSortField%>"> 
  <input type="hidden" name="txtSortOrder" value="<%=iSortOrder%>">
  <input type="hidden" name="subjectOrder" value="<%=sSubjectOrder%>"> 
  <tr>
    <td>
      科目：<B><%=sSubjectName%></B>    
      报名点<input type="text" name="BMMC" class="txtClassSearch" value="<%=BMMC%>">
      学号<input type="text" name="XH" class="txtClassSearch" value="<%=XH %>">
      流水号<input type="text" name="LSH" class="txtClassSearch" value="<%=LSH %>">
      开始<input type="text" name="txtSearchBegin" class="txtClassSearch" value="<%=sSearchBegin%>">
      <%=SzPageHelp.calendar2Select("../../","formBar.txtSearchBegin")%>
      结束<input type="text" name="txtSearchEnd" class="txtClassSearch" value="<%=sSearchEnd%>">
      <%=SzPageHelp.calendar2Select("../../","formBar.txtSearchEnd")%>
      <%=SzPageHelp.button("查询", "javascript:FormSubmit(formBar,'setUpCoachList.jsp');")%>
      <%=SzPageHelp.button("批量保存", "javascript:ShenHeSubmit(formUpdate,'control.jsp');")%>
    </td>
  </tr>
  </form>
</table>

<!-----信息列表----->
<table width="100%" border="1" bordercolorlight="#C0C0C0" bordercolordark="#FFFFFF" class="tableClass" cellpadding="1" cellspacing="0">
  <form name="formUpdate" method="post">
  <input type="hidden" name="OPERATOR" value="add"> 
  <input type="hidden" name="subjectOrder" value="<%=sSubjectOrder%>"> 
  <input type="hidden" name="SzDLXM" value="<%=sDLXM_SESSION %>">
  <input type="hidden" name="SzDLID" value="<%=iDLID_SESSION %>">
  <input type="hidden" name="SzBMMC" value="<%=sBMMC_SESSION %>">
  <input type="hidden" name="SzBMID" value="<%=iBMID_SESSION %>">
  <input type="hidden" name="txtSortField" value="<%=sSortField%>"> 
  <input type="hidden" name="txtSortOrder" value="<%=iSortOrder%>">
  <input type="hidden" name="BMMC" value="<%=BMMC%>">
  <input type="hidden" name="XH" value="<%=XH %>">
  <input type="hidden" name="LSH" value="<%=LSH %>">
  <input type="hidden" name="txtSearchBegin" value="<%=sSearchBegin%>">
  <input type="hidden" name="txtSearchEnd" value="<%=sSearchEnd%>">
  <input type="hidden" name="KSKM" value="<%=subject[1] %>">
  <input type="hidden" name="KSKMID" value="<%=subject[0] %>">
  <!-- 用于存放临时的选择值 -->
  <input type="hidden" name="tempMC" value=""> 
  <input type="hidden" name="tempID" value=""> 
  <tr class="trClassTitle">
  	<!-- 信息ID,学号,报名时间,姓名,性别,户籍,班别,报名点,身份证号,数码回执,接送地址,招收人员 -->
  	<td width="150">场地</td>
    <td>就近学员</td>
    <td width="150">场地&教练</td>
    <td width="40">操作</td>
  </tr>
<%
	//获取当前时间
	String time_str = SzDateHelp.getTimeStr();
	String sKey = "";
	int i = 0;
	for (;i<tpSize;i++){
		tpInfo = tpList.get(i);
		sKey = tpInfo[0];
		//就近学员
		studentList = studentMap.get(sKey);
		iSize = studentList==null? 0:studentList.size();
	%>
	<input type="hidden" name="XYID<%=i %>" value="<%=sKey %>">
	<input type="hidden" name="OPERATOR<%=i %>" value="add"> 
	<tr id="tr<%=i %>">
		<td><input type="checkbox" name="chkCheckAll<%=i %>" OnClick="javascript:CheckAll(formUpdate,this, 'student<%=i %>');"><b><%=tpInfo[1] %></b><br>项目：<%=tpInfo[4] %></td>
		<td valign="top">
		<%
			for(int j = 0;j<iSize;j++){
				//学员ID,学号,姓名,流水号,班别,班别ID
				studentInfo = studentList.get(j);
		%>
		<input type="hidden" name="sName<%=studentInfo[0] %>" value="<%=studentInfo[2] %>">
		<input type="hidden" name="sBJ<%=studentInfo[0] %>" value="<%=studentInfo[4] %>">
		<input type="hidden" name="sBJID<%=studentInfo[0] %>" value="<%=studentInfo[5] %>">
		<input type="hidden" name="sXH<%=studentInfo[0] %>" value="<%=studentInfo[1] %>">
		<input type="hidden" name="sLSH<%=studentInfo[0] %>" value="<%=studentInfo[3] %>">
		<span style="width: 75px;"><input type="checkbox" name="student<%=i %>" value="<%=studentInfo[0] %>">
		<a href="javascript:void(0);" onclick="javascript:OpenWin_Detail('../../share/formEngine2/update.jsp?OPERATOR=detail&FormID=1384&type=0&ShowMode=open&KEY=<%=studentInfo[0] %>');"><%=studentInfo[2] %></a>
		</span>
		<%
			}
			out.println(iSize>0? "":"&nbsp;");
		%>
		</td>
		<td nowrap>
			<input type="hidden" name="CDID<%=i %>" value="<%=sKey %>"> 
			<input type="hidden" name="JLID<%=i %>" value=""> 
			<input type="text" class="txtClass120" name="CD<%=i %>" value="<%=sKey.equals("")? "":tpInfo[1] %>"><INPUT class=btnClassShort onclick="javascript:OpenSelectWin('setUpCoach_CD',formUpdate.CDID<%=i %>,formUpdate.CD<%=i %>);" value=场地 type=button name=btnSelect><br>
			<input type="text" class="txtClass120" name="JL<%=i %>" value=""><INPUT class=btnClassShort onclick="javascript:OpenSelectWin2('setUpCoach_coach','formUpdate.JLID<%=i %>','formUpdate.JL<%=i %>');" value=教练 type=button name=btnSelect>
		</td>
		<td align="center"><INPUT class="btnClassShort" onclick="javascript:singleSubmit(this,<%=i %>);" value="保存" type="button" name="btnSelect" <%=iSize>0? "":"disabled" %>></td>
	</tr>
	<%
	}
%>
	<input type="hidden" name="count" value="<%=i %>">
  </form>
</table>

<!-----下面的工具栏------>
<table width="100%">
  <form name="formPage" method="post">
  </form>
</table>

<!-----这里包含要提交的内容--->
<form name="formHidden" method="post">
</form>
<script language="javascript" type="text/javascript">
	function ShenHeSubmit(formName,sUrl){
		
		var count = document.getElementById("count").value;
		var checkeds = 0;
		for (var i = 0;i<count;i++){
			//学员ID
			var students = document.getElementsByName("student"+i);
			var cd = document.getElementById("CD" + i).value;
			var jl = document.getElementById("JL" + i).value;
			
			var studentID = "",sName = "";
			for (var j = 0;j<students.length;j++){
				studentID = students[j].value;
				sName = document.getElementById("sName" + studentID).value;
				if (students[j].checked){
					checkeds++;
					if (cd == ""){
						alert("请为 "+ sName +" 指定训练场！");
						return false;
					} else if (jl == ""){
						alert("请为 "+ sName +" 指定教练！");
						return false;
					}
				}
			}
		}
		if (checkeds == 0){
			alert("请选择学员!");
			return false;
		}
		if (!confirm("确定是否要批量保存?")){
			return false;
		}
		formName.action = sUrl;
		formName.submit();
	}
	
    function OpenWin_Detail(sUrl) {
  		window.open(sUrl,"","left=100,top=50,width=800,height=600,status=yes,scrollbars=yes,resizable=yes");	
  	}
	

	function singleSubmit(obj,i){
		var dlid = document.getElementById("SzDLID").value;
		var dlxm = document.getElementById("SzDLXM").value;
		var bmmc = document.getElementById("SzBMMC").value;
		var bmid = document.getElementById("SzBMID").value;
		var kskm = document.getElementById("KSKM").value;
		var kskmid = document.getElementById("KSKMID").value;
		
		var cdid = document.getElementById("CDID" + i).value;
		var cd = document.getElementById("CD" + i).value;
		var jlid = document.getElementById("JLID" + i).value;
		var jl = document.getElementById("JL" + i).value;
		
		if (cd == ""){
			alert("请选择训练场！");
			return false;
		} else if (jl == ""){
			alert("请选择教练！");
			return false;
		}
 
		var sUrl = "singleControl.jsp?post=ajaxPost&OPERATOR0=add&count=1";
		var postStr = "SzDLID="+ dlid +"&SzDLXM="+ dlxm + "&SzBMID="+ bmid +"&SzBMMC="+ bmmc +"&KSKM=" + kskm + "&KSKMID=" + kskmid + 
					"&CDID0=" + cdid + "&CD0=" + cd + "&JLID0=" + jlid + "&JL0=" + jl;
		//学员ID
		var students = document.getElementsByName("student"+i);
		var studentID = "";
		var checkeds = 0;
		for (var j = 0;j<students.length;j++){
			if (students[j].checked){
				checkeds++;
				studentID = students[j].value;
				postStr += "&student0=" + studentID;
				postStr += "&sName"+ studentID +"=" + document.getElementById("sName" + studentID).value;
				postStr += "&sBJ"+ studentID +"=" + document.getElementById("sBJ" + studentID).value;
				postStr += "&sBJID"+ studentID +"=" + document.getElementById("sBJID" + studentID).value;
				postStr += "&sXH"+ studentID +"=" + document.getElementById("sXH" + studentID).value;
				postStr += "&sLSH"+ studentID +"=" + document.getElementById("sLSH" + studentID).value;
			}
		}
		if (checkeds == 0){
			alert("请选择学员!");
			return false;
		}
		//进行编码
		postStr = encodeURI(encodeURI(postStr));
		//实例化Ajax
		var ajax = null;
		if (window.XMLHttpRequest){
			ajax = new XMLHttpRequest();
		} else if (window.ActiveXObject){
			ajax = new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			return;
		}
		//obj.disabled = "disabled";
		//通过Post方式打开链接
		ajax.open("POST",sUrl,true);
		//定义传输文件头部信息
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
		//发送Post数据
		ajax.send(postStr);
		//回调函数
		ajax.onreadystatechange = function(){
			if (ajax.readyState == 4 && ajax.status == 200) {
				var message = ajax.responseText;
				message = message.substring(message.indexOf("[")+1,message.indexOf("]"));
				if (message == "true"){
					//("OPERATOR"+i)不能为空
					//document.getElementById("OPERATOR"+i).value = "";
					//已经设置的设为disabled
					for (var j = 0;j<students.length;j++){
						if (students[j].checked){
							students[j].checked = "";
							students[j].disabled = "true";
						}
					}
				} else {
					obj.disabled = "";
				}
				ajax.abort();
				ajax = null;
			}
		}
		
	}
	
	//特殊选择函数
	function OpenSelectWin(vTableName,objID,objMC) {
	  var type= "";
	  var url = "../../share/formEngine2/select_win.jsp";
	  url = url + "?tableName=" + vTableName + "&index=-1";
	  window.showModalDialog(url,window,"status=no;dialogWidth=580px;dialogHeight=350px");
	  //临时hidden值赋值目标文本框
	  if (formUpdate.tempID.value.indexOf(",")>0){
	  	alert("对不起，不能选择多个！");
	  	return false;
	  }
	  objID.value = formUpdate.tempID.value;
	  objMC.value = formUpdate.tempMC.value;
	}
		
	function OpenSelectWin2(vTableName,objID,objMC) {
	  var type= "";
	  //var url = "../../share/formEngine2/select_win.jsp";
	  var url = "coach_select.jsp?selectSubject=<%=iSelectSubject%>&objId=" + objID+ "&objName=" + objMC + "&currentDate=<%=sSearchBegin%>";
	  window.showModalDialog(url,window,"status=no;dialogWidth=580px;dialogHeight=350px");
	  //临时hidden值赋值目标文本框
	  if (formUpdate.tempID.value.indexOf(",")>0){
	  	alert("对不起，不能选择多个！");
	  	return false;
	  }
	  //objID.value = formUpdate.tempID.value;
	  //objMC.value = formUpdate.tempMC.value;
	}

</script>
<%
    ////如果要使用日历控件，必须输出这个calendarFrame
    out.println(SzPageHelp.calendar2Frame("../../"));
%>