<%@ 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="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="ui" uri="http://egovframework.gov/ctl/ui"%>
<script type="text/javaScript" defer="defer">
$(document).ready(function(){
		
	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();
	});
	
	var loginUserId = "${loginUserVO.userid}";
	
	if (loginUserId != 'tcdadmin') {
		$("#divSearch").hide();
	}
});
	
/* 선택아이디로 로그인 */
function loginOtherUser(userid) {
	document.searchForm.userid.value = userid;
	document.searchForm.action = "<c:url value='/admin/loginUser.do'/>";
	document.searchForm.submit();
}	
	
/* pagination 페이지 링크 function */
function fn_link_page(pageNo) {
	document.searchForm.pageIndex.value = pageNo;
	document.searchForm.action = "<c:url value='/admin/memberAuth.do'/>";
	document.searchForm.submit();
}	
/* 글 목록 화면 function */
function fn_searchList() {
	document.searchForm.pageIndex.value = 1;
   	document.searchForm.action = "<c:url value='/admin/memberAuth.do'/>";
   	document.searchForm.submit();
}

		
function get_auth_type(place)
{
	p_type = 10;
	if(place.toUpperCase() == "PS00"){
		p_type = 1;			
	}else if(place.toUpperCase() == "PS92" || place.toUpperCase() == "PS93" || place.toUpperCase() == "PS94" || place.toUpperCase() == "PS91"|| place.toUpperCase() == "ps90"){
		p_type = 6;						
	}
	return p_type;
}


//권한 코드 관련

var AuthCode = {
	inputAuth : 0,
	inputAuthEl : null,
	inputRpt : 0,
	inputRptEl : null,
	place1 : '${targetPlace1}',
	ptype : parseInt(get_auth_type('${targetPlace1}')),
	// 전체 시스템 권한 주기
	applyAuth : function (ev, userid, obj) {
		var usegrade = $(obj).parent().children('input[name=usegrade]').val();
		$.ajax({
			url: '/admin/memberAuth.json',
			type: 'get',
			cache: false,			
			data: {'userid' : userid, 'usegrade' : usegrade},
			dataType: 'json',
			success: function(data){
				if(data.result == '1'){
					alert('적용되었습니다.');
				}
			}
		});
		
	},
	makeAuth : function(ev, obj) {
		ev = domEvent(ev);
		var jqE = jQuery.event.fix(ev);
		
		this.inputAuthEl = obj;
		this.inputAuth = this.inputAuthEl.value;
		
	    $('#auth_control_pannel').css({
			left : jqE.pageX + 'px',
			top : jqE.pageY + 'px'
		}).show();

	    var code = parseInt(this.inputAuth) - this.ptype;
	    switch(code){
			case 1:
				$('#auth_id1').prop('checked', true);
				break;
			case 2: 
				$('#auth_id2').prop('checked', true);
				break;
			case 3: 
				$('#auth_id3').prop('checked', true);
				break;
			case 4: 
				$('#auth_id4').prop('checked', true);
				break;
			default:
				$('#auth_id1').prop('checked', false);
				$('#auth_id2').prop('checked', false);
				$('#auth_id3').prop('checked', false);
				$('#auth_id4').prop('checked', false);
				break;
		}
	}, 
	calcAuth : function() {
		var code = parseInt($('.auth_id:checked').val()) + this.ptype;
		code = code ? code : 0;
		this.inputAuth = code;
		this.inputAuthEl.value = this.inputAuth;
		$('#auth_control_pannel').hide();
	},
	cancelAuth : function() {
		$('#auth_control_pannel').hide();
	},
	
	//견문 권한 설정 함수
	applyRpt : function (ev, userid, obj) {
		var rptgrade = $(obj).parent().children('input[name=rptgrade]').val();
		$.ajax({
			url: '/admin/memberRpt.json',
			type: 'get',
			cache: false,			
			data: {'userid' : userid, 'usegrade' : rptgrade},
			dataType: 'json',
			success: function(data){
				if(data.result == '1'){
					alert('적용되었습니다.');
				}
			}
		});
	},
	makeRpt : function(ev, obj) {
		ev = domEvent(ev);
		var jqE = jQuery.event.fix(ev);		
		
		this.inputRptEl = obj;
		this.inputRpt = this.inputRptEl.value;
		
	    $('#auth_control_pannel_rpt').css({
			left : jqE.pageX + 'px',
			top : jqE.pageY + 'px'
		}).show();
		
		var inputRpt = this.inputRpt;
	    $('.auth_rpt').each(function(){
	    	$(this).prop('checked', ((inputRpt & $(this).val()) != 0) ? true : false);
	    });
		
	}, 
	calcRpt : function() {
		var code = 0;
		$('.auth_rpt').each(function(){
	    	$(this).prop('checked') && (code += parseInt($(this).val()));
	    });
		this.inputRpt = code;
		this.inputRptEl.value = this.inputRpt;
		$('#auth_control_pannel_rpt').hide();
	},
	cancelRpt : function(ev, userid) {
		$('#auth_control_pannel_rpt').hide();
	},
	selectAuthHead : function(userid) {
		var place1 = this.place1;
		$.ajax({
			url: '/admin/updateHead.json',
			type: 'get',
			cache: false,
			data: {'userid' : userid, 'place1' : place1},
			dataType: 'json',
			success: function(data){
				if(data.result != '1'){
					alert('적용에 실패하였습니다.');
				}
			}
		});
	},
	selectAuthSection : function(userid) {
		var place1 = this.place1;
		$.ajax({
			url: '/admin/updateSection.json',
			type: 'get',
			data: {'userid' : userid, 'place1' : place1},
			dataType: 'json',
			cache: false,
			success: function(data){
				if(data.result != '1'){
					alert('적용에 실패하였습니다.');
				}
			}
		});
	}
	
}
</script>

<form:form commandName="memberSearchVO" name="searchForm" method="get">
	<form:hidden path="pageIndex" />
	<form:hidden path="ordertype" />
	<form:hidden path="ordercode" />
	<form:hidden path="userid" />
<c:if test="${loginUserVO.authAdmin eq 1}">	
	<div class="search2" id="divSearch">
		<ul>
			<li class="margin-left"><b>소속</b> 　</li>
			<li class="form_search margin-left">
				<form:select path="place1" cssClass="use">
					<c:forEach var="item" items="${placeList}">
		    			<form:option value="${item.code2}">${item.codenm}</form:option>
		    		</c:forEach>
	 			</form:select>
		 	</li>
			<li>
				<span class="btn_blue"> 
					<a href="javascript:fn_searchList();"><spring:message code="button.goSearch" /></a>
				</span>
			</li>
		</ul>	       	       
	</div>
</c:if>
</form:form>

<div class="tbl_listinfo">
	* <spring:message code="member.view.rptPermission.section" /> : 
	<c:out value="${section.name}" /> (<c:out value="${section.userid}" />)
	* <spring:message code="member.view.rptPermission.head" /> : 
	<c:out value="${head.name}" /> (<c:out value="${head.userid}" />)
</div>
	
<form:form name="listForm" method="get">
       	<!-- List -->
		<div id="table">
			<table class="tbl_list" width="100%" border="0" cellpadding="0" cellspacing="0">
				<colgroup>
					<col width="70">
					<col width="160">
					<col width="180">										
					<col width="80">					
					<col width="100">															
					<col width="55">
					<col width="55">															
					<col width="90">										
					<col width="60">										
					<col width="60">															
					<col width="90">
				</colgroup>
				<thead>
				<tr>
					<th align="center" rowspan="2"><spring:message code="member.view.seq" /></th>
					<th align="center" rowspan="2">
						<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" rowspan="2">
						<span class="order" value="<spring:message code="member.value.order.place2"/>">
							<spring:message code="member.view.place2" />
							<img src="/images/cmmn/blank_1x1.png" />
						</span>
					</th>
					<th align="center" rowspan="2">
						<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 align="center" rowspan="2">
						<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" rowspan="2">
						<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" rowspan="2">
						<span class="order" value="<spring:message code="member.value.order.duties"/>">
							<spring:message code="member.view.duties" />
							<img src="/images/cmmn/blank_1x1.png" />
						</span>
					</th>
					<th align="center" rowspan="2"><spring:message code="member.view.sysPemission" /></th>
					<th class="tbl_list_col" align="center" colspan="3"><spring:message code="member.view.rptPermission" /></th>
				</tr>
				<tr>
					<th align="center"><spring:message code="member.view.rptPermission.section" /></th>
					<th align="center"><spring:message code="member.view.rptPermission.head" /></th>
					<th align="center"><spring:message code="member.view.rptPermission.permission" /></th>
				</tr>
				</thead>
				<c:forEach var="result" items="${resultList}" varStatus="status">
					<tr class="tbl_list_input">
						<td align="center"><c:out value="${result.rnum}" /></td>
						<td><c:out value="${result.place1Str}" /></td>
						<td><c:out value="${result.place2Str}" /> <c:out value="${result.place3Str}" /></td>
						<td align="center"><c:out value="${result.name}" /></td>
						<td align="center"><a href="javascript:loginOtherUser('<c:out value="${result.userid}" />');"><c:out value="${result.userid}" /></a></td>
						<td align="center"><c:out value="${result.positionStr}" /></td>
						<td align="center"><c:out value="${result.dutiesStr}" /></td>
						<td align="center">
							<input class="usegrade" style="width:20px;" type="text" name="usegrade" readOnly="readOnly" value="<c:out value="${result.usegrade}"/>" onclick="AuthCode.makeAuth(event, this);"/>
							<input type="button" value="적용" onclick="AuthCode.applyAuth(event, '${result.userid}', this);"/>
						</td>
						<td align="center"><input type="radio" name="section" <c:if test="${result.authSection == '1'}">checked</c:if> value="<c:out value="${result.userid}"/>" onclick="AuthCode.selectAuthSection('${result.userid}');"/></td>
						<td align="center"><input type="radio" name="head" <c:if test="${result.authHead== '1'}">checked</c:if> value="<c:out value="${result.userid}"/>" onclick="AuthCode.selectAuthHead('${result.userid}');"/></td>
						<td align="center">
							<input style="width:20px;" type="text" name="rptgrade" readOnly="readOnly" value="<c:out value="${result.authCode}"/>" onclick="AuthCode.makeRpt(event, this);"/>
							<input type="button" value="적용" onclick="AuthCode.applyRpt(event, '${result.userid}', this);"/>
						</td>
					</tr>
				</c:forEach>
			</table>
		</div>
</form:form>
<!-- /List -->
<div id="paging">
	<ui:pagination paginationInfo="${paginationInfo}" type="image" jsFunction="fn_link_page" />
	<input type="hidden" name="pageIndex" />
</div>
<div id="auth_control_pannel">
	<ul>
		<li>
			<input class="auth_id" type="radio" name="usergrade" value="1" id="auth_id1">
			<label for="auth_id1">과장&nbsp;&nbsp;</label>
		</li>
		<li>
			<input class="auth_id" type="radio" name="usergrade" value="2" id="auth_id2">
			<label for="auth_id2">계장&nbsp;&nbsp;</label>
		</li>
		<li>
			<input class="auth_id" type="radio" name="usergrade" value="3" id="auth_id3">
			<label for="auth_id3">담당자&nbsp;(관리자)&nbsp;</label>
		</li>
		<li>
			<input class="auth_id" type="radio" name="usergrade" value="4" id="auth_id4">
			<label for="auth_id4">일반요원&nbsp;&nbsp;</label>
		</li>
		<li class="text-center">
			<input type="button" value="확인" onclick="AuthCode.calcAuth();"/> 
			<input type="button" value="취소" onclick="AuthCode.cancelAuth();"/>
		</li>
	</ul>
</div>
<div id="auth_control_pannel_rpt">
	<ul>
		<li>	
			<input type="checkbox" class="auth_rpt" value="1" id="auth_rpt_id1">
			<label for="auth_rpt_id1">검색기능</label>
		</li>
		<li>
		    <input type="checkbox" class="auth_rpt" value="2" id="auth_rpt_id2">
			<label for="auth_rpt_id2">통계기능</label>
		</li>
		<li>
			<input type="checkbox" class="auth_rpt" value="4" id="auth_rpt_id3">
			<label for="auth_rpt_id3">결재조회기능</label>
		</li>
		<li>
			<input type="checkbox" class="auth_rpt" value="8" id="auth_rpt_id4">
			<label for="auth_rpt_id4">관리기능</label>
		</li>
		<li>
			<input type="checkbox" class="auth_rpt" value="16" id="auth_rpt_id5">
			<label for="auth_rpt_id5">계장대행</label>
		</li>
		<li>
			<input type="checkbox" class="auth_rpt" value="32" id="auth_rpt_id6">
			<label for="auth_rpt_id6">과장대행</label>
		</li>
		<li class="text-center">
			<input type="button" value="확인" onclick="AuthCode.calcRpt();" /> 
			<input type="button" value="취소" onclick="AuthCode.cancelRpt();" />
		</li>
	</ul>
</div>
