<%--
  - FileName   : AuthBatchList.jsp
  - Author(s)  : gyeongmo an
  - Date       : 2014.06.22
  - Description: 일괄 권한 등록
  --%>
<%@ page contentType="text/html; charset=utf-8" %>
<%@ page import="com.udapsoft.waf.system.HandlerStorage" %>
<%@ page import="com.udapsoft.waf.common.ui.*" %>
<%@ page import="kr.co.hsnc.common.util.*"%>
<%@ page import="kr.co.hsnc.common.sql.*" %>
<%@ page import="kr.co.udapsoft.common.code.*" %>
<%@ include file="/jsp/work/taglib.jsp" %>

<jsp:useBean id="ctx" class="com.udapsoft.waf.system.context.SessionContext" scope="session" />
<%
    ctx.init(request, response);
    HandlerStorage storage = ctx.getHandlerStorage();
    ValueObject params = storage.getParams();
    ValueObject user = storage.getUser();
    String event = storage.getEvent();
    
    RowSet acctTypeRowSet = storage.getRowSet("AcctTypeRowSet"); //계정 타입
	
  	//권한
  	RowSet userRight_rs = storage.getRowSet("userRight_rs"); //전체목록
%>
<SCRIPT language="JavaScript">
	/*예약함수*/
	var sheet1;
	var sheet1Row;
	var sheet1PageNo = 1;
	var pageLastRowIndex = 1;
	var validationResult = false;
	var filterShow = false;
	
    function _onReady(){
    	gridInit(sheet1);
    	
        jQuery("#btnSearch").click(function() {
        	jQuery("#fevent").val("GET_USER_LIST");
    		var url = Modular.model.PageContext.CONTEXT_ROOT + "/sycs/security/AuthBatchList_sheet.screen";
    		Modular.view.IBSheet7.doSearch(sheet1, url, jQuery("#form1"), 30, "");
		});
		
		jQuery("#btnClear").click(function() {
			jQuery("#dept_cd").val("");
			jQuery("#dept_nm").val("");
			jQuery("#user_sabun").val("");
			jQuery("#user_name").val("");
		});
		
		jQuery("#btnSave").click(function() {
			var target = document.form1.right_id;
	    	var targetLength = target.length;
	    	
	    	if (!sheet1.IsDataModified()){
				alert("저장할 내역이 없습니다.");
	    		return;
			}
		
	    	document.form1.target_v.value='';
	    	for( j = 0 ; j < targetLength ; j++ ) {
	    		document.form1.target_v.value += target.options[j].value + '/';
	    	}
	    	jQuery("#fevent").val("SAVE_USER_LIST");
			var url = Modular.model.PageContext.CONTEXT_ROOT + "/sycs/security/AuthBatchList_sheet.screen";
			var result = Modular.view.IBSheet7.doSave(sheet1, url, jQuery("#form1"), false);
		}); 
		
		jQuery("#btnSearchUser").click(function() {
			var win_id = "_searchUserPopup";
			var link = "/lib/dlgSearchUserPop.screen";
			    link += "?pop_win_id="+win_id;
			fLayerPop(win_id, link, 850,560, '', '', 'NO');
		});
		
		jQuery("#btnSearch").click();
    }
	
	 function gridInit(sheetName){
		var init_sheet = {};
		init_sheet.Cfg = {SearchMode:2, Page:30};
		init_sheet.Cols = [
			       			{Header:'삭제',				Type:"Text",		SaveName:"DEL_CHECK", 	Width:50, 	Align:"Center", Hidden:"True"},
			    			{Header:'상태',			Type:"Status", 		SaveName:"STATUS",		Width:80, 	Align:"Center", Hidden:"True"},
			    			{Header:'선택',			Type:"CheckBox", 	SaveName:"CHECK", 		Width:70, 	Align:"Center"},
			    			{Header:'사원번호',			Type:"Text", 		SaveName:"user_sabun", 	Width:80,  	Align:"Center"},
			    			{Header:'비밀번호',			Type:"Text", 		SaveName:"user_pw", 	Width:80, 	Align:"Center", Hidden:"True"},    
			    			{Header:'이름',				Type:"Text", 		SaveName:"user_name", 	Width:100, 	Align:"Center"  },
			    			{Header:'부서',				Type:"Text",		SaveName:"dept_nm", 	Width:100, 	Align:"Left"  },
			    			{Header:'투입프로젝트',	Type:"Text", 		SaveName:"buseo_nm", 	Width:150, 	Align:"Left"  },
			    			{Header:'직위',			Type:"Text", 		SaveName:"job_dscr", 	Width:80, 	Align:"Center"  },
			    			{Header:'전화번호',			Type:"Text", 		SaveName:"tel_no", 		Width:100, 	Align:"Center"},
			    			{Header:'휴대폰번호',		Type:"Text", 		SaveName:"hand_tel_no", Width:100, 	Align:"Center"},
			    			{Header:'E-mail',			Type:"Text", 		SaveName:"e_mail", 		Width:150, 	Align:"Left"},
			    			{Header:'등록일자',		Type:"Text", 		SaveName:"cre_date", 	Width:100, 	Align:"Center"},
			    			{Header:'계정ID',			Type:"Text", 		SaveName:"acct_id", 	Width:0, 	Align:"Left", Hidden:"True"}
		];

		init_sheet.HeaderMode = {Sort:1};
		IBS_InitSheet(sheetName, init_sheet);
		sheetName.SetDataAutoTrim(0);
		setGridSize();
	}
	
	 function setGridSize() {
		sheet1.FitColWidth();
	}

	/*예약함수*/
	// 저장 처리 결과를 받는 예약 함수
	// objects.js, result.xml  참조
	function doResult(resultObj) {
		try {
			if( resultObj.getRequestEvent() == "GET_RIGHT_DATA" ) {
				var f = document.form1;
				var vo = resultObj.getValueObject();
				injectSelect(vo, document.form1.right_id_pre,"RIGHT_ID","RIGHT_NAME");
				
				document.form1.isSubmit = false;
			}
		}
		catch(errorObject) {
	        showErrorDlg("doResult()", errorObject);
		}
	}
	
	function removeSelect(){
		var selectObj = document.form1.right_id;
		while(selectObj.options.length){
			selectObj.remove(0);
		}
	}
	
	//사용자 권한 변경
	function injectSelect(vo, selectObj, code, value){
		var Code = vo.get(code);
		var Value = vo.get(value);
		var arrCd = Code.split('|');
		var arrValue = Value.split('|');

		while(selectObj.options.length){
			selectObj.remove(0);
		}
		
		var newOpt;
		newOpt = document.createElement('option');
		
		if (arrCd == '') {
			selectObj.length = 1;
			
		} else {		
			for(var i = 0; i < arrCd.length; i++){
				newOpt = document.createElement('option');
				newOpt.text = arrCd[i];
				newOpt.value = arrValue[i];
				selectObj.options.add(newOpt);
			}
		}
	}

	function doKeyPress() {
	    if( event.keyCode == 13 ) {
	    	jQuery("#btnSearch").click();
	    }
	}	
	
	function grantAdd() {
    	var origin = document.form1.right_id_all;
    	var originLength = origin.length;
        var target = document.form1.right_id;
    	var targetLength = target.length;

    	var flag  = true;
        var count = 0;

        for( i = 0 ; i < originLength ; i++ ) {

            flag = true;
    		if( origin.options[i].selected == true ) {
                count++;
                if(flag) {
                	
                	for( var ti = 0; ti < targetLength; ti++ ){
                		if( target.options[ti].value == origin.options[i].value ){
                			alert('이미 선택한 권한입니다.');
                			return;
                		}
                	}
                	
                    target.options[targetLength] = document.createElement("OPTION");
                    target.options[targetLength].text  = origin.options[i].text;
                    target.options[targetLength].value = origin.options[i].value;
                    targetLength++;
                }
    		}
    	}

        if( count==0 ) {
            alert("부여할 권한을 선택하세요!");
        }
    }
	
	function grantDel() {
		var origin = document.form1.right_id_all;
    	var originLength = origin.length;
        var target = document.form1.right_id;
    	var targetLength = target.length;

        var flag  = true;
        var count = 0;
        for( i = 0 ; i < targetLength ; i++ ) {

            flag= true;
            if( target.options[i].selected == true ) {

                count++;
                for( j = 0 ; j < originLength ; j++ ) {

                    if( origin.options[j].value == target.options[i].value ) {

                        flag = false;
                        break;
                    }
                }
                if( flag ) {
                    target.options[i] = null;
                    i--;
                    targetLength--;
                }
                else {
                    target.options[i] = null;
                    i--;
                    targetLength--;
                }
            }
        }

        if( count==0 ) {
            alert("철회할 권한을 선택하세요!");
        }
	}
	
	//선택된 사용자의 권한 검색
	function doChangeRIGHT() {
		submitXmlRequest("/sycs/security/AuthBatchList_getRight.screen", "GET_RIGHT_DATA", document.form1, "VALUEOBJECT"); //xmlhttp.js
	}

	//부서 찾기 
	function searchAccDept(dept_cd, dept_nm){
		var win_id = "_searchAccDept";
		var link = "/lib/dlgSearchAccDept.screen";
		    link += "?pop_win_id="+win_id;
		fLayerPop(win_id, link, 600,400, '', '', 'NO');
	}
	
	function searchAccDeptClose(result) {
		var f = document.form1;
		if( result == null ){
			f.dept_cd.value = "";   //부서코드
			f.dept_nm.value = "";   //부서명

		}else{
			f.dept_cd.value = result[0];   //부서코드
			f.dept_nm.value = result[1];   //부서명
		}
		fLayerPopClose('_searchAccDept');
	}
	

	function sheet1_OnSearchEnd(code, message) {
	    if(code == 0) {
	    	for(var i=0; i<sheet1.RowCount(); i++) {
	    		sheet1.SetCellEditable(i+1, "user_sabun", false);
	    		sheet1.SetCellEditable(i+1, "user_pw", false);
	    		sheet1.SetCellEditable(i+1, "user_name", false);
	    		sheet1.SetCellEditable(i+1, "dept_nm", false);
	    		sheet1.SetCellEditable(i+1, "buseo_nm", false);
	    		sheet1.SetCellEditable(i+1, "job_dscr", false);
	    		sheet1.SetCellEditable(i+1, "tel_no", false);
	    		sheet1.SetCellEditable(i+1, "hand_tel_no", false);
	    		sheet1.SetCellEditable(i+1, "e_mail", false);
	    		sheet1.SetCellEditable(i+1, "cre_date", false);
	    		sheet1.SetCellEditable(i+1, "acct_id", false);
	    	}
	    	setGridSize();
	    	if(sheet1.LastRow()>1){
				document.form1.f_acct_id.value = sheet1.GetCellValue(1, "acct_id");
				doChangeRIGHT();
			}
		} else {
			//alert(message);
			alert("조회 실패");
		}
	}
	
	function sheet1_OnClick(Row, Col, Value, CellX, CellY, CellW, CellH) {
		var f = document.form1;
		if ( sheet1.ColSaveName(Col) != "CHECK" ) {
			document.form1.f_acct_id.value = sheet1.GetCellValue(Row, "acct_id");
			doChangeRIGHT();
		}
	 }	
	
	function sheet1_OnSaveEnd(code, msg) {
	    if(code >= 0) {
			document.form1.isSubmit = false;
	    	alert("저장 성공");
	    	removeSelect();
			jQuery("#btnSearch").click();
	    } else {
	    	//alert(msg);
	    	alert("저장 실패");
        }
	}
	
	function SearchUserClose(result) {
		fLayerPopClose('_searchUserPopup');
	}
	
	function autoResize(){
		var t1_height = document.getElementById("search-table").scrollHeight + document.getElementById("title").scrollHeight + 24;
		var iframeHeight= document.body.scrollHeight - t1_height;   
		
		sheet1.SetSheetHeight(iframeHeight);
	}
	
	function sheet1_OnSmartResize(Width, Height) { 
		sheet1.FitColWidth();
		autoResize();
	}
	document.body.onresize = addEvent( document.body.onresize, "autoResize()");
	
</SCRIPT>
<modular:search id="form1">
	<modular:key id="fevent" />
</modular:search>

<form id="form1" name="form1" method="post" modular:type="search" style="width:100%;height:100%;">
<INPUT type="hidden" name="p_user_sabun" value="">
<INPUT type="hidden" name="fevent" value="">
<INPUT type="hidden" name="p_user_pw" value="">
<INPUT type="hidden" name="p_buseo_nm" value="">
<INPUT type="hidden" name="p_job_dscr" value="">
<INPUT type="hidden" name="p_tel_no" value="">
<INPUT type="hidden" name="p_hand_tel_no" value="">
<INPUT type="hidden" name="p_e_mail" value="">
<INPUT type="hidden" name="p_user_name" value="">
<INPUT type="hidden" name="acct_id" value="">

<INPUT type="hidden" name="j_usertype" value="head">
<input type='hidden' name='login_id' >
<input type='hidden' name='login_pw' >
<INPUT type="hidden" name="j_admin_login_gb" value="Y">
<INPUT type="hidden" name="f_acct_id" value="">
<INPUT type="hidden" name="target_v" value="">

<div align="left" id="title">
    <ul>
     <li>권한 일괄 관리</li>
    </ul>
  </div>
  
  <!-- 검색조건 시작 -->
	<table id="search-table" class="search-form-table" style="width:100%;">
		<colgroup>
			<col width="100" />
			<col width="400" />
			<col width="100" />
			<col width="*" />
			<col width="300" />
		</colgroup>
		<tr>
			<th>부서</th>
			<td>
				<%= InputText.get("dept_cd", params.get("dept_cd"), 12, 10, "ID,ReadOnly,PerSize") %>
		 		<%= InputText.get("dept_nm", DeptCodeManager.getDeptName(params.get("dept_cd")), 50, 30, "ID,ReadOnly,PerSize") %>
		  		<IMG onclick="searchAccDept(document.form1.dept_cd, document.form1.dept_nm)" src="/img/buyer/button/bt_search_right.gif" alt="부서찾기" border="0" align="absmiddle" style="cursor:hand;">
			</td>
			<th>계정타입</th>
			<td>
				<%= HTMLMaker.getSelectTag(acctTypeRowSet, "ACCT_TYPE_ID", "ACCT_TYPE_NAME", "acct_type_id", "width:40%;height=18;", "<option value=''>--선택--</option>", "") %>
			</td>
			<td class="button" rowspan="2">
				<span class="btnSearch center"><a href="javascript:void(0);" id="btnSearch">검색</a></span>
				<span class="btnSearch center"><a href="javascript:void(0);" id="btnClear">초기화</a></span>
				<span class="btnSearch center"><a href="javascript:void(0);" id="btnSave">저장</a></span>
				<!-- <span class="btnSearch center"><a href="javascript:void(0);" id="btnSearchUser">유저검색</a></span> -->
			</td>
		</tr>
		<tr>
			<th>사원번호</th>
			<td>
				<%= InputText.get("user_sabun", "", 65, 50, "hangule, desc=사번, PerSize") %>
			</td>
			<th>이름</th>
			<td>
				<INPUT type="text" name="user_name" value="<%=params.get("user_name") %>" maxlength="50" class="input" style="IME-MODE : active;width:39%;"   desc='성명' onKeyPress="doKeyPress();">
			</td>
		</tr>
	</table>
	<!-- 검색조건 종료 -->
	
<TABLE border="0" cellspacing="0" cellpadding="0" width="100%">
		<TR >
			<TD width="*" rowspan="5">
				<div id="sheet1_div" style="padding-top:5px; height: auto; min-height: 680px; overflow: auto;">
					<SCRIPT>createIBSheet('sheet1', '98%', '680px');</SCRIPT>
				</div>
			</TD>
			<TD width="10px" rowspan="5">
			</TD>
			<TD width="200px" height="30%">
				<TABLE width="100%" cellspacing="0" cellpadding="0" bordercolordark="#FFFFF" bordercolorlight="#d2d2d2" border="1">
					<TR height="26">
						<Th>현재 권한</Th>
					</TR>
					<TR height="180">
						<TD align=center valing=center class="search_table_input2">
							<select name="right_id_pre" size="10" style="width:220px;">
								<OPTION value=''></OPTION>  
							</select>
						</TD>
					</TR>
				</TABLE>
			</TD>
		</TR>
		<TR>
			<td height="5%">
			</td>
		</TR>
		<TR>
			<td height="30%">
				<TABLE width="100%" cellspacing="0" cellpadding="0" bordercolordark="#FFFFF" bordercolorlight="#d2d2d2" border="1">
					<TR height="26">
						<th>권한 종류</th>
					</TR>
					<TR height="180">
						<TD align=center valing=center class="search_table_input2">
							<select name="right_id_all" size="10" onDblClick="grantAdd()" style="width:220px;">
							<% 	for( int i = 0 ; i < userRight_rs.size(); i++ ) { %>
								<OPTION value='<%=userRight_rs.getRow(i).get("RIGHT_ID")%>'><%=userRight_rs.getRow(i).get("RIGHT_NAME")%></OPTION>  
							<% 	} %>  
							</select>
						</TD>
					</TR>
				</TABLE>
			</td>
		</TR>
		<TR>
			<td height="5%" align="center">
				<IMG onClick="grantAdd()" src="/img/buyer/button/bt_to_down.gif" border="0" style="cursor:hand;" alt="선택한 권한을 부여합니다.">
				<IMG onClick="grantDel()" src="/img/buyer/button/bt_to_up.gif" border="0" style="cursor:hand;" alt="부여된 권한중 선택한 권한을 철회합니다.">
			</td>
		</TR>
		<TR>
			<td height="30%">
				<TABLE width="100%" cellspacing="0" cellpadding="0" bordercolordark="#FFFFF" bordercolorlight="#d2d2d2" border="1">
					<TR height="26">
						<th>적용할 권한</th>
					</TR>
					<TR height="180">
						<TD align=center valing=center class="search_table_input2">
							<select name="right_id" size="10" onDblClick="grantDel()" style="width:220px;">
							</select>
						</TD>
					</TR>
				</TABLE>
			</td> 
		</TR>
	</TABLE>
</FORM>

