<%@ 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="/bap/admin/cron/cronMan.json" />';
var url_view = '<c:url value="/bap/admin/cron/cronMan.view" />';

	var isDetail = false;
	function isValid() {

		var schedId = $('input[name=schedId]').val(); 
		if (schedId == "") {
			alert("<%= localeMessage.getString("cronDetail.checkRequired1") %>");
			$('input[name=schedId]').focus();
			return false;
		}
		
		return true;
	}

	function init(key, callback) {
		if (typeof callback === 'function') {
			callback(key);
		}
	
	}
	function detail(key) {

		if (!isDetail)
			return;
		$.ajax({
			type : "POST",
			url : url,
			dataType : "json",
			data : {
				cmd : 'DETAIL',
				schedId : key
			},
			success : function(json) {
				var data = json;
								
				$("input").each(function(){
					var name = $(this).attr('name');
					if (name != null)
						$(this).val(data[name.toUpperCase()]);
				});
				
				$("input[name=schedId]").attr('readonly', true);
				if($("input[name=cronExp]").val() != ""){
					var val = $("input[name=cronExp]").val();
					var cron = val.split(" ");
					
					$("input[name=cronSec]").val(cron[0]);
					$("input[name=cronMin]").val(cron[1]);
					$("input[name=cronHour]").val(cron[2]);
					$("input[name=cronDay]").val(cron[3]);
					$("input[name=cronMon]").val(cron[4]);
					
				}
			},
			error : function(e) {
				alert(e.responseText);
			}
		});

	}
	function makeCronExp(){
		var sec = $("input[name=cronSec]").val();
		var min = $("input[name=cronMin]").val();
		var hour = $("input[name=cronHour]").val();
		var day = $("input[name=cronDay]").val();
		
		var cronExp = sec + " " + min+ " " +hour+ " " +day+ " * ?";
		$("input[name=cronExp]").val(cronExp);
	}
	$(document).ready(function() {
		var returnUrl = getReturnUrlForReturn();
		var key = "${param.schedId}";

		if (key != "" && key != "null") {
			isDetail = true;
		}

		init(key, detail);

		$("#btn_modify").click(function() {
			if (!isValid())return;
			if (isDetail) {
				if ( confirm( "수정하시겠습니까?" ) != true ) return;
			} else {
				if ( confirm( "등록하시겠습니까?" ) != true ) return;
			}
		
			// 공통부만 form으로 구성
			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") %>");
					// LIST로 이동
					goNav(returnUrl);
				},
				error : function(e) {
					alert(e.responseText);
				}
			});
		});
		$("#btn_delete").click(function() {
			if ( confirm( "삭제하시겠습니까?" ) != true ) return;
			
			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") %>");
					// LIST로 이동
					goNav(returnUrl);

				},
				error : function(e) {
					alert(e.responseText);
				}
			});
		});
		$("#btn_previous").click(function() {
			// LIST로 이동
			goNav(returnUrl);
		});
		
		$("input[name^=cron]").keyup(function(){
			var name = $(this).attr('name');
			if(name == "cronExp")	return;
			makeCronExp();
		
		});


		buttonControl(key);
		titleControl(key);
	});
</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_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("cron.title") %></div>						
				
				<form id="ajaxForm">
					<table class="table_row" cellspacing="0">
						<tr>
							<th style="width:20%;"><%= localeMessage.getString("cron.schedId") %></th>
							<td><input type="text" name="schedId"/></td>
						</tr>
						<tr>
							<th rowspan="2"><%= localeMessage.getString("cron.cronExp") %></th>
							<td style="border-bottom:1px solid #ebebec;"><input type="text" name="cronExp" readonly/> </td>
						</tr>
						<tr>
							<td>	
								<input type="text" name="cronSec" style="width:25px" /> <%= localeMessage.getString("common.sec") %> 
								<input type="text" name="cronMin" style="width:25px" /> <%= localeMessage.getString("common.min") %> 
								<input type="text" name="cronHour" style="width:25px"/> <%= localeMessage.getString("common.time") %>  
								<input type="text" name="cronDay" style="width:25px" /> <%= localeMessage.getString("common.day") %> 
								<input type="text" name="cronMon" style="width:25px" /> <%= localeMessage.getString("common.month") %> 
								<span style="display:inline-block; margin-left:20px; color:red;">
									 [ * : 매(초/분/시), / : 증분  {예시 : (0 */1 * * * ?) 매시 0초부터 1분 간격으로 실행} ]
								</span>
							</td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("cron.jobClassName") %></th>
							<td><input type="text" name="jobClassName"/> </td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("cron.schedDstCd") %></th>
							<td><input type="text" name="schedDstCd"/> </td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("cron.scanDirName") %></th>
							<td><input type="text" name="scanDirName"/> </td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("cron.timerMsg") %></th>
							<td><input type="text" name="timerMsg"/> </td>
						</tr>
					</table>
				</form>
			</div><!-- end content_middle -->
		</div><!-- end right_box -->	
	</body>
</html>
