<%@ page contentType="text/html; charset=UTF-8"
	import="java.io.StringReader" import="java.util.*"
	import="java.util.regex.*"
	import="javax.swing.text.html.parser.ParserDelegator"
	import="java.net.URLEncoder" import="kr.co.kihyun.lang.MString"
	import="kr.co.kihyun.lang.Encoder" import="kr.co.kihyun.lang.MLong"
	import="kr.co.kihyun.text.html.CallbackHandler"
	import="kr.co.kihyun.text.html.HtmlUtil"
	import="kr.co.kihyun.text.html.TagFilter"
	import="kr.co.kihyun.text.html.ServletUtil"
	import="kr.co.kihyun.moumi.Moumi"
	import="kr.co.kihyun.moumi.report.MReport"
	import="kr.co.kihyun.moumi.doc.MDoc"
	import="kr.co.kihyun.moumi.doc.table.MTable"
	import="kr.co.kihyun.moumi.doc.table.MTableAnalysis"
	import="kr.co.kihyun.moumi.doc.table.TableCreation"
	import="kr.co.kihyun.moumi.doc.table.TableDefine"
	import="kr.co.kihyun.moumi.doc.table.item.MItem"
	import="kr.co.kihyun.beans.totsys.doc.Doc"
	import="kr.co.kihyun.beans.totsys.doc.table.item.ItemList"
	import="javax.jdo.PersistenceManager" import="javax.jdo.Transaction"
	import="kr.co.kihyun.beans.entity.util.*"
	import="kr.co.kihyun.beans.entity.TotDoc"%>
<%@ include file="/totsys/common/inc/sec/secure.inc.jsp"%>
<%
PersistenceManager pm = new MPersistenceManager(PMF.get().getPersistenceManager());
try{
	/********** parameter **********/	
	Long docID = MLong.parseLong(request.getParameter("docID"),null);	
	String strPage = clearXSS(request.getParameter("strPage"),"");
	String findOption = clearXSS(request.getParameter("findOption"),"");
	String findWord = clearXSS(request.getParameter("findWord"),"");
	String tableTypes = clearXSS(request.getParameter("tableTypes"),"");

	/********** doc values **********/
	TotDoc totDoc = pm.getObjectById(TotDoc.class, docID);

	/****** 서브 집계 문서인지 판단 ******/
	if(totDoc.getMasterTotDoc() != null && !docID.equals(totDoc.getMasterTotDoc())){
		String connURL="./doc_view.jsp?docID="+docID+"&strPage="+strPage+"&findOption="+findOption+"&findWord="+findWord;
		out.println(ServletUtil.alert("서브 집계문서 입니다.\\n입력양식 수정 권한이 없습니다."));
		out.println(ServletUtil.redirect(connURL));	
		return;
	}

	String docName = totDoc.getName();
	String docForm = TagFilter.convertPAll(totDoc.getForm());
	int tableCount = HtmlUtil.getTableCount(docForm);

	session.setAttribute("docForm", docForm);

	ItemList itemList = new ItemList();
	itemList.executeQuery(docID);	
	int itemCount = itemList.getCount();
	String[] itemNameList = itemList.getNameList();
	String[] itemDescList = itemList.getDescList();
	String[] itemExprList = itemList.getExprList();
	int[] itemTypeList = itemList.getTypeList();
	int[] itemNumList = itemList.getNumList();
	int[] itemSizeList = itemList.getSizeList();
	int[] tableNumList = itemList.getTableNumList();
	int[] colNumList = itemList.getFieldNumList();
	int[] rowNumList = itemList.getRecordNumList();
	int[] itemBNWList = itemList.getBNWList();
	String[] itemTypeNameList = itemList.getTypeNameList();
	
	StringBuffer dataBuf = new StringBuffer();
	for(int i=0; i < itemCount; i++){
		dataBuf.append("\n\t\t\t");		
		dataBuf.append("totForm.itemList["+i+"].itemName='"+itemNameList[i]+"';\n\t\t\t");
		dataBuf.append("totForm.itemList["+i+"].size='"+itemSizeList[i]+"';\n\t\t\t");
		dataBuf.append("totForm.itemList["+i+"].tableNum='"+tableNumList[i]+"';\n\t\t\t");
		dataBuf.append("totForm.itemList["+i+"].colNum='"+colNumList[i]+"';\n\t\t\t");
		dataBuf.append("totForm.itemList["+i+"].rowNum='"+rowNumList[i]+"';\n\t\t\t");
		dataBuf.append("totForm.itemList["+i+"].desc='"+itemDescList[i].replaceAll("'","\\'")+"';\n\t\t\t");
		dataBuf.append("totForm.itemList["+i+"].expr='"+itemExprList[i].replaceAll("'","\\'")+"';\n\t\t\t");		
		dataBuf.append("totForm.itemList["+i+"].type= '"+itemTypeNameList[i]+"';\n\t\t\t");
		dataBuf.append("\n");
	}
	TableDefine tableDefine = new TableDefine();
	docForm = tableDefine.insertDiv(docForm, itemNumList);
	String tableForm = MReport.getRemoveErrTable(docForm);
	tableForm = TagFilter.tdTextToSpace(tableForm);
	%>

<script src="/totsys/common/js/formConfig.js"></script>
<script src="/totsys/common/js/drag.js"></script>
<script language='JavaScript'>
	var totForm = new TotForm('totForm', <%= tableCount %>, <%= itemCount %>);
	var defItemSize = '<%= totDoc.getTotTables().iterator().next().getItemSize() %>';

	<%= dataBuf.toString() %>

	function setDform() {
		dform.itemNames.value = '';
		dform.itemSizes.value = '';
		dform.itemDescs.value = '';				
		dform.itemExprs.value = '';
		dform.itemTypes.value = '';
		dform.colNums.value = '';
		dform.rowNums.value = '';
		dform.tableNums.value = '';
		var item_count = 0;
		for (i=0;i<totForm.itemCount;i++){
			if(totForm.itemList[i].itemName != undefined){
				dform.itemNames.value  = dform.itemNames.value  + i + ':' + totForm.itemList[i].itemName + '<%= MItem.LIST_DELIM %>';
				dform.itemSizes.value  = dform.itemSizes.value  + i + ':' + totForm.itemList[i].size + '<%= MItem.LIST_DELIM %>';
				dform.itemDescs.value  = dform.itemDescs.value  + i + ':' + totForm.itemList[i].desc + '<%= MItem.LIST_DELIM %>';
				dform.itemExprs.value  = dform.itemExprs.value  + i + ':' + totForm.itemList[i].expr + '<%= MItem.LIST_DELIM %>';
				dform.itemTypes.value  = dform.itemTypes.value  + i + ':' + totForm.itemList[i].type + '<%= MItem.LIST_DELIM %>';
				dform.colNums.value    = dform.colNums.value    + i + ':' + totForm.itemList[i].colNum + '<%= MItem.LIST_DELIM %>';
				dform.rowNums.value    = dform.rowNums.value    + i + ':' + totForm.itemList[i].rowNum + '<%= MItem.LIST_DELIM %>';
				dform.tableNums.value  = dform.tableNums.value  + i + ':' + totForm.itemList[i].tableNum + '<%= MItem.LIST_DELIM %>';
				item_count ++;
			}
		}
		dform.tableDefines.value = '';
		for(i=0; i<<%= tableCount %>; i++){
			dform.tableDefines.value = dform.tableDefines.value + (i+1) +':rowdiv:rowdiv<%= MItem.LIST_DELIM %>';
		}		
		return item_count;
	}

	function preview() {
		if(!totForm.isValid()){
			alert("입력셀이 하나이상 존재해야됩니다.     \n문서등록을 더이상 진행하실수 없습니다.     ");
			return;
		}
		if(totForm.exprCheck()) return; //수식에 오류가 있다면 정지해라~~
		setDform();

		var farwindow = null;
		//farwindow = window.open('','preview','width=800,height=600,left=0,top=0,status=no,scrollbars=yes,resizable=yes');
		dform.submit();
		//farwindow.focus();
	}
	
	function configStart(){
		for(i=0; i<totForm.itemCount; i++)
			totForm.reShowValue(i);
	}

</script>
<body onload="configStart();">
<tr>
	<td height="31" valign="top">
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
		<tr>
			<td></td>
			<td></td>
			<td width="149"></td>
			<td width="331"></td>
		</tr>
		<tr>
			<td colspan="4">
			<table width="100%" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td valign="top"
						style="padding-left: 20; padding-right: 30; padding-top: 20">
					<!-------------------------------------- 업로드한 표 보기 start -------------------------------------->
					<%= TableCreation.insertTypeButtons("totForm", docForm, tableTypes) %>
					</td>
				</tr>
			</table>
			</td>
		</tr>
		<!---- 양식 셀 정의 끝 ---->
	</table>
	</td>
</tr>

<!---- 버튼(이전단계/다음단계) 시작 ---->
<tr>
	<td height="50" align="center">
	<table height="25" border="0" cellpadding="0" cellspacing="0">
		<tr>
			<!--td><a href="#" onMouseOut="MM_swapImgRestore()"
				onMouseOver="MM_swapImage('bt_pre','','/totsys/common/inc/documents/tot_doc/images/bt_pre.gif',1)"><img
				onclick="history.go(-2);"
				src="/totsys/common/inc/documents/tot_doc/images/bt_pre.gif"
				name="bt_pre" border="0"></a></td>
			<td width="5">&nbsp;</td-->
			<td><a href="#" onMouseOut="MM_swapImgRestore()"
				onMouseOver="MM_swapImage('bt_preview','','/totsys/common/inc/documents/tot_doc/images/bt_preview.gif',1)"><img
				onclick="preview();"
				src="/totsys/common/inc/documents/tot_doc/images/bt_preview.gif"
				name="bt_preview" border="0"></a></td>
		</tr>
	</table>
	</td>
</tr>
<form name="dform" method="post"
	action="/totsys/common/web/documents/tot_doc/sysadm_modify_inform_preview.jsp">
<input type='hidden' name='docID' value='<%= docID %>'> <input
	type='hidden' name='docName' value='<%= docName %>'> <input
	type='hidden' name='docGroupID'
	value="<%= totDoc.getType().ordinal() %>"> <input type='hidden'
	name='tableCount' value="<%= tableCount %>"> <input
	type='hidden' name='itemNames'> <input type='hidden'
	name='itemSizes'> <input type='hidden' name='itemDescs'>
<input type='hidden' name='itemExprs'> <input type='hidden'
	name='itemTypes'> <input type='hidden' name='colNums'>
<input type='hidden' name='rowNums'> <input type='hidden'
	name='tableNums'> <input type='hidden' name='tableDefines'>
<input type='hidden' name='mode' value='modify'> <input
	type="hidden" name="tmpDefItemSize"> <input type="hidden"
	name="defItemSize" value="3"></form>
<!---- 버튼(미리보기/취소) 끝 ---->
</body>
<%
}catch(Exception ex){
	ex.printStackTrace();
	out.println(kr.co.kihyun.text.html.ServletUtil.getJavaScript("location='/servlet/kr.co.kihyun.beans.user.HttpSSOLogin?mode=logout';"));
}finally{
	pm.close();
}
%>
