<%@ page contentType="text/html; charset=UTF-8"
         import="java.net.URLEncoder"
         import="java.util.Date"
         import="java.util.List"
         import="java.text.SimpleDateFormat"
         import="javax.jdo.PersistenceManager"
         import="javax.jdo.Transaction"
         import="kr.co.kihyun.beans.entity.*"
         import="kr.co.kihyun.beans.entity.util.*"
         import="kr.co.kihyun.beans.user.HttpSSOLogin"
         import="kr.co.kihyun.lang.Encoder"
         import="kr.co.kihyun.util.MDate"
         import="kr.co.kihyun.lang.MInteger"
         import="kr.co.kihyun.lang.MString"
         import="kr.co.kihyun.moumi.Moumi"
         import="kr.co.kihyun.moumi.report.MReport"
         import="kr.co.kihyun.moumi.doc.MDoc"
         import="kr.co.kihyun.beans.totsys.doc.Doc"
         import="kr.co.kihyun.beans.user.UserView"
         import="kr.co.kihyun.beans.user.DeptView"
         import="kr.co.kihyun.beans.user.DeptAdd"
         import="kr.co.kihyun.lang.MLong"
         import="kr.co.kihyun.moumi.MoumiConfig"
         import="kr.co.kihyun.db.DBUtil"
         import="kr.co.kihyun.beans.user.MultiCheck"
         import="java.net.URLEncoder"
         import="kr.co.kihyun.lang.StringConverter"
         import="kr.co.kihyun.lang.Encoder" %>
<%@ include file="/totsys/common/inc/sec/secure.inc.jsp"%>
<%    PersistenceManager pm = new MPersistenceManager(PMF.get().getPersistenceManager());
    Transaction tx = pm.currentTransaction();
    try {
        request.setCharacterEncoding("UTF-8");

//        int endPage = 0;
//        int pageSize = 0;
//        int currentPage = 0;
//        int absolutePage = 0;
//        int currentPageSetUp = 0;
//        int recordSize = 10;
//
//        currentPageSetUp = (int) (currentPage / 10) * 10;
//        if (currentPage % 10 == 0) {
//            currentPageSetUp -= 10;
//        }

        String[] DeptID;
        String[] UserId;
        String[] DeptName;
        String[] sOriginDeptFlag;    //2014.08.19 Add by KWON,HAN : 원소속 부서 구분
        Integer count;

        String id = request.getParameter("id");
        String password = request.getParameter("password");

        tx.begin();

        MultiCheck mc = new MultiCheck();
        mc.IdCheck(id);
        count = mc.getCount();
        DeptID = mc.getDeptID();
        DeptName = mc.getDeptName();
        UserId = mc.getUserId();
        sOriginDeptFlag = mc.getOrignDeptFlag();   //2014.08.19 Add by KWON,HAN

//        currentPage = 1;
//
//        if ((count % recordSize) == 0) {
//            pageSize = (int) (count / recordSize);
//        } else {
//            pageSize = (int) (count / recordSize) + 1;
//        }
        //System.out.println("count:::" + count);

        tx.rollback();
%>
<html>
    <head>
        <title>겸임부서 선택</title>
        <link rel="stylesheet" href="/totsys/common/css/text.css" type="text/css">
        <link rel="stylesheet" href="/totsys/common/css/kecttep.css" type="text/css">
        <script type="text/javascript" src="/totsys/common/js/jquery-1.12.4.min.js"></script>
        <script>
        //겸임부서 팝업 자동 닫힘 문제 수정
        $().ready(function(e){
        	setTimeout(function(){
        		window.onbeforeunload=function(){
        			try{
        				opener.login.submit();
        			}catch(e){

        			}
                    window.close();
        		};
        	},300);
        });
            var state = 0;

            function cancelGO() {
                opener.login.submit();
                window.close();
            }

            function loginGO() {
                state = 1;
                var checkedval;
                var dummyDeptId;
                var tmpId;
                var orignDeptFlag;  //2014.08.19 Add by KWON,HAN

                //2014.08.19 Add by KWON,HAN
                dummyDeptId = 'N';
                for (i = 0; i < f.deptID.length; i++) {
                    if (f.deptID[i].checked == true) {
                        dummyDeptId = 'Y';
                        break;
                    }
                }

                if (dummyDeptId == 'N') {
                    alert("겸임할 부서를 선택하여 주십시요.");
                    return false;
                }
                //--------------------------------------------


//                for (i = 0; i < f.deptID.length; i++) {
                    if (f.deptID[i].checked == true) {
                        checkedval = f.deptID[i].value;
                        tmpId = f.userId[i].value;

                        orignDeptFlag = f.orignDeptFlag[i].value;  //2014.08.19 Add by KWON,HAN

                        //여기서 muser 테이블을 업데이트 한다.
                        //2014.08.19 Update by KWON,HAN
                        //location.href="dept_update.jsp?deptId="+checkedval+"&id="+tmpId;
                        location.href = "dept_update.jsp?deptId=" + checkedval + "&id=" + tmpId + "&orignDeptFlag=" + orignDeptFlag;
//                        document.submit();
//                        break;
                    }
//                }
            }
            function closePage(event){
                if(event.clientY < 0 ){
                opener.login.submit();
                window.close();
                }

            }

            document.onkeydown = function(){
                if(event.keyCode === 115){
                opener.login.submit();
                window.close();
                }
            }
        </script>
    </head>
    <!--
    겸임부서 선택 팝업시 자동 닫힘 문제 수정
    <body onbeforeunload="closePage(event)">
     -->
     <body>
        <table width="100%" border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td>
                    <!--스타일시트-->
                    <link rel="stylesheet" href="/totsys/common/css/kecttep.css" type="text/css">
                    <!--타이틀-->
            <tr>
                <td style=" padding-bottom:10">
                    <table width="100%" height="29" border="0" cellpadding="0" cellspacing="0">
                        <tr>
                            <td width="39" background="images/title1.gif">&nbsp;</td>
                            <td class="kecttep_title" background="images/titlebg.gif">부서선택</td>
                            <td width="13" background="images/title2.gif">&nbsp;</td>
                        </tr>
                    </table>
                </td>
            </tr>
            <!--타이틀 끝-->
        </td>
    </tr>
    <!--1픽셀 띄우기-->
    <tr>
        <td height="10">
        </td>
    <tr>
    <tr>
        <td>
            <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <td style="padding-left:10">
                        <form name="f" method="post">
                            <%
                                for (int i = 0; i < count; i++) {
                                    // 2014.08.19 Update by KWON,HAN
                                    //out.print("<input type='radio' name='deptID' value='"+DeptID[i]+"'>"+DeptName[i]+"<br>\n");
                                    //out.print("<input type='hidden' name='userId' value='"+UserId[i]+"'>");
                                    if (sOriginDeptFlag[i].equals("Y")) {
                                        out.print("<label for=''><input type='radio' name='deptID' value='" + DeptID[i] + "'>" + DeptName[i] + "&nbsp;<b>(원소속)</b></label><br />\n");
                                    } else {
                                        out.print("<label for=''><input type='radio' name='deptID' value='" + DeptID[i] + "'>" + DeptName[i] + "</label><br />\n");
                                    }
                                    out.print("<input type='hidden' name='userId' value='" + UserId[i] + "'>");
                                    out.print("<input type='hidden' name='orignDeptFlag' value='" + sOriginDeptFlag[i] + "'>");

                                    //-------------------------------
                                }
                                /*int a=-1;
                                 while(a++ < count){
                                 String DeptID2       = mc.getDeptID()[a];
                                 String DeptName2      = mc.getDeptName()[a];
                                 String UserId2        = mc.getUserId()[a];

                                 System.out.println("test DeptID:::::::"+DeptID2);
                                 }*/
                            %>
                        </form>
                    </td>
                </tr>
            </table>
            <!--1픽셀 띄우기-->
    <tr>
        <td height="10">
        </td>
    <tr>
        <!--1픽셀 띄우기-->
    <tr>
        <td height="10">
        </td>
    <tr>
    <tr>
        <td align="center">
            <a href="#" onclick="loginGO()"><img src="/totsys/common/images/bt_ok.gif" border="0" alt="확인" /></a>
            <a href="#" onclick="cancelGO()"><img src="/totsys/common/images/bt_cancle.gif" border="0" alt="취소" /></a>
        </td>
    </tr></td>
</tr>
</table>
</body>
</html>
<%
    } catch (Exception ex) {
        //38.오류메세지를 통한 정보 노출(getMessage)_CWE-209 : Update by YOUNGJUN,CHO
        ex.printStackTrace();
        //out.println(ex);
        //out.println("<html><body style='overflow=auto' bgcolor='white'><h1>Can't contact servlet runner</h1><br><br>Message : " + ex.getMessage() + "</body></html>");
        //out.println("<META HTTP-EQUIV='refresh' CONTENT='0.1; URL=/error/exception.jsp'>");
        //================================================
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
%>
