<%@ 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/apim/template/messageTemplateMan.json" />';
var url_view = '<c:url value="/onl/apim/template/messageTemplateMan.view" />';

var isDetail = false;

function detail(key){
	if (!isDetail)return;

	$.ajax({
		type : "POST",
		url:url,
		dataType:"json",
		data:{cmd: 'DETAIL', id : key},
		success:function(json){
			var data = json;

			/* Checkbox checked */
			$('.enableCheckbox').each(function(){
				var name = $(this).attr('name');
				$(this).prop('checked', data[name] === 'Y');
			});

			$("#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]);
			});
			$('#emailTemplate').summernote('code', data.emailTemplate);
		},
		error:function(e){
			alert(e.responseText);
		}
	});

}

$(document).ready(function() {
	// Summernote 에디터 초기화 (이미지 붙여넣기 data-uri 방식, 리사이징 지원)
	initSummernote('#emailTemplate', {
		placeholder: '여기에 내용을 입력하세요.',
		height: 300,
		toolbar: [
			['style', ['style']],
			['font', ['bold', 'underline', 'clear']],
			['color', ['color']],
			['para', ['ul', 'ol', 'paragraph']],
			['table', ['table']],
			['insert', ['link', 'picture', 'video']],
			['view', ['codeview', 'help']]
		]
	});

	var key ="${param.id}";
	isDetail = key != "" && key != "null";

	if (isDetail){
		isDetail = true;
		$("#title").append(" 수정");
		buttonControl(true);
	} else {
		$("#title").append(" 등록");
		$("#btn_modify").html('<i class="material-icons">save</i> <%= localeMessage.getString("button.register") %></button>');
		buttonControl(false);
	}

	detail(key)


	$("#btn_modify").click(function(){
		if (!checkRequired("ajaxForm")){
			return;
		}

		if (confirm("<%= localeMessage.getString("common.checkSave")%>") != true)
			return;

		var postData = $('#ajaxForm').serializeArray();

		postData.forEach(function(item) {
	        if ($('.enableCheckbox[name="' + item.name + '"]').length > 0) {
	        	item.value = ($('.enableCheckbox[name="' + item.name + '"]').is(':checked')) ? 'Y' : 'N';
	        }
		});

		['enableSms', 'enableEmail', 'enableMessenger'].forEach(function(name) {
	        if (!postData.some(function(item) { return item.name === name; })) {
	            postData.push({ name: name, value: 'N' });
	        }
	    });

		if (isDetail){
			postData.push({ name: "cmd" , value:"UPDATE"});
		}else{
			postData.push({ name: "cmd" , value:"INSERT"});
		}


		$.ajax({
			type : "POST",
			url:url,
			data:postData,
			success:function(json){
				alert("<%= localeMessage.getString("common.saveMsg") %>");
				$("#btn_close").trigger("click");
			},
			error:function(e){
				alert(e.responseText);
			}
		});
	});

	$("#btn_close").click(function(){
		window.close();
	});

	 $("#btn_delete").click(function(){
	      if(confirm("<%= localeMessage.getString("common.confirmMsg")%> \n템플릿을 삭제하면 해당 메세지 전송이 불가합니다.")){

	    	  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") %>");
	               $("#btn_close").trigger("click");
	            },
	            error:function(e){
	               alert(e.responseText);
	            }
	         });
	      } else{
	         return false;
	      }

	   });

});


</script>
</head>
	<body>
		<div class="popup_box">
			<div class="search_wrap">
				<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_delete" level="W" status="DETAIL"><i class="material-icons">delete</i> <%= localeMessage.getString("button.delete") %></button>
				<button type="button" class="cssbtn" id="btn_close" level="R"><i class="material-icons">cancel</i> <%= localeMessage.getString("button.close") %></button>
			</div>
			<div class="title" id ="title"><%= localeMessage.getString("messageTemplate.titlePopup") %></div>

			<form id="ajaxForm">
				<input type="hidden" name="id">
				<table class="table_row" cellspacing="0">
					<colgroup>
						<col width="200px">
					</colgroup>
					<tr>
						<th><%= localeMessage.getString("messageTemplate.messageCode") %> <font color="red"> *</font></th>
						<td><input type="text"  name="messageCode"   data-required data-warning="메세지 코드는 필수입니다"/></td>
					</tr>
					<tr>
						<th><%= localeMessage.getString("messageTemplate.messageName") %> <font color="red"> *</font></th>
						<td><input type="text"  name="messageName"  data-required data-warning="메세지 제목은 필수입니다"/></td>
					</tr>
					<tr>
						<th><%= localeMessage.getString("messageTemplate.subjectTemplate") %></th>
						<td><input type="text"  name="subjectTemplate" id ="subjectTemplate" /></td>
					</tr>
					<tr>
						<th><%= localeMessage.getString("messageTemplate.enableSms") %> </th>
						<td>
							<input type="checkbox" class="form-check-input enableCheckbox" name="enableSms"/>
						</td>
					</tr>
					<tr>
						<th><%= localeMessage.getString("messageTemplate.smsTemplate") %></th>
						<td><textarea  id="smsTemplate" name="smsTemplate" style="width:100%;height:100px" ></textarea></td>
					</tr>
					<tr>
						<th><%= localeMessage.getString("messageTemplate.enableEmail") %> </th>
						<td>
							<input type="checkbox" class="form-check-input enableCheckbox" name="enableEmail"/>
						</td>
					</tr>
					<tr>
						<th><%= localeMessage.getString("messageTemplate.emailTemplate") %></th>
						<td><textarea  id="emailTemplate" name="emailTemplate" style="width:100%;height:100px" ></textarea></td>
					</tr>
					<tr>
						<th><%= localeMessage.getString("messageTemplate.enableMessenger") %> </th>
						<td>
							<input type="checkbox" class="form-check-input enableCheckbox" name="enableMessenger"/>
						</td>
					</tr>
					<tr>
						<th><%= localeMessage.getString("messageTemplate.messengerTemplate") %></th>
						<td><textarea  id="messengerTemplate" name="messengerTemplate" style="width:100%;height:100px" ></textarea></td>
					</tr>
				</table>
			</form>

		</div><!-- end popup_box -->
	</body>
</html>

