<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="ui" uri="http://egovframework.gov/ctl/ui"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="ct" tagdir="/WEB-INF/tags" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

<form:form commandName="memberSearchVO" name="searchForm" method="get">
	<div class="search2">
		<ul>
			<li class="margin-left"><b>소속</b> 　</li>
			<li class="form_search margin-left">
				<form:select path="place1" cssClass="use">
      					<form:option value="" label="전체" />
					<c:forEach var="item" items="${place1List}">
		    			<form:option value="${item.code2}">${item.codenm}</form:option>
		    		</c:forEach>
	 			</form:select>
		 	</li>
		 	<li class="margin-left"><b>이름</b> 　</li>
		 	<li class="margin-left"><form:input path="name"/></li>	 
			<li>
				<span class="btn_blue"> 
					<a href="javascript:fn_link_page(1);"><spring:message code="button.goSearch" /></a>
				</span>
			</li>
			<li>
				<span class="btn_gray"> 
					 <a href="javascript:fn_selectList();"><spring:message code="button.initSearch" /></a>
				</span>
			</li>
		</ul>	       	       
	</div>
   	<form:hidden path="pageIndex" />
   	<form:hidden path="ordertype" />
   	<form:hidden path="ordercode" />
</form:form>
<form:form name="listForm" method="get">
	<input type="hidden" name="userid" />
      	<!-- List -->
	<div id="table">
		<div class="pageinfo">
			<ct:pageInfo />
			<ul class="right btn-style">		
				<li><a class="btn_excel" href="javascript:fn_goExcel();"></a></li>
				<li><a href="#" class="btn_reset" onclick="location.reload();"></a></li>	
			</ul>
		</div>
		<table class="tbl_list" width="100%" border="0" cellpadding="0" cellspacing="0">
			<colgroup>
				<col width="80"/>
	   			<col width="200">
	   			<col width="100"/>
	   			<col width="100">
	   			<col width="100"/>
	   			<col width="60"/>
	   			<col width="60"/>
	   		</colgroup>
			<thead>
			<tr>
				<th align="center"><spring:message code="member.view.seq" /></th>
				<th align="center">
					<span class="order" value="<spring:message code="member.value.order.place1"/>">
						<spring:message code="member.view.place1" />
						<img src="/images/cmmn/blank_1x1.png" />
					</span>
				</th>
				<th align="center">
					<span class="order" value="<spring:message code="member.value.order.position"/>">
						<spring:message code="member.view.position" />
						<img src="/images/cmmn/blank_1x1.png" />
					</span>
				</th>
				<th align="center">
					<span class="order" value="<spring:message code="member.value.order.userid"/>">
						<spring:message code="member.view.userid" />
						<img src="/images/cmmn/blank_1x1.png" />
					</span>
				</th>
				<th align="center">
					<span class="order" value="<spring:message code="member.value.order.name"/>">
						<spring:message code="member.view.name" />
						<img src="/images/cmmn/blank_1x1.png" />
					</span>
				</th>
	   			<th><spring:message code="button.modify"/></th>
	   			<th><spring:message code="button.delete"/></th>
			</tr>
			</thead>
			<tbody>
			<c:choose>
			<c:when test="${fn:length(resultList) > 0}">
			<c:forEach var="result" items="${resultList}" varStatus="status">
				<tr>
					<td align="center" class="listtd"><c:out value="${result.rnum}" /></td>
					<td align="center" class="listtd"><c:out value="${result.place3Str}" /></td>
					<td align="center" class="listtd"><c:out value="${result.positionStr}" /></td>
					<td align="center" class="listtd"><c:out value="${result.useridStr}"/></td>
					<td align="center" class="listtd"><c:out value="${result.name}" /></td>
					
		   			<td align="center" class="listtd">
		   				<c:if test="${userVO.isSysAdmin == true or (userVO.isAdmin == true and result.place1 eq userVO.place1)}">
		   					<a href="#" onclick="modifyUser('<c:out value="${result.userid}"/>', '<c:out value="${result.name}"/>', '<c:out value="${result.place3Str}"/>');"><spring:message code="button.modify" /></a>
		   				</c:if>
		   			</td>
		   			<td align="center" class="listtd">
		   				<c:if test="${userVO.isSysAdmin == true or (userVO.isAdmin == true and result.place1 eq userVO.place1)}">
		   					<a href="#" onclick="deleteUser('<c:out value="${result.userid}"/>', '<c:out value="${result.name}"/>');"><spring:message code="button.delete" /></a>
		   				</c:if>
		   			</td>
		   									
				</tr>
			</c:forEach>
			</c:when>
			<c:otherwise>
				<tr><td align="center" class="listtd" colspan="7"><spring:message code="msg.nodata" /></td></tr>				
			</c:otherwise>
			</c:choose>
			</tbody>
		</table>
	</div>
	<!-- /List -->
	<div id="paging">
		<ui:pagination paginationInfo="${paginationInfo}" type="image" jsFunction="fn_link_page" />
	</div>
</form:form>

<div id="editPopup">
	<div id="divCmmnMemberEditPopup" title="사용자 정보수정">
		<table class="tbl_add">
			<tr>
				<th><spring:message code="member.view.name" /></th>
				<td align="left">
					<input type="hidden" id="hdUserId"/>
					<span id="spanUserName"></span>
				</td>
			</tr>
			<tr>
				<th><spring:message code="member.view.place1" /></th>
				<td align="left"><span id="spanUserPlace"></span></td>
			</tr>
			<tr>
				<th><spring:message code="member.view.password" /></th>
				<td align="left"><input type="password" id="newPassword" /></td>
			</tr>
		</table>
	</div>
</div>

<script type="text/javaScript" defer="defer">
	var popup = null;
	$(document).ready(function(){
		popup = $("#divCmmnMemberEditPopup").dialog({
			autoOpen: false,
			modal: true,
			draggable: false,
			width: 460,
			maxHeight: 600,
			buttons: [{
				text: "확인",
				click: function() {
					updateCmmnMemberPw();
				}
			},
			{
				text: "닫기",
				click: function() {
					$( this ).dialog( "close" );
				}
			}]
		});
		
		
		if (document.searchForm.ordercode && document.searchForm.ordercode.value.length > 0) {
			$('.order[value=' + document.searchForm.ordercode.value + '] img')
				.attr('src', (document.searchForm.ordertype.value == '1') ? '/images/cmmn/order_desc.gif' : '/images/cmmn/order_asc.gif');
		}
		
		$('.order').click(function(){
			var ordercode = $(this).attr('value');
			if (document.searchForm.ordercode.value == ordercode) {
				if (document.searchForm.ordertype.value == 1) {
					document.searchForm.ordertype.value = 0;
				} else {
					document.searchForm.ordertype.value = 1;
				}
			} else {
				document.searchForm.ordertype.value = 0;
			}
			document.searchForm.ordercode.value = ordercode;
			fn_searchList();
		});
		
		$("#name").keydown(function(e){
			if (e.keyCode == 13) {
				fn_link_page(1);
			}
		});
	});
	
	/* 글 목록 화면 function */
    function fn_searchList() {
       	document.searchForm.action = "<c:url value='/admin/cmmnMemberList.do'/>";
       	document.searchForm.submit();
    }
	
	/* 검색초기화 function */
    function fn_selectList() {
       	location.href = "/admin/cmmnMemberList.do";
    }
	
    /* 엑셀 다운로드 function */
    function fn_goExcel() {
       	document.searchForm.method = "post";
       	document.searchForm.action = "<c:url value='/admin/cmmnMemberListExcel.do'/>";
       	document.searchForm.submit();
    }
	
	/* pagination 페이지 링크 function */
	function fn_link_page(pageNo) {
		document.searchForm.pageIndex.value = pageNo;
		document.searchForm.action = "<c:url value='/admin/cmmnMemberList.do'/>";
		document.searchForm.submit();
	}
	
	function modifyUser(userId, userName, placeStr) {
		setPopupData(userId, userName, placeStr);
	}
	
	function deleteUser(userid, username) {
		if (confirm('해당 사용자(' + username + ')의 정보를 삭제하시겠습니까?')){
			$.ajax({
				url : '/member/deleteCmmn.json',
				type : 'post',
				data : { 'userid' : userid },
				dataType : 'json',
				success : function(data) {
					if (data && data.result == 1){
						alert('<spring:message code="member.deleteSuccess.msg"/>');
						location.reload();
					} else {
						alert('<spring:message code="msg.failproc"/>');
					}
				}
			});
		}
	}
	
	function updateCmmnMemberPw() {
		$.ajax({
			type: "post",
			url: "/admin/initPw.json",
			data: {
				targetId: $("#hdUserId").val(),
				passwd: $("#newPassword").val(),
				usertype: "g"
			},
			success: function(data) {
				if (data.result == 1) {
					alert("<spring:message code='member.changePwSuccess.msg'/>");
					popup.dialog( "close" );					
				} else {
					alert("<spring:message code='msg.failproc'/>");
				}
			}
		})
	}
	
	function setPopupData(userId, userName, placeStr) {
		$("#hdUserId").val(userId);
		$("#spanUserName").html(userName);
		$("#spanUserPlace").html(placeStr);
		
		popup.dialog("open");
	};	
</script>