
<%
		/************************************************************@@
		 * Program Name	: doc_list.jsp
		 * Description	: /집계자/내문서함/등록함/문서 목록
		 * Author		: 기현테크
		 * Create Date	: 2010.11.25
		 * History		:
		@@************************************************************/
%>

<%@ page contentType="text/html; charset=UTF-8"
		 import="java.net.URLEncoder"
         import="java.net.URLDecoder"
		 import="kr.co.kihyun.lang.MString"
         import="kr.co.kihyun.lang.MLong"
		 import="kr.co.kihyun.text.html.HtmlUtil"
		 import="kr.co.kihyun.lang.StringConverter"
		 import="kr.co.kihyun.text.html.ServletUtil"
		 import="kr.co.kihyun.beans.user.HttpSSOLogin"
		 import="kr.co.kihyun.moumi.Moumi"
		 import="kr.co.kihyun.moumi.report.MReport"
     	 import="kr.co.kihyun.moumi.MoumiConfig"
		 import="kr.co.kihyun.beans.user.Dept"
		 import="kr.co.kihyun.beans.user.UserView"
		 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.text.hml.Hwp"
		 import="kr.co.kihyun.text.html.HtmlUtil"
		 import="kr.co.kihyun.beans.totsys.report.tots.ReportsViewer"
		 import="kr.co.kihyun.lang.MInteger"
		 import="kr.co.kihyun.beans.totsys.doc.table.Table"
		 import="kr.co.kihyun.beans.totsys.doc.table.item.Item"
		 import="kr.co.kihyun.text.excel.Excel"
         import="org.slf4j.Logger"
		 import="org.slf4j.LoggerFactory"
         import="kr.co.kihyun.beans.user.User"
		 import="kr.co.kihyun.text.html.TagFilter"
		 import="kr.co.kihyun.lang.MLong"
         import="javax.jdo.PersistenceManager"
		 import="javax.jdo.Transaction"
		 import="kr.co.kihyun.beans.entity.util.*"
		 import="kr.co.kihyun.beans.entity.*"%>
<%@page import="java.util.List"%>
<%@ page import="java.util.Map.Entry"%>
<%@page import="javax.jdo.Query"%>

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

<%		Logger LOG = LoggerFactory.getLogger(this.getClass());

		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);

			// TODO: extract method.
			String report = null;
			if (totReport.getSlaveTotDoc() == null) {
				report = TotReportUtil.toHTML(totReport);
			} else {
				TotDoc slaveDoc = totReport.getSlaveTotDoc();
				if (!slaveDoc.getCompReports(pm).isEmpty())
					report = TotReportUtil.toHTML(totReport.getTotDoc(), slaveDoc.getCompReports(pm));
			}
                        //report 없어도 무조건 제출진행자료페이지로 가기
			if (report == null) {
		//		out.println(ServletUtil.alert(Moumi.getMessageBundle().getString("moumi.message.popup.notReport")));
		//		out.println(ServletUtil.getJavaScript(""));
				return;
			}
			/********* report view *******/
			String note = MString.checkNull(totReport.getNote());
			String realFileName = "";
			String viewFileName = "";
			for (String fileName : totReport.getAttachments().keySet()) {
				viewFileName += fileName + ";";
				realFileName += fileName + ";";
			}
			Long downDocID = totReport.getSlaveTotDoc() == null ? null : totReport.getSlaveTotDoc().getId();

			String[] realFileNameList = realFileName.split(";");
			String[] viewFileNameList = viewFileName.split(";");

			// 연계수정
			String memo = MString.checkNull(totReport.getRecentHistory() == null ? null : totReport.getRecentHistory().getMemo());

			String url = clearXSS(request.getParameter("url"), "");

			if (url == null) {
				url = "";
			}
//			int visit = 0;
//
//			Board totperComment = totReport.getTotDoc().getBoard();

//			if (totperComment != null) {
////				tx.begin();
//				visit = totperComment.getVisit();
//				totperComment.setVisit(visit + 1);
//				pm.makePersistent(totperComment);
//				tx.commit();
//			}
%>
<html>
<head>
<link rel="stylesheet" href="/totsys/common/css/text.css" type="text/css">
<link rel="stylesheet" href="/totsys/common/css/kecttep.css" type="text/css">
<link rel="stylesheet" href="/totsys/common/css/tabs.css" type="text/css">
<link rel="stylesheet" href="/totsys/common/css/SquareButtons.css" type="text/css">
<script src="/totsys/common/js/rollover.js"></script>
<script src="/totsys/common/js/popup.js"></script>
<script type="text/javascript" src="/totsys/common/js/jquery-1.12.4.min.js"></script>
<script type="text/javascript" src="/totsys/common/js/jquery.fixFormTable.js"></script>
<script type="text/javascript">
/*
 * 테이블 각 셀 garbage 요소를 제거하여, 레이아웃을 정돈한다.
 * 2014.09.19 by YoungJun Cho.
 */
$(document).ready(function() {
    $.fixViewFormTableForXMLNameSpace($("#formDiv").find("table td").children(), {});
  //ie10,11 호환성보기 수정 - 테두리 없어지는 문제 수정
	$('#formDiv > table').attr('border', '1').css('border','1px solid #000');
	$('#formDiv span').css('line-height','1');
});
/* 자료입력 테이블 input element 뒤에 font, span 등의 불필요한 element 를 삭제처리. 끝 */
</script>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
	<tr valign="top"
		style="padding-top: 10; padding-bottom: 25; padding-left: 10;">

			<!---- 본문  시작---->
		<td>
			<%
					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;
						}
					}
			%> <!---------------------- reportForm start --------------------> <%
					if (downDocID != null) {
						tx.begin();
						TotDoc totDoc = pm.getObjectById(TotDoc.class, downDocID);
						String partID = totDoc.getDept(pm).getOrgan(pm) == null ? null : totDoc.getDept(pm).getOrgan(pm).getId();
						MDept organ = partID == null ? null : pm.getObjectById(MDept.class, partID);
						int searchType = MInteger.parseInt(request.getParameter("searchType"), -1);

						List<TotReport> reportList = totDoc.getAllReports(pm);
						List<TotReport> compReportList;
						Query q = pm.newNamedQuery(TotReport.class, "CompReports");
						q.setFilter("totDoc == tot_doc");
						q.declareParameters("kr.co.kihyun.beans.entity.TotDoc tot_doc");
						q.setOrdering("mDept");
						compReportList = (List<TotReport>) q.execute(totDoc);
						if (searchType >= 0) {
							if (searchType == TotReportProcess.COMP.ordinal()) {
								reportList = compReportList;
							} else if (searchType == TotReportProcess.ACCEPT.ordinal()) {
								q = pm.newQuery(TotReport.class, reportList);
								q.setFilter("process == kr.co.kihyun.beans.entity.TotReportProcess.ACCEPT");
								q = pm.newQuery(TotReport.class, (List<TotReport>) q.execute());
								q.setFilter("totDoc == tot_doc");
								q.declareParameters("kr.co.kihyun.beans.entity.TotDoc tot_doc");
								q.setOrdering("mDept");
								reportList = (List<TotReport>) q.execute(totDoc);
							} else {
								q = pm.newQuery(TotReport.class, reportList);
								q.setFilter("totDoc == tot_doc");
								q.declareParameters("kr.co.kihyun.beans.entity.TotDoc tot_doc");
								q.setOrdering("mDept");
								reportList = (List<TotReport>) q.execute(totDoc);
							}
						}

						int reportCount = reportList.size();
						for (int d = 0; d < reportCount; d++) {
			%>
			<table border="0" width="100%" cellpadding="0" cellspacing="0">
				<tr>
					<td class="kecttep_title" style="padding-left: 5" align="left"
						width="100%"><img src="/totsys/common/images/ico_1.gif">
						<%= reportList.get(d).getDept(pm).getOrgan(pm) == null ? "" : reportList.get(d).getDept(pm).getOrgan(pm).getName()%>&nbsp;&nbsp;
						<%= reportList.get(d).getDept(pm) == null ? "" : reportList.get(d).getDept(pm).getName()%>&nbsp;&nbsp;
						<%= reportList.get(d).getDept(pm) == null ? "" : reportList.get(d).getDept(pm).getName()%></td>
                                </tr>
                                <tr>
					<td>
						<div id="formDiv"
							 style="padding-top:20;  overflow-x: auto;">
							<%=Excel.adjustFmlaToExcel(TagFilter.removeBlankPara(TotReportUtil.toHTML(reportList.get(d))))%></div>
					</td>
				</tr>
                                <% if(!reportList.get(d).getAttachments().isEmpty()) { %>
				<tr>
					<td class="kecttep_title" style="padding-left: 5" align="left"><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.add")%><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.file")%></td>
				</tr>
				<tr>
					<td style="padding-left: 10" align="left">
					<%
											for (String fileName : reportList.get(d).getAttachments().keySet()) {
										%> <a
						href="/servlet/kr.co.kihyun.text.html.HttpViewer?saveAs=<%=URLEncoder.encode(fileName, "UTF-8")%>&fileName=<%=URLEncoder.encode(MString.checkNull(fileName), "UTF-8")%>&reportID=<%=reportList.get(d).getId() %>"
						target="new"><%= MString.checkNull(fileName)%></a>
					<%
											}
										%>
					</td>
				</tr>
				<% } %>



			</table>
                                ----------------------------------------------------------------------------------------------------------------------------------
                                <br />
				<%
                                    }
				tx.rollback();
                                    } else {
				%>
			<div id="formDiv" class="formDiv" style="padding-top:20; overflow-x: auto;">
                <%--
                	ie10,11 호환성 보기 스타일 문제 수정
                    <%=report.replaceAll("<td", "<td style='word-break:break-all;' ")%>
                 --%>
					<%=report%>
                </div>
                <script>
                	$('#formDiv td').each(function(){
						$(this).css('word-break','break-all');
                	});
                </script>
                <%}%>
		</td>
		<!---- 본문  끝---->
	</tr>
	<!--세미타이틀-->
	<tr class="kecttep_include_page">
		<td colspan="4">
			<table border="0" cellpadding="0" cellspacing="0">
				<tr>
					<%if (downDocID != null && !MString.isNull(memo)) {%>
					<td style="width: 14"><img
							src="/totsys/common/images/ico_1.gif"></td>
					<td class="kecttep_title" style="padding-left: 5" align="left"><%=TotDocType.SUB_DOC%><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.modify")%><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.content")%></td>
					<td class="kecttep_div_show"><span id="downDocCommentShow"><a
								href="#" class="textbutton"
								onclick="listView('downDocComment','downDocCommentShow','downDocCommentNone');"><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.view")%></a></span><span
							id="downDocCommentNone" style="display: none;"><a href="#"
																		  class="textbutton"
																		  onclick="listView('downDocComment','downDocCommentShow','downDocCommentNone');"><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.hidden")%></a></span></td>
					<td width="10px"></td>
					<%}%>
				</tr>
			</table>
		</td>
	</tr>

	<%if (downDocID != null && !MString.isNull(memo)) {%>
	<tr>
		<td colspan="4">
			<div id="downDocComment" style="display: none;">
				<table border="0" cellpadding="0" cellspacing="0">
					<tr>
						<td class="body" style="padding-left: 20"><%=memo.trim()%></td>
					</tr>
				</table>
			</div>
		</td>
	</tr>
	<%}%>

	<%
			if (realFileNameList.length != 0 && !MString.isNull(realFileNameList[0])) {
	%>
	<tr>
		<td colspan="4">
			<table border="0" 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">[<%= Moumi.getMessageBundle().getString("moumi.message.tot_doc.add")%><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.file")%>]</td>
				</tr>
			</table>
		</td>
	</tr>

	<tr>
		<td colspan="4">
			<div id="repoperMessage" style="display: '';">
				<table border="0" cellpadding="0" cellspacing="0">
					<%if (realFileNameList[0] != null && realFileNameList[0] != "") {
						for (int i = 0; i < realFileNameList.length; i++) {
							if (i == 0) {%>
					<%}%>
					<tr>
						<td class="body" style="padding-left: 20"><font color="#FF6600"><b>+</b><%= Moumi.getMessageBundle().getString("moumi.message.tot_doc.add")%><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.file")%>
								<%=(i + 1)%></font>&nbsp;&nbsp; <a
									href="/servlet/kr.co.kihyun.text.html.HttpViewer?saveAs=<%=URLEncoder.encode(viewFileNameList[i], "UTF-8")%>&fileName=<%=URLEncoder.encode(realFileNameList[i], "UTF-8")%>&reportID=<%=totReport.getId()%>"
								target="hiddenFrame"><%= viewFileNameList[i]%></a></td>
					</tr>
					<%}
					}%>
					<tr>
						<td style="padding-left: 20">
							<p class="body"><font class="body"><%=HtmlUtil.toHtml(note)%></font></p>
						</td>
					</tr>
				</table>
			</div>
		</td>
	</tr>
	<%
			}
	%>
</table>
<iframe name='hiddenFrame' width=0 height=0 style='display:none;'></iframe>
</head>
</html>

<%
		} catch (Exception ex) {
			ex.printStackTrace();
			out.println(kr.co.kihyun.text.html.ServletUtil.getJavaScript("alert('" + Moumi.getMessageBundle().getString("moumi.message.popup.requestAdmin") + "');"));
			out.println(kr.co.kihyun.text.html.ServletUtil.getJavaScript("history.back(-1);"));
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}
%>
