
<%@page import="java.util.ArrayList"%>
<%@page import="java.net.URLDecoder"%>
<%
/************************************************************@@
  * 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.util.Date"
	import="java.text.SimpleDateFormat"
	import="kr.co.kihyun.beans.user.HttpSSOLogin"
	import="kr.co.kihyun.db.DBUtil"
	import="kr.co.kihyun.moumi.report.MReport"
	import="kr.co.kihyun.util.MDate" import="kr.co.kihyun.lang.Encoder"
	import="kr.co.kihyun.lang.MInteger" import="kr.co.kihyun.lang.MString"
	import="kr.co.kihyun.moumi.Moumi" import="kr.co.kihyun.moumi.doc.MDoc"
	import="javax.jdo.PersistenceManager" import="javax.jdo.Transaction"
	import="kr.co.kihyun.beans.entity.util.*"
	import="kr.co.kihyun.beans.entity.*" import="org.slf4j.Logger"
	import="org.slf4j.LoggerFactory" import="java.util.Set"
	import="javax.jdo.Query"%>
<%@page import="kr.co.kihyun.beans.entity.TotDoc"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.HashMap"%>

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

<%
PersistenceManager pm = new MPersistenceManager(PMF.get().getPersistenceManager());
Transaction tx = pm.currentTransaction();

try{

	Logger LOG = LoggerFactory.getLogger("jsp");
	/********** session내의 userId **********/
	String usID = HttpSSOLogin.getLoginID(request);
	String dpID = HttpSSOLogin.getDeptID(request);
	int sysAuth = HttpSSOLogin.getSysAuth(request);

	int wordLengthLimit = 30;

	/****** Parameter ******/
	int strPage = MInteger.parseInt(request.getParameter("strPage"), 1);
	String findOption = clearXSS(request.getParameter("findOption"),null);
	String findWord = clearXSS(Encoder.toJava(request.getParameter("findWord")),null);
	//String uri = URLEncoder.encode("doc_list.jsp?&findOption="+MString.checkNull(findOption)+"&findWord="+MString.checkNull(findWord), "UTF-8");

    String method = clearXSS(request.getParameter("method"),"");
    if(method.equals("GET")) findWord = URLDecoder.decode(MString.checkNull(request.getParameter("findWord")), "UTF-8");
    String uri = URLEncoder.encode("doc_list.jsp?&findOption="+MString.checkNull(findOption),"UTF-8")+"&method=GET";

	/************ 리스트 페이지 구성 ************/
	int endPage = 0;
	int pageSize =0 ;
	int currentPage = 0;
	int absolutePage = 0;
	int currentPageSetUp = 0;
	int recordSize = 15;

	currentPage=strPage;

	tx.begin();

	MUser mUser = pm.getObjectById(MUser.class, usID);
	List<TotDoc> filterDoc = null;
	
	if(mUser.getSysAuth() == SysAuth.SYSADM){
		Object filterValue = findWord;
		if(findOption != null && findOption.endsWith("Date"))
			filterValue = new SimpleDateFormat("yy-MM-dd").parse(findWord);
			
		Map<String, Object> filterMap = new HashMap<String, Object>(1);
		filterMap.put(findOption, filterValue);
		
		QueryImpl q = new QueryImpl(pm, TotDoc.class);
		q = new QueryImpl(pm, TotDoc.class, (List<TotDoc>) q.execute());
		
		StringBuilder filter = new StringBuilder(110)
		.append("isDeleted == false && type == kr.co.kihyun.beans.entity.TotDocType.FORM_DOC");
		q.setFilter(filter.toString());
		q.setSearchFilter(filterMap);
		q.setOrdering("id DESC");
		filterDoc = (List<TotDoc>) q.execute();
      
	}else{
		Object filterValue = findWord;
		if(findOption != null && findOption.endsWith("Date"))
		filterValue = new SimpleDateFormat("yy-MM-dd").parse(findWord);
		filterDoc = mUser.getForms(pm, findOption, filterValue);
 
	}
	int count = filterDoc.size();


        List<TotDoc> filterDoc2 = new ArrayList<TotDoc>();
        if(count>0 && findOption!=null && findOption.equals("totDoc.mUser.name") && findWord!=null && findWord.length()>1)
        {
            for(int index=0;index<count;index++)
            {
                String tmp=filterDoc.get(index).getUser(pm).getName(pm);
                if(tmp==null) continue;
                if(tmp.indexOf(findWord)>=0) filterDoc2.add(filterDoc.get(index));
            }
            filterDoc=filterDoc2;
            count=filterDoc.size();
        }


	tx.rollback();

	if((count%recordSize) == 0){
		pageSize = (int)(count/recordSize);
	}else{
		pageSize = (int)(count/recordSize)+1;
	}

	currentPageSetUp = (int)(currentPage/10)*10;
	if (currentPage%10 == 0){
		currentPageSetUp-=10;
	}
	%>

<HTML>
<HEAD>
<TITLE><%=Moumi.getTitle()%></TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<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/SquareButtons.css"
	type="text/css">
<script src="/totsys/common/js/rollover.js"></script>
<script src="/totsys/common/js/popup.js"></script>
<script src="/totsys/common/js/find.js"></script>

<script>
	function showMSG(){
		if(findForm.findOption.value == "regDate"){
			msg.style.display="block";
		}else{
			msg.style.display="none";
		}
	}

	function selectAll(){
		var obj = document.getElementsByName("check");
		for(var i =0; i < obj.length;i++){
			obj[i].checked = true;
		}
	}

	function cancelAll(){
		var obj = document.getElementsByName("check");
		for(var i =0; i < obj.length;i++){
			obj[i].checked = false;
		}
	}

	function docDelete(){
		var checkValue = 0;
		var docIDList = "";

		if(<%=count%> == 1 || <%=count%> == <%=((currentPage-1)*recordSize)+1%>){
			if(check.checked == true){
				if(docIDList.length == 0){
					if(typeof(checkDocID0) == 'undefined'){
						docIDList += document.getElementById("checkDocID<%=((currentPage-1)*recordSize)%>").value;
					}else{
						docIDList += document.getElementById("checkDocID0").value;
					}
				}else{
					if(typeof(checkDocID0) == 'undefined'){
						docIDList += ","+document.getElementById("checkDocID<%=((currentPage-1)*recordSize)%>").value;
					}else{
						docIDList += ","+document.getElementById("checkDocID0").value;
					}
				}
				checkValue ++;
			}
		}else if(<%=count%> != 0){
			tmp1 = eval(<%=((currentPage-1)*recordSize)%>);
			tmp2 = eval(<%=((currentPage-1)*recordSize)%>+check.length);
			tmp3 = 0;
			for(i=tmp1; i<tmp2; i++){
				if(check[tmp3].checked == true){
					if(checkValue == 0){
						checkValue ++;
						if(docIDList.length == 0)
							docIDList += document.getElementById("checkDocID"+i).value;
						else
							docIDList += ","+document.getElementById("checkDocID"+i).value;

					}else{
						checkValue ++;
						if(docIDList.length == 0)
							docIDList += document.getElementById("checkDocID"+i).value;
						else
							docIDList += ","+document.getElementById("checkDocID"+i).value;
					}
				}
				tmp3 ++;
			}
		}
		if(checkValue > 0){
			if(confirm(checkValue+'<%=Moumi.getMessageBundle().getString("moumi.message.popup.docCounterDelete")%>')){
				document.Del.docID.value = docIDList;
				document.Del.submit();
			}
		}else{
			alert('<%=Moumi.getMessageBundle().getString("moumi.message.popup.docDeleteSelect")%>');
		}
	}

    /**
             *체크박스 전체선택 해제 스크립트 추가
             **/
            function checkALL(checkYn) {
                frm = document.approOpen;
                  var checklist = document.getElementsByName("check");
                    if(checklist.length > 0) {
                       for(var i=0; i<checklist.length; i++) {
                              checklist[i].checked = checkYn;
                       }
                  }
            }
</script>
 </HEAD>
 <body style='' BGCOLOR="#FFFFFF" LEFTMARGIN="0" TOPMARGIN="0"
	MARGINWIDTH="0" MARGINHEIGHT="0">
    <FORM name="approOpen" action="#" method="post">
       <input type="hidden" name="reportID">
       <input type="hidden" name="reportType">
       <input type="hidden" name="userID">
        <input type="hidden" name="strGubun">
    </FORM>
 <jsp:include page="/totsys/common/inc/totper/top/top.jsp" flush="true" />
 <table width="100%" height="70" border="0" cellpadding="0"
	cellspacing="0">
	<jsp:include page="/totsys/common/inc/title/title.jsp" flush="true" />
	<tr>
		<td>
		<table width="100%" border="0" cellpadding="0" cellspacing="0">
			<tr valign="top" class="toolbar">
				<td align="left">
				<form name="findForm" method="post" action="./doc_list.jsp"
					onsubmit="return findCheck(this);" style="margin: 0px"><span
					style="width: 300px"> <select name="findOption"
					class="inputtxt"
					onchange="showMSG(this.options[this.selectedIndex].value, msg)">
					<option value="name"><%=TotDocType.FORM_DOC %><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.name")%></option>
					<option value="totDoc.mUser.name"><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.registrition")%><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.person")%></option>
					<option value="regDate"><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.registrition")%><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.day")%></option>
				</select> <input name="findWord" value="<%= MString.checkNull(findWord) %>"
					type="text" class="inputtxt" size="30"> <!---- 검색 끝 ----> <input
					type="image" src="/totsys/common/images/bt_search_inquiry.gif"
					border="0" align="middle" style="MARGIN: -3px 0px 0px 0px">
				</span></form>
				</td>
				<td align="left" valign="top"><font color="#3c64ab"><span
					id="msg" class="kecttep_date_search"
					style='display: none; padding-top: 5; width: 220px'>&nbsp;&nbsp;&nbsp;<%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.daySearchForm")%></span></font></td>
				<td align="right" valign="top">
				<table border="0" cellpadding="0" cellspacing="0">
				 <tr>
                 <!--<td><a class="simplebutton"
							href="./doc_regist.jsp?strPage=<%= strPage %>&findOption=<%= findOption %>&findWord=<%= findWord %>"><span
							class="regist" style="width: 90px"><%= Moumi.getMessageBundle().getString("moumi.message.button_small.formRegist") %></span></a>
						</td> -->
					<!--//	<td class="kecttep_button_td"><a class="simplebutton"
							href="#"><span onclick="selectAll()"
							style="cursor: hand; width: 70px" class="check"><%= Moumi.getMessageBundle().getString("moumi.message.button_small.selectAll") %></span></a>
						</td>
						<td class="kecttep_button_td"><a class="simplebutton"
							href="#"><span onclick="cancelAll()"
							style="cursor: hand; width: 70px" class="check"><%= Moumi.getMessageBundle().getString("moumi.message.button_small.selectCancel") %></span></a>
						</td>//-->
						<td class="kecttep_button_td"><a class="simplebutton"
							href="#"><span onclick="docDelete()"
							style="cursor: hand; width: 70px" class="remove"><%= Moumi.getMessageBundle().getString("moumi.message.button_small.formRemove") %></span></a>
						</td>
				 </tr>
				</table>
				</td>
		</table>
		</td>
	</tr>

	<tr>
		<td valign="top">
		<table border="1px" bordercolor="#e3e3e3" class="kecttep_list_table"
			width="100%" cellpadding="0" cellspacing="0">
			<tr>
				<td class="kecttep_tdmenu" align="center" nowrap width="50">
                    <!--//%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.select")%>//-->
                     <input type="checkbox" name="check0" onclick="javascript:checkALL(this.checked)"/>
                </td>
				<td class="kecttep_tdmenu" align="center" nowrap><%=TotDocType.FORM_DOC %><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.number")%></td>
				<td class="kecttep_tdmenu" align="center"><%=TotDocType.FORM_DOC %><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.name")%></td>
				<td class="kecttep_tdmenu" align="center" nowrap width="97"><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.registrition")%><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.person")%></td>
				<td class="kecttep_tdmenu" align="center" nowrap width="140"><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.coporation")%>/<%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.devision")%></td>
				<td class="kecttep_tdmenu" align="center" nowrap width="97"><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.registrition")%><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.day")%></td>
                                <td class="kecttep_last_tdmenu" align="center" nowrap width="97"><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.preview")%></td>
<!--				<td class="kecttep_tdmenu" align="center" nowrap width="97"><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.preview")%></td>
				<td class="kecttep_last_tdmenu" align="center" nowrap width="97"><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.info")%><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.modify")%></td>-->
			</tr>
			<%
						int index = 0;     //배열에서 값을 꺼내오는 키값
						int nextCount = 0; //

						if(currentPage > 1){
							nextCount = ((currentPage-1)*recordSize);
						}

						tx.begin();
						for(int i = 0; (i < recordSize) && ((nextCount+i) < count); i++){
							index = nextCount+i;
							%>
			<tr style="padding-top: 3" valign="middle"
				onmouseover="this.style.backgroundColor='F9F9F9'"
				onmouseout="this.style.backgroundColor='white'" bgColor=#ffffff>
				<td width="50" nowrap class="kecttep_graytd" height="25" align="center">
                    <input type="hidden" name="checkDocID<%=index%>" value="<%= filterDoc.get(index).getId() %>">
                    <input type="checkbox" name="check" <%if(!filterDoc.get(index).getUser(pm).getId().equals(usID) && sysAuth == Moumi.TOTPER){%> disabled <%}%> style="width: 50px">
                </td>
				<td width="80" nowrap class="kecttep_graytd" height="25" 
					align="center"><%= filterDoc.get(index).getId() %></td>
				<td nowrap class="kecttep_graytd">&nbsp;&nbsp;<a
					href="./doc_view.jsp?docID=<%= filterDoc.get(index).getId() %>"><%= MString.getEllipsis(filterDoc.get(index).getName(),wordLengthLimit) %></a></td>
				<td width="97" nowrap class="kecttep_graytd" align="center"><%= filterDoc.get(index).getUser(pm).getName(pm) %></td>
				<td width="140" nowrap class="kecttep_graytd" align="center"><%= filterDoc.get(index).getDept(pm).getName() %></td>
				<td width="97" nowrap class="kecttep_graytd" align="center"><%= new SimpleDateFormat("yyyy-MM-dd").format(filterDoc.get(index).getRegDate()) %></td>
                <td width="97" nowrap class="kecttep_last_graytd" align="center">
                   <img style="cursor: hand;"  onclick="opener('/totsys/common/web/documents/tot_doc/popup_inform_preview.jsp?docID=<%= filterDoc.get(index).getId() %>&mode=preview', 'preview', '840', '600', 'center', 'middle', 'yes', 'yes');"
					src="/totsys/common/images/icon_preview.jpg" border="0">
                </td>
<!--				<td width="97" nowrap class="kecttep_graytd" align="center"><img
					style="cursor: hand;"
					onclick="opener('/totsys/common/web/documents/tot_doc/popup_inform_preview.jsp?docID=<%= filterDoc.get(index).getId() %>&mode=preview', 'preview', '840', '600', 'center', 'middle', 'yes', 'yes');"
					src="/totsys/common/images/icon_preview.jpg" border="0"></td>
				<td width="97" align="center" nowrap class="kecttep_last_graytd"><img
					style="cursor: hand;"
					onclick="opener('/totsys/common/web/documents/tot_doc/popup_modify.jsp?docID=<%= filterDoc.get(index).getId() %>', 'preview', '840', '130', 'center', 'middle', 'no', 'no');"
					src="/totsys/common/images/icon_modify.jpg" border="0"></td>-->
			</tr>
			<%}
						tx.rollback();
						%>
		</table>
		</td>
	</tr>

	<tr>
		<td height="15"></td>
	</tr>
	<%if(count > 0){%>
	<tr>
		<td class="kecttep_board" height="25" colspan="13" align="center">
		<!------------------------------- 페이지수 나오는 부분 시작 ------------------------------->
		<jsp:include page="/totsys/common/inc/board/page.jsp" flush="true">
			<jsp:param name="uri" value="<%= uri %>" />
			<jsp:param name="pageSize" value="<%= pageSize %>" />
			<jsp:param name="currentPage" value="<%= currentPage %>" />
			<jsp:param name="currentPageSetUp" value="<%= currentPageSetUp %>" />
            <jsp:param name="findWord" value='<%=URLEncoder.encode(MString.checkNull(findWord),"UTF-8")%>' />
		</jsp:include> <!------------------------------- 페이지수 나오는 부분 끝 ------------------------------->
		</td>
	</tr>
	<%}else{%>
	<tr>
		<td class="kecttep_none_tdmenu" colspan="13" height="25"
			align="center"><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.notRegistForm")%></td>
	</tr>
	<%}%>
</table>

<!--검색후 검색조건 선택하기위한 스크립트(히든값포함)-->
<input type="hidden" name="tmpFind" value="<%=findOption%>">
<FORM name="Del" action="/servlet/kr.co.kihyun.beans.totsys.doc.HttpDocDeleter" method="post">
    <INPUT type="hidden" name="docID" value="">
    <INPUT type="hidden" name="userID" value="<%= usID %>">
    <INPUT type="hidden" name="url" value="form"></FORM>
         <script>
			if(tmpFind.value == "name"){
				findForm.findOption.selectedIndex=0;
			}else if(tmpFind.value == "totDoc.mUser.name"){
				findForm.findOption.selectedIndex=1;
			}else if(tmpFind.value == "regDate"){
				findForm.findOption.selectedIndex=2;
			}
		</script>
<!--검색후 검색조건 선택하기위한 스크립트 끝-->
<!------------------------------- top menu start ------------------------------->
<%@ include file="/totsys/common/inc/buttom/buttom.jsp"%>
<!------------------------------- top menu end ------------------------------->

</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{
	if(tx.isActive())
		tx.rollback();
	pm.close();
}
%>
