<%@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">
                    <input type="text" name="userid"  id="userid" />
					<button type="button" class="ui-button ui-corner-all ui-widget" onclick="checkId()">아이디확인</button>
				</td>
			</tr>
			<tr>
				<th>비밀번호</th>
				<td colspan="3">
					<form:password path="passwd" disabled="false"/>
				</td>
			</tr>
			<tr>
				<th>비밀번호확인</th>
				<td colspan="3">
					<form:password path="passwd2" disabled="false"/>
				</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">중간관리자</option>
						<option value="3">국제협력관</option>
						<option value="4">국제법규팀장</option>
						<option value="6" selected="selected">사용자(쓰기)</option>
						<option value="8">사용중지</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);
			}
		});
		$("#passchg2").on("click", function(){
			if($(this).is(":checked")){
				$("#passwd2").prop("disabled", false);	
			}else{
				$("#passwd2").val("");
				$("#passwd2").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() {
    	if(!registValidation())
    		return;
    	
    	/*아이디 입력하면 disabled 로 막히는 부분인데 그거 false 인 부분 저장되게 추가*/
    	$("#userid").prop("disabled", false);
    	
    	document.memberForm.action = '<c:out value="/admin/memberNewSave.do"/>';
    	document.memberForm.submit();
    }
    
function registValidation(){
  		
  		if($("#userid").val() == ""){
  			alert("아이디를 입력하세요.");
  			$("#userid").focus();
  			return false;
  		}
  		
  		if($("#userid").val().lenth < 4){
  			alert("아이디는 최소 4자 이상으로 입력하세요.");
  			$("#userid").focus();
  			return false;
  		}
  		
  		if($("#userid").prop("disabled") == false){
  			alert("아이디중복을 확인하세요.");
  			return false;	  			
  		}
  		
  		if($("#passwd").val() == ""){
  			alert("비밀번호를 입력하세요.");
  			$("#passwd").focus();
  			return false;
  		}
  		
  		if($("#passwd2").val() == ""){
  			alert("비밀번호확인을 입력하세요.");
  			$("#passwd2").focus();
  			return false;
  		}
  		
  		if($("#passwd").val() != $("#passwd2").val()){
  			alert("비밀번호와 비밀번호 확인이 다릅니다.");
  			$("#passwd").val('');
  			$("#passwd2").val('');
  			$("#passwd").focus();
  			return false;
  		}
  		

  		if($("#name").val() == ""){
  			alert("이름을 입력하세요.");
  			$("#name").focus();
  			return false;
  		}
  		
  		
  		return true;
  	}
  	
	function checkId(){
		 $.ajax({
			url: '<c:url value="/member/checkId.json" />',
			type: 'get',
			cache: false,
			data: {
				userid : $("#userid").val()
				},
			dataType: 'json',
			success: function(data){
				if($("#userid").val()==''){
					alert("아이디를 입력하세요");
					return false;
				}if(data.cnt > 0){
					alert("사용중인 아이디입니다.");
					$("#userid").val('');
					$("#userid").focus();
				}else{
					alert("사용가능한 아이디입니다.");
					$("#userid").prop("disabled", true);
				}

			}
		}); 
	}
	
	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>