<%@ 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="fn"  uri="http://java.sun.com/jsp/jstl/functions" %>
<script src="IDC/js/setToken.js"></script>
<script type="text/javascript">
$(function()
{
	//권한설정
	$("#client_phone1,#client_phone2,#client_email1,#client_email2").click(function()
	{
		if($("#p1").val()=="x")
		{
			alert("권한이 없습니다. 관리자에게 문의하세요.");
			$(this).blur();
		}
	});
	//수정페이지
	$(".modify").click(function()
	{
		var sendData = "";
		var tr = $(this).parent().parent();
		tr.find("input[type='text'],#client_code").each(function()
		{
			sendData+="client="+$(this).val()+"&";
		});
		$.ajax(
		{
			url:"IdcClientDBModify.jso",
			data: sendData,
			type:"get",
			timeout:"30000",
			success : function()
			{
				alert("수정 완료");
			},
			error : function()
			{
				alert("수정하기 오류");	
			}
		});
	});
	//삭제페이지
	$(".delBtn").click(function()
	{		
		var client_check = $(this).attr("client_check");
		var client_code = $(this).attr("client_code");
		var token = $("#token").val();
		if(client_check == "y")
		{
			var result = confirm("정말 삭제하시겠습니까?");
			if(result == true){
				
				var sendData="IdcClientDBDelete.cli?";			
				sendData+= "code="+ client_code;
				sendData+="&isClientDB=true";
				sendData+="&token="+token;
				$("#delForm").attr({method:"post",action:sendData}).submit();
				
			}else{
				return false;
			}
						
		}else{
			alert("서비스를 사용 중인 고객은 삭제할 수 없습니다.");
		}
		
	});
	//검색하기 셀렉트
	$("#search > li > a").click(function()
	{
		$("#searchBtn").html($(this).html());
		$("#searchOption").attr("value",$(this).attr("value"));
	});
	
	//수정 버튼 눌렀을 때	
	$(document).on("click", ".submit_btn", function(){
		//var num = $(this).parent().parent().children().next().html();		
		//alert("나 누름");
		
		var td1 = $(this).parent().parent().parent().children();
		var td2 = td1.next();
		var td3 = td2.next();
		var td4 = td3.next();
		var td5 = td4.next();
		var td6 = td5.next();
		var td7 = td6.next();
		var td8 = td7.next();
		var td9 = td8.next();
		var td10 = td9.next();
		var td11 = td10.next();		
		
		var code = td2.children().val();		
		var name = td3.children().val();		
		var reseller = td4.children().val();
		var phone1 = td5.children().val();
		var phone2 = td6.children().val();
		var email1 = td7.children().val();
		var email2 = td8.children().val();
		var date1 = td9.children().val();
		//var date2 = td10.children().val();
		var date2 = td9.children().val();
		var note = td10.children().val();
		
		$(".code").val(code);
		$(".name").val(name);
		$(".reseller").val(reseller);
		$(".phone1").val(phone1);
		$(".phone2").val(phone2);
		$(".email1").val(email1);
		$(".email2").val(email2);
		$(".date1").val(date1);
		$(".date2").val(date2);		
		$(".note").val(note);
	});
	
	//해지 검색인지 고객검색인지 판단
	$(document).ready(function()
	{
		if($("#isClientDB").val()=="true")
			$("#cliForm").attr("action","IdcClientDBList.cli");
		else
			$("#cliForm").attr("action","IdcClient_DeleteDBList.cli");
	});
});

function validateTest(){
	//alert("유효성 체크");
	//return false;
	/*
	var code = document.getElementById("client_code").value;
	document.getElementById("code").value = code;	
	
	var name = document.getElementById("client_name").value;
	document.getElementById("name").value = name;
	
	var reseller = document.getElementById("client_reseller").value;
	document.getElementById("reseller").value = reseller;
	
	var phone1 = document.getElementById("client_phone1").value;
	document.getElementById("phone1").value = phone1;
	
	var phone2 = document.getElementById("client_phone2").value;
	document.getElementById("phone2").value = phone2;
	
	var email1 = document.getElementById("client_email1").value;
	document.getElementById("email1").value = email1;
	
	var email2 = document.getElementById("client_email2").value;
	document.getElementById("email2").value = email2;
	
	var note = document.getElementById("client_note").value;
	document.getElementById("note").value = note;

	//alert(document.getElementById("client_note").value);
	*/
	//return false;
	return true;
}

var tableToExcel = (function(){
	
	var specialElementHandlers = {
            '#bypassme': function (element, renderer) {
                return true;
            }
	};
	
	var uri = 'data:application/vnd.ms-excel;base64,'
	    , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{table}</table></body></html>'
	    , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
	    , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }	    
	    
	  return function(table, name) {
	    if (!table.nodeType) table = document.getElementById(table)
	    var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
	    window.location.href = uri + base64(format(template, ctx))
	  }
})();

</script>

<style>

.table-responsive input {display:block; width:100%;padding:0;border-width:2px;}

</style>
<!-- 여기가 본 페이지이다 -->
<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> 고객 리스트</h4>
			</div>
			<div class="panel-body">
				<div class="row">
					<div class="col-lg-7"><strong> * 주의 : 전화번호 수정시 ( 숫자 or - (하이픈) 이외 문자 사용금지 )</strong>
						<input type="hidden" id="token">
						<input id="isClientDB" type="hidden" value="${isClientDB }"> 
						<form id="cliForm" action="" METHOD="get">
							<div class="input-group">							
								<div class=" input-group-btn" >
									<a class="btn btn-outline btn-default" href="IdcClientDBWriteForm.cli" >등록</a>
									<c:if test="${isClientDB}" >
										<a class="btn btn-outline btn-default" href="IdcClient_DeleteDBList.cli?isClientDB=FALSE">해지리스트</a>
									</c:if>
									<c:if test="${isClientDB == false }" >
										<a class="btn btn-outline btn-default" href="IdcClientDBList.cli?isClientDB=TRUE">고객리스트</a>
									</c:if>
									<a class="btn btn-outline btn-default" href="IdcClientAllServerList.cli">보유현황가기</a>
									<!-- 
									<a class="btn btn-outline btn-default" type="button" value="엑셀" onclick="tableToExcel('table','name')">엑셀</a>
									 -->
									 <a class="btn btn-outline btn-default" type="button" href="IdcClientDBListExcel.cli">엑셀</a>
								<!-- <div class="input-group-btn" > -->
								</div>
								<div class="input-group-btn">
									<button type="button" class="btn btn-info" data-toggle="dropdown" id="searchBtn">${searchOptionName }<span class="caret"></span></button>
									<input type="hidden" value="${searchOption }" id="searchOption" name="searchOption">
									<ul class="dropdown-menu" role="menu" id="search">
										<li><a class="btn btn-default" value="client_code">고객코드</a></li>
										<li><a class="btn btn-default" value="client_name">고객명</a></li>
										<li><a class="btn btn-default" value="client_reseller">리셀러</a></li>
										<li><a class="btn btn-default" value="client_phone">연락처</a></li>
										<li><a class="btn btn-default" value="client_mail">E-Mail</a></li>
										<li><a class="btn btn-default" value="client_note">비고</a></li>
									</ul>
								</div>

								<input class="form-control"  type="text" name ="searchContent" value="${searchContent }">
								<div class="input-group-btn">
									<input class="btn btn-outline btn-default btn-disabled" type="submit" value="검색">
								</div>
							</div>
						</form>
					</div>
				</div>
				<!-- btn-group -->
	   			<div class="table-responsive" id="table">	 
					<table class="table table-bordered table-hover table-striped">
						<thead>
						<tr>
							<c:if test="${isClientDB==true }">
								<th style="text-align:center;width:20px;">No</th>
							</c:if>
							<th style="text-align:center;width:50px;">코드</th>
							<th style="text-align:center;width:120px;">고객명</th>
							<th style="text-align:center;width:70px;">리셀러</th>
							<th style="text-align:center;width:140px;">연락처1</th>
							<th style="text-align:center;width:140px;">연락처2</th>
							<th style="text-align:center;width:200px;">E-mail</th>
							<th style="text-align:center;width:200px;">E-mail</th>
							<th style="text-align:center;width:100px;">등록일</th>
							<!-- <th>갱신일</th> -->
							<c:if test="${isClientDB==false }">
							<th style="text-align:center;">삭제일</th>
							</c:if>
						<th style="text-align:center;width:350px;">비고</th>
						<c:if test="${isClientDB==true }">
							<th style="text-align:center;width:50px">수정</th>
							<th style="text-align:center;width:50px">입금</th>
							<th style="text-align:centerwidth:50px;">삭제</th>
						</c:if>
						<c:if test="${isClientDB == false }" >
							<th style="text-align:center;">복원</th>
						</c:if>
						<!-- 
							<td align="center">개통서</th>
						 -->
					</tr>
					</thead>
					
					
					
<style>

.table-responsive input[type="date"],
.table-responsive input[type="time"],
.table-responsive input[type="datetime-local"],
.table-responsive input[type="month"] {
  line-height: 12px;
  line-height: 1.42857143 \0;
}
.table-responsive input[type="date"].input-sm,
.table-responsive input[type="time"].input-sm,
.table-responsive input[type="datetime-local"].input-sm,
.table-responsive input[type="month"].input-sm {
  line-height: 12px;
  line-height: 1.5 \0;
}
.table-responsive input[type="date"].input-lg,
.table-responsive input[type="time"].input-lg,
.table-responsive input[type="datetime-local"].input-lg,
.table-responsive input[type="month"].input-lg {
  line-height: 12px;
  line-height: 1.33 \0;
}
_:-ms-fullscreen,
:root input[type="date"],
_:-ms-fullscreen,
:root input[type="time"],
_:-ms-fullscreen,
:root input[type="datetime-local"],
_:-ms-fullscreen,
:root input[type="month"] {
  line-height: 1.42857143;
}
_:-ms-fullscreen.input-sm,
:root input[type="date"].input-sm,
_:-ms-fullscreen.input-sm,
:root input[type="time"].input-sm,
_:-ms-fullscreen.input-sm,
:root input[type="datetime-local"].input-sm,
_:-ms-fullscreen.input-sm,
:root input[type="month"].input-sm {
  line-height: 1.5;
}
_:-ms-fullscreen.input-lg,
:root input[type="date"].input-lg,
_:-ms-fullscreen.input-lg,
:root input[type="time"].input-lg,
_:-ms-fullscreen.input-lg,
:root input[type="datetime-local"].input-lg,
_:-ms-fullscreen.input-lg,
:root input[type="month"].input-lg {
  line-height: 1.33;
}
</style>

					
						<tbody>
							<c:forEach items="${IdcClientDBList }" var="dto">
							<tr>
							<c:if test="${isClientDB==true }">
								<c:choose>
									<c:when test="${member.id eq 'khy' || member.id eq 'ko' || member.id eq 'kim.eh' || member.id eq 'leeph' || member.id eq 'choi.jh'}">									
										<td style="text-align:center;">${dto.client_num }</td>
										<td style="text-align:center;"><input type="hidden" id="client_code" value="${dto.client_code }"><a href="/serviceDetail.sev?client_code=${dto.client_code }">${dto.client_code }</a>
										<!-- [<a href="/IdcDepositNonPaymentList.dep?searchContent=${dto.client_name }">청구서</a>] -->
										</td>
										<td style="text-align:center;"><input type="text"  id="client_name" value="${dto.client_name }" size="5"></td>
										<td style="text-align:center;"><input type="text"  id="client_reseller" value="${dto.client_reseller }" size="4"></td>
										<td style="text-align:center;"><input type="text"  id="client_phone1" value="${dto.client_phone1 }" size="5"></td>
										<td style="text-align:center;"><input type="text"  id="client_phone2" value="${dto.client_phone2 }" size="5"></td>
										<td style="text-align:center;"><input type="text"  id="client_email1" value="${dto.client_email1 }" size="5"></td>
										<td style="text-align:center;"><input type="text"  id="client_email2" value="${dto.client_email2 }"  size="5"></td>
										<td style="text-align:center;"><input type="date"  id="client_receive_date" value="${dto.client_receive_date}"  size="5">
										<input type="hidden"  id="client_renew_date" value="${dto.client_renew_date}"  size="5"></td>
										<!-- <td><input type="date"  id="client_receive_date" value="${dto.client_renew_date}"  size="5"></td> -->
										<td style="text-align:center;"><textarea rows="1" cols="80" name="client_note">${dto.client_note }</textarea><!-- <input type="text"  id="client_note" value="${dto.client_note }">--></td>
										<td  style="text-align:center;">
												<!-- 
												<button class="btn btn-outline btn-default btn-xs modify" type="button" value="수정하기"><i class="fa fa-edit"></i></button>
												 -->
												 <c:if test="${dto.client_code ne 'IDCJP'}">																								 
													<form name="frm" action="IdcClientDirectUpdate.cli" method="get" onsubmit="return validateTest();">
														<input type="hidden" name="code" class="code" />
														<input type="hidden" name="name" class="name" />
														<input type="hidden" name="reseller" class="reseller" />
														<input type="hidden" name="phone1" class="phone1" />
														<input type="hidden" name="phone2" class="phone2"/>
														<input type="hidden" name="email1" class="email1" />
														<input type="hidden" name="email2" class="email2" />
														<input type="hidden" name ="date1" class="date1">
														<input type="hidden" name="date2" class="date2">																	
														<input type="hidden" name="note"  class="note"/>
														<input type="hidden" name="token" class="token"/>														
														<input type="submit" class="btn btn-outline btn-default btn-xs submit_btn" value="수정" />
													</form>
												</c:if>
										</td>
										</c:when>
										<c:otherwise>
												<td style="text-align:center;">${dto.client_num }</td>
												<td style="text-align:center;"><a href="/serviceDetail.sev?client_code=${dto.client_code }">${dto.client_code }</a></td>
												<td style="text-align:center;">${dto.client_name }</td>
												<td style="text-align:center;">${dto.client_reseller }</td>
												<td style="text-align:center;"><c:set var="temp_phone1" value="${fn:split(dto.client_phone1,'-')[2]}" />${temp_phone1}</td>
												<td style="text-align:center;"><c:set var="temp_phone2" value="${fn:split(dto.client_phone2,'-')[2]}" />${temp_phone2}</td>
												<td style="text-align:center;"><c:set var="temp_email1" value="${fn:substringBefore(dto.client_email1,'@')}" />${temp_email1}</td>
												<td style="text-align:center;"><c:set var="temp_email2" value="${fn:substringBefore(dto.client_email2,'@')}" />${temp_email2}</td>
												<td style="text-align:center;">${dto.client_receive_date}</td>
												<td style="text-align:center;"><textarea rows="1" cols="80" name="client_note">${dto.client_note }</textarea></td>
										</c:otherwise>
									</c:choose>
								</c:if>
								<c:if test="${isClientDB == false }" >
									<td>${dto.client_code }</td>
									<td>${dto.client_name }</td>
									<td>${dto.client_reseller }</td>
									<td>${dto.client_phone1 }</td>
									<td>${dto.client_phone2 }</td>
									<td>${dto.client_email1 }</td>
									<td>${dto.client_email2 }</td>
									<td>${dto.client_receive_date }</td>
									<td>${dto.client_renew_date }</td>
									<c:if test="${isClientDB==false }">
										<td>${dto.client_delete_date }</td>
									</c:if>
									<td>${dto.client_note }</td>
									<td align="center">
										<form action="IdcClientDBDelete.cli?isClientDB=true">
											<input type="hidden" name="token" class="token">
											<input type="hidden" name="code" value="${dto.client_code}" size="1">
											<button class="btn btn-outline btn-info" type="submit" value="복원하기"><i class="fa fa-edit"></i></button>
										</form>
									</td>
								</c:if>
								
								<c:if test="${isClientDB == true }" >
									<td align="center">
										<form action="IdcDepositForm.dep" METHOD="get">
											<input type="hidden" name="client_code" value="${dto.client_code}" size="1">
											<button class="btn btn-outline btn-default btn-xs" type="submit" value="입금처리"><i class="fa fa-krw"></i></button>
										</form>
									</td>
									<td>
										<c:if test="${dto.client_code ne 'IDCJP'}">
										<form id="delForm">
											<button class="btn btn-danger btn-outline delBtn" client_code="${dto.client_code }" client_check="${dto.client_check}" type="button" >삭제</button>
										</form>
										</c:if>
									</td>
								</c:if>
							</tr>
							</c:forEach>
						</tbody>
					</table>
				</div>
				<!-- table-responsive -->
				<div>
					<ul class="pagination" style="margin:0px;">
						<!-- 고객리스트 -->
						<c:if test="${isClientDB }">
							<li>
								<a class="btn btn-outline btn-default" 
									href="IdcClientDBList.cli?searchOption=${searchOption }&searchContent=${searchContent}">
									<i class="fa fa-angle-double-left "></i>
								</a>
							</li>
							<li>
								<c:if test="${curPage!=1 }">
									<a class="btn btn-outline_btn-default" href="IdcClientDBList.cli?searchOption=${searchOption }&curPage=${curPage-1 }&searchContent=${searchContent}">
										<i class="fa fa-angle-left"></i>
									</a>
								</c:if>
								<c:if test="${curPage==1 }">
									<a class="btn btn-outline_btn-default" href="IdcClientDBList.cli?searchOption=${searchOption }&curPage=1&searchContent=${searchContent}">
										<i class="fa fa-angle-left"></i>
									</a>
								</c:if>
							</li>
							<c:forEach var="i" begin="${firstPage }" end="${pageCnt }">
								<li>
									<a class="btn btn-outline btn-defualt" 
									href="IdcClientDBList.cli?curPage=${i}&searchOption=${searchOption }&searchContent=${searchContent}">${i}</a>	
								</li>
							</c:forEach>
							<li>
								<c:if test="${curPage!=pageCnt }">
									<a class="btn btn-outline_btn-default" href="IdcClientDBList.cli?searchOption=${searchOption }&curPage=${curPage+1 }&searchContent=${searchContent}">
										<i class="fa fa-angle-right"></i>
									</a>
								</c:if>
								<c:if test="${curPage==pageCnt }">
									<a class="btn btn-outline_btn-default" href="IdcClientDBList.cli?searchOption=${searchOption }&curPage=${pageCnt }&searchContent=${searchContent}">
										<i class="fa fa-angle-right"></i>
									</a>
								</c:if>
							</li>
							<li>
								<a class="btn btn-outline btn-default" 
								href="IdcClientDBList.cli?curPage=${i}&searchOption=${searchOption }&searchContent=${searchContent}">
									<i class="fa fa-angle-double-right "></i>
								</a>
							</li>
						</c:if>
						<!-- 해지리스트 -->
						<c:if test="${!isClientDB }">
							<li>
								<a class="btn btn-outline btn-default" 
									href="IdcClient_DeleteDBList.cli?searchOption=${searchOption }&searchContent=${searchContent}">
									<i class="fa fa-angle-double-left "></i>
								</a>
							</li>
							<li>
								<c:if test="${curPage!=1 }">
									<a class="btn btn-outline_btn-default" href="IdcClient_DeleteDBList.cli?searchOption=${searchOption }&curPage=${curPage-1 }&searchContent=${searchContent}">
										<i class="fa fa-angle-left"></i>
									</a>
								</c:if>
								<c:if test="${curPage==1 }">
									<a class="btn btn-outline_btn-default" href="IdcClient_DeleteDBList.cli?searchOption=${searchOption }&curPage=1&searchContent=${searchContent}">
										<i class="fa fa-angle-left"></i>
									</a>
								</c:if>
							</li>
							<c:forEach var="i" begin="${firstPage }" end="${pageCnt }">
								<li>
									<a class="btn btn-outline btn-defualt" 
									href="IdcClient_DeleteDBList.cli?curPage=${i}&searchOption=${searchOption }&searchContent=${searchContent}">${i}</a>	
								</li>
							</c:forEach>
							<li>
								<c:if test="${curPage!=pageCnt }">
									<a class="btn btn-outline_btn-default" href="IdcClient_DeleteDBList.cli?searchOption=${searchOption }&curPage=${curPage+1 }&searchContent=${searchContent}">
										<i class="fa fa-angle-right"></i>
									</a>
								</c:if>
								<c:if test="${curPage==pageCnt }">
									<a class="btn btn-outline_btn-default" href="IdcClient_DeleteDBList.cli?searchOption=${searchOption }&curPage=${pageCnt }&searchContent=${searchContent}">
										<i class="fa fa-angle-right"></i>
									</a>
								</c:if>
							</li>
							<li>
								<a class="btn btn-outline btn-default" 
								href="IdcClient_DeleteDBList.cli?curPage=${i}&searchOption=${searchOption }&searchContent=${searchContent}">
									<i class="fa fa-angle-double-right "></i>
								</a>
							</li>
						</c:if>
					</ul>
					<!-- ul -->
				</div>
			</div>
			<!-- panel-body -->
		</div>
		<!-- panel panel-default -->
	</div>
	<!-- col-lg-12 -->
</div>
<!-- row -->
<!-- /#page-wrapper -->
