<%@ 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="validator" uri="http://www.springmodules.org/tags/commons-validator" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="ckeditor" uri="http://ckeditor.com" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<script type="text/javaScript" language="javascript" defer="defer">
	
</script>

<form:form id="arrestSearchForm" name="arrestSearchForm" action="#" method="post" commandName="arrestSearchVO">

	<div class="search2 m-bottom">
		<ul>
		 	<li><b><spring:message code="arrest.arrestDate"/></b></li>
		 	<li class="margin-left w-94">
		 		
		 		<input id="startDate" name="startDate" path="startDate" kind="date" type="text" value="${arrestSearchVO.startDate }"/> ~ 
		 		<input id="endDate" name="endDate" path="endDate" kind="date" type="text" value="${arrestSearchVO.endDate }"/>
		 		
		 	</li>
		 	<li class="margin-left"><b><spring:message code="arrest.agencyType"/></b></li>
		 	<li class="form_search3 margin-left">
				<form:select id="agencyType" path="agencyType" class="sw-210 fl">
					<form:option value="" label="전체" />
					<c:forEach var="result" items="${agencyTypeList}" varStatus="status">
						<form:option value="${result.code2}" label="${result.codenm}" />
					</c:forEach>
		    	</form:select>
		 	</li>
		 	<li class="form_search3 w-210">
		 	<div style="width:210px;" width="210">				    		
				<form:select id="agencyGuard" path="agencyGuard" class="sw-210 fl" style="display:none;">
					<form:option value="" label="전체" />
					<c:forEach var="result" items="${placeList}" varStatus="status">
						<form:option value="${result.code2}" label="${result.codenm}" />
					</c:forEach>
	    		</form:select>
	    		<form:select id="agencyGuard2" path="" class="sw-210 fl">
	    			<form:option value="" label="전체" />
	    		</form:select>	    		
	    	</div>
			</li>
			<li class="margin-left"><b><spring:message code="arrest.vessel"/></b></li>
			<li class="margin-left w-130">
				<input id="agencyVessel" name="agencyVessel" path="agencyVessel" type="text" value="${arrestSearchVO.agencyVessel }"/>
			</li>
		</ul>
		<ul class="mt-20">
		 	<li><b>위반장소</b></li>
		 	<li class="form_search margin-left">
				<form:select id="addrArea" name="addrArea" path="addrArea" cssClass="use fl" onchange="fncGetCombo($('#divAddressCity'), 'addrCity', this.value);">
					<form:option value="" label="전체" selected="true" />							
						<c:forEach var="result" items="${addressList}" varStatus="status">
							<form:option value="${result.code1}" label="${result.codenm}" />
						</c:forEach>
	    		</form:select>			    
		 	</li>
		 	<li class="form_search">
		 		
		 		<span id="divAddressCity">
			    	<form:select id="addrCity" name="addrCity" path="addrCity" class="fl" value="">
			    		<form:option value="" label="전체" />
			    	</form:select>
			    </span>				
			</li>
		 	<li class="margin-left"><b><spring:message code="arrest.violateType"/></b></li>
		 	<li class="form_search margin-left" style="margin-left:9px;">
				<form:select id="violateType" path="violateType" cssClass="use">				
					<form:option value="" label="전체" />			
					<c:forEach var="result" items="${violateTypeList}" varStatus="status">
						<form:option value="${result.code2}" label="${result.codenm}" />
					</c:forEach>
	    		</form:select>
		 	</li>
			<li class="margin-left">
				<b>선명</b>
			</li>
			<li class="margin-left w-130">
				<input id="shipName" name="shipName" path="shipName" type="text" value="${arrestSearchVO.shipName }"/>
			</li>
		</ul>
	</div>
	<div class="search4 m-bottom">
		<div class="search_list_wrap">
			<div class="search_list">
				<div class="list-box list1">
					<h2 class="select_title"><spring:message code="arrest.invasionType"/></h2>
					<ul class="select_list">
						
						
						
						<c:forEach var="invasionTypeList" items="${invasionTypeList}" varStatus="status">								
							<li><form:checkbox path="invasionType" value="${invasionTypeList.code2}" label="${invasionTypeList.codenm}" checked="checked"/></li>								
						</c:forEach>
												
												
						<li><hr class="chn_hr"/></li>
						<li class="chn_radio">
							<label for="specBatlYn_Y" class="chn_label"><spring:message code="arrest.invasionType.specBatl"/></label>
							<input id="specBatlYn_Y" name="specBatlYn" path="specBatlYn" type="checkbox" value="Y" checked="checked"/><label for="specBatlYn_Y">Y</label>
							<input id="specBatlYn_N" name="specBatlYn" path="specBatlYn" type="checkbox" value="N" checked="checked" class="ml-8"/><label for="specBatlYn_N">N</label>
						</li>						
						<li class="chn_radio">
							<label for="nllYn_Y" class="chn_label">NLL</label>
							<input id="nllYn_Y" name="nllYn" path="nllYn" type="checkbox" value="Y" checked="checked"/><label for="nllYn_Y">Y</label>
							<input id="nllYn_N" name="nllYn" path="nllYn" type="checkbox" value="N" checked="checked" class="ml-8"/><label for="nllYn_N">N</label>
						</li>
						
					</ul>
				</div>
			</div>
			<div class="search_list">
				<div class="list-box list2">
					<h2 class="select_title"><spring:message code="arrest.shipType"/></h2>
					<ul class="select_list">						
						<c:forEach var="shipTypeList" items="${shipTypeList}">
							<li><form:checkbox path="shipType" value="${shipTypeList.code2}" label="${shipTypeList.codenm}" checked="checked"/></li>
						</c:forEach>
					</ul>
				</div>
			</div>
			<div class="search_list">
				<div class="list-box list3">
					<h2 class="select_title"><spring:message code="arrest.procCondition"/></h2>
					<ul class="select_list">
						<li><input id="check301" name="procCondition" path="procCondition" type="checkbox" value="01" checked="checked"/><label for="check301">조사중</label></li>
						<li><input id="check302" name="procCondition" path="procCondition" type="checkbox" value="02" checked="checked"/><label for="check302">처리완료</label></li>
					</ul>
				</div>
			</div>
			<div class="search_list">
				<div class="list-box list4">
					<h2 class="select_title"><spring:message code="arrest.list.shipTonnage"/></h2>
					<ul class="select_list">
						<li><input id="check401" name="arrShipTonnage" path="arrShipTonnage" type="checkbox" value="01" checked="checked"/><label for="check401">50톤 미만</label></li>
						<li><input id="check402" name="arrShipTonnage" path="arrShipTonnage" type="checkbox" value="02" checked="checked"/><label for="check402">50톤이상 ~ 100톤 미만</label></li>
						<li><input id="check403" name="arrShipTonnage" path="arrShipTonnage" type="checkbox" value="03" checked="checked"/><label for="check403">100톤 이상</label></li>
					</ul>
				</div>
			</div>
		</div>
		<div class="search_list_wrap mt-20">
			<div class="search_list">
				<div class="list-box list5">
					<h2 class="select_title"><spring:message code="arrest.marginPayment"/></h2>
					<ul class="select_list">
						<li><input id="check501" name="marginPayment" path="marginPayment" type="checkbox" value="Y" checked="checked"/><label for="check501"><spring:message code="arrest.marginPayment.finish"/></label></li>
						<li><input id="check502" name="marginPayment" path="marginPayment" type="checkbox" value="N" checked="checked"/><label for="check502"><spring:message code="arrest.marginPayment.default"/></label></li>
					</ul>
				</div>
			</div>
			<div class="search_list">
				<div class="list-box list6">
					<h2 class="select_title"><spring:message code="arrest.shipProcType"/></h2>
					<ul class="select_list">						
						<c:forEach var="shipProcTypeList" items="${shipProcTypeList}">
							<li><form:checkbox path="shipProcType" value="${shipProcTypeList.code2}" label="${shipProcTypeList.codenm}"  checked="checked"/></li>
						</c:forEach>
					</ul>
				</div>
			</div>
			<div class="search_list">
				<div class="list-box list7">
					<h2 class="select_title"><spring:message code="arrest.list.shipQuality"/></h2>
					<ul class="select_list">						
						<c:forEach var="shipQualityList" items="${shipQualityList}">
							<li><form:checkbox path="shipQuality" value="${shipQualityList.code2}" label="${shipQualityList.codenm}"  checked="checked"/></li>
						</c:forEach>
					</ul>
				</div>
			</div>
			<div class="search_list">
				<div class="list-box list8">
					<h2 class="select_title"><spring:message code="arrest.list.shipMent"/></h2>
					<ul class="select_list">						
						<c:forEach var="shipMentList" items="${shipMentList}">
							<li><form:checkbox path="shipMent" value="${shipMentList.code2}" label="${shipMentList.codenm}" checked="checked"/></li>
						</c:forEach>
					</ul>
					
				</div>
			</div>
		</div>
	</div>
	<div class="search2">
		<ul class="pull-right">
		 	<li>
		 		<input type="button" value="<spring:message code="arrest.list.search"/>" onclick="javascript:goSearch();"/>				
			</li>
			<li class="ml-5">
				<input class="gray" type="button" value="<spring:message code="arrest.list.insert"/>" onclick="javascript:fnArrestAdd();"/>
			</li>
		</ul>
	</div>
</form:form>

<div class="chn_loading_wrap">
	<div class="chn_loading">
		<img src="/images/cmmn/loading.gif" alt="결과화면 로딩 중"/>
	</div>
</div>
<div id="divArrestList" class="chn_table" style="width:100%;">
	

</div>
<div id="divUserInfo" style="display:none">
	소속: <c:out value="${userVO.place1}"/> ## 소속2: <c:out value="${userVO.place2}"/> 
	useGrade: <c:out value="${userVO.usegrade}"/> ## isAdmin:<c:out value="${userVO.isAdmin}"/>
</div>
<form id="detailForm" name="detailForm" method="post" action="">
	<input type="hidden" id="arrestId" name="arrestId" path="arrestId" value=""/>
</form>
<div id="divResult"></div>

	

<script type="text/javaScript" language="javascript" defer="defer">
<!--
	/* initialize function */
	$(document).ready(function(){
		//날짜 설정
		$('input[kind="date"]').datepicker();
				
		//검거기관 변경 이벤트
		$("#agencyType").on("change", function(){
			if($("#agencyType").val() == "01"){
				// 해경
				$("#agencyGuard").css("display", "block");
				$("#agencyGuard2").css("display", "none");
			}else{
				$("#agencyGuard").css("display", "none");
				$("#agencyGuard2").css("display", "block");
				$("#agencyGuard").val("");
						
			}
		});
		
		$("#agencyVessel").on("keyup", function(e){
			if( e.keyCode == 13 ){
				goSearch();
			}
		});
		
		$("#shipName").on("keyup", function(e){		
			if( e.keyCode == 13 ){
				goSearch();
			}
		});
		
		var listFlag = '<c:out value="${listFlag}"/>';
		
		if( listFlag == "search" ){
			
		}else{
			
			var dateS = "";
			var settingDate = new Date();

			
			if (dateS.length <= 0) {
				var today = new Date();
				var todayStr = $.datepicker.formatDate('yy-mm-dd', today);
				
				$('#endDate').val(todayStr);
				
				if(todayStr.substr(5,5) == "03-30" || todayStr.substr(5,5) == "03-31"){
					today.setMonth(2);
					today.setDate(1);
				}else{
					today.setMonth(today.getMonth()-12);	
				}
				
				
				
				$('#startDate').val($.datepicker.formatDate('yy-mm-dd', today));
				
				
			}
			
			var marginTop = "margin-top:" + ($("#tblArrest").height() + 150);
			var browserWidth = $(window).width() / 2 - 40;
						
			$("#footer").css("margin-top", marginTop);
			$("#divEmpty").css("margin-left", browserWidth);
		}
		
		
		$(window).resize(function(){
			var browserWidth = $(window).width() / 2 - 40;
			$("#divEmpty").css("margin-left", browserWidth);
		});
		
		$(".content").css("margin-bottom", 240);
		
		
		
	});
	
	function setAMonthAgo(date){
	    var selectDate = date.split("-");
	    var changeDate = new Date();
	    changeDate.setFullYear(selectDate[0], selectDate[1]-1);
	    
	    var y = changeDate.getFullYear();
	    var m = changeDate.getMonth();
	    var d = changeDate.getDay();
	    
	    if(m < 10)    { m = "0" + m; }
	    if(d < 10)    { d = "0" + d; }
	    var resultDate = y + "-" + m + "-" + d;
	    return resultDate;
	}

	
	
	/* 중국어선 나포정보 등록화면 이동 function */
	function fnArrestAdd(){
		var place = '<c:out value="${userVO.place1}"/>';
		var isAdmin = '<c:out value="${userVO.isAdmin}"/>';
				
		if( place != 'PS00' && isAdmin == 'false'){
			alert('기관 담당자만 입력가능합니다.');
			return;
		}
		
		document.arrestSearchForm.action = "<c:url value='/arrest/arrestAdd.do'/>";
	   	document.arrestSearchForm.submit();
	}

	
	/* 중국어선 나포정보 상세화면 이동 function */
	function fnArrestView(arrestId){		
		document.detailForm.arrestId.value = arrestId;
		document.detailForm.action = "<c:url value='/arrest/arrestView.do'/>";
	   	document.detailForm.submit();
	}
	
	
	/* 숫자 콤마찍기 function */
	function comma(str) {
	    str = String(str);
	    return str.replace(/(\d)(?=(?:\d{3})+(?!\d))/g, '$1,');
	}
	
	/* 시군구 목록 가져오기 function */
	function fncGetCombo(_obj, _id, _value) {		
		var select = $('#addrCity').empty();
	
		$.ajax({
			url: '/arrest/getAddressList.json', 
			type: 'get',
			data : {code1: _value},
			dataType: 'json',
			success: function (result) {
				var data = result.data;
				if (select.length == 0) {
					select = $('<select id="addrCity" name="addrCity" path="addrCity">');
				}			
				if (data.length > 0) {
					var alloption = $('<option>').html("전체").val("");
					select.append(alloption);
					for (var i=0; i < data.length; i++) {
						option = $('<option>')
									.html(data[i].codenm)
									.val(data[i].code1);
						if ('${arrestSearchVO.addrCity}' == data[i].code1) {
							option.attr('selected', true);
						}
						select.append(option);
					}
				}else{
					var option = $('<option>').html("전체").val("");
					option.attr('selected', true);
					select.append(option);
					
				}
				$("#divAddressCity").append(select);
			}
		});
		
		
	}
	
	/* date 배열 return function */
	function returnDateArray(date){
		var dateArr = new Array();
		
		if( date.length == 8 ){
			dateArr[0] = date.substr(0,4);
			dateArr[1] = date.substr(4,2);
			dateArr[2] = date.substr(6,2);			
			
		}else if( date.length == 10 ){
			if( date.indexOf("-") > 0 ){
				dateArr = date.split("-");	
			}else if( date.indexOf(".") > 0 ){
				dateArr = date.split(".");	
			}else{				
				return null;
			}
		}else{
			return null;
		}
		
		if( dateArr.length != 3){
			return null;
		}
		
		return dateArr;
	}
	
	
	/* 출력 */
	function fn_print(){
		var divPrintTitle = document.getElementById("divPrintTitle");
		divPrintTitle.style.display = "inline";
		printDiv($("#divListArea").html());
		divPrintTitle.style.display = "none";
		
		//var printThis = document.getElementById("divArrestList").innerHTML;
    	//printDiv(printThis);
	}
	
	/* 검색 function */
	function goSearch(){
		
		/* 조회기간 Validation */
		
		var startDate = $("#startDate").val();
		var endDate = $("#endDate").val();
		
		var startArray = returnDateArray(startDate);
		var endArray = returnDateArray(endDate);
		
		if(startArray == null){
			alert('시작일자 형식이 잘못되었습니다.');
			$("#startDate").focus();
			return;
		}
		if(endArray == null){
			alert('종료일자 형식이 잘못되었습니다.');
			$("#endDate").focus();
			return;
		}
			
		
		var startObj = new Date(startArray[0], Number(startArray[1]) -1, startArray[2]);
		var endObj = new Date(endArray[0], Number(endArray[1]) -1, endArray[2]);
		var dayCnt = (endObj.getTime() - startObj.getTime() ) / 1000 / 60 / 60 / 24;
		var year = startObj.getYear() + 1900;
		
		if(startObj.getMonth()+1 > 2){
			year = endObj.getYear() + 1900;
		}else{
			year = startObj.getYear() + 1900;
		}
		var yearCnt = ((year%4==0 && year%100!=0) || year%400==0) ? 366 : 365;
		
		
		if( dayCnt > yearCnt ){
			alert('조회기간은 최대 1년을 넘을 수 없습니다.');
			return;
		}else if( dayCnt < 0 ){
			alert('시작일이 종료일보다 클 수 없습니다.');
			return;
		}
		
		
	   	$('#arrestSearchForm').ajaxForm({
	   		url: "<c:url value='/arrest/arrestList.do'/>", 
			//url: "<c:url value='/WEB-INF/jsp/arrest/arrestResult.tiles'/>", 
			type: 'post',
			//data: $("#divArrestList").html(),
			dataType: 'html',
			success: function(result){
				
				//$("#divArrestList").empty();
				$("#divArrestList").html(result);
				
				//var marginTop = "margin-top:" + ($("#tblArrest").height() + 150) + "px;";				
				//$("#footer").prop("style", marginTop);
				
				$(".chn_loading_wrap").css("display", "none");
				
				var browserWidth = $(window).width() / 2 - 19;
				$("#divEmpty").prop("style", "margin-left:"+browserWidth+"px;");
				
				// class content margin-bottom 설정
				if( $("#divPrintArea").height() < 700 ){
					var chnTableHeight = $("#divPrintArea").height() + 260;
					$(".content").css("margin-bottom", chnTableHeight);
				}else{
					$(".content").css("margin-bottom", 936);
				}
				
			},
			fail : function (result){
				alert("등록실패 오류입니다.");
			}
		});
	   	
	   	
	   	
	   	$(".chn_loading_wrap").css("display", "table");
	   	//$(".chn_loading").css("margin-left", $(window).width() / 2 - 40);
	   	//$(".chn_loading").css("margin-top", $(window).height() / 2 );
		$("#arrestSearchForm").submit();
    }
	
	
	 
	/* debugging function */ 	
	function show(){ 
		if(location.host == "localhost:8080"){
			if( $("#divUserInfo").css("display") == "block"){
				$("#divUserInfo").css("display","none");
			}else{
				$("#divUserInfo").css("display","block");
			}			
		}
	}
	
	/* 엑셀받기 function */ 	
	function fn_goExcel() {
		var cnt = '<c:out value="${arrestCntList.totalCnt}"/>';
		
		document.arrestSearchForm.method = "post";
		document.arrestSearchForm.action = "<c:out value='/arrest/arrestExcelDownload.do' />";
		document.arrestSearchForm.submit();
	}
		
	
	window.onload = function(){
		goSearch();
	}
	-->
	
</script>
