<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.io.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ 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/admin/useDate/useDateControlMan.json" />';
var url_view ='<c:url value="/onl/admin/useDate/useDateControlMan.view" />';
var isDetail = false;

function dateFormater(cellvalue){
	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(url,key,callback){
	$.ajax({  
		type : "POST",
		url:url,
		dataType:"json",
		data:{cmd: 'LIST_INIT_COMBO'},
		success:function(json){
			new makeOptions("CODE","NAME").setObj($("select[name=useYn]")).setData(json.useYnRows).setFormat(codeName3OptionFormat).rendering();
			//$("input[name=startDate]").inputmask("yyyy-mm-dd",{'autoUnmask':true});
			//$("input[name=endDate]").inputmask("yyyy-mm-dd",{'autoUnmask':true});
			
			if(typeof callback === 'function') {
				callback(url,key);
			}
			
		},
		error:function(e){
			alert(e.responseText);
		}
	});
}
function detail(url,key){
	if (!isDetail)return;
	$.ajax({  
		type : "POST",
		url:url,
		dataType:"json",
		data:{cmd: 'DETAIL', name : key},
		success:function(json){
			var data = json;
			$("input[name=name]").attr('readonly',true);
			$("input[name=desc]").attr('readonly',true);
			
			$("#ajaxForm input[type!=radio],#ajaxForm select,#ajaxForm textarea").each(function(){
				var name = $(this).attr("name");
				var tag  = $(this).prop("tagName").toLowerCase();
				$(tag+"[name="+name+"]").val(data[name.toUpperCase()]);
			});
			$("input[name=startYYYYMMDD]").val(dateFormater($("input[name=startDate]").val()));
			$("input[name=endYYYYMMDD]").val(dateFormater($("input[name=endDate]").val()));
			

		},
		error:function(e){
			alert(e.responseText);
		}
	});
}

$.datepicker.setDefaults({
	  dateFormat : 'yyyy-mm-dd', 
	  prevText:'이전 달',
	  nextText:'다음 달',
	  monthNames : ['1월','2월','3월','4월','5월','6월','7월','8월','9월','10월','11월','12월'],
	  monthNamesShort : ['1월','2월','3월','4월','5월','6월','7월','8월','9월','10월','11월','12월'],
	  dayNames : ['일','월','화','수','목','금','토'],
	  dayNamesMin : ['일','월','화','수','목','금','토'],
	  dayNamesShort : ['일','월','화','수','목','금','토'],
	  showMonthAfterYear:true,
	  yearSuffix:"년"	
});


$(document).ready(function() {	
	var returnUrl = getReturnUrlForReturn();
	var key ="${param.name}";
	if (key != "" && key !="null"){
		isDetail = true;
	}	
	init(url,key,detail);
	$("#startDatepicker").datepicker();
	$("#endDatepicker").datepicker();
	
	
	$("#btn_modify").click(function(){
		//날짜 셋팅
		var start = $("input[name=startYYYYMMDD]").val().replace(/-/gi,"");
		var end = $("input[name=endYYYYMMDD]").val().replace(/-/gi,"");
		
		if(start > end){
			alert("조회기간을 확인해주세요.");
			return false;
		}
		$("input[name=startDate]").val(start);
		$("input[name=endDate]").val(end);
			
	
		var postData = $('#ajaxForm').serializeArray();
		if (isDetail){
			postData.push({ name: "cmd" , value:"UPDATE"});
		}else{
			postData.push({ name: "cmd" , value:"INSERT"});
		}
		
		$.ajax({
			type : "POST",
			url:url,
			data:postData,
			success:function(args){
				alert("<%= localeMessage.getString("common.saveMsg") %>");
				goNav(returnUrl);//LIST로 이동
			},
			error:function(e){
				alert(e.responseText);
			}
		});
	});
	$("#btn_delete").click(function(){
		var postData = $('#ajaxForm').serializeArray();
		postData.push({ name: "cmd" , value:"DELETE"});
		$.ajax({
			type : "POST",
			url:url,
			data:postData,
			success:function(args){
				alert("<%= localeMessage.getString("common.deleteMsg") %>");
				goNav(returnUrl);//LIST로 이동

			},
			error:function(e){
				alert(e.responseText);
			}
		});
	});	
	$("#btn_previous").click(function(){
		goNav(returnUrl);//LIST로 이동
	});

	buttonControl(isDetail);
	titleControl(isDetail);
});
 
</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_delete" level="W" status="DETAIL"><i class="material-icons">delete</i> <%= localeMessage.getString("button.delete") %></button>
					<button type="button" class="cssbtn" id="btn_modify" level="W" status="DETAIL,NEW"><i class="material-icons">save</i> <%= localeMessage.getString("button.modify") %></button>
					<button type="button" class="cssbtn" id="btn_previous" level="R" status="DETAIL,NEW"><i class="material-icons">arrow_back</i> <%= localeMessage.getString("button.previous") %></button>
<%-- 					<img src="<c:url value="/img/btn_delete.png"/>" alt="" id="btn_delete" level="W" status="DETAIL"/> --%>
<%-- 					<img src="<c:url value="/img/btn_modify.png"/>" alt="" id="btn_modify" level="W" status="DETAIL,NEW" />				 --%>
<%-- 					<img src="<c:url value="/img/btn_previous.png"/>" alt="" id="btn_previous" level="R" status="DETAIL,NEW"/> --%>
				</div>
				<div class="title"><%= localeMessage.getString("useDateConMan.title") %><span class="tooltip" ><%= localeMessage.getString("useDateConMan.title") %></span></div>				
				
				<table id="grid" ></table>
				<div id="pager"></div>
				
				<!-- detail -->
				<form id="ajaxForm">
				<table class="table_row" cellspacing="0">
					<tr>
						<th style="width:20%;"><%= localeMessage.getString("infIfConMan.ifNm") %></th>
						<td><input type="text" name="name" /></td>
					</tr>
					<tr>
						<th style="width:20%;"><%= localeMessage.getString("infIfConMan.ifDesc") %></th>
						<td><input type="text" name="desc" /></td>
					</tr>
					<tr>
						<th style="width:20%;"><%= localeMessage.getString("useDateConMan.startDate") %></th>
						<td>
						<input type="text" name="startYYYYMMDD" id="startDatepicker" readonly="readonly" value="" size="10" style="width:80px; border:1px solid #ebebec;">
						<input type="hidden"  name="startDate" style="width:100px;" />
						</td>
					</tr>
					<tr>
						<th style="width:20%;"><%= localeMessage.getString("useDateConMan.endDate") %></th>
						<td>
						<input type="text" name="endYYYYMMDD" value="" id="endDatepicker" size="10" readonly="readonly" style="width:80px; border:1px solid #ebebec;">
						<input type="hidden"  name="endDate" style="width:100px;" />
						</td>
					</tr>
					<tr>
						<th style="width:20%;"><%= localeMessage.getString("infAdpConMan.useYn") %></th>
						<td>
						<div class="select-style">
									<select name="useYn" />
								</div>
						</td>
					</tr>
				</table>
				</form>
				
			</div><!-- end content_middle -->
		</div><!-- end right_box -->	
	</body>
</html>

