<%--
  - FileName   : UserForm.jsp
  - Author     : gyeongmo an
  - Date       : 2014.06.18
  - Copyright  : Copyright (c) www.udapsoft.co.kr, Inc.
  - All rights reserved.
--%>
 
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="com.udapsoft.waf.system.HandlerStorage" %>
<%@ page import="kr.co.hsnc.common.sql.*" %>
<%@ page import="kr.co.hsnc.common.util.*"%>
<%@ page import="kr.co.udapsoft.ebid.buyer.common.file.FileManager"%>
<%@ page import="com.udapsoft.waf.common.ui.*" %>
<%@ page import="com.udapsoft.waf.common.util.DateUtil" %>

<jsp:useBean id="ctx" class="com.udapsoft.waf.system.context.SessionContext" scope="session" />
<%
    ctx.init(request, response);
	String fevent = ctx.get("fevent");
	HandlerStorage storage = ctx.getHandlerStorage();

	ValueObject params = storage.getParams();
	ValueObject user = storage.getValueObject("User");
	ValueObject vo = storage.getValueObject("USER_INFO_FILE");
	
	RowSet rightRowSet = storage.getRowSet("RightRowSet");
	RowSet userRightRowSet = storage.getRowSet("UserRightRowSet");
	RowSet acctTypeRowSet = storage.getRowSet("AcctTypeRowSet"); //계정 타입
%>

<SCRIPT language="JavaScript">

function _onReady(){
	jQuery("#btnList").click(function() {
		var f = document.form1;
		f.fevent.value ="";
	    f.action = "/sycs/user/UserList.screen";
	    f.target = "_self";
	    f.submit();
	}); 

	jQuery("#btnTest").click(function() {
		
		alert("1");
		submitXmlRequest("/sycs/user/UserList_process.screen", "SMS_TEST", document.form1, "SUCCESS");
	}); 
	
	jQuery("#btnPwdInit").click(function() {
		if(confirm("비밀번호 초기화 하시겠습니까?")){	//비밀번호 사번으로 초기화
			submitXmlRequest("/sycs/user/UserList_process.screen", "SAVE_PWD_INIT", document.form1, "SUCCESS");
		}
	}); 
	
	jQuery("#btnSave").click(function() {
		var isModify = <%= (user.get("USER_SABUN")!="")?"true":"false"%>;
		
		var f = document.form1;
		
		if( isEmpty(f.user_sabun) ) {	// 사원번호
			ErrMsg(f.user_sabun, "사원번호를 입력해 주세요.");
	        return false;
	    }

	    if( isEmpty(f.user_pw) ) {	// 암호
	    	ErrMsg(f.user_pw, "비밀번호를 입력해 주세요.");
	        return false;
	    }

	    if( f.user_pw.value != f.user_pw2.value ) { 	//Verify Password check

	    	ErrMsg(f.user_pw2, "비밀번호가 일치하지 않습니다.");
	        return false;
	    }

	    if( getSelectedValue(f.acct_type_id) == "" ) {
	    	ErrMsg(f.acct_type_id, "계정타입을 선택해 주세요.");
	        return false;
	    } 
	    
		if(!checkTelno(f.tel_no.value)) {
			ErrMsg(f.tel_no,"전화번호가 올바르지 않은 형식 입니다.");
			return false;
		}    
		
		if(!checkTelno(f.hand_tel_no.value)) {
			ErrMsg(f.hand_tel_no,"휴대폰번호가 올바르지 않은 형식입니다.");
			return false;
		}    
		
		if(!checkTelno(f.fax_no.value)) {
			ErrMsg(f.fax_no,"팩스번호가 올바르지 않은 형식입니다.");
			return false;
		}    

		var rightIDs = "";
		for( var i = 0 ; i < f.right_id.options.length ; i++ ) {
		
			rightIDs += f.right_id.options[i].value;

			if( i != f.right_id.options.length - 1 )
				rightIDs += ", ";
		}
		f.selected_right_ids.value = rightIDs;
		
		var port = location.port;
		if(port == "") port ="80";
		
		if(confirm("저장하시겠습니까?")){
			if(isModify == false){	   
				//저장
		    	submitXmlRequest("/sycs/user/UserList_process.screen", "UserForm_Reg", f, "SUCCESS");
			}else{
				//수정
				submitXmlRequest("/sycs/user/UserList_process.screen", "SAVE_USER_LIST", f, "SUCCESS");
			}
		}
	}); 
}

function dlgSearchInsaUser(){
    //var win_id = "_searchUserPopup";
	//var link = "/lib/dlgSearchUserPop.screen";
    var win_id = "_searchInsaUser";
	var link = "/sycs/user/dlgSearchUser.screen";
	link += "?pop_win_id="+win_id;
	fLayerPop(win_id, link, 870,500, '', '', 'NO');
}

function SearchUserClose(result) {
	fLayerPopClose('_searchUserPopup');
}

function searchInsaUserClose(resultArray){
	var f = document.form1; 
	if( resultArray != null ) {
	    	
	        f.user_sabun.value = resultArray[0];
	        f.user_name.value = resultArray[1];
		    
			f.job_Cd.value = resultArray[2];	//현장코드
			f.job_dscr.value = resultArray[3];	//현장명
		
			f.buseo_Cd.value = resultArray[4];	//직책코드
			f.buseo_nm.value = resultArray[5];	//직책명

			f.tel_no.value = resultArray[6];	//전화번호
			f.hand_tel_no.value = resultArray[7];	//핸드폰번호
			f.e_mail.value = resultArray[8];	//이메일
		
			f.dept_cd.value = resultArray[10];	//부서코드
			f.dept_nm.value = resultArray[11];	//부서명
	    }
	
	fLayerPopClose('_searchInsaUser');
}

function grantRight() {
	var origin = document.form1.right_ids;
	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++;
			for( j = 0 ; j < targetLength ; j++ ) {
				if( target.options[j].value == origin.options[i].value ) {
					alert(origin.options[i].text+" 권한은 이미 설정되어 있습니다.");
					flag = false;
                    break;
				}
			}
            if(flag) {
                target.options[targetLength] = document.createElement("OPTION");
                target.options[targetLength].text  = origin.options[i].text;
                target.options[targetLength].value = origin.options[i].value;
                targetLength++;
                origin.options[i] = null;
                originLength--;
                i--;
            }
		}
	}

    if( count==0 ) {
        alert("부여할 권한을 선택해 주세요.");
    }
}

function revokeRight() {
    var origin = document.form1.right_ids;
	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 ) {

                origin.options[originLength] = document.createElement("OPTION")
                origin.options[originLength].text  = target.options[i].text;
                origin.options[originLength].value = target.options[i].value;

                originLength++;
                target.options[i] = null;
                i--;
                targetLength--;
            }
            else {

                target.options[i] = null;
                i--;
                targetLength--;
            }
        }
    }

    if( count==0 ) {
        alert("제거할 권한을 선택해 주세요.");
    }
}

function searchAccDept(){
	var win_id = "_searchDept";
	var link = "/lib/dlgSearchAccDept.screen";
	    link += "?pop_win_id="+win_id;
	fLayerPop(win_id, link, 620,400, '', '', 'NO');
}

function searchAccDeptClose(result){
	if( result == null ){
	}else{
		document.form1.dept_cd.value = result[0];   //부서코드
		document.form1.dept_nm.value = result[1];   //부서명
	}
	fLayerPopClose('_searchDept');
}
	
function doResult(resultObj) {
	try {
		if( resultObj.getRequestEvent() == "UserForm_Reg" || resultObj.getRequestEvent() == "SAVE_USER_LIST") {
			if( resultObj.getSuccess() ) {
				alert("저장했습니다.");
				jQuery("#btnList").click();
			}
			else {
				dlgErrorMsg(resultObj);
			}
		}else if(resultObj.getRequestEvent() == "GET_FILE_LIST_TEXTAREA"){
			var resultVO = resultObj.getValueObject();
			document.form1.p_AttachFile.value = resultVO.get("file_txt_list");
			
		}else if(resultObj.getRequestEvent() == "SMS_TEST"){
			if(resultObj.getSuccess()){
				alert("성공하였습니다.");
			}else{
				alert(resultObj);
			}
		}else if(resultObj.getRequestEvent() == "SAVE_PWD_INIT"){
			if(resultObj.getSuccess()){
				alert("비밀번호 초기화하였습니다.");
				jQuery("#btnList").click();
			}else{
				alert(resultObj);
			}
		}
		document.form1.isSubmit = false;
	}
	catch(errorObject) {
		showErrorDlg("doResult()", errorObject);
	}
}

//첨부파일   
function doAttachFile(obj) {
	 if(obj==''){
		alert("저장을 한 후 파일을 첨부할 수 있습니다.");
	}
	else if(obj != ''){ 
//		viewFileListNew('USER_INFO', obj.value, document.form1.p_AttachFile, "Y", "cont/cont-1001");
		viewFileList('USER_INFO', obj.value, document.form1.p_AttachFile, "Y");
		//viewFileList('USER_INFO', obj.value, document.form1.p_AttachFile, "N", "N", "Y");
	} 
}

/*win dialog로 뛰울시 필수 예약 함수*/
function doAttachFileListSet(){
	
	var f = document.form1; 
	var url = "/common/file/FileListData.screen?file_gb=USER_INFO&file_id=<%= params.get("acct_id") %>"; 
	submitXmlRequest(url, "GET_FILE_LIST_TEXTAREA", f, "VALUEOBJECT");
}

function doAttachFileDiv(obj) {
	 if(obj==''){
		alert("저장을 한 후 파일을 첨부할 수 있습니다.");
	}
	else if(obj != ''){ 
		viewFileListNew('USER_INFO', obj.value, document.getElementById("attach_file_view"), "Y", "");
	} 
}
/**
 * 첨부파일 예약 함수
 */
function fileDown(file_id){
		var url="/common/file/FileDownloadServlet";
 		url += "?file_id=" + file_id;
 		
 		window.open(url, "_fileDown", "toolbar=no, width=400, height=100, top=500, left=400, toolbar=no,directories=no,status=no,scrollbars=yes,resize=no,menubar=no,target=new")
}
</SCRIPT>

<FORM name="form1" method="post">

<INPUT type="hidden" name="fevent" value="<%= params.get("fevent") %>">
<INPUT type="hidden" name="acct_id" value="<%= params.get("acct_id") %>">
<INPUT type="hidden" name="dept_nm" value="<%= params.get("dept_nm") %>">
<INPUT type="hidden" name="selected_right_ids" value="">

<INPUT type="hidden" name="job_Cd" value="<%= user.get("PSOSO")%>">
<INPUT type="hidden" name="buseo_Cd" value="<%= user.get("GRADE_CODE")%>">
<INPUT type="hidden" name="p_dept_cd" value="<%= params.get("p_dept_cd")%>">
<INPUT type="hidden" name="p_dept_nm" value="<%= params.get("p_dept_nm")%>">
<INPUT type="hidden" name="p_acct_type_id" value="<%= params.get("p_acct_type_id")%>">

<INPUT type="hidden" name="p_user_sabun" value="<%= params.get("p_user_sabun")%>">
<INPUT type="hidden" name="p_user_name" value="<%= params.get("p_user_name")%>">
<INPUT type="hidden" name="dept_cd2" value="<%= params.get("dept_cd2")%>">
<INPUT type="hidden" name="dept_nm2" value="<%= params.get("dept_nm2")%>">


<INPUT type="file" name="photo" style="display:none;">

	<!-- 제목 및 버튼 -->
   	<table width="100%">
		<colgroup>
			<col width="500" />
			<col width="*" />
		</colgroup>
	<tr>
	   	<th>
		   <div align="left" id="title">
		    <ul>
		     <li>사용자 등록</li>
		    </ul>
		   </div>
	   </th>
	   <td align="right">
			<span class="btnSearch center"><a href="javascript:void(0);" id="btnSave">저장</a></span>
			<span class="btnSearch center"><a href="javascript:void(0);" id="btnList">목록</a></span>
			<span class="btnSearch center"><a href="javascript:void(0);" id="btnTest">smsTest</a></span>
			<span class="btnSearch center"><a href="javascript:void(0);" id="btnPwdInit">비밀번호 초기화</a></span>
	   </td>
   	</tr>
   	</table>


<!-- 데이터 영역 : 시작 -->
<TABLE class="form-table">
		<colgroup>
			<col width="10%" />
			<col width="40%" />
			<col width="10%" />
			<col width="*" />
		</colgroup>	
  <TR height="25"> 
    <th class="required"> 사원번호</th>
	<TD>
			<%= InputText.get("user_sabun", user.get("user_sabun"), 30, 20, "hangule,desc=사번,readOnly, persize") %> 
			<%	if( fevent.equals("UserForm_Reg") ) { %>
			 	<IMG src="/img/buyer/button/bt_search_right.gif" alt="사번검색" border="0" style="cursor:hand" align="absmiddle" onClick="dlgSearchInsaUser();">
			<% 	} %>
    </TD>
	<th class="required"> 이름</th>
	<TD>
		<%= InputText.get("user_name", user.get("user_name_k"), 30, 20, "hangule,readOnly, persize") %>
	</TD>
  </TR>		
  <TR height="25"> 
	<th class="required"> 비밀번호</th>
	<TD> 
		<%= InputText.get("user_pw", user.get("user_pw"), 30, 10, "password, persize") %>
	</TD>
	<th class="required"> 비밀번호 확인</th>
	<TD>
		<%= InputText.get("user_pw2", user.get("user_pw"), 30, 10, "password, persize") %> 
 	</TD>
  </TR>  
  <TR height="25"> 
    <th>전화번호</th>
	<TD>
		<%= InputText.get("tel_no", user.get("tel_no"), 30, 20, "tel, persize") %> 
    </TD>
	<th>휴대폰번호</th>
	<TD>
		<%= InputText.get("hand_tel_no", user.get("hand_tel_no"), 30, 20, "tel, persize") %>
	</TD>
  </TR>
  <TR height="25"> 
	<th>팩스번호</th>
	<TD>
		<%= InputText.get("fax_no", user.get("fax_no"), 30, 20, "tel, persize") %>
	</TD>
	<th> E-Mail</th>
	<TD>
		<%= InputText.get("e_mail", user.get("e_mail"), 30, 50, "persize") %>
	</TD>
  </TR>
   <TR height="25"> 
      <th> 투입프로젝트</th>
      <TD>
      	<%= InputText.get("job_dscr", user.get("Assigned_Dept"), 30, 20, "readOnly, persize") %>
      </TD>      
      <th> 직위</th>
      <TD>
      	<%= InputText.get("buseo_nm", user.get("grade_name"), 30, 50, "readOnly, persize") %>
      </TD>
   </TR>
   <TR height="25">
      <th> 계정타입</th>
      <TD> 
		<%= HTMLMaker.getSelectTag(acctTypeRowSet, "ACCT_TYPE_ID", "ACCT_TYPE_NAME", "acct_type_id", "width:150px;height=18;", "<option value=''>--Select--</option>", "") %>
		<SCRIPT language="JavaScript"> setSelect(document.form1.acct_type_id, "<%= user.get("acct_type_id") %>"); </SCRIPT>
      </TD>
      <th> 퇴사일자</th>
      <TD>
        <%= InputText.get("inactive_date", DateUtil.formatDateTime(user.get("inactive_date"), "yyyy-MM-dd", "yyyy-MM-dd"), 30, 50, "READONLY, persize") %>
       	<%	if(!fevent.equals("UserForm_Reg") ) { %>
		      <IMG name="start_img" src="/img/buyer/button/bt_calender.gif" border="0" align="absmiddle" onClick="JavaScript:dlgCalendar('inactive_date');">
      	<% 	} %>
      </TD>
   </TR>
   <TR height="25">
	<th> 부서</th>
      <TD colspan="3"> 
	    <%= InputText.get("dept_cd", user.get("BUSEO"), 5, 10, "ID,ReadOnly,PerSize") %>
		<%= InputText.get("dept_nm", user.get("Dept_NAME"), 10, 27, "ID,ReadOnly,PerSize") %>
		<%	if(!fevent.equals("UserForm_Reg") ) { %>
	  		<IMG onclick="searchAccDept()" src="/img/buyer/button/bt_search_right.gif" alt="부서찾기" border="0" align="absmiddle" style="cursor:hand;"> 
	    <% 	} %>
      </TD>   
   </TR>
  	<tr>
	    <th> 첨부파일</th>
        <td width="30%" style="padding-top: 3px;" colspan="2"> 
			<TEXTAREA name="p_AttachFile" style="width:95%" rows="1" class="input" readOnly><%=vo.get("USER_INFO_FILE") %></TEXTAREA>
			<IMG src="/img/buyer/icon/icon_plus.gif" alt="adjust size" border="0" align="top" onClick="JavaScript:toggleTextarea(this, document.form1.p_AttachFile, 1, 5);" style="cursor:hand;">
	        <IMG id='search_spot_button' onClick="doAttachFile(document.form1.acct_id);" src='/img/buyer/button/bt_change.gif' border='0'  align="top" align='absmiddle' alt='[Attach File]' style='cursor:hand;'>  		 	
	    </td>
	    <td>
	    	<table width="100%">
	    		<colgroup>
		    		<col width="90%" />
					<col width="10%" />
	    		</colgroup>
	    		<tr>
	    		<td>
				    <div id="attach_file_view" class="attach_file_view">
				    <!-- 첨부 파일 화면에 바로보일때 사용 -->
				    <%=FileManager.getFileListForDiv("USER_INFO", params.get("acct_id")) %>
				    </div>
	    		</td>
	    		<td>
	    			<IMG id='search_spot_button' onClick="doAttachFileDiv(document.form1.acct_id);" src='/img/buyer/button/bt_change.gif' border='0'  align="top" align='absmiddle' alt='[Attach File]' style='cursor:hand;'>
	    		</td>
	    		</tr>
	    	</table>
	    </td>
    </tr>	
    <TR height="155"> 
      <th >권한</th>
      <TD colspan="3">
		       <TABLE cellspacing="0" cellpadding="0" border="0">
		                     <TR>
		                         <TD width="210" align=left>
		                             <SELECT class="input" name="right_ids" size="10" style="width:210px;font-size:12px;" multiple onDblClick="grantRight()"  >
		<% 	for( int i = 0 ; i < rightRowSet.size() ; i++ ) { %>
		
		                                 <OPTION value='<%= rightRowSet.getRow(i).get("RIGHT_ID") %>'><%= rightRowSet.getRow(i).get("DSCR") %></OPTION>
		<% 	} %>
		                             </SELECT>
		                         </TD>
		                         <TD width="50" align=center>
		                             <IMG onClick="grantRight()" src="/img/buyer/button/bt_to_right.gif" border="0" style="cursor:hand;" alt="선택한 권한을 부여합니다.">
		                         	<BR><BR>
		                         	<IMG onClick="revokeRight()" src="/img/buyer/button/bt_to_left.gif" border="0" style="cursor:hand;" alt="부여된 권한중 선택한 권한을 철회합니다.">
		                         </TD>
		                         <TD width="210" align="left" valign="bottom">
		                             <SELECT class="input" name="right_id" size="10" style="width:210px;font-size:12px;" multiple onDblClick="revokeRight()" >
		<% 	for( int i = 0 ; i < userRightRowSet.size() ; i++ ) { %>
		
		                                 <OPTION value='<%= userRightRowSet.getRow(i).get("right_id") %>'><%= userRightRowSet.getRow(i).get("DSCR") %></OPTION>
		<% 	} %>
		                             </SELECT>
		                         </TD>
		                     </TR>
		       </TABLE>
   	 </TD>
   </TR>  
 </TABLE>
</FORM>