<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="ui" uri="http://egovframework.gov/ctl/ui"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<script language=javascript>
$(document).ready(function(){
	usrDeptChk();
	
});

function fn_reg_delete (p_accessNo){


		if(confirm("신청을 취소하시겠습니까?")) {	
			$.ajax({
				type: "POST",
				 //url: "/uss/umt/user/selectMberSttusAjax.do",
				 url: "/uss/ion/apikey/AdminAPIKEYDeleteRealAjax.do",

				 data: {
					 "accessNo" : p_accessNo
					 },
				 
				 dataType:'json',
				 success: function (returnData, status) {
					 if(status == 'success'){
						 alert("취소 처리 완료 하였습니다.");
						 location.reload();
				   }else{
						   alert("취소 처리에 실패하였습니다.");
						   location.reload();
				   }
			   },
			   error: function (e) {
				   alert("변경에 실패하였습니다.~2");
				   location.reload();
			   }
			});		
		}    
		
}




/* API KEY 사용/미사용 처리 */
function fn_delete(p_accessNo, p_useYn){
	//frm = document.writeForm;
	//var p_accessNo	=	frm.accessNo.value;

	if(confirm("API KEY 사용 상태 값을 변경합니까?")) {	
		$.ajax({
			type: "POST",
			 //url: "/uss/umt/user/selectMberSttusAjax.do",
			 url: "/uss/ion/apikey/UserAPIKEYDeleteAjax.do",

			 data: {
				 "accessNo" : p_accessNo
				 , "useYn" : p_useYn
				 },
			 
			 dataType:'json',
			 success: function (returnData, status) {
				 if(status == 'success'){
					 alert("변경 처리 완료 하였습니다.");
					 location.reload();
			   }else{
					   alert("변경에 실패하였습니다.~1");
					   location.reload();
			   }
		   },
		   error: function (e) {
			   alert("변경에 실패하였습니다.~2");
			   location.reload();
		   }
		});		
	}    
	

	
}


/* API KEY 실제 DB 삭제 - 임시 */
function fn_delete_real(p_accessNo){
	//frm = document.writeForm;
	//var p_accessNo	=	frm.accessNo.value;

// 	if(confirm("API신청 정보(API KEY, 발송서버 IP 데이터 모두 포함)를 삭제처리합니다.삭제후 복구 불가능합니다. 진행하시겠습니까?\n\n(임시 미사용시에는 API KEY 관리의 미사용처리 기능을 이용해주세요~)")) {	
	if(confirm("API신청 정보를 삭제처리합니다.삭제후 복구 불가능합니다. 진행하시겠습니까?")) {	
		$.ajax({
			type: "POST",
			 //url: "/uss/umt/user/selectMberSttusAjax.do",
			 url: "/uss/ion/apikey/UserAPIKEYDeleteRealAjax.do",

			 data: {
				 "accessNo" : p_accessNo
				 },
			 
			 dataType:'json',
			 success: function (returnData, status) {
				 if(status == 'success'){
					 alert("API KEY 정보 모두 삭제 하였습니다.");
					 location.reload();
			   }else{
					   alert("삭제에 실패하였습니다.~1");
					   location.reload();
			   }
		   },
		   error: function (e) {
			   alert("삭제에 실패하였습니다.~2");
			   location.reload();
		   }
		});		
	}    
}

/* API KEY 변경 */
function fn_update_apikey(p_accessNo){
	//frm = document.writeForm;
	//var p_accessNo	=	frm.accessNo.value;

	if(confirm("API KEY 변경 후 복구 불가능합니다. 기존 API KEY는 사용할수 없습니다. 진행하시겠습니까?")) {	
		$.ajax({
			type: "POST",
			 //url: "/uss/umt/user/selectMberSttusAjax.do",
			 url: "/uss/ion/apikey/UserAPIUpdateAPIKEYAjax.do",

			 data: {
				 "accessNo" : p_accessNo
				 },
			 
			 dataType:'json',
			 success: function (returnData, status) {
				 if(status == 'success'){
					 alert("API KEY 변경 하였습니다.");
					 location.reload();
			   }else{
					   alert("API KEY 변경 실패하였습니다.1");
					   location.reload();
			   }
		   },
		   error: function (e) {
			   alert("API KEY 변경 실패하였습니다.2");
			   location.reload();
		   }
		});		
	}
}

/* CALL INFO 호출정보 생성 */
function fn_insert_callInfo(p_accessNo){
	//frm = document.writeForm;
	//var p_accessNo	=	frm.accessNo.value;
	
	
	
	
	var callInfo = $("#callInfo").val();
	if ($("#callInfo").val()==''){
		alert("호출지 정보는 필수입니다.");
		return false;
	}
	
	var ipPattern = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;


	if (!ipPattern.test(callInfo)) {
		alert("IP 형식이 아닙니다. 다시 확인하시고 입력해 주세요");
		return false;
	}
	
	
	$.ajax({
		type: "POST",
		 //url: "/uss/umt/user/selectMberSttusAjax.do",
		 url: "/uss/ion/apikey/UserAPIInsertCallInfoAjax.do",

		 data: {
			 "accessNo" : p_accessNo
			 ,"callInfo" : $("#callInfo").val()
			 ,"callType" : 'IP'
			 },
		 
		 dataType:'json',
		 success: function (returnData, status) {
			 if(status == 'success'){
				 alert("API 호출정보생성 하였습니다.");
				 location.reload();
		   }else{
				   alert("호출정보생성에 실패하였습니다.");
				   location.reload();
		   }
	   },
	   error: function (e) {
		   alert("호출정보생성에 실패하였습니다.");
		   location.reload();
	   }
	});
}

/* CALL INFO 호출정보 삭제 */
function fn_delete_callInfo(p_infoNo, p_accessNo 
		){
	//frm = document.writeForm;
	//var p_accessNo	=	frm.accessNo.value;

	if(confirm("삭제후 복구 불가능합니다. 진행하시겠습니까?")) {	
		$.ajax({
			type: "POST",
			 //url: "/uss/umt/user/selectMberSttusAjax.do",
			 url: "/uss/ion/apikey/UserAPIDeleteCallInfoAjax.do",

			 data: {
				 "accessNo" : p_accessNo
				 ,"infoNo" : p_infoNo
				 },
			 
			 dataType:'json',
			 success: function (returnData, status) {
				 if(status == 'success'){
					 alert("API 호출정보삭제 하였습니다.");
					 location.reload();
			   }else{
					   alert("호출정보삭제에 실패하였습니다.");
					   location.reload();
			   }
		   },
		   error: function (e) {
			   alert("호출정보삭제에 실패하였습니다.");
			   location.reload();
		   }
		});		
	}
}

/* API KEY 생성11 */
function fn_reg_user_4_apikey(){
	//frm = document.listForm;
	var p_mberId	=	$('#mberId').val();

	var form = document.checkForm;
	var data = new FormData(form);

	if(confirm("API를 신청하시겠습니까?")) {
		$.ajax({
			type: "POST",
			 url: "/uss/ion/apikey/UserAPIInsertAPIKEYAjax.do",
			 data: {
					"mberId": p_mberId
				},
			dataType:'json',
			success: function (returnData, status) {
				if(returnData.result == 'success'){
					alert("API KEY 사용자 등록을 하였습니다.");
					location.reload();
				}else if(returnData.result == 'db'){
					alert("API KEY 사용자 등록을 실패하였습니다.(아이디 중복여부 확인해주세요)");
					location.reload();			       
				}else if(returnData.result == 'fail'){
					alert("API KEY 사용자 등록을 실패하였습니다.(아이디 정상여부 확인해주세요)");
					//location.reload();			       
				}else if(returnData.result == 'deptC'){
					alert(returnData.message);
					
				}else{
					alert("API KEY 사용자 등록을 실패하였습니다.(아이디 중복 확인해주세요)");
					location.href='<c:url value="/web/user/membershipChange.do" />';
				}
			},
			error: function (e) {
				alert("API KEY 생성 실패하였습니다. 관리자에게 문의해 주세요");
				location.reload();
			}
		});
	}
}

</script>


<form id="checkForm" name="checkForm">
	<input type="hidden" id="mberId" value="<c:out value="${mberId }" />" />
</form>
<!-- content 영역 -->
<div class="inner">
	<!-- send top -->
	<div class="send_top">
	
		<c:import url="./top_tepMenu.jsp" />
		

			<!-- 문자보내기 -->
			<div class="top_content api_guide_cont current">
					<div class="heading">
						<h2>신청/관리</h2>
					</div>                   
					
					<!--신청/관리 내용-->
					<!--API 신청 정보 : 신청 전-->
					<div class="api_admin">
						<ul class="info">
							<li>- 서비스 사용  신청 시, 문자 및 카카오톡 연동서비스를 모두 이용하실 수 있습니다.</li>
							<li>- 관리자 승인 이후 발송 서버 IP를 등록해 주셔야 합니다.</li>
						</ul>
						<div class="admin_title">API 신청 정보</div>
						<div class="tb_wrap_t1">
							<table class="tType4">
								<caption>API 신청 일시, 승인 일시, 상태, 관리 테이블</caption>
								<colgroup>
									<col style="width:35%">
									<col style="width:35%">
									<col style="width:15%">
									<col style="width:15%">
								</colgroup>
								<thead>
									<tr>
										<th>신청 일시</th>
										<th>승인 일시</th>
										<th>상태</th>
										<th>관리</th>
									</tr>
								</thead>
								<tbody>
									<tr>
									<c:choose>
										<c:when test="${apiVO.useYn eq 'A'}"><!-- API 신청 중 -->
											<td>${apiVO.frstRegistPnttm}</td>
											<td>-</td>
											<td>심사중</td>
											<td><button type="button" class="btnType btnType23" onclick="fn_reg_delete('${apiVO.accessNo}')">신청 취소</button></td>             
										</c:when>					
										<c:when test="${apiVO.useYn eq 'N' or apiVO.accessKey eq ''}"><!-- API 미승인 -->
											<td>${apiVO.frstRegistPnttm}</td>
											<td>${apiVO.lastUpdtPnttm}</td>
											<td>미승인</td>
											<td>
												<button type="button" class="btnType btnType23" onclick="fn_delete_real('${apiVO.accessNo}'); return false;">삭제</button>
												<button type="button" class="btnType btnType14" onclick="fn_delete('${apiVO.accessNo}','A'); return false;">재신청</button>
											</td>             
										</c:when>
										<c:when test="${apiVO.useYn eq 'Y' or (apiVO.useYn eq 'N' and apiVO.accessKey ne '') or apiVO.useYn eq 'W' }"><!-- API 사용 중 -->
											<td>${apiVO.frstRegistPnttm}</td>
											<td>${apiVO.lastUpdtPnttm}</td>
											<td>승인</td>
											<td>
												<button type="button" class="btnType btnType23" onclick="fn_delete_real('${apiVO.accessNo}'); return false;">사용 취소</button>
											</td>  
										</c:when>
										<c:otherwise>
											<td colspan="4">신청 내역이 없습니다.</td>         
										</c:otherwise>
									</c:choose>
									</tr>
								</tbody>
							</table>
						</div>
						<c:if test="${apiVO.useYn eq null}">
							<button type="button" class="btnType btnType2" onclick="fn_reg_user_4_apikey(); return false;">사용 신청</button>              
						</c:if>
					</div>
					<!--// API 신청 정보 : 신청 전-->                                     
					<!--API KEY 관리 : 발급 전-->
					<div class="api_admin">
						<div class="admin_title">API KEY 관리</div>
						<div class="tb_wrap_t1">
							<table class="tType4">
								<caption>API KEY 생성 일시, API KEY, 상태, 관리 테이블</caption>
								<colgroup>
									<col style="width:35%">
									<col style="width:35%">
									<col style="width:15%">
									<col style="width:15%">
								</colgroup>
								<thead>
									<tr>
										<th>생성 일시</th>
										<th>API KEY</th>
										<th>상태</th>
										<th>관리</th>
									</tr>
								</thead>
								<tbody>
									<tr>
										<c:choose>
											<c:when test="${apiVO.useYn eq 'Y' }">
												<td>
													${apiVO.lastUpdtPnttm}
												</td>
												<td>
													${apiVO.accessKey}
												</td>
												<td>
													사용
												</td>
												<td>
													<button type="button" class="btnType btnType23" onclick="fn_delete('${apiVO.accessNo}','W'); return false;">정지</button>
													<button type="button" class="btnType btnType14" onclick="fn_update_apikey('${apiVO.accessNo}'); return false;">재발급</button>
												</td>
											</c:when>
											<c:when test="${apiVO.useYn eq 'W' }">
												<td>
													${apiVO.lastUpdtPnttm}
												</td>
												<td>
													${apiVO.accessKey}
												</td>
												<td>
													미사용
												</td>
												<td>
													<button type="button" class="btnType btnType14" onclick="fn_delete('${apiVO.accessNo}','Y'); return false;">사용</button>
												</td>
											</c:when>
											<c:otherwise>
												<td colspan="4">API KEY 발급 내역이 없습니다.</td>
											</c:otherwise>
										</c:choose>
									</tr>
								</tbody>
							</table>
						</div>
						<p class="admin_info">* API KEY는 계정당 1개만 제공됩니다.</p>              
					</div>
					<!--// API KEY 관리 : 발급 전-->                    
					<!--API 사용 IP 관리 : IP등록 전-->
					<div class="api_admin">
						<div class="admin_title">API 사용 IP 관리</div>
						<div class="tb_wrap_t1">
							<table class="tType4">
								<caption>IP 등록 일시, IP, 관리 테이블</caption>
								<colgroup>
									<col style="width:35%">
									<col style="width:35%">
									<col style="width:30%">
								</colgroup>
								<thead>
									<tr>
										<th>등록 일시</th>
										<th>IP</th>
										<th>관리</th>
									</tr>
								</thead>
								<tbody>
										<c:choose>
											<c:when test="${apiVO.useYn eq 'Y' or apiVO.useYn eq 'N' or apiVO.useYn eq 'W'}">
												<c:forEach var="result" items="${apiCallInfoVOList}" varStatus="status">
													<tr>
														<td>${result.frstRegistPnttm}</td>
														<td>${result.callInfo}</td>
														<td><button type="button" class="btnType btnType23" onclick="fn_delete_callInfo('${result.infoNo}', '${result.accessNo}'); return false;">삭제</button></td>
													</tr>
												</c:forEach>
												<c:if test="${apiCallInfoVOListSize eq 0}">
													<tr>
														<td colspan="3">사용 IP 등록 내역이 없습니다.</td>
													</tr>
												</c:if>
											</c:when>
											<c:otherwise>
												<tr>
													<td colspan="3">사용 IP 등록 내역이 없습니다.</td>
												</tr>
											</c:otherwise>
										</c:choose>
								</tbody>
							</table>
						</div>
                        <!--사용 승인 될 경우에만 노출-->
                        <c:choose>
                        	<c:when test="${apiVO.useYn eq 'Y' and apiCallInfoVOListSize lt 11}">
		                        <div class="input_box">
		                            <div class="box_in">IP입력 : <input type="text" id="callInfo" placeholder="IP를 입력해주세요"><button type="botton" class="btnType btnType2" onclick="fn_insert_callInfo('${apiVO.accessNo}'); return false;">IP 등록</button></div>                            
		                        </div>
	                        </c:when>
	                        <c:otherwise>
<!-- 		                        <p class="admin_info">* IP는 10개 까지만 등록 가능합니다.</p> -->
	                        </c:otherwise>
                        </c:choose>
<!--                         <p class="admin_info">* 대역 등록을 원하실 경우 상위 IP만 작성하시면 됩니다. ex&#65289; 192.168.255.0 ~ 192.168.255.255 대역을 등록할 경우 192.168. 255. 입력</p> -->
<!--                         <p class="admin_info">* 대역 등록을 원하실 경우 상위 IP만 작성하시면 됩니다. ex&#65289; 192.168.255.0 입력</p> -->
                        <p class="admin_info">* IP는 최대 10개까지 등록 가능합니다.</p>              
                        <!--//사용 승인 될 경우에만 노출-->
					</div>
					<!--// API 사용 IP 관리 : IP등록 전-->
					<!--// 신청/관리 내용-->           
				</div>
						
	</div>
</div>
