
<%
    /*
     * ***********************************************************
     * @@ Program Name	: group_dept_list.jsp
     * @@ Description	: 사용자정의 그룹변경
     * @@ Author	: 기현테크
     * @@ Create Date	: 2010-11-27
     * @@ History       : 2014.09.18 Update by KWON,HAN
     * **********************************************************
     */
%>

<%@ page contentType="text/html; charset=UTF-8"
         import="kr.co.kihyun.lang.MString"
         import="kr.co.kihyun.moumi.Moumi"
         import="java.net.URLDecoder"
         import="kr.co.kihyun.lang.Encoder"
         import="kr.co.kihyun.beans.user.HttpSSOLogin"
         import="kr.co.kihyun.beans.user.UserGroup"%>
<%@ include file="/totsys/common/inc/sec/secure.inc.jsp"%>
<%    /**
     * ******** session내의 userId *********
     */
    String usID = HttpSSOLogin.getLoginID(request);
%>
<!DOCTYPE html>
<html lang="ko">
	<head>
        <meta charset="UTF-8">
        <title>사용자 그룹 설정</title>
        <script type="text/javascript" src="/totsys/common/js/jquery-1.12.4.min.js"></script>
        <script type="text/javascript" src="/totsys/common/js/jquery.cookie.js"></script>
		<script type="text/javascript" src="/totsys/common/js/jquery.hotkeys.js"></script>
		<script type="text/javascript" src="/totsys/common/js/jquery.jstree.js"></script>
        <script type="text/javascript" src="/totsys/common/js/jquery.custom.indicator.js"></script>
        <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">
        <link rel="stylesheet" href="/totsys/common/css/popup.css" type="text/css">
        <link rel="stylesheet" href="/totsys/common/css/basic.css" type="text/css">
        <script src="/totsys/common/js/org_search.js"></script>
        <script>
            var delstate = false;
            var userGroup = false; // 사용자정의 그룹인지 여부지정 변수

            var selid = ''; // 지금 선택한 부서
            var seltype = '';
            var selname = '';
            var idList = ''; // 선택되어진 부서 리스트
            var typeList = '';
            var nameList = '';
            var orgSearch = new OrgSearch(onNodeCallback);
            var focusedButton = null;

            //추가버튼
            function getDeptId() {
                var $ff = $("#leftFrame");
                $ff[0].contentWindow.$("#checkbtn").click();
            }

            function putSelId(id) {
                selid = id;
                deptConfirm2();
            }

            function putSelId2(sId, sDeptName, sOrganDeptName, sUpperDeptName) {
                //alert("id = " + sId + ", deptName = " + sDeptName + ", OrganDeptName = " + sOrganDeptName + ", UpperDeptName = " + sUpperDeptName)

                selid = sId;
                //selname = sOrganDeptName;
                //if(!sOrganDeptName.equals(sUpperDeptName)) {
                //    selname = selname + "-" + sUpperDeptName;
                //}
                //selname = selname + "-" + sDeptName;
                selname = sOrganDeptName + "|" + sUpperDeptName + "|" + sDeptName;
                deptConfirm2();
            }

            // 부서 추가
            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 == '') {
                        document.list.idList.value = idList = id;
                        document.list.nameList.value = nameList = name;
                        document.list.typeList.value = typeList = type;

                        idList = idList + "";
                        nameList = nameList + "";
                        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;
                    }
                    document.list.submit();
                }
            }

            function deptConfirm2(id) {
                if (selid == '')
                    alert("<%=Moumi.getMessageBundle().getString("moumi.message.popup.deptConfirm")%>");   //부서를 먼저 선택해주세요
                else if (seltype == "group")
                    modSelect();
                else {
                    var id = selid;
                    //type=seltype;
                    var type = 'dept';
                    var name = selname;

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

                        idList = idList + "";
                        typeList = typeList + "";
                        nameList = nameList + "";
                    } 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;
                    }
                    //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;
                            document.list.typeList.value = typeList = type;
                            typeList = typeList + "";
                            idList = idList + "";
                        } else {
                            var isAdd = listAdd(idList, id, typeList, type);
                            document.list.idList.value = idList;
                            document.list.typeList.value = typeList;
                        }

                        isChange = true;
                    }
                }

                if (isChange) {
                    document.list.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=275, height=465, toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,left=" + leftpos + ",top=" + toppos);
                post2 = window.open(urlname, "post2", "width=450, height=465, toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,left=" + leftpos + ",top=" + toppos);
                post2.focus();
            }

            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) {
                id = lastStrDel(id, ",");
                type = lastStrDel(type, ",");
                name = lastStrDel(name, ",");

                if (nameList == '' && idList == '') {
                    document.list.idList.value = idList = id;
                    document.list.typeList.value = typeList = type;
                    document.list.nameList.value = nameList = name;
                } else {
                    delList(id, type, name);
                    document.list.idList.value = idList;
                    document.list.typeList.value = typeList;
                    document.list.nameList.value = nameList;
                }

                document.list.submit();
                return;
            }

            function lastStrDel(str, delStr) {
                var lastStr = str.lastIndexOf(delStr);
                return str.substring(0, lastStr);
            }

            function delList(idList1, tyList1, naList1) {
                idList = idList1;
                typeList = tyList1;
                nameList = naList1;
                return;
            }

            // 부서 선택 시 유저 뽑아내기.
            function userList(id, name, type) {
                if (type != '') {
                    idAll = id;
                    selid = id;
                    selname = name;
                } else {
                    idAll = '';
                    selid = '';
                    selname = '';
                }
                seltype = type;
                idUpper = false;
                userGroup = false;
            }

            function groupList(id, name, type) {
                idAll = id;
                selid = id;
                selname = name;
                seltype = type;
                idUpper = false;
                userGroup = false;
            }

            function ProcKeyDown() {
                keyPressed = String.fromCharCode(window.event.keyCode);
                if ((keyPressed == '\r') || (keyPressed == '\n')) {
                    if (focusedButton == null) {
                        document.getElementById('nextButton').focus();
                    }
                }
            }

            function previous() {
                try {
                    var locationString = document.leftFrame.location + '';
                    if (locationString.indexOf('groupTree.jsp') !== -1)		// onNodeCallback does not works for group_list.jsp yet.
                        return;
                    orgSearch.previous(document.searchForm.keyword.value);
                } catch (e) {
                    alert(e);
                    return;
                }
            }

            function next() {
                try {
                    var locationString = document.leftFrame.location + '';
                    if (locationString.indexOf('groupTree.jsp') !== -1)		// onNodeCallback does not works for group_list.jsp yet.
                        return;
                    orgSearch.next(document.searchForm.keyword.value);
                } catch (e) {
                    alert(e);
                    return;
                }
            }

            function onNodeCallback(node) {
                for (i = 0; i < node.pathList.length; i++)
                    document.leftFrame.openF(node.pathList[i]);
                document.leftFrame.selectItem(node.id, node.type);
                userList(node.id, node.name, node.type);
            }

            function searchWord() {
                var word = $('#search').val();
                word = $.trim(word);
                $('#search').val(word);
                if (word.length < 2) {
                    alert("검색어는 2글자 이상이어야 합니다.");
                    return;
                }
                var $ff = $("#leftFrame");
                $ff[0].contentWindow.$("#searchWord").text(word);
                $ff[0].contentWindow.$("#search").click();
            }

            //조직도에서 검색된 노드로 포커스를 이동시킨다.  strType : down, up
            function focusMove(strType) {
                //alert("strType = " + strType);
                var $ff = $("#leftFrame");
                $ff[0].contentWindow.$("#moveType").text(strType);      //포커스이동타입
                $ff[0].contentWindow.$("#focusMove").click();
            }

            var lv_processing = false;
            //확인 버튼
            function deptComp() {
                var sGroupName = $('#groupName').val();
                sGroupName = $.trim(sGroupName);
                $('#groupName').val(sGroupName);

                if (document.group.groupName.value == null || document.group.groupName.value == "") {
                    alert('<%=Moumi.getMessageBundle().getString("moumi.message.popup.groupName")%>');   //설정할 그룹명을 입력 해 주세요.
                    document.group.groupName.focus();
                    return;
                }

                if (document.list.idList.value == "") {
                    alert('<%=Moumi.getMessageBundle().getString("moumi.message.popup.deptComp")%>');    //제출요청 부서를 추가 해 주세요
                    return;
                } else {
                    if( lv_processing == true ) {
                    return false;
                    }
                    $.customIndicator.show(window.self);
                    lv_processing = true;
                    document.group.deptIDs.value = document.list.idList.value;
                    document.group.deptNames.value = document.list.nameList.value;
                    document.group.deptTypes.value = document.list.typeList.value;

                    //var deptTemp = document.list.idList.value.split(",");
                    //var deptCounter = deptTemp.length;

                    document.group.submit();
                }
            }

            //취소 버튼
            function deptCancel() {
                window.close();
            }

            /*
             * 현재페이지의 검색조건을 갖고 Relead하는 함수
             */
//            function fn_reloadParentPage() {
//                if( opener.fn_reload != null && typeof(opener.fn_reload) == "function" ) {
//                    opener.fn_reload();
//                }
//            }

        </script>
    </HEAD>

    <BODY>
        <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>
        <!--타이틀 //-->
        <div class="title">
        	<div><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.user")%><%=Moumi.getMessageBundle().getString("moumi.message.button_small.groupRegist")%></div>
        </div>
        <!--타이틀 //-->

        <!-- //본문 -->
		<div class="disFlex lv1">
        	<div class="deptListSide">
         	<img alt="말머리" src="/totsys/common/images/ico_3.gif">
         	<b><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.deptMap")%></b>
        	</div>
        	<div class="deptListCenter"></div>
        	<div class="deptListSide"><img alt="말머리" src="/totsys/common/images/ico_3.gif"><b>제출기관(부서)<!--%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.submitDept")%--></b></div>
        </div>
     
		<div class="disFlex ml_5">
           	<!-- 분류 선택 아이프레임 -->
           	<iframe src="iframe/new_section_list.html" width="300px;" height="21px;"></iframe>
           </div>
           <div class="disFlex">
           	<div class="deptListSide">
           		<!-- 부서 선택 아이프레임 -->
           		<iframe style="height: 380px;" id="leftFrame" name="leftFrame" src="./iframe/groupCommon.jsp"></iframe>
           	</div>
           	<div class="deptListCenter center deptPt">
                   <div>

			<!--추가,삭제 -->
                   	<a href="javascript:getDeptId();"><img alt="추가" src="./images/new_dept_add.gif"/></a>
                   </div>
                   <div class="mt_5">
                	<!--삭제-->
                    <a href="javascript:deptDelete();"><img alt="삭제" src="./images/new_dept_del.gif"/></a>
                   </div>
           	</div>
           	<div class="deptListSide">
           		<!-- 선택된 부서 리스트 아이프레임 -->
           		<iframe style="height: 380px;" name="deptFrame" src="./iframe/selected_list.jsp"></iframe>
           	</div>
           </div>
		<form name="group" method="post" action="/servlet/kr.co.kihyun.beans.user.HttpUserGroup">
			 <input type="hidden" name="deptIDs">
			 <input type="hidden" name="deptNames">
			 <input type="hidden" name="userIDs">
			 <input type="hidden" name="userNames">
			 <input type="hidden" name="deptTypes">
			 <input type="hidden" name="userID" value="<%=usID%>">
			<input type="hidden" name="callbackFunc" value="opener.fn_reload()">
			<table>
            <tr style="padding-top: 20px;">
                <td colspan="3">
                    
                        <table>
                            <tr>
                                <td style="width: 14px;"><img src="./images/ico_1.gif"></td>
                                <td class="kecttep_title" style="padding-left: 5px; text-align: left; width: 500px;">
                                    <%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.group")%><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.name")%> :
                                    <input name="groupName" type="text" value='' size="30"  maxlength="20" class="inputtxt" style="ime-mode: active;" />
                                </td>
                                <td style="width: 14px; padding-left: 13px;">&nbsp;</td>
                                <td class="kecttep_title" style="padding-left: 5px; text-align: left;">&nbsp;</td>
                            </tr>
                            <tr>
                                <td style="height: 3px;"></td>
                            </tr>                            
                        </table>

                </td>
            </tr>
        </table>
        <table>
            <tr>
                <td> 검 색 &nbsp;&nbsp;</td>
                <td><input id="search" name="search" type="text" class="inputtxt" value="" size="35" onkeyDown="if(event.keyCode === 13) { searchWord(); }" style="ime-mode:active;">&nbsp;</td>
                <td><a href="#" onclick="searchWord();"><img alt="찾기" src="/totsys/common/images/bt_search_inquiry2.gif"></a>&nbsp;</td>
                <td><a href="#" onclick="focusMove('down');"><img alt="다음" src="/totsys/common/images/bt_down.jpg"></a>&nbsp;</td>
                <td><a href="#" onclick="focusMove('up');"><img alt="이전" src="/totsys/common/images/bt_up.jpg"></a></td>
                <td></td>
            </tr>
        </table>
	  </form>

        <!-- 확인, 취소 버튼 -->
        <div class="center mt_10" style="border-top: 1px solid #8ea6d6">
        	<div class="mt_10">
	         	<a href="#" onclick="deptComp()"><img alt="확인" src="images/bt_enter.gif"></a>
	         	<a href="#" onClick="deptCancel()"><img alt="취소" src="images/bt_cancle3.gif"></a>
        	</div>
		</div>
        <!-- //본문 -->
    </BODY>
</HTML>
