<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.io.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ include file="/jsp/common/include/localemessage.jsp" %>
<%
	response.setHeader("Pragma", "No-cache");
	response.setHeader("Cache-Control", "no-cache");
	response.setHeader("Expires", "0");

%>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<jsp:include page="/jsp/common/include/css.jsp"/>
<jsp:include page="/jsp/common/include/script.jsp"/>
<script language="javascript" >
	var url      = '<c:url value="/onl/transaction/tracking/integrationLogMan.json" />';
	var url_view = '<c:url value="/onl/transaction/tracking/integrationLogMan.view" />';
	var url_excel = '<c:url value="/onl/transaction/tracking/integrationLogMan.excel" />';
	var selectName = "searchEaiBzwkDstcd";	// selectBox Name

	function isValidGuid()
	{
		var guid = $("input[name=searchGuid]").val();
		
		if(guid != "")
		{
			if(guid.length != 33)
			{
				alert("GUID는 33자리만 입력 할 수 있습니다.");
				$("input[name=searchGuid]").focus();
				return false;
			}
		}
		
		return true;
	}

	function getStartTime(){
		var now = new Date();
		var tm = right("0" + now.getHours(), 2);
		tm += right("0" + now.getMinutes(), 2);

		tm = (Math.floor(tm.substr(0, 2)) * 60) + Math.floor(right(tm, 2)) - 5;
		tm = right("0" + parseInt(tm/60), 2) + right("0" + tm%60, 2);
		return tm + '00';	
	}
	function getEndTime(){
		var now = new Date();
		var tm = right("0" + now.getHours(), 2);
		tm += right("0" + now.getMinutes(), 2);
		return tm + '59';
	}
	function init( callback) {
		$.ajax({
			type : "POST",
			url:url,
			dataType:"json",
			data:{cmd: 'LIST_COMBO'},
			success:function(json){
				new makeOptions("BIZCODE","BIZNAME").setObj($("select[name=searchEaiBzwkDstcd]")).setNoValueInclude(true).setNoValue("","전체").setData(json.bizList).setFormat(codeName3OptionFormat).rendering();
				new makeOptions("CODE","NAME").setObj($("select[name=searchLogPrcssSerno]")).setNoValueInclude(true).setNoValue("","전체").setData(json.logPrcssSernoList).rendering();
				
				setSearchable(selectName);	// 콤보에 searchable 설정
				
				if (typeof callback === 'function') {
					callback();
				}
			},
			error:function(e){
				alert(e.responseText);
			}
		});
		
	}
	function detail(){
		var start = $("input[name=searchStartYYYYMMDD]").val().replace(/-/gi,"");
		start = start + $("input[name=searchStartHHMM]").val();
		
		var end = $("input[name=searchEndYYYYMMDD]").val().replace(/-/gi,"");
		end = end + $("input[name=searchEndHHMM]").val();
		
		if(start > end){
			alert("조회기간을 확인해주세요.");
			return false;
		}
		
		$("#grid").clearGridData();		// 이전 데이터 삭제.
		
		$("input[name=searchStartTime]").val(start);
		$("input[name=searchEndTime]").val(end);
		
		// guid로 검색 할 때 자리수 체크.
		if(!isValidGuid()) return;
		
		var postData = getSearchForJqgrid("cmd","LIST"); //jqgrid에서는 object 로 
		$("#grid").setGridParam({ url:url,postData: postData ,datatype:'json' }).trigger("reloadGrid");
	}

	function guidEvent(){
		var searchGuid = $("input[name=searchGuid]").val();
		if (searchGuid.trim() == '')
			return ;
		
		var searchTm = searchGuid.trim();
		
		var yy = searchTm.substr(13,4);
		var mm = searchTm.substr(17,2); 
		var dd = searchTm.substr(19,2);
		var hh = searchTm.substr(21,2);
		var MM = searchTm.substr(23,2);
		
		var ms = Date.UTC(yy,mm-1,dd,hh,MM,00);
		var date = new Date(ms);
		var startYYYYMMDD;
		var startHHMM;
		var endYYYYMMDD;
		var endHHMM;
		//Local Time Setting
		date.setHours( date.getHours() - 9 );
		//5분 간격
		date.setMinutes( date.getMinutes() - 5 );
		
		startYYYYMMDD = date.getFullYear() + right("0" + (date.getMonth() + 1), 2) + right("0" + (date.getDate()), 2);
		startHHMM = right("0" + date.getHours() , 2) + right("0" + date.getMinutes() , 2) + "00";
		
		date.setMinutes( date.getMinutes() + 10 );
		
		endYYYYMMDD = date.getFullYear() + right("0" + (date.getMonth() + 1), 2) + right("0" + (date.getDate()), 2);
		endHHMM = right("0" + date.getHours() , 2) + right("0" + date.getMinutes() , 2) + "00";

		//추가 코드 입력 2012.05.16 권세환 (일자가 바뀔경우 시간 간격을 +-5분 -> +-1분)
		if (startYYYYMMDD.substr(6,2) != endYYYYMMDD.substr(6,2)){
			date.setMinutes( date.getMinutes() - 6 );
			
			startYYYYMMDD = date.getFullYear() + right("0" + (date.getMonth() + 1), 2) + right("0" + (date.getDate()), 2);
			startHHMM = right("0" + date.getHours() , 2) + right("0" + date.getMinutes() , 2) + "00";
			
			date.setMinutes( date.getMinutes() + 2 );
		
			endYYYYMMDD = date.getFullYear() + right("0" + (date.getMonth() + 1), 2) + right("0" + (date.getDate()), 2);
			endHHMM = right("0" + date.getHours() , 2) + right("0" + date.getMinutes() , 2) + "00";
		}
		$("input[name=searchStartYYYYMMDD]").val(startYYYYMMDD);
		$("input[name=searchEndYYYYMMDD]").val(endYYYYMMDD);
		$("input[name=searchStartHHMM]").val(startHHMM);
		$("input[name=searchEndHHMM]").val(endHHMM);
		
	}
	
function timeStampFormat2(cellvalue, options, rowObject){
	if ( cellvalue == null || cellvalue.length < 17 )
		return '';
	return cellvalue.substr(0,4) + '-' +
	       cellvalue.substr(4,2) + '-' +
	       cellvalue.substr(6,2) + ' ' +
	       cellvalue.substr(8,2) + ':' +
	       cellvalue.substr(10,2) + ':' +
	       cellvalue.substr(12,2) +  ' ' + cellvalue.substr(14,3);
	       
}	

$(document).ready(function() {	
	$("input[name=searchStartYYYYMMDD],input[name=searchEndYYYYMMDD]").inputmask("yyyy-mm-dd",{'autoUnmask':true});
	$("input[name=searchStartHHMM],input[name=searchEndHHMM]").inputmask("hh:mm:ss",{'autoUnmask':true});
	
	$("input[name=searchStartYYYYMMDD],input[name=searchEndYYYYMMDD]").each(function(){
		if ($(this).val() == undefined || $(this).val() == null || $(this).val() == ""){
			$(this).val(getToday());
		}
	});
	$("input[name=searchStartHHMM]").each(function(){
		if ($(this).val() == undefined || $(this).val() == null || $(this).val() == ""){
			$(this).val(getStartTime());
		}
	});
	$("input[name=searchEndHHMM]").each(function(){
		if ($(this).val() == undefined || $(this).val() == null || $(this).val() == ""){
			$(this).val(getEndTime());
		}
	});

	init();
	$('#grid').jqGrid({
		datatype : "local",
		mtype : 'POST',
		colNames : ['<%=localeMessage.getString("tracking.eaisvcserno")%>',
            '<%=localeMessage.getString("tracking.logprcssserno")%>',
            '<%=localeMessage.getString("tracking.eaibzwkdstcd")%>',
            '<%=localeMessage.getString("tracking.msgdpstyms")%>',
            '<%=localeMessage.getString("tracking.erryn")%>',
            '<%=localeMessage.getString("tracking.trackasiskey1ctnt")%>',
            '<%=localeMessage.getString("tracking.trackasiskey2ctnt")%>',
            
            '<%=localeMessage.getString("tracking.eaisvcserno_tmp")%>',
            '<%=localeMessage.getString("tracking.keymgtmsgctnt")%>',
            '<%=localeMessage.getString("tracking.logprcssserno_tmp")%>',
            '<%=localeMessage.getString("tracking.telgmrecvtrancd")%>',
            '<%=localeMessage.getString("tracking.eaisvcname")%>',
            '<%=localeMessage.getString("tracking.eaisvcdesc")%>',
            '<%=localeMessage.getString("tracking.eaibzwkdstcd_tmp")%>',
            '<%=localeMessage.getString("tracking.msgdpstyms_tmp")%>',
            '<%=localeMessage.getString("tracking.msgprcssyms")%>',
            '<%=localeMessage.getString("tracking.companycode")%>',
            '<%=localeMessage.getString("tracking.companyname")%>',
            '<%=localeMessage.getString("tracking.osidinstinm")%>',
            '<%=localeMessage.getString("tracking.companyitcontactname")%>',
            '<%=localeMessage.getString("tracking.companyitcontacttelno")%>',
            '<%=localeMessage.getString("tracking.itcontactpointperson1")%>',
            '<%=localeMessage.getString("tracking.bzwkcontactperson")%>'
             ],
colModel : [ 
			{ name : 'EAISVCSERNO'        , hidden:true },
            { name : 'LOGPRCSSSERNO'      , hidden:true },
            { name : 'EAIBZWKDSTCD'       , hidden:true },
            { name : 'MSGDPSTYMS'         , hidden:true },
            { name : 'ERRYN'         	  , hidden:true },
            { name : 'TRACKASISKEY1CTNT'  , hidden:true },
            { name : 'TRACKASISKEY2CTNT'  , hidden:true },
            
            { name : 'EAISVCSERNO_TMP'    , align : 'left'   , width:'125' },
            { name : 'KEYMGTMSGCTNT'      , align : 'left'   , width:'235' },
            { name : 'LOGPRCSSSERNO_TMP'  , align : 'center' , width:'40'  },
            { name : 'TELGMRECVTRANCD'    , align : 'left'   , width:'130' },
            { name : 'EAISVCNAME'         , align : 'left'   , width:'150' },
            { name : 'EAISVCDESC'         , align : 'left'   , width:'190' },
            { name : 'EAIBZWKDSTCD_TMP'   , align : 'center' , width:'60'  },
            { name : 'MSGDPSTYMS_TMP'     , align : 'center' , width:'150' , formatter: timeStampFormat2 },
            { name : 'MSGPRCSSYMS'        , align : 'center' , width:'150' , formatter: timeStampFormat2 },
            { name : 'COMPANYCODE'   	  , align : 'center' , width:'80'  },
        	{ name : 'COMPANYNAME'   	  , align : 'center' , width:'100' },
       		{ name : 'ADPTRINSTINAME'     , align : 'left'   , width:'160' },
            { name : 'COMPANYITCONTACTNAME'     , align : 'left'   , width:'110' },
            { name : 'COMPANYITCONTACTTELNO'    , align : 'left'   , width:'120' },
            { name : 'ITCONTACTPOINTPERSON1'    , align : 'left'   , width:'120' },
            { name : 'BZWKCONTACTPERSON'        , align : 'left'   , width:'120' }
              ],
		jsonReader : {
			repeatitems : false
		},
		rowNum: 10000,
		height : '520',
		viewrecords : true,
		gridview: true,
		//autowidth: true,
		ondblClickRow : function(rowId) {
			var rowData = $(this).getRowData(rowId);
		    var args = new Object();
		    args['eaiBzwkDstcd']  = rowData['EAIBZWKDSTCD'];
		    args['msgDpstYMS']    = rowData['MSGDPSTYMS'];
		    args['logPrcssSerno'] = rowData['LOGPRCSSSERNO'];
		    args['eaiSvcSerno']   = rowData['EAISVCSERNO'];
		    
		    var url2 = '<c:url value="/onl/transaction/tracking/integrationLogMan.view"/>';
		    url2 = url2 + "?cmd=DETAIL";
		    showModal(url2,args,1000,1218,undefined,"scroll:yes;status:no;resizable:yes;");

		    //url2 = url2 + "&eaiBzwkDstcd="  +rowData['EAIBZWKDSTCD'];
		    //url2 = url2 + "&msgDpstYMS="    +rowData['MSGDPSTYMS'];
		    //url2 = url2 + "&logPrcssSerno=" +rowData['LOGPRCSSSERNO'];
		    //url2 = url2 + "&eaiSvcSerno="   +rowData['EAISVCSERNO'];
		    //goNav(url2);
		},
		gridComplete:function (d){
		// 헤더 화살표 없애기
			var colModel = $(this).getGridParam("colModel");
			for(var i = 0 ; i< colModel.length; i++){
				$(this).setColProp(colModel[i].name, {sortable : false});			
			}
			 
		},			
	    loadComplete:function (d){
	    	$("#grid").tuiTableRowSpan("7");
	    	
	    	//에러 행 색상 표시
	    	var rows = $(this).getDataIDs();
	    	for( var i= 0; i < rows.length; i++){
	    		var val = $("#grid").getCell(rows[i],'ERRYN');
	    		if(val =="Y"){
	    			$("#"+rows[i]).find("td").css("background-color","#FFCC99");
	    		}
	    	}
	    	
	    	//1000건이상 alert
	    	if(d.records == 1000){
	    		alert("조회결과 1,000건이 넘습니다.\n최근 1,000건만 출력됩니다.");	
	    	}
	    	
	    },
	    loadError: function(jqXHR, textStatus, errorThrown){
	    	if(jqXHR.status == 401) {
	    		alert('HTTP status code: ' + jqXHR.status + '\n' +
	                    '에러:  세션이 종료되었습니다. ');
	    		parent.location.href='<%=request.getContextPath()%>/';	
	    	}
	    	else if(jqXHR.status == 400) {
	    		$("#grid").clearGridData();
	        	alert(JSON.parse(jqXHR.responseText).errorMsg);
	    	}
	    	else {
	        	alert('HTTP status code: ' + jqXHR.status + '\n' +
	                    'textStatus: ' + textStatus + '\n' +
	                    'errorThrown: ' + errorThrown);
//	             alert('HTTP message body (jqXHR.responseText): ' + '\n' + jqXHR.responseText);
	    	}
	        	
	     }		

	});
	
	resizeJqGridWidth('grid','content_middle','1680');
	//$("#grid").jqGrid('setFrozenColumns');
	
	$("#btn_search").click(function(){
		//$("input[name=searchGuid]").blur();
		detail();
	});
	
	$("input[name^=search]").keydown(function(key){
		if (key.keyCode == 13){
			guidEvent();
			$("#btn_search").click();
		}
	});
	
	$("input[name=searchStartYYYYMMDD]").change(function(e){
		$("input[name=searchEndYYYYMMDD]").val($(this).val());
	})
	
	//검색조건 달력
	$("#startDatepicker").datepicker();
	
	//업체명 검색
	$("#btn_popup").click(function(){
		var key = "";
		var args = new Object();
    	args['companyName'] = $('input[name=companyName1]').val();
	    var url='<c:url value="/onl/admin/service/companyMan.view" />';
	    url = url + "?cmd=POPUP";
	    var ret = showModal(url,args,1020,630, function(arg){
	    	var args = null;
	        if(arg == null || arg == undefined ) {//chrome
	            args = this.dialogArguments;
	            args.returnValue = this.returnValue;
	        } else {//ie
	            args = arg;
	        }
	        
	        if( !args || !args.returnValue ) return;
	        
	        var ret = args.returnValue;
	        console.log("ret",ret);
			key = ret['key'];
			$("input[name=companyName1]").val(key);
			$("input[name=searchCompanyName]").val(key);
			
	    });
	});
	
	//업체명 검색
	$("#btn_init").click(function(){
		$("input[name=companyName1]").val("");
		$("input[name=searchCompanyName]").val("");
		
	});
	
	$("#btn_excel").click(function(event) {
        event.stopPropagation(); // Do not propagate the event.
        // Create an object that will manage to download the file.
        var target = "excelDown" ; // iframe의 name
        //(url,cmd,gridObj,formObject,fileName,merge)
        
        var start = $("input[name=searchStartYYYYMMDD]").val().replace(/-/gi,"");
		start = start + $("input[name=searchStartHHMM]").val();
		var end = $("input[name=searchEndYYYYMMDD]").val().replace(/-/gi,"");
		end = end + $("input[name=searchEndHHMM]").val();
		
		$("input[name=searchStartTime]").val(start);
		$("input[name=searchEndTime]").val(end);
        
		gridToExcelSubmit2(url_excel,"LIST_GRID_TO_EXCEL",$("#grid"),$("#ajaxForm"),'<%= localeMessage.getString("tracking.title")%>');
		return false;
	});
	
	//GUID 
	$("input[name=searchGuid]").blur(function(event) {
		guidEvent();
		
	});
	
	buttonControl();
	
	var local = localStorage.getItem("guid");
	
	if(local != null){
		if (local.length > 33) local=local.substr(0,33);
		
		$("input[name=searchGuid]").val(local);
		localStorage.removeItem("guid");
		
		local = null;		
	}
	
});
 
</script>
</head>
	<body>		
		<div class="right_box">
			<div class="content_top">
				<ul class="path">
					<li><a href="#">${rmsMenuPath}</a></li>					
				</ul>					
			</div><!-- end content_top -->
			<div class="content_middle">
				<div class="search_wrap">
					<img src="<c:url value="/img/btn_excel.png"/>" id="btn_excel" level="W"/> 
					<img src="<c:url value="/img/btn_search.png"/>" alt="" id="btn_search" level="R" />
				</div>
				<div class="title">통합로그 조회</div>
				
				<form id = "ajaxForm">
				<table class="search_condition" cellspacing=0;>
					<tbody> 						 
						<tr>
							<th width="130px"><%=localeMessage.getString("tracking.bzwkdstcd")%></th>
							<td style="width: 18%;">
								<div style="position: relative; width: 100%;">									
										<select name="searchEaiBzwkDstcd" value="${param.searchEaiBzwkDstcd}">
										</select>	
								</div>
							</td>
							<th width="110px">GUID</th>
							<td style="width: 18%;"><input type="text" name="searchGuid" value="" maxlength="33"></td>
							<th width="130px"><%=localeMessage.getString("tracking.msgrecvtm")%></th>
							<td style="width: 25%;">
								<input type="text" name="searchStartYYYYMMDD" id="startDatepicker" readonly="readonly" value="" size="10" style="width:80px; border:1px solid #ebebec;">
								<input type="text" name="searchStartHHMM" value="" size="8" style="width:80px; border:1px solid #ebebec;">
								~
								<input type="text" name="searchEndYYYYMMDD" value="" id="endDatepicker" size="10" readonly="readonly" style="width:80px; border:1px solid #ebebec;">
								<input type="text" name="searchEndHHMM" value="" size="8" style="width:80px; border:1px solid #ebebec;">
								<input type="hidden" name="searchStartTime" value="" style="width:0px;">
								<input type="hidden" name="searchEndTime" value="" style="width:0px;">
							</td>														
						</tr>
						<tr>
							<th><%=localeMessage.getString("tracking.eaisvcname")%></th>
							<td><input type="text" name="searchEaiSvcName" value="${param.searchEaiSvcName}"></td>	
							<th><%=localeMessage.getString("tracking.companycode")%></th>
							<td><input type="text" name="searchCompanyCode" value="${param.searchCompanyCode}"></td>
							<th><%=localeMessage.getString("tracking.companyname")%></th>
							<td><input type="hidden" name="searchCompanyName" value="" style="width:0px;">
								<input type="text" name="companyName1" value="${param.searchCompanyName}" style="width:70%;" readonly>
								<img src="<c:url value="/img/icon_search.png"/>" alt="" id="btn_popup" style="margin: 5px;cursor:hand "/>
								<img src="<c:url value="/img/btn_pop_initialize.png"/>" alt="" id="btn_init" style="margin: 5px;cursor:hand "/>
							</td>													
						</tr>				
						<tr>
							<th><%=localeMessage.getString("tracking.eaisvcserno_tmp")%></th>
							<td><input type="text" name="searchEaiSvcSerno" value="${param.searchEaiSvcSerno}"></td>
							<th>seq</th>
							<td>
								<div >	
									<select name="searchLogPrcssSerno" value="${param.searchLogPrcssSerno}" style="width: 100%;">									
									</select>
								</div>		
							</td>
							<th ><%=localeMessage.getString("tracking.logtype")%></th>
							<td>
								<input type="radio" name="searchErrorYn" value="N" checked="checked" id="sub2_2_1_1"><label for="sub2_2_1_1">전체로그</label>
								<input type="radio" name="searchErrorYn" value="Y" id="sub2_2_1_2"><label for="sub2_2_1_2">에러로그</label>
							</td>	
						</tr>
						<tr>
							<th><%=localeMessage.getString("tracking.keymgtmsgctnt")%></th>
							<td><input type="text" name="searchKeyMgtMsgCtnt" value="${param.searchKeyMgtMsgCtnt}"></td>								
							<th><%=localeMessage.getString("tracking.trackasiskeyctnt")%></th>
							<td colspan="4">
								<input type="text" name="searchTrackAsisKey1Ctnt" value="${param.searchTrackAsisKey1Ctnt}" style="width: 20%;">
								<input type="text" name="searchTrackAsisKey4Ctnt" value="${param.searchTrackAsisKey4Ctnt}" style="width: 20%;">
							</td>
						</tr>
					</tbody>
				</table>
				</form>
				
				<table id="grid" ></table>
				<div id="pager"></div>
				
			</div><!-- end content_middle -->
		</div><!-- end right_box -->				
	</body>		
</html>

