<%@ page contentType="text/html; charset=UTF-8"
         import="java.net.URLEncoder"
         import="kr.co.kihyun.beans.user.HttpSSOLogin"
         import="kr.co.kihyun.text.html.ServletUtil"
         import="kr.co.kihyun.lang.MString"
         import="kr.co.kihyun.moumi.Moumi"
         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="kr.co.kihyun.service.*, kr.co.kihyun.service.vo.*, kr.co.kihyun.service.util.*"%>
<%@ include file="/totsys/common/inc/sec/secure.inc.jsp"%>

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

        /**
         * ********* User HttpSSOLogin check **********
         */
        if (!HttpSSOLogin.isLogin(request)) {
            String connURL = "opener.location='/totsys/login/login.jsp';";
            out.println(ServletUtil.getJavaScript(connURL));
            out.println(ServletUtil.getJavaScript("window.close();"));
            return;
        }

        /**
         * ******** session내의 userId *********
         */
        String usID = HttpSSOLogin.getLoginID(request);
        String dtID = HttpSSOLogin.getDeptID(request);
        int sysAuth = HttpSSOLogin.getSysAuth(request);

        /**
         * **** Parameter *****
         */
        Long docID = MLong.parseLong(request.getParameter("docID"), null);

        Long reportID = MLong.parseLong(request.getParameter("reportID"), null);
        String boxName = clearXSS(request.getParameter("boxName"), "");
        String callbackFunc = MString.checkNull(request.getParameter("callbackFunc"));
        String procMode = MString.checkNull(request.getParameter("proc_mode"));
        boolean hideUser = MString.isNull(request.getParameter("hideUser")) ? true : new Boolean(request.getParameter("hideUser"));
        String userYN = "N";
        if (hideUser == false) {
            userYN = "Y";
        } else {
            userYN = "N";
        }
        String reportType = clearXSS(request.getParameter("reportType"), "");
        if (MString.isNull(procMode)) {
        tx.begin();
            String titleName = Moumi.getMessageBundle().getString("moumi.message.tot_doc.docList") + Moumi.getMessageBundle().getString("moumi.message.tot_doc.name");
            MDept dept = (MDept) pm.getObjectById(MDept.class, dtID);

            String deptID = "";

			if(sysAuth == Moumi.SUB_SYSADM ){
            	deptID = dept.getOrgan(pm).getId();
            }else{
            	deptID = dept.getId();
			}

%>

<!DOCTYPE html>
<html lang="ko">
	<head>
		<meta charset="UTF-8">
		<title>담당변경</title>
        <script src="/totsys/common/js/jquery-1.12.4.min.js"></script>
        <script src="/totsys/common/js/jquery.cookie.js"></script>
		<script src="/totsys/common/js/jquery.hotkeys.js"></script>
		<script src="/totsys/common/js/jquery.jstree.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/rollover.js"></script>
        <script>
            var nodeType;
            var deptName;
            var userName;
            var userID;

            function fnInit() {
                /* @@@ test
                 document.getElementById('search').value = "손기영";
                */
            }

            function searchWord() {
               var type = $('#findOption').val();
                type = type.toUpperCase();
                //alert("type = " + type);
                var word = $('#search').val();
                word = $.trim(word);
                $('#search').val(word);
                if (word.length < 2) {
                    alert("검색어는 2글자 이상이어야 합니다.");
                    return;
                }
                var $ff = $("#leftFrame");
                $ff[0].contentWindow.$("#searchType").text(type);  //검색유형
                $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();
            }

            function userList(id, deptname, name, type) {
                if (type == 'user')
                    id = id.split('/')[0];
                nodeType = type;
                deptName = deptname;
                userName = name;
                userID = id;
            }

            function putNodeType(type, id, deptname, name) {
                nodeType = type.toLowerCase();
                userID = id;
                deptName = deptname;
                userName = name;
            }

            function onOk() {
                if (nodeType != 'user') {
                    //사용자를 선택하세요!
                    alert('<%=Moumi.getMessageBundle().getString("moumi.message.popup.selectUser")%>');

                    //deptName / userName 으로 담당자를 변경하시겠습니까?
                } else if (confirm("'" + deptName + " / " + userName + "' \n\n으로 담당자를 변경하시겠습니까?")) {
                    document.transfer.userID.value = userID;
                    document.transfer.submit();
                }
            }
        </script>
    </head>
    <body class="over_hidden">
        <form name="transfer" method="post">
            <input type="hidden" name="proc_mode" value="change_owner">
            <input type="hidden" name="reportID" value="<%= reportID%>">
            <input type="hidden" name="userID" value="">
            <input type="hidden" name="reportType" value="<%= reportType%>">
            <input type="hidden" name="callbackFunc" value="<%= callbackFunc%>">
        </form>

        <table>
            <tr>
                <td>
                    <!--타이틀 //-->
                    <div class="title">
                    	<div>담당변경</div>
                    </div>
                    <!--타이틀 //-->
                    <table>
                        <%if (sysAuth == Moumi.TOTPER && 1==2) {  //2014.09.12 Update by KWON,HAN : && 1==2 추가하여 사용안함 %>
                        <jsp:include page="/totsys/common/inc/title/title.jsp" flush="true">
                            <jsp:param name="title" value='<%=URLEncoder.encode("담당자변경")%>' />
                            <jsp:param name="flag"	value='99' />
                        </jsp:include>
                        <%}%>
                        <tr>
                            <td>
                                <table>
                                    <tr>
                                        <td>
                                            <div>
                                    <!-- 기관관리자일때 하위부서까지 조회되고 grade가 null인 사용자 제외로 수정 by wonseok Lee 20171107 -->
                                    <%
                                    if (sysAuth == Moumi.SUB_SYSADM) {
                                    %>
                                      	    	<iframe id="leftFrame" src="/totsys/common/web/report_dept/iframe/newTree2.jsp?rootID=<%= deptID%>&userYN=<%= userYN%>&grade=N" style="margin: 10px 0 0 0; border: 1px solid #728ab5;" width="554" height="530"></iframe>
                                    <%
                                    } else {
                                    %>
                                    			<iframe id="leftFrame" src="/totsys/common/web/report_dept/iframe/newTree3.jsp?rootID=<%= deptID%>&userYN=<%= userYN%>" style="margin: 10px 0 0 0; border: 1px solid #728ab5;" width="554" height="530"></iframe>
                                    <%
                                    }
                                    %>
                                            </div>
                                        </td>
                                    </tr>
                                </table>
                                <div class="mt_5">
                                    <table>
				                        <tr>
				                            <td> 검 색 &nbsp;</td>
				                            <td style="text-align: left;">
                                                <span style="width: 70px;">
                                                    <select id="findOption" name="findOption" class="inputtxt" onchange="$('#search').val('');
                                                        showMSG(this.options[this.selectedIndex].value, msg)" disabled>
                                                        <option value="user" selected><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.user")%></option>
                                                        <option value="dept"><%=Moumi.getMessageBundle().getString("moumi.message.doc.Devision")%><%=Moumi.getMessageBundle().getString("moumi.message.doc.Name")%></option>
                                                    </select>
                                                </span>
                                            </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>
                                    <!-- 확인, 취소 버튼 -->
					                <div class="center mt_10" style="border-top: 1px solid #8ea6d6">
					                    <div class="mt_15">
					                        <a href="#" onclick="javascript:onOk();"><img alt="확인" src="/totsys/common/images/bt_ok.gif"></a>
					                        <a href="#" onClick="javascript:window.close();"><img class="ml_5" alt="취소" src="/totsys/common/images/bt_cancle.gif"></a>
					                    </div>
					                </div>
                                </div>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </body>
</html>
<%
	tx.rollback();
} else {
    String userID = MString.checkNull(request.getParameter("userID"));
    tx.begin();
    TotReportService reportService = new TotReportService(pm);
    reportService.procChangeOwnerForReport(reportID, usID, userID);
    tx.commit();
    String funcName = Util.getFuncNameFromSignature(callbackFunc);
%>
	<head>
		<meta charset="UTF-8">
		<title>담당변경</title>
		<script src="/totsys/common/js/jquery-1.12.4.min.js"></script>
        <script>
            $(document).ready(function(){
            	fn_onLoad();
            });
            
            function fn_onLoad() {
                alert("<%= Moumi.getMessageBundle().getString("moumi.message.popup.chargeTrans")%>");
            <% if (!callbackFunc.equals("")) {%>
                if (typeof (opener.<%= funcName%>) != "undefined") {
                    opener.<%= callbackFunc%>;
                } else if (typeof (parent.opener.<%= funcName%>) != "undefined") {
                    parent.opener.<%= callbackFunc%>;
                }
            <%}%>
                self.close();
            }
        </script>
    </head>
</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();
    }
%>
