<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="../common/init.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="${ctx}/resources/css/main.css" type="text/css" />
<link rel='stylesheet' href='${ctx}/resources/css/easyui.css' type='text/css' media='screen' />
<link rel='stylesheet' href='${ctx}/resources/css/easyui/icon.css' type='text/css' media='screen' />
<link rel='stylesheet' href='${ctx}/resources/css/user-center-style.css' type='text/css' media='screen' />

<%@ include file="../common/top.jsp"%>
<script type="text/javascript" src="${ctx}/resources/js/jquery.easyui.min.js"></script>
<%@ include file="../common/validation.jsp"%>
<title>新建细目表</title>

<script language="javascript">
function getCurDateStr(){
	var nowDate = new Date();
	var monthStr = (nowDate.getMonth() + 1)+"";
	if(monthStr.length == 1){
		monthStr = '0' + monthStr;
	}
	var dayStr = (nowDate.getDate())+"";
	if(dayStr.length == 1){
		dayStr = '0' + dayStr;
	}
	var dateStr = nowDate.getFullYear() + monthStr + dayStr;
	return dateStr;
}

$(document).ready(function(){
	if('${param.courseId}' != ''){
		$('#course').combogrid('setValue', '${param.courseId}');
		var courseId = '${param.courseId}';
		var url = '${ctx}/questionType/queryByCourseId';
		$('#book').combogrid('setValue', '${param.bookId}');
	}
	
    $('#newSkeleton').click(function(){
    	var courseId = $('#course').combogrid('getValue');
    	var bookId = $('#book').combogrid('getValue');
    	if(courseId == ''){
    		$.messager.alert('提示','请选择课程!', 'error');
    		return false;
    	}
    	if(bookId == ''){
    		$.messager.alert('提示','请选择教材!', 'error');
    		return false;
    	}
    	var courseCode = $('#course').combogrid('grid').datagrid('getSelected').code;
    	var name = encodeURIComponent($('input[name="name"]').val());
    	window.location.href="${ctx}/paperSkeleton/redirect/paperSkeleton?courseId="+courseId+"&courseCode="+courseCode+"&bookId="+bookId+"&name="+name;
    });
});  
function onCourseChange(){
	var courseId = $('#course').combogrid('getValue');
	var course = $('#course').combogrid('grid').datagrid('getSelected');
	if(course != null){
		$('input[name="name"]').val(course.code + '-' + getCurDateStr());
	}
	
	$('#book').combogrid('clear');
	$('#book').combogrid('grid').datagrid('load', {'courseId':courseId});
}

function onCourseLoadSuccess(){
	var allCourses = $('#course').combogrid('grid').datagrid('getRows');
	if(allCourses.length == 1){
		$('#course').combogrid('setValue',allCourses[0].id);
	}
}

function onBookLoadSuccess(){
	var allBooks = $('#book').combogrid('grid').datagrid('getRows');
	if(allBooks.length == 1){
		$('#book').combogrid('setValue',allBooks[0].id);
	}
}

function getSelect(rowIndex, rowData){
	//获取章节
	var chapterSize = rowData.chapterSize;
	$('#chapterSize').val(chapterSize);
}
</script>
</head>
<body>
    <%@ include file="../common/header.jsp"%>
    <div id="content" class="content">
        <%@ include file="../common/sub_menu.jsp"%>
        <div class="page" id="page" >
            <div class="page-header" >新建细目表</div>
	        <div class="page-body">
	             <table class="searchTable">
	               <tr>
	                   <th>课程：</th>
	                   <td style="padding:0px 5px;">
	                       <input id="course" name="course" class="easyui-combogrid" style="width:300px;" 
	                           data-options="url:'${ctx}/course/queryByCurrentUser',
		                                   method:'get',
		                                   panelHeight:'160',
		                                   idField: 'id',
		                                   textField: 'showName',
		                                   columns: [[
						                    {field:'code',title:'code',width:30},
						                    {field:'name',title:'课程名称',width:160}
						                    ]],
						                    fitColumns: true,
						                    editable:false,
						                    onLoadSuccess: onCourseLoadSuccess,
						                    onChange: onCourseChange                    
		                                    "/>
                       </td>
                       <th>教材：</th>
	                   <td style="padding:0px 5px;">
                           <input id="book" name="book" class="easyui-combogrid" style="width:300px;" 
                               data-options="url:'${ctx}/book/queryByCourseId',
	                                         method:'get',
	                                         panelHeight:'160',
	                                         idField: 'id',
	                                         textField: 'name',
	                                         columns: [[
	                                         {field:'id',title:'id',width:40},
	                                         {field:'code',title:'编号',width:40},
	                                         {field:'name',title:'名称',width:160},
	                                         {field:'author',title:'编者',width:40},
	                                         {field:'press',title:'出版社',width:100},
	                                         {field:'version',title:'版次',width:40}
	                                         ]],
	                                         fitColumns: true,
						                     editable:false,
	                                         onSelect: getSelect,
	                                         onLoadSuccess: onBookLoadSuccess	                                         
	                                          "/>
							<input id="chapterSize" name="chapterSize" type="hidden"/>
                       </td>
                  </tr>
                  <tr>
                       <th>细目表名称：</th>
	                   <td style="padding:0px 5px;"><input name="name" style="width:300px;" placeholder="请输入细目表名称" value="<c:out value='${param.name}'/>"/></td>
	                   <td colspan="2" align="center"><button class="btn" id="newSkeleton">新建</button></td>
	              </tr>
	             </table>
	             <div style="clear:both;"></div>
	    </div>
      </div>
    </div>
</body>
</html>
        