
<%
/************************************************************@@
  * Program Name	: report_regist.jsp
  * Description		: /보고자/내문서함/임시보고함/보고자료 변경
  * Author				: 강원중
  * Create Date		: 2004-11-06
  * History				:
@@************************************************************/
%>

<%@ page contentType="text/html; charset=UTF-8" import="java.net.*"
	import="java.net.URLEncoder" import="javax.jdo.PersistenceManager"
	import="javax.jdo.Transaction"
	import="kr.co.kihyun.beans.entity.util.*"
	import="kr.co.kihyun.beans.entity.*"
	import="kr.co.kihyun.beans.user.HttpSSOLogin"
	import="kr.co.kihyun.util.MUtil" import="kr.co.kihyun.lang.MString"
	import="kr.co.kihyun.lang.MLong" import="kr.co.kihyun.lang.Encoder"
	import="kr.co.kihyun.lang.MInteger"
	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.report.MReportData"
	import="kr.co.kihyun.moumi.report.stat.StaticMReport"
	import="kr.co.kihyun.moumi.doc.table.MTable"
	import="kr.co.kihyun.moumi.doc.table.item.MItem"
	import="kr.co.kihyun.moumi.report.MReportData"
	import="kr.co.kihyun.beans.totsys.report.ReportModifyManager"
	import="kr.co.kihyun.beans.totsys.report.ReportView"
	import="kr.co.kihyun.beans.totsys.report.ReportDataSet"
	import="kr.co.kihyun.text.javascript.JavaScriptUtil"%>

<%@ include file="/totsys/common/inc/sec/secure.inc.jsp"%>

<%
PersistenceManager pm = new MPersistenceManager(PMF.get().getPersistenceManager());
Transaction tx = pm.currentTransaction();
try{
	/********** session내의 userId **********/
	String usID = HttpSSOLogin.getLoginID(request);
	String dtID = HttpSSOLogin.getDeptID(request);
	String dpName = HttpSSOLogin.getDeptName(request);
	String savePath = Moumi.getRepoPerRoot().getPath() + "/fileUpload";

	/********* Parameter *********/
	Long reportID = MLong.parseLong(request.getParameter("reportID"),null);
	int rowCount = MInteger.parseInt(request.getParameter("rowCount"), 1);
	int delRowNum = MInteger.parseInt(request.getParameter("delRowNum"), 1);
	String modifyMode = clearXSS(request.getParameter("modifyMode"),"");
	String copyForm = MString.checkNull( Encoder.toJava(request.getParameter("copyForm")) );
	String tableRows =  MString.checkNull( Encoder.toJava(request.getParameter("tableRows")) );

	TotReport totReport = pm.getObjectById(TotReport.class, reportID);

	if(tableRows.equals("") || tableRows==null){
		String datas = JavaScriptUtil.toCRLF(totReport.getData());
		int[] tableRowList = MReportData.getRowNumList(datas);

		for(int r=0; r<tableRowList.length; r++){
			if(r==0)
				tableRows=Integer.toString(tableRowList[r]);
			else
				tableRows=tableRows+","+Integer.toString(tableRowList[r]);
		}
	}

	tx.begin();

	/********* report *********/
	ReportModifyManager reportModifyManager = new ReportModifyManager(request, totReport.getTotDoc(), totReport.getDept(pm).getId(), totReport.getUser(pm).getId(), rowCount, tableRows);
	String data = totReport.getData();
	String realFileName = "";
	String viewFileName = "";
	for (String fileName : totReport.getAttachments().keySet()) {
		viewFileName += fileName + ";";
		realFileName += fileName + ";";
	}
	String comment = MString.checkNull(totReport.getNote());

	tx.rollback();

	int tableType = reportModifyManager.getTableType();
	String tableTypes = reportModifyManager.getTableTypes();
	rowCount = reportModifyManager.getRowCount();
	String insertForm = reportModifyManager.getInsertForm();
	String baseInsertForm = reportModifyManager.getInsertForm();
	String putValScript = reportModifyManager.getPutValScript();
	String changeAllScript = reportModifyManager.getChangeAllScript();
	String inDataJavaScript = reportModifyManager.getInDataJavaScript();

	if(tableTypes.indexOf("1")==-1){
		tableType = MTable.STATIC;
	}else{
		tableType = MTable.DYNA;
	}

	if(MString.isNull(data)){
		out.println(ServletUtil.alert("잘못된 작업 진행입니다.\\n 로그인화면으로 이동합니다."));
		out.println(ServletUtil.getJavaScript("javascript:location.replace('/servlet/kr.co.kihyun.beans.user.HttpSSOLogin?mode=logout');"));
		return;
	}
	String addJavaScript = "";
	String hiddenData = "";
	if(tableType == MTable.DYNA){
		addJavaScript = reportModifyManager.getAddJavaScript();
		hiddenData = reportModifyManager.getHiddenData();
	}
reportModifyManager=null;
	session.setAttribute("modifySuccTargetURI", "/totsys/repoper/mydocbox/tempbox/report_list.jsp");

	if(!copyForm.equals("")){
		ReportDataSet reportDataSet = new ReportDataSet();
		insertForm = reportDataSet.setForm(insertForm,copyForm);
		if(reportDataSet.getChange()==0){%>
<script>alert("입력하신 양식이 등록양식과 같지 않거나 오류가 존재합니다.")</script>
<%}else{
			inDataJavaScript="";
		}
	}

	String[] svrFilenameList = realFileName.split(";");
	%>

<HTML>
<HEAD>
<TITLE><%=Moumi.getTitle()%></TITLE>
<link rel="stylesheet" href="/totsys/common/css/kecttep.css"
	type="text/css">
<script src="/totsys/common/js/rollover.js"></script>
<script src='/totsys/common/js/checkNum.js'></script>
<script src="/totsys/common/js/util.js"></script>
<script src="/totsys/common/js/calendar.js"></script>
<script src="/totsys/common/js/drag.js"></script>
<script src="/totsys/common/js/popup.js"></script>
<script language="javascript">
			num = <%= svrFilenameList.length %>;
			usrFilenames = "<%= viewFileName %>";
			svrFilenames = "<%= realFileName %>";
			fileInfos = "";
			userFilePath = "";

			function checkFile(Filename, Filename2){
				mkFileList(Filename, Filename2, 'add');
				num++;
			}

			function mkFileList(Filename, Filename2, size, mode) {
				var tmpSvrFilenames = "";
				var tmpUsrFilenames = "";
				var tmpFileInfos = "";
				//var nList4 = "";

				if (!mode){
					mode = "add";
				}

				subFilename = Filename;
				subFilename2 = cut_str( 1, Filename2, "/");
				subinfo = size;

				usrFilenameList = usrFilenames.split(";", num);
				svrFilenameList = svrFilenames.split(";", num);
				fileInfoList = fileInfos.split(";", num);

				if (mode == "add") {
					if (svrFilenames && usrFilenames) {
						for (i=0; i<num; i++) {
							tmpSvrFilenames += svrFilenameList[i] + ";";
							tmpUsrFilenames += usrFilenameList[i] + ";";
							tmpFileInfos += fileInfoList[i] + ";";
						}
						tmpSvrFilenames += subFilename;
						tmpUsrFilenames += subFilename2;
						tmpFileInfos += subinfo;
					} else {
						tmpSvrFilenames = subFilename;
						tmpUsrFilenames = subFilename2;
						tmpFileInfos = subinfo;
					}
				} else {
					for (i=0; i<num; i++) {
						if (subFilename == svrFilenameList[i])
							continue;
						else {
							tmpSvrFilenames += svrFilenameList[i] + ";";
							tmpUsrFilenames += usrFilenameList[i] + ";";
							tmpFileInfos += fileInfoList[i] + ";";
						}
					}
				}

				usrFilenames = tmpUsrFilenames;
				svrFilenames = tmpSvrFilenames;
				fileInfos = tmpFileInfos;
			}

			function cut_str( state, value, separator ) {
				if (!separator){
					separator = ".";
				}

				if ( state == 0 ){
					return value.substring(0, value.lastIndexOf(separator));
				}

				if ( state == 1 ){
					return value.substring(value.lastIndexOf(separator)+1, value.length );
				}
			}

			function delFrm() {
				var list = document.subUpload.delFrm.fileList;
				var a = list.options[list.selectedIndex].value;
				var no = -1;

				usrFilenameList = usrFilenames.split(";", num);
				svrFilenameList = svrFilenames.split(";", num);

				for(i=0; i<num; i++) {
					if (a == svrFilenameList[i]) {
						no = i;
						mkFileList(svrFilenameList[i], usrFilenameList[i], 0,"delete");
					}
				}

				if (no != -1) {
					if (num > 0){
						num--;
					}

					document.subUpload.delFrm.submit();
				}
			}

			<%= putValScript %>
			function changeAll(){
				<%= changeAllScript %>
			}

			function insertData(){
				<%= inDataJavaScript.toString() %>
			}

			/********** userfile list와 serverfile list를 배열로 저장 **********/
			function viewFrm(num) {
				name = "fileUp"+num;
				eval(name + ".style.visibility = \"visible\";" );
				eval(name + ".style.display    = \"block\";" );
				//dform.upFile.focus();
			}

			function viewNote(){
				var width = 600;
				var height = 450;
				var leftpos = (screen.width - width) / 2;
				var toppos = (screen.height - height) / 2;
				window.open("/totsys/common/web/note/popup_note.jsp?docID=<%=totReport.getTotDoc().getId()%>","","width="+width+", height="+height+", toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,left= "+ leftpos +" ,top= "+ toppos);
			}

			function deptCheck(){
				var target = 'deptCheck';
				var width = 385;
				//var height = 221;
				var height = 271;
				var align = 'center';
				var valign = 'middle';
				var scroll = 'no';
				var resize = 'no';
				<%if (changeAllScript.length() > 0) {%>
					changeAll();
				<%}%>

				changeAll();

				document.dform.usrFilenames.value = usrFilenames;
				document.dform.svrFilenames.value = svrFilenames;

				dform.submit();
			}

			function totApply(){
				totApplyForm.submit();
			}

			function ShowCopy(){
				window.open('./popup_copy_form.jsp','makecopy','width=950,height=595,scrollbars=no,resizable=no');
			}

		<%if(tableType == MTable.DYNA){%>
			//동적 표에서만 사용
			function insertData(){
				<%= inDataJavaScript %>
			}

			function lineAdd(){
				<%= addJavaScript %>
				modifyForm.rowCount.value='<%= (rowCount+2) %>';
				modifyForm.modifyMode.value='addRow';
				modifyForm.submit();
			}

			function lineAddNum(){
				var tmp = document.modifyForm.addRowNum.value;
				var tr = document.modifyForm.tableRows.value;
				var st = document.modifyForm.tables.value;
				if(tmp == ''){
					alert('추가할 라인 수를 입력하세요!');
					modifyForm.addRowNum.focus();
					return;
				}else if(isNaN(tmp)){
					alert('숫자를 입력하세요!');
					document.modifyForm.addRowNum.value = '';
					modifyForm.delRowNum.focus();
					return;
				}else if(1 > parseInt(tmp)){
					alert('1이상의 값을 입력 하시기 바랍니다.');
					document.modifyForm.addRowNum.value = '';
					modifyForm.delRowNum.focus();
					return;
				}

				var trlist = tr.split(",");
				trlist[parseInt(st)-1] = parseInt(tmp)+parseInt(trlist[parseInt(st)-1]);

				for(i=0; i<trlist.length; i++){
					if(i==0) tr=trlist[i];
					else tr=tr+","+trlist[i];
				}

				var num = parseInt(tmp)+parseInt('<%= (rowCount) %>');

				<%= addJavaScript %>
				modifyForm.rowCount.value=num;
				modifyForm.modifyMode.value='addRow';
				modifyForm.tableRows.value=tr;
				modifyForm.submit();
			}

			function lineDel(){
				var tmp = document.modifyForm.delRowNum.value;
				var tr = document.modifyForm.tableRows.value;
				var st = document.modifyForm.tables.value;
				var trlist = tr.split(",");

				if(tmp == ''){
					alert('삭제할 라인을 입력하세요!');
					modifyForm.delRowNum.focus();
					return;
				}else if(isNaN(tmp)){
					alert('숫자를 입력하세요!');
					document.modifyForm.delRowNum.value = '';
					modifyForm.delRowNum.focus();
					return;
				}else if(1 > parseInt(tmp)){
					alert('1이상의 값을 입력 하시기 바랍니다.');
					document.modifyForm.delRowNum.value = '';
					modifyForm.delRowNum.focus();
					return;
				}else if(parseInt(tmp) > parseInt(trlist[parseInt(st)-1])){
					alert('현재보여진 라인보다 작은값 입력!');
					document.modifyForm.delRowNum.value = '';
					modifyForm.delRowNum.focus();
					return;
				}

				trlist[parseInt(st)-1] = parseInt(trlist[parseInt(st)-1])-1;

				for(i=0; i<trlist.length; i++){
					if(i==0) tr=trlist[i];
					else tr=tr+","+trlist[i];
				}

				<%= addJavaScript %>
				modifyForm.rowCount.value='<%= (rowCount-1) %>';
				modifyForm.modifyMode.value='delRow';
				modifyForm.tableRows.value=tr;
				modifyForm.submit();
			}
		</script>
<%}%>
</HEAD>

<body style='' BGCOLOR="#FFFFFF" LEFTMARGIN="0" TOPMARGIN="0"
	MARGINWIDTH="0" MARGINHEIGHT="0" onload="insertData();">
<table width="100%" height="70" border="0" cellpadding="0"
	cellspacing="0">
	<jsp:include page="/totsys/common/inc/repoper/top/top.jsp" flush="true" />
	<tr>
		<td nowrap width="0"></td>
		<td valign="top">
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<jsp:include page="/totsys/common/inc/title/title.jsp" flush="true" />
			<tr>
				<td>
				<table width="100%" border="0" cellpadding="0" cellspacing="0">
					<tr>
						<td width="537"><a
							href="/totsys/repoper/mydocbox/tempbox/report_list.jsp"><img
							src="/totsys/common/images/bt_doc_list.gif" border="0"></a></td>
						<td align="right">&nbsp;</td>
					</tr>
				</table>
				</td>
			</tr>
			<!----  topbar  버튼 끝---->

			<!--1픽셀 띄우기-->
			<tr>
				<td height="1"></td>
			</tr>

			<!--파란색 밑줄-->
			<tr>
				<td bgcolor="6891cb" height="2"></td>
			</tr>

			<tr>
				<!---- 내용---->
				<td height="31" valign="top">
				<table width="100%" border="0" cellspacing="0" cellpadding="0">
					<tr>
						<td colspan="4" valign="top">
						<table width="100%" border="0" cellspacing="0" cellpadding="0">
							<tr>
								<td height="50" colspan="4" valign="top">
								<table width="100%" border="0" cellspacing="0" cellpadding="0">
									<tr>
										<td valign="top">
										<table width="100%" border="0" cellspacing="0" cellpadding="0">
											<tr>
												<td valign="top"
													style="padding-top: 25; padding-bottom: 25; padding-left: 25; padding-right: 25;">
												<!---- 행추가, 행삭제 시작 ---->
												<table width="100%" border="0" cellspacing="0"
													cellpadding="0">
													<!---- 행추가, 행삭제 시작 ---->
													<%if(tableType == MTable.DYNA){%>
													<tr>
														<td>
														<table width="100%" border="0" cellspacing="0"
															cellpadding="0">
															<form name="modifyForm" method="post"
																action="./report_modify_temp.jsp"><input
																type="hidden" name="comment" value="<%= comment %>">
															<input type="hidden" name="reportID"
																value="<%= reportID %>"> <input type="hidden"
																name="rowCount" value="<%= rowCount %>"> <input
																type="hidden" name="inForm"
																value='<%= URLEncoder.encode(baseInsertForm, "UTF-8") %>'>
															<input type="hidden" name="tableType"
																value="<%= tableType %>"> <input type="hidden"
																name="modifyMode" value="addRow"> <input
																type="hidden" name="tableRows" value="<%= tableRows%>">
															<%= hiddenData %>
															<tr>
																<td width="80" align="right"><select name="tables">
																	<%
																									String[] tableDiv = tableTypes.split(",");
																									for(int i=0; i<tableDiv.length; i++){
																										if(tableDiv[i].equals("1")){%>
																	<option value="<%=i+1%>"><%=i+1%></option>
																	<%}
																									}
																									%>
																</select>번 표</td>
																<td width="5">&nbsp;</td>
																<td width="62" align="right"
																	background="/totsys/common/images/bt_line_addbg.jpg">
																<input name="addRowNum" type="text" size="3"
																	maxlength="2" class="inputtxt"></td>
																<td width="47"><img onclick="lineAddNum();"
																	style="cursor: hand;"
																	src="/totsys/common/images/bt_line_add.jpg" width="47"
																	height="22" border="0"></td>
																<td width="5">&nbsp;</td>
																<td width="62" align="right"
																	background="/totsys/common/images/bt_line_delbg.jpg">
																<input name="delRowNum" type="text" size="3"
																	maxlength="2" class="inputtxt"></td>
															</form>
															<td width="47"><img onclick="lineDel();"
																style="cursor: hand;"
																src="/totsys/common/images/bt_line_del.jpg" width="47"
																height="22" border="0"></td>
															<td width="*">&nbsp;</td>
															</tr>
															</form>
														</table>
														</td>
													</tr>
													<tr>
														<td nowrap height="20"></td>
													</tr>
													<%}%>
													<!---- 행추가, 행삭제 끝---->
													<% tx.begin(); %>
													<tr>
														<td>
														<form name="dform" method="post"
															action="/servlet/kr.co.kihyun.beans.totsys.report.HttpReportModifyer"
															enctype="multipart/form-data"><input type="hidden"
															name="reportID" value="<%= reportID %>"> <input
															type="hidden" name="rowCount" value="<%= rowCount %>">
														<input type="hidden" name="inForm"
															value='<%= URLEncoder.encode(baseInsertForm, "UTF-8") %>'>
														<input type="hidden" name="tableType"
															value="<%= tableType %>"> <input type="hidden"
															name="tableTypes" value="<%= tableTypes %>"> <input
															type="hidden" name="tableRows" value="<%= tableRows%>">
														<INPUT type="hidden" name="usrFilenames"> <INPUT
															type="hidden" name="svrFilenames"> <input
															type="hidden" name="isRecog" value="recog"> <input
															type="hidden" name="deptID"
															value="<%=totReport.getDept(pm).getId()%>"> <input
															type="hidden" name="repUserID"
															value="<%=totReport.getUser(pm).getId()%>"> <!-------------------------------------------- 보고 폼 시작 -------------------------------------------->
														<% out.println(insertForm); %> <!-------------------------------------------- 보고 폼 끝   -------------------------------------------->
														</td>
													</tr>
													<% tx.rollback(); %>

													<tr>
														<td align="center">
														<%if (changeAllScript.length() > 0) {%> <img
															style="cursor: hand;"
															onClick='changeAll();changeAll();changeAll();changeAll();'
															src="/totsys/common/images/bt_cal.gif" border="0">
														<%}%>
														</td>
													</tr>

													<tr>
														<td height="15"></td>
													</tr>

													<!--세미타이틀-->
													<tr>
														<td colspan="4">
														<table border="0" width="100%" cellpadding="0"
															cellspacing="0">
															<tr>
																<td style="width: 14"><img
																	src="/totsys/common/images/ico_1.gif"></td>
																<td class="kecttep_title" style="padding-left: 5"
																	align="left" width="100%">[의견 및 <%= Moumi.getMessageBundle().getString("moumi.message.file") %>]</td>
																<td>&nbsp;</td>
																<td>&nbsp;</td>
															</tr>
															<tr>
																<td height="3"></td>
															</tr>
														</table>
														</td>
													</tr>

													<tr>
														<td><textarea name="comment"
															style="width: 100%; height: 60;"><%= comment %></textarea>
														</td>
													</tr>

													<tr>
														<td><!-- 파일 첨부 시작 -->
														<div id='fileUp1'>
														<table width="100%" border="0" cellpadding="0"
															cellspacing="0">
															<!---- 첨부파일 찾아보기 ---->

															<!---- 파일 찾아보기 ---->
															<tr>
																<td colspan="2"><input type=file
																	style="width: 100%" name="upFile" class="inputtxt">
																<input type=hidden name="upFile"></td>
															</tr>
															<!---- 기존파일 / 첨부파일 삭제 ---->
															<tr>
																<%if(!MString.isNull(realFileName)){%>
																<td class="body">File #1&nbsp;&nbsp;<a class="a"
																	href="/servlet/kr.co.kihyun.text.Viewer/<%= viewFileName %>?fileName=<%= savePath %>/<%= realFileName %>"
																	target="new"><%= viewFileName %></a>&nbsp;&nbsp;&nbsp;<input
																	type=checkbox name=dropfile value=1>첨부파일 삭제</td>
																<%}else{%>
																<input type="hidden" name=delFile>
																<%}%>
															</tr>
														</table>
														</div>
														<!-- 파일 첨부 끝 --></td>
														</form>
														<!-- dform end -->
													</tr>
													<tr>
														<td height="37" align="center">
														<table border="0" cellspacing="0" cellpadding="0">
															<tr>
																<td><a href="#" onClick='ShowCopy();'><img
																	src="/totsys/common/images/bt_copyin.gif" border="0"></a></td>
																<td width="3"></td>
																<td><a href="#" onClick="return deptCheck();"><img
																	src="/totsys/common/images/bt_reportsend.gif"
																	border="0"></a></td>
																<td width="3"></td>
															</tr>
															<form name="totApplyForm" method="post"
																action="./tot_report_modify.jsp"><input
																type="hidden" name="reportID" value="<%= reportID %>">
															<input type="hidden" name="rowCount"
																value="<%= rowCount %>"> <input type="hidden"
																name="delRowNum" value="<%= delRowNum %>"> <input
																type="hidden" name="modifyMode"
																value="<%= modifyMode %>"></form>
														</table>
														</td>
													</tr>
												</table>
												</td>
											</tr>
										</table>
										</td>
									</tr>
								</table>
								</td>
								<!---- 문서내용 끝---->
							</tr>
							<!--파란색 밑줄-->
							<tr>
								<td bgcolor="6891cb" height="2" colspan="4"></td>
							</tr>
						</table>
						</td>
					</tr>
				</table>
				</td>
			</tr>

			<!--1픽셀 띄우기-->
			<tr>
				<td height="1"></td>
			</tr>

			<!----  topbar  버튼 시작---->
			<tr>
				<td>
				<table width="100%" border="0" cellpadding="0" cellspacing="0">
					<tr>
						<td width="537"><a
							href="/totsys/repoper/mydocbox/tempbox/report_list.jsp"><img
							src="/totsys/common/images/bt_doc_list.gif" border="0"></a></td>
						<td align="right">&nbsp;</td>
					</tr>
				</table>
				</td>
			</tr>
			<!----  topbar  버튼 끝---->
		</table>
		</td>
		<td nowrap width="10"></td>
	</tr>

	<!--1픽셀 띄우기-->
	<tr>
		<td height="30"></td>
	</tr>

</table>
</BODY>
<div id="SetWin" style="position: absolute; visibility: hidden; width: 100px; height: 100px; background: silver; border: 1px solid #333333; line-height: 1.4em; padding: 3px 3px"></div>
</HTML>
<%
}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{
	if(tx.isActive())
		tx.rollback();
	pm.close();
}
%>
