<%@ page contentType="text/html; charset=UTF-8"
	import="kr.co.kihyun.lang.MString" import="java.net.URLDecoder"
	import="kr.co.kihyun.lang.Encoder" import="kr.co.kihyun.lang.MLong"
	import="kr.co.kihyun.moumi.Moumi"
	import="kr.co.kihyun.beans.totsys.report.ReportList"
	import="kr.co.kihyun.beans.user.User"
	import="kr.co.kihyun.moumi.report.MReport"%>
<%@page import="kr.co.kihyun.beans.entity.Repoadm"%>
<%@page import="javax.jdo.PersistenceManager"%>
<%@page import="javax.jdo.Transaction"%>
<%@page import="kr.co.kihyun.beans.entity.util.*"%>
<%@page import="kr.co.kihyun.beans.entity.TotReport"%>

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

<%
	Long docID = MLong.parseLong(request.getParameter("docID"),null);
	Long reportID = MLong.parseLong(request.getParameter("reportID"),null);
	String usedType = MString.checkNull(request.getParameter("usedType"));
	String totCount = clearXSS(Encoder.toJava(MString.checkNull(request.getParameter("totCount"))),"");

	int deptCount=0;
	String deptIDs = "";
	String deptNames="";
	String deptTypes="";
	Integer[] docTypeList = null;
	String[] deptIDList = null;
	String[] userIDList = null;
	String[] deptNameList = null;
	
	if(!"RECOG".equals(usedType)){
		ReportList reportList = new ReportList();
                //v2. 13.SQL 삽입 : JDO형식이므로 해결책에 따른 prepare SQL 문으로 변경할 수 없음
		reportList.executeQuery(docID, MReport.ALL);
                //=================
		deptCount = reportList.getCount();

		User user = new User();
	
		if(deptCount>0){
			deptIDList = new String [deptCount];
			deptNameList = new String [deptCount];

			deptIDList = reportList.getDeptIDList();
			userIDList = reportList.getUserIDList();
			deptNameList = reportList.getDeptNameList();

			docTypeList = reportList.getDocTypeList();
			
			if(docTypeList[0] == 1){
				deptIDs = userIDList[0]+"/"+deptIDList[0];
				deptNames = user.getName(userIDList[0]);
				deptTypes = "user";
			}else{
				deptIDs = deptIDList[0];
				deptNames = deptNameList[0];
				deptTypes = "dept";
			}

			for(int d=1; d<deptCount; d++){
				if(docTypeList[d] == 1){
					deptIDs = deptIDs+","+userIDList[d]+"/"+deptIDList[d];
					deptNames = deptNames+","+user.getName(userIDList[d]);
					deptTypes = deptTypes+","+"user";
				}else{
					deptIDs = deptIDs+","+deptIDList[d];
					deptNames = deptNames+","+deptNameList[d];
					deptTypes = deptTypes+","+"dept";
				}
			}
		}
	}else{
		PersistenceManager pm = new MPersistenceManager(PMF.get().getPersistenceManager());
		Transaction tx = pm.currentTransaction();
		TotReport totReport = null;
		try {
			tx.begin();

			totReport = (TotReport)pm.getObjectById(TotReport.class, reportID);
			for(int i=0; i<totReport.getRepoadms().size(); i++){
				if(i == 0){
					deptIDs = totReport.getRepoadms().get(i).getRecogUser(pm).getId()+"/"+totReport.getRepoadms().get(i).getRecogUser(pm).getUpperDept(pm).getId();
					deptNames = totReport.getRepoadms().get(i).getRecogUser(pm).getUpperDept(pm).getName()+"-"+totReport.getRepoadms().get(i).getRecogUser(pm).getName();
					deptTypes = "user";
				}else{
					deptIDs = deptIDs +","+totReport.getRepoadms().get(i).getRecogUser(pm).getId()+"/"+totReport.getRepoadms().get(i).getRecogUser(pm).getUpperDept(pm).getId();
					deptNames = deptNames +","+totReport.getRepoadms().get(i).getRecogUser(pm).getUpperDept(pm).getName()+"-"+totReport.getRepoadms().get(i).getRecogUser(pm).getName();
					deptTypes = deptTypes +","+"user";
				}
			}

			tx.rollback();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(tx.isActive())
				tx.rollback();
			pm.close();
		}
	}
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>제출부서 선택</TITLE>

<style>
a:link {
	font-style: none;
	line-height: normal;
	color: #004080;
	text-decoration: none;
}

a:visited {
	font-style: none;
	line-height: normal;
	color: #3366CC;
	text-decoration: none;
}

a:active {
	font-style: none;
	line-height: normal;
	color: #535353;
	text-decoration: none;
}

a:hover {
	font-style: none;
	line-height: normal;
	color: #FF6600;;
	text-decoration: none;
}

img {
	border: none;
}
</style>
<script Language="javascript">

		var tstate = false;
		var delstate = false;
		var userGroup = false; // 사용자정의 그룹인지 여부지정 변수

		var selid=''; // 지금 선택한 부서
		var selname='';
		var seltype='';
		var idList = ''; // 선택되어진 부서 리스트
		var nameList = '';
		var typeList = '';

			// 부서 추가
			function setDepts(){
					document.list.idList.value = idList = "<%=deptIDs%>";
					idList=idList+"";
					document.list.nameList.value = nameList = "<%=deptNames%>";
					nameList=nameList+"";
					document.list.typeList.value = nameList = "<%=deptTypes%>";
					typeList=typeList+"";

					if(idList != "")
						document.list.submit();
			}

			// 부서 추가

			function deptConfirm(){
				if(selid == '' ){
					alert('<%=Moumi.getMessageBundle().getString("moumi.message.popup.deptConfirm")%>');
                                }else if(seltype == "group"){
                                        modSelect();

				}else{
						id = selid;
						name = selname;
						type = seltype;

						if(	idList == '' ){
							<%if("RECOG".equals(usedType)){%>
							if(type == "user"){
							<%}%>
								document.list.idList.value = idList = id;
								idList=idList+"";
								document.list.nameList.value = nameList = name;
								nameList=nameList+"";
								document.list.typeList.value = typeList = type;
								alert(id+"::"+name+"::"+type);
								alert(document.list.idList.value);
								//alert(document.list.nameList.value);
								alert(document.list.typeList.value);
								typeList=typeList+"";
							<%if("RECOG".equals(usedType)){%>
							}
							<%}%>
						}else{
							<%if("RECOG".equals(usedType)){%>
							if(type == "user"){
							<%}%>
								var isAdd = listAdd(idList, id, typeList, type);
								document.list.idList.value = idList;
								document.list.typeList.value = typeList;
								if(isAdd){
									listAddn(nameList, name);
								}
								document.list.nameList.value = nameList;
								alert(id+"::"+name+"::"+type);
								alert(document.list.idList.value);
								//alert(document.list.nameList.value);
								alert(document.list.typeList.value);
							<%if("RECOG".equals(usedType)){%>
							}
							<%}%>
						}
					document.list.submit();
				}	
			}

			
			
			
			
			function openerdeptConfirm(){ //구성원으로 추가
				var gid = document.list.openeridList.value;
				var gname = document.list.openernameList.value;
				var gtype = document.list.openertypeList.value;

				var arrayID = gid.split(",");
				var arrayName = gname.split(",");
				var arrayType = gtype.split(",");

				var isChange = false;

				for(var i=0; i < arrayID.length; i++){
					selid = arrayID[i];
					selname = arrayName[i];
					seltype = arrayType[i];
				
					if(selid == '' ){
						alert('<%=Moumi.getMessageBundle().getString("moumi.message.popup.deptConfirm")%>');
					}else{
							id = selid;
							name = selname;
							type = seltype;

							if(	idList == '' ){
								document.list.idList.value = idList = id;
								idList=idList+"";
								document.list.nameList.value = nameList = name;
								nameList=nameList+"";
								document.list.typeList.value = typeList = type;
								typeList=typeList+"";

							}else{
								var isAdd = listAdd(idList, id, typeList, type);
								document.list.idList.value = idList;
								document.list.typeList.value = typeList;
								if(isAdd){
									listAddn(nameList, name);
								}
								document.list.nameList.value = nameList;
							}		

							isChange = true;
					}	
				
				}

				if(isChange){
					document.list.submit();
				}
			}


			function listAdd(idList1, idList2, tList, tItem){
				var arrayID1 = idList1.split(",");
				var arrayType = tList.split(",");

				var isEquals = true;
				
				if(idList1 == ''){	
					idList = idList1;
					typeList = tList;
					return false;
				}
				for(var i=0; i < arrayID1.length; i++){
					if(idList2 == arrayID1[i] && tItem == arrayType[i]){
						isEquals = false;
						break;
					}
				}
				if(isEquals){
					idList1 += ","+idList2;
					tList += ","+tItem;
				}				
				idList = idList1;
				typeList = tList;
				return isEquals;
			}

			function listAddn(nameList1, nameList2){
				var arrayName1 = nameList1.split(",");

				if(nameList1 == ''){
					nameList = nameList1;
					return;
				}

				nameList1 += ","+nameList2;
				nameList = nameList1;
				
				return;
			}

			// 부서삭제
			function deptDelete(){
				if(idList != '' ){
					deptFrame.movelist();
					document.list.submit();
				}
			}

			function leftFrameDel(name, id, type){
				name = lastStrDel(name, ",");
				id = lastStrDel(id, ",");
				type = lastStrDel(type, ",");
				if(nameList == '' && idList == ''){
					document.list.nameList.value = nameList = name;					
					document.list.idList.value = idList = id;
					document.list.typeList.value = typeList = type;
					
				}else{
					delList(id, name, type);
					document.list.nameList.value = nameList;	
					document.list.idList.value = idList;
					document.list.typeList.value = typeList;
				}
				
				tstate = true;

				document.list.submit();
				return;
			}	
			function delList(idList1, naList1, tyList1){
				idList = idList1;
				nameList = naList1;	
				typeList = tyList1;
				return;
			}
			function lastStrDel(str, delStr){
				var lastStr = str.lastIndexOf(delStr);
				return str.substring(0, lastStr);				
			}


			// 부서 선택 시 유저 뽑아내기.
			function userList(id, name, type){
				if(type != '') {
					idAll=id;
					selid = id;
					selname = name;
				} else {
					idAll='';
					selid = '';
					selname = '';
				}
				seltype = type;
				idUpper = false;
				userGroup = false;
/*
				document.userlist.id.value = id;
				document.userlist.target="rightFrame";
				document.userlist.selectType.value="right";
				document.userlist.didList.value = idList;

				document.userlist.submit();
*/
			}
			function groupList(id, name, type){
				idAll=id;
				selid = id;
				selname = name;
				seltype = type;
				idUpper = false;
				userGroup = false;
/*
				document.userlist.id.value = id;
				document.userlist.target="rightFrame";
				document.userlist.selectType.value="right";
				document.userlist.didList.value = idList;

				document.userlist.submit();
*/
			}

			function modSelect() {
				var height = screen.height; 
				var width = screen.width; 
				var leftpos = width / 2 - 235; 
				var toppos = height / 2 - 235; 

				if(seltype != "group"){
					alert('<%=Moumi.getMessageBundle().getString("moumi.message.popup.groupSelect")%>');
					return;
				}

				var urlname = "/totsys/common/web/report_dept/modifier.jsp?groupID="+selid;
				post2= window.open(urlname, "post2","width=450, height=435, toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,left=" + leftpos + ",top=" + toppos);
				post2.focus(); 
			}

			function deptComp(){
				if(document.list.idList.value=="")
				{
					<%if("RECOG".equals(usedType)){%>
					alert('<%=Moumi.getMessageBundle().getString("moumi.message.popup.approvalSelect")%>');
					<%}else{%>
					alert('<%=Moumi.getMessageBundle().getString("moumi.message.popup.deptComp")%>');
					<%}%>
					return;
				}
				else{

					document.deptSet.deptIDs.value = document.list.idList.value;
					document.deptSet.types.value = document.list.typeList.value;

					var deptTemp = document.list.idList.value.split(",");
					var deptCounter = deptTemp.length;
					opener.dform.deptCountText.value = deptCounter + '<%=Moumi.getMessageBundle().getString("moumi.message.popup.deptCounter")%>';
					opener.dform.deptChange.value = "1";
                       
					deptSet.submit();
					//window.close();
				}
			}

			function deptCancel(){
				window.close();
			}


	</script>
<link rel="stylesheet" href="/totsys/common/css/kecttep.css" type="text/css">
</HEAD>

<BODY <%if(totCount==null || totCount.equals("")){%>
	onload="setDepts();" <%}%>>

<form name="deptSet" method="post"
	action="/servlet/kr.co.kihyun.beans.totsys.doc.HttpDeptSet"
	target="_self"><input type="hidden" name="docID"
	value="<%=docID%>"> <input type="hidden" name="reportID"
	value="<%=reportID%>"> <input type="hidden" name="usedType"
	value="<%=usedType %>"> <input type="hidden" name="deptIDs">
<input type="hidden" name="types"></form>

<form name="list" method="post"
	action="/servlet/kr.co.kihyun.beans.user.HttpDeptList"
	target="deptFrame"><input type="hidden" name="idList"> <input
	type="hidden" name="nameList"> <input type="hidden"
	name="typeList"> <input type="hidden" name="openeridList">
<input type="hidden" name="openernameList"> <input type="hidden"
	name="openertypeList"></form>
	 <!--타이틀 //-->
<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr>
          <td bgcolor="728ab5" style="padding: 10 20 10 20"><strong><font color="#FFFFFF">제출기관지정</font></strong></td>
  </tr></table>
  <!--타이틀 //-->
<table width="100%" border="0" cellpadding="0" cellspacing="0">
	<!-- 제목 -->
	<tr>
		<td colspan="3">
		<table width="100%" height="26" border="0" cellpadding="0" cellspacing="0" style="padding-top:10px ; padding-bottom:10px">
              <tr> 
                <td width="300" ><img src="/totsys/common/images/ico_3.gif"  border="0" align="absmiddle">&nbsp;<b><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.deptMap")%></b></td>
                <td  align="left" width="100" ></td>
                <td width="470"><img src="/totsys/common/images/ico_3.gif"  border="0" align="absmiddle">&nbsp;<b><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.submitDept")%></b></td>
              </tr>
            </table>
	</td>
	</tr>
	
	

	<!-- 분류,부서 선택 -->
	<tr>
		<td width="300"><!-- 분류 선택 아이프레임 --> <%if(!"RECOG".equals(usedType)){%>
		<table border="0" cellspacing="0" cellpadding="0">
			<tr>
				<td><iframe src="iframe/new_section_list.html" width="135"
					height="21" frameborder="0" scrolling="no"> </iframe></td>
			</tr>
		</table>
		<%}%> <!-- 부서 선택 아이프레임 -->
		<table border="2" bordercolor="#abbddd" cellspacing="0"
			cellpadding="0">
			<tr>
				<td>
				<%if(!"RECOG".equals(usedType)){%> <iframe name="leftFrame"
					src="./iframe/group_list.jsp" width="300" height="360"
					frameborder="0"></iframe> <%}else{%> <iframe name="leftFrame"
					src="./iframe/part_list.jsp" width="300" height="360"
					frameborder="0"></iframe> <%}%>
				</td>
			</tr>
		</table>
		</td>

		<!-- 추가,삭제 -->
		<td width="100" align="center">
		<table border="0" cellspacing="0" cellpadding="0" widht=50>
			<!--추가-->
			<tr>
				<td height="35" style="padding-left: 10px; padding-right: 10px"><a
					href="javascript:deptConfirm();" border="0"><img
					src="./images/new_dept_add.gif"></a></td>
			</tr>
			<!--삭제-->
			<tr>
				<td height="35" style="padding-left: 10px; padding-right: 10px"><a
					href="javascript:deptDelete();" border="0"><img
					src="./images/new_dept_del.gif"></a></td>
			</tr>
			<!--구성원-->
			<%if(!"RECOG".equals(usedType)){%>
			<tr>
				<td height="35" style="padding-left: 10px; padding-right: 10px"><a
					href="javascript:modSelect();" border="0"><img
					src="./images/new_modifier.gif"></a></td>
			</tr>
			<%}%>
		</table>
		</td>

		<!-- 선택 된 아이프레임 -->
		<td width="470" style="padding-top: 20px;">
		<table border="1" bordercolor="#abbddd" cellspacing="0"
			cellpadding="0">
			<tr> 
				<td><iframe name="deptFrame" src="./iframe/selected_list.jsp" width="470" height="365" frameborder="0"></iframe></td>
			</tr>
		</table>
		</td>
	</tr>
</table>

	<!-- 확인, 취소 버튼 -->
<table border=0 cellspacing=0 cellpadding=0 width="100%" style="margin-top:8px">
	<tr><td height="1" bgcolor="728ab5"></td></tr>
	
	<tr>
		
		<td height="40" align="center" >
			<table width="200" height="0" border="0" cellpadding="0" cellspacing="0" >
			<tr>
				<td width="100" align="center"><a href="#" onclick="deptComp()"><img src="/totsys/common/images/bt_ok.gif" border="0" align="absmiddle"></a>
				</td>
				<td width="100" align="left"><a href="#" onClick="deptCancel()"><img src="/totsys/common/images/bt_cancle.gif" border="0" align="absmiddle"></a>
				</td>
			</tr>
		</table>
		</td>
	</tr>
</table>

</BODY>
</HTML>
