<%@ page contentType="text/html; charset=UTF-8"
	import="java.net.URLEncoder"
	import="kr.co.kihyun.lang.Encoder" import="java.net.URLDecoder"
	import="kr.co.kihyun.lang.MString" import="kr.co.kihyun.lang.MLong"
	import="kr.co.kihyun.text.excel.Excel"
	import="kr.co.kihyun.text.html.HtmlUtil"
	import="kr.co.kihyun.lang.StringConverter"
	import="kr.co.kihyun.text.html.ServletUtil"
	import="kr.co.kihyun.moumi.Moumi"
	import="kr.co.kihyun.moumi.report.MReport"
	import="kr.co.kihyun.beans.user.Dept"
	import="kr.co.kihyun.beans.user.UserView"
	import="kr.co.kihyun.beans.totsys.doc.table.item.ItemList"
	import="kr.co.kihyun.beans.totsys.report.Report"
	import="kr.co.kihyun.beans.totsys.report.ReportView"
	import="kr.co.kihyun.beans.totsys.report.ReportViewManager"
	import="kr.co.kihyun.beans.totsys.report.ReportFormElement"
	import="kr.co.kihyun.text.hml.Hwp"
	import="kr.co.kihyun.text.html.HtmlUtil"
	import="kr.co.kihyun.text.html.TagFilter"
	import="javax.jdo.PersistenceManager" import="javax.jdo.Transaction"
	import="kr.co.kihyun.beans.entity.util.*"
	import="kr.co.kihyun.beans.entity.*"%>

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

<%
	PersistenceManager pm = new MPersistenceManager(PMF.get().getPersistenceManager());
	Transaction tx = pm.currentTransaction();
	try {
		/********* Parameter *********/
		Long reportID = MLong.parseLong(request.getParameter("reportID"),null);

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

		tx.begin();

		/********* report *********/
		ReportViewManager reportViewManager = new ReportViewManager(totReport.getTotDoc());
		reportViewManager.setReportData(totReport.getDept(pm).getId(), totReport.getUser(pm).getId());
		String docName = totReport.getTotDoc().getName();

		tx.rollback();

		ReportFormElement reportFormElement = new ReportFormElement(totReport.getTotDoc());
		String Form = reportFormElement.getForm();
		ItemList itemTypes = reportFormElement.getItemList();
		int[] tmpItemTypeList = itemTypes.getTypeList();

		String report = reportViewManager.getReportNBSP();
		String savePath = Moumi.getRepoPerRoot().getPath() + "/fileUpload";

		response.setHeader("Accept-Ranges", "bytes");
		response.setHeader("Content-Disposition",
				"attachment; filename="
						+ (URLEncoder.encode(docName, "UTF-8")).replaceAll(":", "_").replace("+", "%20")
						+ ".xls;");

		/*********** HWP save  ***********/
		Hwp hwp = new Hwp();
		hwp.setFileName(docName);
		StringBuffer hwpSaveBuf = new StringBuffer(0);
%>

<html>
<head>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
	<tr>
		<!---- 본문  시작---->
		<td valign="top" style="padding-left: 25; padding-right: 25;">
		<%
			hwpSaveBuf.append("<Table>");
			hwpSaveBuf.append("<tr><td><P><TEXT>");

			StringBuffer htm = null;
			htm = new StringBuffer();
			htm.append(report);

			int tag_textinput_idx = 0;

			while (true) { //자료입력창 한글에서 박스로 안묶이고 보이게 하기.
				tag_textinput_idx = htm.indexOf("<table width=700 border=0><tr><td width='100%' align=left>");
				if (tag_textinput_idx != -1) {
					int serch_textinput = htm.indexOf("</td></tr></table>", tag_textinput_idx);
					htm = htm.replace(serch_textinput, serch_textinput + 18, "</TEXT></P></td></tr><tr><td><P><TEXT>");
					htm = htm.replace(tag_textinput_idx, tag_textinput_idx + 58, "</TEXT></P></td></tr><tr><td><P><TEXT>");
				} else
					break;
			}

			tag_textinput_idx = 0;

			while (true) {
				tag_textinput_idx = htm.indexOf("<table width=700 border=0>");
				if (tag_textinput_idx != -1) {
					int serch_textinput = htm.indexOf("</td></tr></table>",	tag_textinput_idx);
					htm = htm.replace(serch_textinput,serch_textinput + 18,	"</TEXT></P></td></tr><tr><td><P><TEXT>");
					htm = htm.replace(tag_textinput_idx, tag_textinput_idx + 71, "</TEXT></P></td></tr><tr><td><P><TEXT>");
				} else
					break;
			}

			String tmphwp = (htm.toString()).replaceAll("</table>", "</table></td></tr><tr><td><P><TEXT>");
			tmphwp = tmphwp.replaceAll("<table", "</TEXT></P></td></tr><tr><td><table");

			hwpSaveBuf.append(tmphwp);

			hwpSaveBuf.append("</TEXT></P></td></tr>");
			hwpSaveBuf.append("</table>");
			hwp.inputStreamHWP((hwpSaveBuf.toString()));
		%> <%=Excel.adjustFmlaToExcel(TagFilter.removeBlankPara(Excel.setCellTypes(report,tmpItemTypeList)))%></td>
	</tr>
</table>
</body>
</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 {
		pm.close();
	}
%>
