<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>
<script src="IDC/js/setToken.js"></script>
<script>
function calc(){	
	var f = document.frm;
	
	var payments = document.getElementsByName("payment_amount");
	var united_check = document.getElementsByName("united_check");
	var total = 0;
	
	
	//1회성 과금표에서 합계 추가
	var onetime_checks = document.getElementsByName("onetime_check");
	var onetime_payment_amounts = document.getElementsByName("onetime_payment_amount");
	for(i=0; i<onetime_payment_amounts.length; i++){
		if(onetime_checks[i].checked == true){
			total += parseInt(onetime_payment_amounts[i].value);
		}
	}
	
	var client_money = f.client_money.value;	
	
	if(total > parseInt(client_money)){		
		alert("결제 금액이 예치금을 초과했습니다.");
		return false;		
	}else{
		alert("결제처리 되었습니다.");
		return true;
	}	
	
}

$(function()
{
	//결제할 금액 입력초과 판단.
	$(".checkPrice").bind("input",function()
	{
		var price = $(this).attr("price");
		var inPrice = $(this).val();
		if(parseInt(price)<parseInt(inPrice))
		{
			alert("금액을 초과하여 입력할 수 없습니다.\n"+"금액 : "+price+"\n입금액: "+inPrice);
			$(this).val(price);
		}
	}); 		
	
	$(".nounitedlist").click(function()
	{
		var check = $(this).is(":checked");

		if(check)
			$(this).parent().children(":eq(0)").val("o");	
		else
			$(this).parent().children(":eq(0)").val("x");	
	});
	
	//취소버튼
	$(".cancel").click(function(){
		var client_code = $(this).attr("client_code"); 
		//location.replace("serviceDetail.sev?client_code="+client_code);
		history.go(-1);
	});
	
	//날짜 기입 했는지 안했는지 판단
	$(".accCheck").click(function()
	{
		var a =$(this).parents("tr").find("[type=date]");
		if(a.hasClass("inDate"))
			a.removeClass("inDate");
		else
			a.addClass("inDate");
	});
	
	//입금일 체크
	$("#accSub").click(function()
	{
		var checkSubmit = true;
		var checkboxCheck=false;
		var a = true;
		
		$(".accCheck").each(function()
		{
			if($(this).prop("checked"))
				checkboxCheck=true;
		});
		if(!checkboxCheck)
		{
			alert("결제할 대상이 없습니다.");
			checkSubmit=false;
			return;
		}
		$(".inDate").each(function()
		{
			if($(this).val()==null || $(this).val()=="")
			{
				a = false;
				checkSubmit=false;
			}
		});
		
		if(a)
		{
			var tot=0;
			$(".accCheck").each(function()
			{
				if($(this).prop("checked"))
				{
					tot+=parseInt($(this).parents("tr").find(".checkPrice").val());
				}	
			});
			if(tot>parseInt($("#client_money").val()))
			{
				alert("예치금보다 초과하여 결제할 수 없습니다.");
				checkSubmit=false;				
			}
		}
		
		if(checkSubmit)
			$("#accForm").submit();
	});
});
</script>


<!-- 여기가 본 페이지이다 -->
<div class="row">
	<div class="col-lg-12">
		<div class="panel panel-default">
			<form name="frm" id="accForm" action="serviceAccountHandle.sev" method="POST" onsubmit="return calc();">
				<input type="hidden" id="token" name="token">
				<input type="hidden" name="client_code" value="${client_code}"/>				
				<div class="panel-heading">
					<h4><i class="fa fa-desktop fa-fw"></i> 부가서비스 추가</h4>
				</div>
				<a class="btn btn-outline btn-default " href="serviceDetail.sev?client_code=${client_code}">서비스 리스트</a>
				<div class="panel-body">
				<div class="row">
					
				</div>
				<!-- row -->
	   			<div class="table-responsive">
					<table class="table table-bordered table-hover table-striped">
						<thead>
						</thead>
						<tbody>
								<tr>
									<td>고객코드</td>
									<td>${client_code}</td>
								</tr>
								<tr>
									<td>고객명</td>
									<td>${client_name }</td>
								</tr>
								<tr>
									<td>예치금</td>
									<td><input name="client_money" id="client_money" type="text" readonly="readonly" value="${client_money}"/></td>
								</tr>
						</tbody>
						<tfoot>
						</tfoot>
					</table>
				</div>
				<!-- table-responsive -->
				<div class="table-responsive">
					<table class="table table-bordered table-hover table-striped">
						<thead>
						</thead>
						<tbody>
							<tr>
								<td>은행명</td>
								<td>입금자명</td>
								<td>금액</td>
								<td>날짜</td>
								<td>비고</td>
							</tr>
							<c:forEach items="${deposit_list}" var="deposit_list">
								<tr>
									<td>${deposit_list.deposit_bank}</td>
									<td>${deposit_list.deposit_name}</td>
									<td>${deposit_list.deposit_amount}</td>
									<td>${deposit_list.deposit_date}</td>
									<td>${deposit_list.deposit_note}</td>
								</tr>
							</c:forEach>
						</tbody>
						<tfoot>
						</tfoot>
					</table>
				</div>
				<!-- table-responsive -->
				<label name="mkservice" id="mkservice"">과금표</label>
				<div class="table-responsive" >
					<table class="table table-bordered table-hover table-striped">
						<thead>
							<tr>
								<td>체크</td>
								<td>장비명</td>
								<td>IP</td>
								<td>서비스비용</td>
								<td>미결제 금액</td>
								<td>결제일</td>
								<td>입금일</td>
								<td>결제금액</td>
								<td>비고</td>
							</tr>
						</thead>
						<tbody>
							<c:forEach items="${service_list}" var="service_list">
								<input type="hidden" name="service_code" value="${service_list.service_code}"/>									
								<tr>
									<!-- 				
									<td><input type="checkbox" name="${service_list.service_code}" /></td>
									 -->
									 <td></td>
									<td>${service_list.server_code}</td>
									<td>${service_list.service_ip}</td>
									<td>${service_list.service_amount}</td>
									<td><font color="red">${service_list.service_nonpayment}</font></td>
									<td>${service_list.service_payment_date}</td>
									<td></td>
									<td></td>
									<td>${service_list.service_note}</td>
								</tr>
								<c:forEach items="${total_list }" var="temp" varStatus="i">
									<c:if test="${temp.service_code == service_list.service_code }">
										<tr>
											<!-- 
											<td><input type="checkbox" class="accCheck" name="checkList" value="${i.count-1}"></td>
											 -->
											<td><input type="checkbox" class="accCheck" name="checkList" value="${temp.addDB_num}"></td>
											<td>${temp.addDB_case }</td>
											<td>${temp.addDB_ip }</td>
											<td>${temp.addDB_amount }</td>
											<td><font color="red">${temp.addDB_nonpayment }</font></td>
											<td>${temp.addDB_payment_date }</td>
											<td><input type="date" name="payday" value="${temp.today }"></td>
											<td><input type="text" name="payment_amount" class="checkPrice" price="${temp.addDB_nonpayment }" value="${temp.addDB_nonpayment}"/></td>
											<td>${temp.addDB_note }<input type="hidden" name="num" value="${temp.addDB_num }"></td>
										</tr>
									</c:if>
								</c:forEach>					
							</c:forEach>
						</tbody>
						<tfoot>
						</tfoot>
					</table>
				</div>
				<!-- table-responsive -->
				<label>1회성 과금표</label>
				<div class="table-responsive">
					<table class="table table-bordered table-hover table-striped">
						<thead>
							<tr>
								<td>체크</td>
								<td>서비스코드</td>
								<td>1회성 서비스 내용</td>
								<td>서비스비용(미결제 금액)</td>
								<td>결제일</td>
								<td>입금일</td>
								<td>등록일</td>
								<td>결제금액</td>
								<td>비고</td>
							</tr>
						</thead>
						<tbody>
							<c:forEach items="${onetime_list}" var="one">
								<input type="hidden" name="onetime_num" value="${one.onetime_num}"/>									
								<tr>				
									<td><input type="checkbox" class="accCheck" name="onetime_check" value="${one.onetime_num}"/></td>
									<td>${one.service_code}</td>
									<td>${one.onetime_case} ${one.onetime_sub}</td>
									<td>${one.onetime_amount}(${one.onetime_nonpayment})</td>
									<td>${one.onetime_payment_date}</td>
									<td><input type="date" name="onetime_insert" id="curdate" value="${one.onetime_insert_date}"></td>
									<td>${one.onetime_handle_date}</td>
									<td><input type="text" name="onetime_payment_amount" value="${one.onetime_nonpayment}"/></td>
									<td>${one.onetime_note}</td>
								</tr>					
							</c:forEach>
						</tbody>
						<tfoot>
						</tfoot>						
					</table>
				</div>
				<!-- table-responsive -->
				
				<div class="btn-group">
					<input type="button" class="btn btn-outline btn-primary" id="accSub"  value="입금처리" >
					<input class="btn btn-outline btn-default cancel" type="button" value="취소" client_code="${client_code}">		
				</div>
			</div>
			<!-- panel-body -->
			</form>
		</div>
		<!-- panel panel-default -->
	</div>
	<!-- col-lg-12 -->
</div>
<!-- row -->
<!-- /#page-wrapper -->
