<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.io.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	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_excel = '<c:url value="/common/noauth/excel/listGridToExcel.json" />';

function dateFormat(cellvalue, options, rowObject) {
	if ( cellvalue == null || cellvalue.length < 8 )
		return '';
	return  cellvalue.substr(0 ,4) + '-' +
			cellvalue.substr(4, 2) + '-' +
			cellvalue.substr(6, 2) ;
}

function getDateStr(cellvalue, options, rowObject) {
	var base_dt =  rowObject['BASE_DT'];
			
	var yyyyMMdd = String(base_dt);
	var sYear = yyyyMMdd.substring(0,4);
	var sMonth = yyyyMMdd.substring(4,6);
	var sDate = yyyyMMdd.substring(6,8);
	
	var date = new Date(Number(sYear), Number(sMonth)-1, Number(sDate));
	
	var week = ['일요일', '월요일', '화요일', '수요일', '목요일', '금요일', '토요일'];
	
	var returnStr = '';
	if(date.getDay() == 0) { // 일요일
		returnStr = '<span style="color:red">'+week[date.getDay()]+'</span>';
	} else if (date.getDay() == 6) { // 토요일
		returnStr = '<span style="color:blue">'+week[date.getDay()]+'</span>';
	} else {
		returnStr = week[date.getDay()];
	}
	
	return returnStr;
}

function getHoldayCd ( holday_cd ) {
	if ( holday_cd == '영업일' ) {
		return '1';
	} else if( holday_cd == '휴일' ) {
		return '2';
	} else if( holday_cd == '토요일' ) {
		return '3';
	}
}

function getLrclDd ( lpmn_yn ) {
	if ( lpmn_yn == '윤달' ) {
		return '1';
	} else if( lpmn_yn == '' ) {
		return '0';
	} 
}

$(document).ready(function() {
	var gridPostData = getSearchForJqgrid("cmd","LIST"); //jqgrid에서는 object 로 
	$('#grid').jqGrid({
		datatype: "json",
		mtype: 'POST',
		url: '<c:url value="/bap/admin/schedule/businessDayMan.json" />',
		postData: gridPostData,
		colNames: [ '기준일자',
					'음력일자',
					'요일',
					'일자구분',
					'윤달여부',
				  ],
		colModel: [
					{ name : 'BASE_DT'   , align:'center', 	sortable:false , formatter: dateFormat },
					{ name : 'LRCL_DT'   , align:'center', 	sortable:false , formatter: dateFormat  },
					{ name : 'WEEK'   , align:'center', 	sortable:false , formatter: getDateStr },
					{ name : 'HOLDAY_CD' , align:'center', 
						formatter: function (cellvalue) {
							if ( cellvalue == '1' ) {
								return '영업일';
							} else if( cellvalue == '2' ) {
								return '휴일';
							} else if( cellvalue == '3' ) {
								return '토요일';
							} else {
								return cellvalue;
							}
						}
					 },
					 { name : 'LPMN_YN'   , align:'center',	
						formatter: function (cellvalue) {
							if ( cellvalue == '1' ) {
								return '윤달';
							} else if( cellvalue == '0') {
								return '';
							} else {
								return cellvalue;
							}
						}	
					}
				  ],
		jsonReader: {
			repeatitems:false
		},
		pager: $('#pager'),
		page: '${param.page}',
//		rowNum: '${rmsDefaultRowNum}',
		rowNum: '50',
		autoheight: true,
		height: $("#container").height(),
		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)
			}
		},
		ondblClickRow: function(rowId) {
			var rowData = $(this).getRowData(rowId); 
			var base_dt = rowData['BASE_DT'];
			var lrcl_dt = rowData['LRCL_DT'];
			var lpmn_yn = rowData['LPMN_YN'];
			var holday_cd = rowData['HOLDAY_CD'];
			var url = '<c:url value="/bap/admin/schedule/businessDayMan.view" />';
			url += '?cmd=DETAIL';
			url += '&page=' + $(this).getGridParam("page");
			url += '&returnUrl=' + getReturnUrl();
			url += '&menuId=' + '${param.menuId}';
			//검색값
			url += '&' + getSearchUrl();
			//key값
			//url += '&base_dt=' + base_dt;
			url += '&base_dt=' + base_dt.replaceAll('-', '');
			url += '&lrcl_dt=' + lrcl_dt.replaceAll('-', '');
			url += '&lpmn_yn=' + getLrclDd(lpmn_yn);
			url += '&holday_cd=' + getHoldayCd(holday_cd);
			url += '&end=Yes';
			goNav(url);
		}
		, loadComplete:function (d) {
			// 토요일, 휴일 
			var rows = $(this).getDataIDs();
	    	for( var i= 0; i < rows.length; i++){
	    		var val = $("#grid").getCell(rows[i],'HOLDAY_CD');
	    		if(val =="토요일"){
	    			$("#"+rows[i]).find("td").css("background-color","#D9E5FF");
	    		} else if(val =="휴일"){
	    			$("#"+rows[i]).find("td").css("background-color","#FFD8D8");
	    		}
	    		
	    		// 금일 bold처리 필요시 주석제거
		    	/*
	    		var val = $("#grid").getCell(rows[i],'BASE_DT').replaceAll("-","");
		    	if(val == getToday()) {
		    		$("#"+rows[i]).find("td").css("font-weight","bold");
		    	}
		    	*/
	    	}
	    	
	    	
		}
	});
	
	resizeJqGridWidth('grid', 'title', '1000');

	$("#btn_search").click(function() {
		var postData = getSearchForJqgrid("cmd", "LIST"); //jqgrid에서는 object 로 
		postData["likeBaseDT"] = postData["likeBaseDT"].replaceAll("-","");
		$("#grid").setGridParam({ postData: postData ,page : "1" }).trigger("reloadGrid");
	});

	$("#btn_new").click(function() {
		var url = '<c:url value="/bap/admin/schedule/businessDayMan.view"/>';
		url += '?cmd=DETAIL';
		url += '&page=' + $("#grid").getGridParam("page");
		url += '&returnUrl=' + getReturnUrl();
		url += '&menuId=' + '${param.menuId}';
		//검색값
		url += '&' + getSearchUrl();

		goNav(url);
	});
	
	$("input[name^=like]").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 merge = new Array();
		gridToExcelSubmit(url_excel, "LIST_GRID_TO_EXCEL", $("#grid"), $("#ajaxForm"), "영업일 관리", merge);
		return false;
	});
	
	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" id="title">
			<div class="search_wrap">
				<img src="<c:url value="/img/btn_excel.png"/>" id="btn_excel" level="R"/>
				<img src="<c:url value="/img/btn_new.png"/>" alt="" id="btn_new" level="W" />
				<img src="<c:url value="/img/btn_search.png"/>" alt="" id="btn_search" level="R" />
			</div>
			<div class="title">영업일 관리<span class="tooltip">일괄전송 시스템에서 관리 하는 영업일 목록을 조회합니다</span></div>
			
			<table class="search_condition" cellspacing=0;>
				<tbody>
					<tr>
						<th style="width:250px;">기준일자</th><td><input type="text" name="likeBaseDT" maxlength="10" value="${param.likeBaseDT}"></td>
					</tr>
				</tbody>
			</table>
			<table id="grid" ></table>
			<div id="pager"></div>
		</div><!-- end content_middle -->
	</div><!-- end right_box -->
</body>
</html>