<%@page import="kcg.imis.sec.LoginUserVO"%>
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<script type="text/javaScript" defer="defer">
	if('${isEmpty}' == 'true'){
		location.href = '/';
	}
</script>

<div class="memberAdd" id="table">
	<form:form commandName="memberVO" name="memberForm" method="post">
	<table class="tbl_add" width="100%" border="0" cellpadding="0" cellspacing="0"">
		<colgroup>
   			<col width="140"/>
   			<col width="">
   			<col width="140"/>
   			<col width="">
   			<col width="140"/>
   			<col width="">
	    </colgroup>	   		
		<tbody>
			<tr>				
				<th>성 명</th>
				<td colspan="3">
					<form:input path="name"/>
				</td>
			</tr>
			<tr>
				<th>아이디</th>
				<td colspan="3">
					<form:hidden path="userid"/>
					<c:out value="${memberVO.userid}"/>
				</td>
			</tr>
			<tr>
				<th>비밀번호</th>
				<td colspan="3">
					<form:password path="passwd" disabled="true"/> <input type="checkbox" id="passchg" />
				</td>
			</tr>
			<tr>
				<th>소속</th>
				<td>
					<select name="place1" id="place1" onchange="createPlace2()">
					</select>
				</td>
				<th>소속2</th>
				<td>
					<select id="place2" name="place2"></select>
				</td>
			</tr>
			<tr>
				<th>계급</th>
				<td colspan="3">
					<select id="duties" name="duties"></select>
				</td>
			</tr>
			<tr>
				<th>사용자등급</th>
				<td colspan="3">
					<select id="usegrade" name="usegrade">
						<option value="2" <c:if test="${memberVO.usegrade eq '2'}">selected="selected"</c:if>>중간관리자</option>
						<option value="3" <c:if test="${memberVO.usegrade eq '3'}">selected="selected"</c:if>>국제협력관</option>
						<option value="4" <c:if test="${memberVO.usegrade eq '4'}">selected="selected"</c:if>>국제법규팀장</option>
						<option value="6" <c:if test="${memberVO.usegrade eq '6'}">selected="selected"</c:if>>사용자(쓰기)</option>
						<option value="8" <c:if test="${memberVO.usegrade eq '8'}">selected="selected"</c:if>>사용중지</option>
					</select>
				</td>
			</tr>
		</tbody>
	</table>
	</form:form>
</div>
<% 
	LoginUserVO userVO = (LoginUserVO) session.getAttribute("userVO");
%>
<form name="menuForm" method="post">
	<input type="hidden" name="userid" value="${memberVO.userid}"/>
		<div class="boardviewinfo">
		<ul class="right btn-style">
			<c:if test='${(isAdmin and userVO.place1 eq memberVO.place1) or userVO.userid eq memberVO.userid or isSysAdmin}'>
				<li>
	 				<a href="javascript:fn_modify();">저장</a>
	            </li>
			</c:if>
 			<li>
 			 	<a href="javascript:fn_selectList();"><spring:message code="button.list" /></a>
            </li>            
		</ul>	
	</div>
</form>
<script type="text/javaScript" defer="defer">
	$(document).ready(function(){
		init();
		
		$("#passchg").on("click", function(){
			if($(this).is(":checked")){
				$("#passwd").prop("disabled", false);	
			}else{
				$("#passwd").val("");
				$("#passwd").prop("disabled", true);
			}
		});
	});
	
	var isFirst = true;
	function init(){
		createPlace1();
		createDuty();
	}
	
	/* 글 목록 화면 function */
    function fn_selectList() {
       	location.href = "<c:url value='/admin/memberList.do'/>";
    }
	
    /* 수정 */
    function fn_modify() {
    	document.memberForm.action = '<c:out value="/admin/memberUpdate.do"/>';
    	document.memberForm.submit();
    }
	
	function createPlace1(){
		$("#place1").empty();
		$(getCode('PLA1')).each(function(idx, item){
			if('<c:out value="${memberVO.place1}"></c:out>' == item.code)
				$("#place1").append('<option value="'+item.code+'" selected="selected">'+item.name+'</option>')
			else
				$("#place1").append('<option value="'+item.code+'">'+item.name+'</option>')
		});
		createPlace2();
	}
	
	function createPlace2(){
		$("#place2").empty();
		var place1Val = $("#place1").val();
		$(getCode(place1Val)).each(function(idx, item){
			if( '<c:out value="${memberVO.place2}"></c:out>' == item.code && isFirst == true){
					$("#place2").append('<option value="'+item.code+'" selected="selected">'+item.name+'</option>');
					isFirst = false;
			} else
				$("#place2").append('<option value="'+item.code+'">'+item.name+'</option>')
		});
	}
	
	function createDuty(){
		$("#duties").empty();
		$(getCode('DUTY')).each(function(idx, item){
			if( '<c:out value="${memberVO.duties}"></c:out>' == item.code)
				$("#duties").append('<option value="'+item.code+'" selected="selected">'+item.name+'</option>')
			else
				$("#duties").append('<option value="'+item.code+'">'+item.name+'</option>')
		});
	}
	
	function getCode(group){
		return $(codeList).filter(function(idx, item){
			return item.group === group;
		});
	}
	
	
	var codeList = [
       		<c:forEach var="item" items="${code}">
       		{
       			group : '<c:out value="${item.group}"></c:out>',
       			code : '<c:out value="${item.code}"></c:out>',
       			name : '<c:out value="${item.name}"></c:out>'
       		},
       		</c:forEach>
       	];
</script>