<%@ 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 arg = window.dialogArguments["check"];

function unformatterFunction(cellvalue, options, rowObject){
	return "";
}

function formatterFunction(cellvalue, options, rowObject) {
	var rowId = options["rowId"];
	var url = 'src=<c:url value="/img/btn_grid_delete.png" />';
	return "<img id='btn_delete' name='img_del_"+rowId+"' "+url+ " class='btn_img'  />";
}

function gridCloneName(cellvalue, options, rowObject) {
 	var rowId = options["rowId"];
	return "<input type='text' name='text_clone_"+rowId+"' style='display:inline-block; border:0; margin:0; height:20px; width:calc(100% - 10px);'>";
}

function gridCloneFunction(cellvalue, options, rowObject) {
	var rowId = options["rowId"];
	var url = 'src=<c:url value="/img/btn_grid_clone.png" />';
	return "<img id='btn_clone' name='img_clone_"+rowId+"' "+url+ " class='btn_img'  />";
}

function getGridData() {
	var id = "grid";
	var data = new Array();
	var ids = $("#grid").jqGrid('getDataIDs');
	
	for(var i=0;i<ids.length;i++){
		if($("input:checkbox[id='jqg_"+id+"_"+ids[i]+"']").is(":checked")){
			data.push($("#grid").getRowData(ids[i]));
		}
	}
	
	return data;
}

 $(document).ready(function() {	

	var gridPostData = getSearchForJqgrid("cmd","LIST"); //jqgrid에서는 object 로 
	$('#grid').jqGrid({
		datatype:"json",
		mtype: 'POST',
		url: '<c:url value="/bap/admin/message/messageItemMan.json" />',
		postData : gridPostData,
		colNames:['메시지항목구분명',
                  '메시지컬럼명',
                  '메시지컬럼 속성유형코드',
                  '메시지컬럼값',
                  '당메시지 컬럼 사용여부',
                  '복제항목명',
                  '복제',
                  '삭제'
                  ],
		colModel:[
				{ name : 'MSGITEMDSTICNAME'  	, align:'left'		,width:40  ,sortable:false  },
				{ name : 'MSGCLMNNAME'          , align:'center'	,width:40  },
				{ name : 'MSGCLMNATTRIPTRNCD'   , align:'center'	,width:40  },
				{ name : 'MSGCLMNVAL'         	, align:'center'  	,width:40  },
				{ name : 'THISMSGCLMNUSEYN'  	, align:'center' 	,width:40  ,
				  formatter: function (cellvalue) {
				                 if ( !cellvalue ) {
				                 	return '<span style="color:red">사용안함</span>';
                                 } else if ( cellvalue ) {
				                 	return '사용함';
                                 } else {
                                  	return cellvalue;
                                 }
                              }	
                },
                { name : 'MSGITEMCLONENAME', align:'left', sortable:false, width:40, unformat:unformatterFunction, formatter:gridCloneName, hidden:true  },			
                { name : 'MSGITEMCLONE'	, align:'center', sortable:false, width:20, unformat:unformatterFunction, formatter:gridCloneFunction, hidden:true },			
                { name : 'DELETEYN'	 	, align:'center', sortable:false, width:20, unformat:unformatterFunction, formatter:formatterFunction, hidden:true }
				],
        jsonReader: {
             repeatitems:false
        },	          
		pager : $('#pager'),
		page : '${param.page}',
		rowNum : '${rmsDefaultRowNum}',
	    autoheight: true,
	    height: $("#container").height(),
		autowidth: true,
		viewrecords: true,
		/* multiselect: true,
		multiboxonly: false,	 */	
		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 result = [];
			result.push(rowData);
			
			window.returnValue = result;
 			$("#btn_close").trigger("click");
		},
	    onSelectRow: function(rowId,status) {		
			var rowData = $(this).getRowData(rowId); 				
			$("img[name='img_del_"+rowId+"']").click(function() {
				var postData = $('#ajaxForm').serializeArray();
				postData.push({ name: "cmd" , value:"DELETE"});
				postData.push({ name: "msgItemDsticName" , value:rowData['MSGITEMDSTICNAME']});
				var url2 = '<c:url value="/bap/admin/message/messageItemMan.json" />';

				$.ajax({
					type:"POST",
					url:url2,
					data:postData,
					success:function(args){
						alert("삭제 되었습니다.");
						$('#grid').trigger('reloadGrid');
					},
					error:function(e){
						alert(e.responseText);
					}
				});	
			});
 			$("img[name='img_clone_"+rowId+"']").click(function() {
 				var postData = $('#ajaxForm').serializeArray();
				var thisMsgClmnUseYn = rowData['THISMSGCLMNUSEYN'] == '사용함' ? '1' : '0';
				postData.push({ name: "cmd" , value:"INSERT"});
				postData.push({ name: "msgItemDsticName" , value:$("input[name='text_clone_"+rowId+"']").val()});
				postData.push({ name: "msgClmnName" , value:rowData['MSGCLMNNAME']});
				postData.push({ name: "msgClmnAttriPtrnCd" , value:rowData['MSGCLMNATTRIPTRNCD']});
				postData.push({ name: "msgClmnVal" , value:rowData['MSGCLMNVAL']});
				postData.push({ name: "thisMsgClmnUseYn" , value:thisMsgClmnUseYn});
				var url2 = '<c:url value="/bap/admin/message/messageItemMan.json" />';

   				$.ajax({
					type:"POST",
					url:url2,
					data:postData,
					success:function(args){
						alert("복제 되었습니다.");
						$('#grid').trigger('reloadGrid');
					},
					error:function(e){
						alert(e.responseText);
					}
				});
			});
        }		
	});
	
	
	resizeJqGridWidth('grid','content_middle','1160');

	$("#btn_search").click(function(){
	    var postData = getSearchForJqgrid("cmd","LIST"); //jqgrid에서는 object 로 
		$("#grid").setGridParam({ postData: postData ,page : "1" }).trigger("reloadGrid");
	});
	$("#btn_close").click(function(){
		window.close();
	});
	$("#btn_new").click(function(){
		
		var args = new Object();
    	args['check'] = "";
    
	    var url = '<c:url value="/bap/admin/message/messageItemMan.view" />';
	    url = url + '?cmd=DETAIL';
	    url = url + '&subPopup='+true;
	    var ret = showModal(url,args,800,500, function(arg){
	    	var args = null;
		    if(arg == null || arg == undefined ) {//chrome
		        args = this.dialogArguments;
		        args.returnValue = this.returnValue;
		    } else {//ie
		        args = arg;
		    }
	    });
	    $('#grid').trigger('reloadGrid');
	});	
	
	$("input[name^=search]").keydown(function(key){
		if (key.keyCode == 13){
			$("#btn_search").click();
		}
	});
	
	$("#btn_input").click(function(){
		
		var data = getGridData();

		if(data.length == 0){
			alert("입력할 메시지항목을 선택해 주세요");
			return false;
		}

		if ( confirm( "입력하시겠습니까?" ) != true ) return;
		
		window.returnValue = data;

		$("#btn_close").trigger("click");
	});

//	buttonControl();
	
}); 
 
</script>
</head>
	<body>
		<div class="popup_box">
			<div class="search_wrap">
				<%-- <img src="<c:url value="/img/btn_input.png"/>" alt="" id="btn_input" 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" />
				<img src="<c:url value="/img/btn_close.png"/>" alt="" id="btn_close" level="R" />	
			</div>
			<div class="title">메시지항목 관리</div>	
			<form id="ajaxForm">					
				<table class="search_condition" cellspacing="0">
					<tr>
						<th style="width:150px;">메시지항목 구분명</th>
						<td><input type="text" name="searchMsgItemDsticName" value="${param.searchMsgItemDsticName}"></td>
						<th style="width:150px;">메시지 컬럼명</th>
						<td><input type="text" name="searchMsgClmnName" value="${param.searchMsgClmnName}"></td>
					</tr>
				</table>
			</form>
			<!-- tree -->
			<table id="grid" ></table>
			<div id="pager"></div>	
			</div><!-- end content_middle -->
		</div><!-- end popup_box -->	
	</body>
</html>