<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>
<script type="text/javascript" src="IDC/js/getIP.js"></script>
<script type="text/javascript" src="IDC/js/setToken.js"></script>
<script>

$(function(){	
	
	//금액입력시 금액과 일할부분으로 금액이 완납인지 아닌지 판단
	//통합결제인지 나눠서결제인지 판단.

	$(".account").bind("input", function(){
		var account1 = $("#account1").val();		
		var account2 = $("#account2").val();
		
		$("#result").val(account1 - account2);
		
		if($("#result").val() == '0'){
			$("#accountStatus").val("complete");
			$("#accountStatus2").val("완납");
		}else{
			$("#accountStatus").val("incomplete");
			$("#accountStatus2").val("미납");
		}
		
		if(account1 != 0 && account2 == 0){
			$("#united1").val("분할");
			$("#united").val("x");
			$("#payment_div").attr("style", "display:block;");
		}else if(account2 != 0){			
			$("#united1").val("통합");
			$("#united").val("o");
			$("#payment_div").attr("style", "display:none;");
		}
		
		
	});
	
});

function back_btn(sc){
	window.location ="additionalService.sev?service_code="+sc;
}


function selectCs(selectObj){	
	if(selectObj.value == "cs"){
		var cs_code = document.getElementById("cs_code");
		document.getElementById("addDB_code").value = cs_code.value;
		document.getElementById("addDB_case").value = "cs";
	}else if(selectObj.value == "kcs"){
		var kcs_code = document.getElementById("kcs_code");
		document.getElementById("addDB_code").value = kcs_code.value;
		document.getElementById("addDB_case").value = "kcs";
	}	
}

function clickUnited(selectObj){
	if(selectObj.value == "x"){
		document.getElementById("payment_div").style.display = "block";
	}else{
		document.getElementById("payment_div").style.display = "none";
	}
}

function init(){
}

//유효성 검사
function validateTest()
{
	var addDB_case = document.getElementById("case").value;
	var result = document.getElementById("result").value;
	
	var addDB_ip = $("#addDB_ip").val();
	if(addDB_case == "ip" && addDB_ip == ""){
		alert("추가 내용을 입력해주세요.");	
		return false;
	}
	var account1 = document.getElementById("account1").value;
	var account2 = document.getElementById("account2").value;
	
	if(parseInt(account1||0) < parseInt(account2||0)){
		alert("일할 과금 액수가 과금액을 초과했습니다.");
		return false;
	}
	
	if(addDB_case == "ip" && account1 == ""){
		alert("서비스 과금을 입력해주세요.");	
		return false;
	}
	
	if(addDB_case == "ip" && account2 == ""){
		alert("일할 과금을 입력해주세요.");	
		return false;
	}
	
	//cs추가시 유효성 테스트
	if(addDB_case == "cs" && addDB_ip == ""){
		alert("cs주소를 입력해주세요.");	
		return false;
	}
	
	if(addDB_case == "cs" && account1 == ""){
		alert("서비스 과금을 입력해주세요.");	
		return false;
	}
	
	if(addDB_case == "cs" && account2 == ""){
		alert("일할 과금을 입력해주세요.");	
		return false;
	}
	//방어추가시 유효성 테스트
	var service_defence = document.getElementById("service_defence").value;
	if(addDB_case == "defence" && service_defence == "x"){
		alert("추가할 방어 서비스 종류를 선택해주세요.");	
		return false;
	}
	if(addDB_case == "defence" && addDB_ip == ""){
		alert("추가 내용을 입력해주세요.");	
		return false;
	}
	
	if(addDB_case == "defence" && account1 == ""){
		alert("서비스 과금을 입력해주세요.");	
		return false;
	}
	
	if(addDB_case == "defence" && account2 == ""){
		alert("일할 과금을 입력해주세요.");	
		return false;
	}
	
	return true;
}

</script>
<script type="text/javascript">
$(function()
{
	$("#service_defence").change(function()
	{
		if($(this).val()=='x')
		{
			alert("방어서비스를 해지하여 주십시오.");
			$("#service_defence").val($("#hide_sel").val())
		}
		setSelect($(this))
	});
	$(document).ready(function()
	{
		$("#service_defence").val($("#hide_sel").val())
		if($("#service_defence").val()!=null)
			setSelect($("#service_defence"));
	});
});

var setSelect = function(temp)
{
	var a = $(temp).val()
	var p = $(temp).children("[value="+a+"]").attr("price");
		//alert($(this).children("[value="+a+"]").attr("price"));
	$("#account1").val(p);
}

</script>
<!-- 여기가 본 페이지이다 -->
<div class="row">
	<div class="col-lg-12">
		<div class="panel panel-default">
			<div class="panel-heading">
				<h4><i class="fa fa-desktop fa-fw">
				</i><c:if test="${ipAdd.addDB_case == 'ip'}">IP 추가</c:if>
				<c:if test="${ipAdd.addDB_case == 'cs'}">CS 및 KCS 추가  </c:if>
				<c:if test="${ipAdd.addDB_case == 'defence'}">방어서비스 추가</c:if>		
				</h4>
			</div>
			<div class="panel-body" onload="init()">
   				<form name="frm" action="ipAddHandle.sev?client_code=${ipAdd.client_code}" method="post" onsubmit="return validateTest();">
		   			<input type="hidden" id="token" name="token">
		   			<div class="table-responsive">
	   					<input type="hidden" name="service_code" id="service_code" value="${ipAdd.service_code}">
						<input type="hidden" name="server_code" id="server_code" value="${ipAdd.server_code}">
						<input type="hidden" name="case" id="case" value="${ipAdd.addDB_case}">
						<table class="table table-bordered table-hover table-striped">
							<thead>
							</thead>
							<tbody>
								<tr>
									<td>고객명</td>
									<td>${ipAdd.client_name}</td>
									<td>라인종류</td>
									<td>${ipAdd.service_line}</td>
								</tr>
								<tr>
									<td>장비명</td>
									<td>${ipAdd.server_code}</td>
									<td>IP</td>			
									<td>${ipAdd.ip_address}</td>					
								</tr>
								<tr>
									<td>CS</td>
									<td>
										<c:forEach items="${csList}" var="dto" >
										${dto.addDB_code}<br>
										</c:forEach>
									</td>
									<td>KCS(VPN)</td>
									<td>
										<c:forEach items="${kcsList}" var="dto" >
										${dto.addDB_code}<br>
										</c:forEach>
									</td>									
								</tr>
								<tr>
									<td>구분</td>
									<td>
										<c:if test="${ipAdd.addDB_case ne 'defence'}">IP입력</c:if>
										<c:if test="${ipAdd.addDB_case eq 'defence'}">추가내용</c:if>
									</td>
									<td>과금</td>
									<td>
										일할과금
										<!-- 
										<select name="accountMethod" id="accountMethod">						
											<option value="all">일할과금</option>
											<option value="part">부분과금</option>
										</select>
										 -->					
									</td>
								</tr>

								<tr>
									<c:if test="${ipAdd.addDB_case == 'ip'}">
										<td>IP추가</td>
										<input type="hidden" name="addDB_case" id="addDB_case" value="ip">
									</c:if>
									<c:if test="${ipAdd.addDB_case == 'cs'}">				
										<td>											
											<select name="selected_case" id="selected_case" onchange="selectCs(this);">
												<option value="cs">CS 추가</option>
											</select>
											<input type="hidden" name="addDB_case" id="addDB_case" value="cs">																															
										</td>																														
									</c:if>
									<c:if test="${ipAdd.addDB_case == 'defence'}">
										<td>
											<input type="hidden" id="hide_sel" value="${ipAdd.service_defence }">
											<select name="service_defence" id="service_defence" value="">											
												<option value="x" price="0">없음</option>
												<option value="normal" price="400000">일반</option>
												<option value="ordinary" price="2000000">상시</option>
												<option value="cs-pre" price="3000000">CS-PRE</option>																																														
											</select>
										</td>
										<input type="hidden" name="addDB_case" id="addDB_case" value="defence">																														
									</c:if>
									<td>
										<c:if test="${ipAdd.addDB_case == 'ip'}">													
											<div class="row">
												<div class="col-lg-4">
											  		<div class="input-group">
										  				<span class="input-group-btn">
												  			<ul class="dropdown-menu getIPul" role="menu" id="getIPul">
												  				<li>ffe</li>
												  			</ul>
											  			</span>
											  			<input type="text" class="form-control getIP" name="addDB_ip" id="addDB_ip"/>
												  		<input type="hidden" class="client_code" value="${ipAdd.client_code}">
												  		<input type="hidden" class="client_name" value="">
													</div>
												</div>
											</div>
										</c:if>
										</td>
									<td>
										<input type="text" name="account1" id="account1" class="account" value="100000"/>
									</td>
									<td>
										<input type="text" name="account2" id="account2" class="account" value="0"/>
										<input type="hidden" name="addDB_code" id="addDB_code" value="${ipAdd.addDB_code}">
										<input type="hidden" name="cs_code" id="cs_code" value="${ipAdd.addDB_code}" >
										<input type="hidden" name="kcs_code" id="kcs_code" value="${ipAdd.kcs_code}" >
										<input type="hidden" name="service_defence1" id="service_defence1" value="${ipAdd.service_defence}" >
									</td>						
								</tr>
								<!-- 
								<tr>
									<td>code</td>
									<td><input type="text" name="addDB_code" value="${ipAdd.addDB_code}" readonly="readonly"></td>
									<td>설명</td>
									<td><input type="text" name="addDB_comment"></td>				
								</tr>
								 -->
								<tr>
									<td>기존 결제일</td>
									<td colspan="3"><input type="date" name="service_payment_date" id="service_payment_date" value="${ipAdd.service_payment_date}"/></td>
								</tr>
								<tr>
									<td>신청일</td>
									<td colspan="3"><input type="date" name="request_date" id="request_date"/ value="${ipAdd.curdate}"></td>			
								</tr>
								<tr>
									<td>해지일</td>
									<td colspan="3"><input type="date" name="cancel_date" id="cancel_date"/>
										<input type="hidden" name="accountStatus"  id="accountStatus" readonly="readonly" value="incomplete"/>
										<input type="hidden" name="accountStatus2"  id="accountStatus2" readonly="readonly" value="미납"/>
										<input type="hidden" name="result"  id="result" value="100000"/>
										<input type="hidden" name="united"  id="united" value="o"/>
									</td>												
								</tr>
								<!-- 
								<tr>
									<td>과금상태</td>
									<td></td>
									<td>
										<input type="text" name="result"  id="result"/>										
									</td>
									<td>
										<input type="hidden" name="accountStatus"  id="accountStatus" readonly="readonly"/>
										<input type="text" name="accountStatus2"  id="accountStatus2" readonly="readonly"/>																			
									</td>											
								</tr>
								 -->
								<tr>
									<td>결제 방식</td>
									<td>
										<input type="text" id="united1" value="통합" readonly="readonly"/>										
									</td>									
									<td colspan="2"><div id="payment_div" style="display:none;">결제일<input type="date" name="payment_date"></div></td>
													
								</tr>	
								<tr>
									<td>비고</td>
									<td colspan="3">
										<textarea name="addDB_note" id="addDB_note" cols="100" rows="4"></textarea>
									</td>
								</tr>
							</tbody>
							<tfoot>
								<tr>
									<td colspan="4">
										<input class="btn btn-outline btn-primary" type="submit" name="save" value="저장">
										<input class="btn btn-outline btn-default" type="button" name="back" value="취소" onclick="back_btn('${ipAdd.service_code}')">
									</td>
								</tr>	
							</tfoot>
						</table>
					</div>
					<!-- table-responsive -->
				</form>
			</div>
			<!-- panel-body -->
		</div>
		<!-- panel panel-default -->
	</div>
	<!-- col-lg-12 -->
</div>
<!-- row -->
<!-- /#page-wrapper -->
