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

var selectName = "searchTypeCode,searchEaiBzwkDstcd,searchAdptrInstiCode";	// selectBox Name

function detail(){
	var startMonth =parseInt($("select[name=searchStart]").val());
	var endMonth = parseInt($("select[name=searchEnd]").val());
	if(startMonth<10){
		startMonth='0'+startMonth
	}
	if(endMonth<10){
		endMonth='0'+endMonth
	}
	var startDate =  $("select[name=selectStartYear]").val()+startMonth;
	var endDate = $("select[name=selectEndYear]").val()+endMonth;
	
	if(startDate > endDate){
		alert("<%= localeMessage.getString("statisticsByCompany.alert")%>");
		return;
	}

	$("input[name=searchStartYYYYMM]").val(startDate);
	$("input[name=searchEndYYYYMM]").val(endDate);
	
	var postData = getSearchForJqgrid("cmd","LIST"); //jqgrid에서는 object 로
	postData['searchAppDstcd'] = postData['searchAppDstcd'].toUpperCase();
	postData['searchCompanyCode'] = postData['searchCompanyCode'].toUpperCase();
	postData['searchAppName'] = postData['searchAppName'].toUpperCase();
	$("#grid").setGridParam({ url:url,postData: postData ,page:1 }).trigger("reloadGrid");
}

function dateFormater(cellvalue, options, rowObject){
	if (cellvalue==null|| cellvalue==undefined) return "";
	if (cellvalue.trim().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;
}	
function init(callback){
	$.ajax({  
		type : "POST",
		url:url,
		dataType:"json",
		data:{cmd: 'LIST_INIT_COMBO'}, 
		success:function(json){
			new makeOptions("CODE","NAME").setObj($("select[name=searchTypeCode]")).setNoValueInclude(true).setNoValue("", "<%= localeMessage.getString("combo.all")%>").setData(json.sndRecvCdRows).setFormat(codeName3OptionFormat).rendering();
			new makeOptions("CODE","NAME").setObj($("select[name=searchEaiBzwkDstcd]")).setNoValueInclude(true).setNoValue("", "<%= localeMessage.getString("combo.all")%>").setData(json.bzwkDstcdRows).setFormat(codeName3OptionFormat).rendering(); 
			new makeOptions("CODE","NAME").setObj($("select[name=searchAdptrInstiCode]")).setNoValueInclude(true).setNoValue("", "<%= localeMessage.getString("combo.all")%>").setData(json.adptrDescRows).setFormat(codeName3OptionFormat).rendering();
			
			setSearchable(selectName);	// 콤보에 searchable 설정
			
			if (typeof callback === 'function') { 
				callback();
			}
		},
		error:function(e){
			alert(e.responseText);
		}
	});
}
$(document).ready(function() {
	
	var now = new Date();
	var month =now.getMonth() + 1;
	
	var beforeYear = now.getFullYear()-1;
	
	for(var y = now.getFullYear()-10; y<now.getFullYear(); y++){
		$("select[name=selectStartYear]").append("<option value='"+y+"'>"+y+"</option>");
	}
	$("select[name=selectStartYear]").append("<option value='"+now.getFullYear()+"' selected>"+now.getFullYear()+"</option>");
	
	for(var y = now.getFullYear()-10; y<now.getFullYear(); y++){
		$("select[name=selectEndYear]").append("<option value='"+y+"'>"+y+"</option>");
	}
	$("select[name=selectEndYear]").append("<option value='"+now.getFullYear()+"' selected>"+now.getFullYear()+"</option>");
	
	var beforeMonth= now.getMonth();
	if(beforeMonth==0){
		beforeMonth=12;
		$("select[name=selectStartYear]").val(now.getFullYear()-1);
	}
	
	$("select[name=searchStart],select[name=searchEnd]").each(function(){
		$("select[name=searchStart]").val(beforeMonth);
		$("select[name=searchEnd]").val(month);
		$("input[name=searchStartYYYYMM]").val(beforeYear+beforeMonth);
		$("input[name=searchEndYYYYMM]").val(now.getFullYear()+month);
	});

	
	
	var gridPostData = getSearchForJqgrid("cmd","LIST"); //jqgrid에서는 object 로 
	gridPostData['dateMode']='M';
	$('#grid').jqGrid({
		datatype:"json",
		mtype: 'POST',
		url: url,
		postData : gridPostData,
		colNames:[
					'<%= localeMessage.getString("statisticsByCompany.monthly")%>'
					,'<%= localeMessage.getString("statisticsByCompany.eaibzwkDstcd")%>'
					,'<%= localeMessage.getString("statisticsByCompany.bzwkName")%>'
					,'<%= localeMessage.getString("statisticsByCompany.appCode")%>'
					,'<%= localeMessage.getString("statisticsByCompany.appName")%>'
					,'<%= localeMessage.getString("statisticsByCompany.companyCode")%>'
					,'<%= localeMessage.getString("statisticsByCompany.companyName")%>'
					,'<%= localeMessage.getString("statisticsByCompany.instiName")%>'
					,'<%= localeMessage.getString("statisticsByCompany.instiCode")%>'
					,'<%= localeMessage.getString("statisticsByCompany.sendRecvType")%>'
					,'<%= localeMessage.getString("statisticsByCompany.totCnt")%>'
					,'<%= localeMessage.getString("statisticsByCompany.succCnt")%>'
					,'<%= localeMessage.getString("statisticsByCompany.errCnt")%>'
					,'<%= localeMessage.getString("statisticsByCompany.totTelgmLen")%>'
					,'<%= localeMessage.getString("statisticsByCompany.succTelgmLen")%>'
					,'<%= localeMessage.getString("statisticsByCompany.errTelmLen")%>'
                  ],
		colModel:[
					{ name : 'MONTHLY'				, align:'center'	},
					{ name : 'EAIBZWKDSTCD'		, align:'center'		,width:'85px'	 },
					{ name : 'BZWKDSTICNAME'	, align:'left'		 				 },
					{ name : 'APPDSTCD'			, align:'center'	,width:'95px'	 },
					{ name : 'APPNAME'			, align:'left'		 			 },
					{ name : 'COMPANYCODE'		, align:'left'		,width:'80px'	 },
					{ name : 'COMPANYNAME'		, align:'left'			 			 },
					{ name : 'ADPTRINSTINAME'	, align:'left'						 },
					{ name : 'ADPTRINSTICODE'	, align:'left'		,hidden:true	 },
					{ name : 'TYPECODE'			, align:'center'	,width:'80px'	 },
					{ name : 'TOTALCNT'			, align:'right'		,width:'80px'	, formatter: 'integer', formatoptions:{thousandsSeparator:","} },
					{ name : 'SUCCESSCNT'		, align:'right'		,width:'80px'	, formatter: 'integer', formatoptions:{thousandsSeparator:","} },
					{ name : 'ERRORCNT'			, align:'right'		,width:'80px'	, formatter: 'integer', formatoptions:{thousandsSeparator:","} },
					{ name : 'TOTALLEN'			, align:'right'		,width:'80px'	, formatter: 'integer', formatoptions:{thousandsSeparator:","} },
					{ name : 'SUCCESSLEN'		, align:'right'		,width:'85px'	, formatter: 'integer', formatoptions:{thousandsSeparator:","} },
					{ name : 'ERRORLEN'			, align:'right'		,width:'85px'	, formatter: 'integer', formatoptions:{thousandsSeparator:","} }
				],
				
        jsonReader: {
             repeatitems:false
        },
        pager : $('#pager'),
		page : '${param.page}',
		rowNum : '${rmsDefaultRowNum}',
	    height: '500',
		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");
		},
        loadError: function (jqXHR, textStatus, errorThrown) {
        	
        	var location ='<%=request.getContextPath()%>/';
        	comloadError(jqXHR, textStatus, errorThrown ,location);
        	
    	}
	});
	
	$("#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_search").click(function(){ 
		detail();
	});
	
	$("input[name^=search]").keydown(function(key){
		if (key.keyCode == 13){
			$("#btn_search").click();
		}
	});
	
	
	$("#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.titleMonthly")%>');
		return false;
	});
	$("#startDatepicker").datepicker();
	$("#endDatepicker").datepicker();
	
	init(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">
					<button type="button" class="cssbtn" id="btn_excel" level="W" ><i class="material-icons">table_view</i> <%= localeMessage.getString("button.excel") %></button>
					<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_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"><%= localeMessage.getString("statisticsByCompany.titleMonthly")%></div>
				<form id="ajaxForm"  onsubmit="return false;">
					<table class="search_condition" cellspacing=0;>
						<tbody>
							<tr>
								<th style="width:120px;"><%= localeMessage.getString("statisticsByCompany.searchDate")%></th>
								<td width="600px">
									<select name="selectStartYear" style="padding: 2px;">
									</select>
									-
									<select name="searchStart" style="padding: 2px;">
										<c:forEach var="month" begin="1" end="12">
											<option value="${month}">${month }<%= localeMessage.getString("statisticsByCompany.month")%></option>
										</c:forEach> 
									</select>
									~
									<select name="selectEndYear" style="padding: 2px;">
									</select>
									-
									<select name="searchEnd" style="padding: 2px;">
										<c:forEach var="month" begin="1" end="12">
											<option value="${month}">${month }<%= localeMessage.getString("statisticsByCompany.month")%></option>
										</c:forEach>
									</select>
									
									<input type="hidden" name="searchStartYYYYMM" value="201902">
									<input type="hidden" name="searchEndYYYYMM" value="201902">
									
								</td>	
								<th style="width:120px;"><%= localeMessage.getString("statisticsByCompany.eaibzwkDstcd")%></th>
								<td>
									<div class="select-style">
										<select name="searchEaiBzwkDstcd" value="${param.searchEaiBzwkDstcd}"></select>
									</div>
								</td>						
							</tr>	
							<tr>
								<th style="width:120px;"><%= localeMessage.getString("statisticsByCompany.appCode")%></th> 
								<td>
									<input type="text" name="searchAppDstcd" value="${param.searchAppDstcd}">
								</td>		
								<th style="width:120px;"><%= localeMessage.getString("statisticsByCompany.appName")%></th> 
								<td>
									<input type="text" name="searchAppName" value="${param.searchAppName}">
								</td>		
							</tr>
							<tr>
								<th style="width:120px;"><%= localeMessage.getString("statisticsByCompany.companyCode")%></th>
								<td>
									<input type="text" name="searchCompanyCode" value="${param.searchCompanyCode}">
								</td>		
								<th><%= localeMessage.getString("statisticsByCompany.companyName")%></th>
								<td>
									<input type="hidden" name="searchCompanyName" value="" style="width:0px;">
									<input type="text" name="companyName1" value="${param.searchCompanyName}" style="width:85%;" 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 "/> --%>
									<button type="button" class="cssbtn smallBtn2" id="btn_popup" style="vertical-align:middle; font-weight:bold;"><i class="material-icons">search</i><%= localeMessage.getString("button.search") %></button>
									<button type="button" class="cssbtn smallBtn2" id="btn_init" style="vertical-align:middle; font-weight:bold;"><i class="material-icons">restart_alt</i><%= localeMessage.getString("button.reset") %></button>
								</td>
							</tr>
							<tr>
								<th style="width:120px;"><%= localeMessage.getString("statisticsByCompany.sendRecvType")%></th>
								<td>
									<div class="select-style">
										<select name="searchTypeCode" value="${param.searchTypeCode}"></select>
									</div>
								</td>						
								<th style="width:120px;"><%= localeMessage.getString("statisticsByCompany.instiName2")%></th>
								<td>
									<div class="select-style">
										<select name="searchAdptrInstiCode" value="${param.searchAdptrInstiCode}"></select>
									</div>
								</td>		
							</tr>			
						</tbody>
					</table>
				</form>
				<table id="grid" ></table>
				<div id="pager"></div>				
			</div><!-- end content_middle -->
		</div><!-- end right_box -->				
	</body>	
</html>

