<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.io.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ include file="/jsp/common/include/localemessage.jsp" %>
<%
	response.setHeader("Pragma", "No-cache");
	response.setHeader("Cache-Control", "no-cache");
	response.setHeader("Expires", "0");
%>

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<jsp:include page="/jsp/common/include/css.jsp"/>
<jsp:include page="/jsp/common/include/script.jsp"/>

<script language="javascript" >
var url      = '<c:url value="/common/acl/user/userMan.json" />';
var url_view = '<c:url value="/common/acl/user/userMan.view" />';

var isDetail = false;
function isValid(){
	if($('input[name=userId]').val() == ""){
		alert("<%= localeMessage.getString("user.checkRequired1") %>");
		return false;
	}else if($('input[name=userName]').val() == ""){
		alert("<%= localeMessage.getString("user.checkRequired2") %>");
		return false;
	}
	return true;
}
function init(key,callback){
	$.ajax({  
		type : "POST",
		url:url,
		dataType:"json",
		data:{cmd: 'LIST_INIT_COMBO'},
		success:function(json){
			//new makeOptions("ROLEID","ROLENAME").setObj($("select[name=roleidnfiname]")).setData(json.authRows).setFormat('this.data[index][this.name]').rendering();
			
			if(typeof callback === 'function') {
				callback(key);
			}
		},
		error:function(e){
			alert(e.responseText);
		}
	});
}
function detail(key){
	if (!isDetail)return;
	$.ajax({  
		type : "POST",
		url:url,
		dataType:"json",
		data:{cmd: 'DETAIL', userId : key},
		success:function(json){
			var data = json;
			$("input[name=userId]").attr('readonly',true);
			
			// 상세 권한 보이기
			$(".datailT").css("display","");
			$("input[name=roleidnfiname]").attr('readonly',true);
			
			//W 권한 아닐경우 readonly 
			if ("${rmsMenuAuth}" !="W"){
				$("input[name=userName]").attr('readonly',true);
				$("input[name=dvsnname]").attr('readonly',true);
				$("input[name=teamName]").attr('readonly',true);
				$("input[name=jobtlname]").attr('readonly',true);
				$("input[name=pafiarinfobrncd]").attr('readonly',true);
				$("input[name=intnloutsrcdsticname]").attr('readonly',true);
				$("input[name=cphnno]").attr('readonly',true);
				$("input[name=ofctelno]").attr('readonly',true);
				$("input[name=emad]").attr('readonly',true);
				$("input[name=eaigroupcodstcd]").attr('readonly',true);
				$("input[name=spotprxyempid]").attr('readonly',true);
				$("input[name=spotprxyname]").attr('readonly',true);
				$("input[name=allowIp]").attr('readonly',true);
				$("select[name=status]").attr('readonly',true);	
			}			

			$("#ajaxForm input[type!=radio],#ajaxForm select,#ajaxForm textarea").each(function(){
				var name = $(this).attr("name");
				var tag  = $(this).prop("tagName").toLowerCase();
				$(tag+"[name="+name+"]").val(data[name.toUpperCase()] ? data[name.toUpperCase()].trim():"");
			});			
			
			$("input[name=LASTAMNDYMS]").inputmask("9999-99-99 99:99:99",{'autoUnmask':true});
			/* $("input[name=SECONDMENTSTDT]").inputmask("9999-99-99",{'autoUnmask':true});
			$("input[name=SECONDMENTENDT]").inputmask("9999-99-99",{'autoUnmask':true}); */
			
		},
		error:function(e){
			alert(e.responseText);
		}
	});

}


$(document).ready(function() {	
	var returnUrl = getReturnUrlForReturn();
	var key ="${param.userId}";
	
	$(".datailT").css("display","none");
	
	if (key != "" && key !="null"){
		isDetail = true;
	}
	init(key,detail);
	
	function checkForUpdates() {
		var updatedRoleId = localStorage.getItem('updatedRoleId');
		if(updatedRoleId) {
			$("input[name='roleidnfiname']").val(updatedRoleId);
			localStorage.removeItem('updatedRoleId');
			
			var key = $("input[name='userId']").val();
			 if (key) {
				 init(key, detail);
			}
		}
	}
	
	$("#btn_modify").click(function(){
		
		if (confirm("<%= localeMessage.getString("common.checkModify")%>") != true)
			return;
		
		if (!isValid()){
			return;
		}
		var postData = $('#ajaxForm').serializeArray();
		if (isDetail){
			postData.push({ name: "cmd" , value:"UPDATE"});
		}else{
			postData.push({ name: "cmd" , value:"INSERT"});
		}
		
		$.ajax({
			type : "POST",
			url:url,
			data:postData,
			dataType: "json",
			success:function(json){
				if(json.result == "success"){
					alert("<%= localeMessage.getString("common.saveMsg") %>");
					goNav(returnUrl);//LIST로 이동
				}else{
					alert(json.result);
				}
				
			},
			error:function(e){
				alert(e.responseText);
			}
		});
	});
	$("#btn_previous").click(function(){
		goNav(returnUrl);//LIST로 이동
	});
	
	$("#btn_change").click(function(){
	    var args = {};
	    args['userId'] = $("input[name=userId]").val();
	    
	    var url2 = url_view;
	    url2 += "?cmd=POPUP";
	    //showModal(url2,args,550,600);
	    
	    showModal(url2, args, 550, 600);
    	var checkInterval = setInterval(function() {
    		var updatedRoleId = localStorage.getItem('updatedRoleId');
    		if (updatedRoleId) {
    			clearInterval(checkInterval);
    			$("input[name='roleidnfiname']").val(updatedRoleId);
    			localStorage.removeItem('updatedRoleId');
    		}
   		}, 500);//time out 500ms
	});
	$("#btn_biz_change").click(function(){
	    var args = {};
	    args['userId'] = $("input[name=userId]").val();
	    
	    var url2 = url_view;
	    url2 += "?cmd=POPUP2";
	    showModal(url2,args,980,650);
	});
	$("#btn_service_change").click(function(){
	    var args = {};
	    args['userId'] = $("input[name=userId]").val();
	    
	    var url2 = url_view;
	    url2 += "?cmd=POPUP3";
	    showModal(url2,args,980,650);
	});	
	/* 사용자 권한 및 역할 변경 이력 */
	$("#btn_change_history").click(function(){
	    var args = new Object();
	    args['userId'] = $("input[name=userId]").val();
	    
	    var url2 = url_view;
	    url2 += "?cmd=POPUP4";
	    showModal(url2,args,1050,700);
	});	
	
	$("#btn_initialize").click(function(){
		if (confirm('<%= localeMessage.getString("common.checkModify")%>') != true) return;
		
		$.ajax({  
			type : "POST",
			url:url,
			dataType:"json",
			data:{cmd: 'UPDATE_PASSWORD', userId : $("input[name=userId]").val()},
			success:function(json){
				alert(json.result);
			},
			error:function(e){
				alert("ERROR : " + e.responseText);
			}
		});
	});	
	  $("#btn_delete").click(function(){
	      
	      if(confirm("<%= localeMessage.getString("common.confirmMsg")%>")){  
	         var postData = $('#ajaxForm').serializeArray();
	         postData.push({ name: "cmd" , value:"DELETE"});
	         $.ajax({
	            type : "POST",
	            url:url,
	            data:postData,
	            success:function(args){
	               alert("<%= localeMessage.getString("common.deleteMsg") %>");
	               goNav(returnUrl);//LIST로 이동

	            },
	            error:function(e){
	               alert(e.responseText);
	            }
	         }); 
	      }else{
	         return false;
	      }
	      
	   });   

	buttonControl(isDetail);
	titleControl(isDetail);
});
 
</script>
</head>
	<body>
		<div class="right_box">
			<div class="content_top">
				<ul class="path">
					<li><a href="#">${rmsMenuPath}</a></li>					
				</ul>					
			</div><!-- end content_top -->
			<div class="content_middle">
				<div class="search_wrap">				
					<button type="button" class="cssbtn" id="btn_change_history" level="W" status="DETAIL"><i class="material-icons">sync_alt</i> <%= localeMessage.getString("button.chage_history") %></button>
					<button type="button" class="cssbtn" id="btn_initialize" level="W" status="DETAIL"><i class="material-icons">restart_alt</i> <%= localeMessage.getString("button.initialize") %></button>
					<button type="button" class="cssbtn" id="btn_service_change" level="W" status="DETAIL"><i class="material-icons">sync_alt</i> <%= localeMessage.getString("button.serviceChange") %></button>
					<button type="button" class="cssbtn" id="btn_biz_change" level="W" status="DETAIL"><i class="material-icons">sync_alt</i> <%= localeMessage.getString("button.bizChange") %></button>
					<button type="button" class="cssbtn" id="btn_change" level="W" status="DETAIL"><i class="material-icons">group</i> <%= localeMessage.getString("button.roleChange") %></button>
					<button type="button" class="cssbtn" id="btn_modify" level="W" status="DETAIL,NEW"><i class="material-icons">save</i> <%= localeMessage.getString("button.modify") %></button>
					<button type="button" class="cssbtn" id="btn_delete" level="W" status="DETAIL"><i class="material-icons">delete</i> <%= localeMessage.getString("button.delete") %></button>
					<button type="button" class="cssbtn" id="btn_previous" level="R" status="DETAIL,NEW"><i class="material-icons">arrow_back</i> <%= localeMessage.getString("button.previous") %></button>
				</div>
				<div class="title"><%= localeMessage.getString("userDetail.title") %></div>	
				
				<form id="ajaxForm">
					<table class="table_row" cellspacing="0">
						<input type="hidden" name="jobclcd">
						<tr>
							<th style="width:180px;"><%= localeMessage.getString("login.userId") %></th><td ><input type="text" name="userId"/> </td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("user.name") %> </th><td><input type="text"  name="userName" /></td>
						</tr>
						<tr class="datailT">
							<th><%= localeMessage.getString("user.auth") %> </th><td><input type="text"  name="roleidnfiname" /></td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("user.depart") %> </th><td><input type="text"  name="dvsnname"/></td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("user.teamName") %> </th><td><input type="text"  name="teamName"/></td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("user.position") %> </th><td><input type="text"  name="jobtlname"/></td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("user.depCode") %> </th><td><input type="text"  name="pafiarinfobrncd"/></td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("user.inOutCode") %> </th><td><input type="text"  name="intnloutsrcdsticname"/></td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("user.mobile") %> </th><td><input type="text"  name="cphnno" /></td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("user.phone") %> </th><td><input type="text"  name="ofctelno"/></td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("user.email") %> </th><td><input type="text"  name="emad" /></td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("user.allowIp") %> </th><td><input type="text"  name="allowIp" /></td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("user.status") %> </th>
								<td>
									<div class="select-style">
									<select name="status">
										<option value=""><%= localeMessage.getString("code.normal") %></option>
										<option value="1"><%= localeMessage.getString("code.normal") %></option>
										<option value="2"><%= localeMessage.getString("code.lock") %></option>
									</select>
								</div>	
								</td>
						</tr>
						<%-- <tr>
							<th><%= localeMessage.getString("user.eaiGrupCd") %> </th><td><input type="text"  name="eaigroupcodstcd" /></td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("user.fieldAgentId") %> </th><td><input type="text"  name="spotprxyempid"/></td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("user.fieldAgentName") %> </th><td><input type="text"  name="spotprxyname" /></td>
						</tr> --%>
						<tr>
							<th><%= localeMessage.getString("user.createOn") %> </th><td><input type="text"  name="LASTAMNDYMS" readonly/></td>
						</tr>
				<%-- 		<tr>
							<th><%= localeMessage.getString("user.secondmentbrncd") %> </th><td><input type="text"  name="SECONDMENTBRNCD" readonly/></td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("user.secondmentstdt") %> </th><td><input type="text"  name="SECONDMENTSTDT" readonly/></td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("user.secondmentstdt") %> </th><td><input type="text"  name="SECONDMENTENDT" readonly/></td>
						</tr> --%>
					</table>
				</form>
				
			</div><!-- end content_middle -->
		</div><!-- end right_box -->	
	</body>
</html>

