<%@ 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/statistics/statisticinfo/dailyStatisticMan.json" />';
var url_view  ='<c:url value="/onl/statistics/statisticinfo/dailyStatisticMan.view" />';
var url_excel ='<c:url value="/onl/statistics/statisticinfo/dailyStatisticMan.excel" />';


//합계만보기
var onlySum = "N";

function detail(){
	var year = $("select[name=year]").val();
	var month = $("select[name=month]").val();
	
	var start = new Date(year,month-1,1);
	var end = new Date(year,month,0);
	var monthTmp = start.getMonth()+1;
	var dayTmp = start.getDate();
	if(month<10){
		month="0"+month;
	}
	if(dayTmp<10){
		dayTmp="0"+dayTmp;
	}
	var startDay = start.getFullYear()+month+dayTmp;
	var endDay = end.getFullYear()+""+month+end.getDate();

	var postData = getSearchForJqgrid("cmd","LIST"); //jqgrid에서는 object 로
	postData["searchStart"] = startDay;
	postData["searchEnd"] = endDay;
	postData['onlySum']=onlySum;
	
	$("#grid").setGridParam({ url:url,postData: postData ,page:1 }).trigger("reloadGrid");
}

function dateFormater(cellvalue, options, rowObject){
	if(isNaN(cellvalue) == true) return cellvalue;
	if (cellvalue==null|| cellvalue==undefined) return "";
	if (cellvalue.length !=8) return "";
	var display = "";
	display=display+cellvalue.substr(0,4)+"-";
	display=display+cellvalue.substr(4,2)+"-";
	display=display+cellvalue.substr(6,2)+" ";
	
	return display;
}	

$(document).ready(function() {
	var now = new Date();
	var month =now.getMonth() + 1;
	
	var beforeYear = now.getFullYear()-1;
	for(var y = now.getFullYear(); y<now.getFullYear(); y++){
		$("select[name=year]").append("<option value='"+y+"'>"+y+"</option>");
	}
	$("select[name=year]").append("<option value='"+now.getFullYear()+"' selected>"+now.getFullYear()+"</option>");
	var beforeMonth= now.getMonth();
	if(beforeMonth==0){
		beforeMonth=12;
		$("select[name=year]").val(now.getFullYear()-1);
	}
	$("select[name=month]").val(beforeMonth);
	
	var searchYear = $("select[name=year]").val();
	var searchMonth = $("select[name=month]").val();
	
	var start = new Date(searchYear,searchMonth-1,1);
	var end = new Date(searchYear,searchMonth,0);
	var monthTmp = start.getMonth()+1;
	var dayTmp = start.getDate();
	
	if(searchMonth<10){
		searchMonth="0"+searchMonth;
	}
	if(dayTmp<10){
		dayTmp="0"+dayTmp;
	}
	var startDay = start.getFullYear()+searchMonth+dayTmp;
	var endDay = end.getFullYear()+""+searchMonth+end.getDate();
	
	
	var gridPostData = getSearchForJqgrid("cmd","LIST"); //jqgrid에서는 object 로 
	gridPostData["searchStart"] = startDay;
	gridPostData["searchEnd"] = endDay;
	gridPostData['onlySum']=onlySum;
	
	$('#grid').jqGrid({
		datatype:"json",
		mtype: 'POST',
		url: url,
		postData : gridPostData,
		colNames:[
					 '<%= localeMessage.getString("dayStatMan.div")%>'
					,'<%= localeMessage.getString("dayStatMan.tot")%>'
					,'<%= localeMessage.getString("dayStatMan.int")%>'
					,'<%= localeMessage.getString("dayStatMan.ext")%>'
					,'공동망개설거래'
					,'공동망취급거래'
					,'DMZ'
					,'<%= localeMessage.getString("dayStatMan.open")%>'
					,'<%= localeMessage.getString("dayStatMan.bap")%>'
                  ],
		colModel:[
					{ name : 'BASEYMD'			, align:'center'	,formatter:dateFormater				 },
					{ name : 'TOTALCNT'			, align:'right'		,width:'80px'	, formatter: 'integer', formatoptions:{thousandsSeparator:","} },
					{ name : 'TOTALCNT1'		, align:'right'		,width:'80px'	, formatter: 'integer', formatoptions:{thousandsSeparator:","} },
					{ name : 'TOTALCNT2'		, align:'right'		,width:'80px'	, formatter: 'integer', formatoptions:{thousandsSeparator:","} },
					{ name : 'TOTALCNT3'		, align:'right'		,width:'80px'	, formatter: 'integer', formatoptions:{thousandsSeparator:","} },
					{ name : 'TOTALCNT4'		, align:'right'		,width:'80px'	, formatter: 'integer', formatoptions:{thousandsSeparator:","} },
					{ name : 'TOTALCNT5'		, align:'right'		,width:'85px'	, formatter: 'integer', formatoptions:{thousandsSeparator:","} },
					{ name : 'TOTALCNT6'		, align:'right'		,width:'85px'	, formatter: 'integer', formatoptions:{thousandsSeparator:","} },
					{ name : 'TOTALCNT7'		, align:'right'		,width:'85px'	, formatter: 'integer', formatoptions:{thousandsSeparator:","} }
				],
				
        jsonReader: {
             repeatitems:false
        },
//         pager : $('#pager'),
// 		page : '${param.page}',
// 		rowNum : '${rmsDefaultRowNum}',
		footerrow:true,
		userDataOnFooter:true,
 	    height : '560',
		rowNum : 10000,
		autowidth: true,
		viewrecords: true,
		rowList : eval('[${rmsDefaultRowList}]'),
		gridComplete:function (d){
			var colModel = $(this).getGridParam("colModel");
			for(var i = 0 ; i< colModel.length; i++){
				$(this).setColProp(colModel[i].name, {sortable : false});	//그리드 헤더 화살표 삭제(정렬X)		
			}
		},		
		 loadComplete:function (d){
		    	//$("#grid").tuiTableRowSpan("0");
		    	var rowCnt = $('#grid').getGridParam("reccount");
		    	var colModel = $(this).getGridParam("colModel");
		    	for(var i = 0 ; i< colModel.length; i++){
					$(this).setColProp(colModel[i].name, {sortable : false});
					var propName = colModel[i].name;
					var summaryType = colModel[i].summaryType;
					var Total = $('#grid').jqGrid('getCol',propName,false,'sum');
					if(summaryType =='avg'){
						if(rowCnt == 0) 
							Total = 0;
						else
		    				Total = (Total/rowCnt).toFixed(3);		//avg를 바로 쓰면 row 수가 +1 되어 계산됨.
		    		}	
		    		eval("$('#grid').jqGrid('footerData','set',{BASEYMD:'합계', "+propName+":Total});");
		    	}
				
				$(".ui-jqgrid-ftable tr:first").css("height", "25px");								// tr 높이
				$(".ui-jqgrid-ftable tr:first").css("font-size", "10pt");							// tr 글자 크기
				$(".ui-jqgrid-ftable").css('bottom','0');
				
		    },
        loadError: function (jqXHR, textStatus, errorThrown) {
        	
        	var location ='<%=request.getContextPath()%>/';
        	comloadError(jqXHR, textStatus, errorThrown ,location);
        	
    	}
	});
	
	$("#grid").jqGrid('setGroupHeaders', {
  		useColSpanStyle: true, 
  		 groupHeaders:[
 			{startColumnName: 'TOTALCNT3', numberOfColumns: 2, titleText: '공동망'},
   		]
	});
	//resizeJqGridWidth('grid','content_middle','1680');
	
	$("#btn_search").click(function(){ 
		detail();
	});
	
	$("input[name^=search]").keydown(function(key){
		if (key.keyCode == 13){
			$("#btn_search").click();
		}
	});
	
	//refresh 체크박스
	$("input:checkbox").on('click',function(){
		//합계만보기
		if($("#onlySum").is(":checked")){ //최근10분여부
			onlySum="Y";
		}else{
			onlySum="N";
		}
		
		
	});
	

	$("#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)
		gridToExcelSubmit2(url_excel,"LIST_GRID_TO_EXCEL",$("#grid"),$("#ajaxForm"),'<%= localeMessage.getString("statisticsByCompany.titleDaily")%>');
		return false;
	});
	$("#startDatepicker").datepicker();
	$("#endDatepicker").datepicker();
	
	//detail();
	buttonControl();
});
 
</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">
					<input type="checkbox" id="onlySum" value="Y" />&nbsp;<b><%= localeMessage.getString("dayStatMan.onTot")%></b>&nbsp;	
<%-- 					<img src="<c:url value="/img/btn_excel.png"/>" id="btn_excel" level="W"/> --%>
					<button type="button" class="cssbtn" id="btn_search" level="R" ><i class="material-icons">search</i> <%= localeMessage.getString("button.search") %></button>		
<%-- 					<img src="<c:url value="/img/btn_search.png"/>" alt="" id="btn_search" level="R" />					 --%>
				</div>
				<div class="title"><%= localeMessage.getString("dayStatMan.dayTranStat")%></div>
				<form id="ajaxForm"  onsubmit="return false;">
					<table class="search_condition" cellspacing=0;>
						<tbody>
							<tr>
								<th style="width:180px;"><%= localeMessage.getString("dayStatMan.date")%></th>
								<td width="600px">
									<select name="year" style="padding: 2px;">
									</select>
									<%= localeMessage.getString("dayStatMan.year")%>
									<select name="month" style="padding: 2px;">
										<c:forEach var="month" begin="1" end="12">
											<option value="${month}">${month }</option>
										</c:forEach> 
									</select>
									<%= localeMessage.getString("dayStatMan.mon")%>
									</td>		
							</tr>	
						</tbody>
					</table>
				</form>
				<table id="grid" ></table>
				<div id="pager"></div>				
			</div><!-- end content_middle -->
		</div><!-- end right_box -->				
	</body>	
</html>

