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

var isDetail=false;
var lastsel2;
var domainName = new Array();			//도메인 콤보박스
function isValid(){
	if($('input[name=adptrCd]').val() == ""){
		alert("<%=localeMessage.getString("adapterTypeDetail.checkRequierd1")%>");
		return false;
	}else if($('input[name=adptrName]').val() == ""){
		alert("<%=localeMessage.getString("adapterTypeDetail.checkRequierd2")%>");
		return false;
	}
	
	return true;
}
function isValidGrid(){
	if($('select[name=addAdptrIoDstCd]').val() == ""){
		alert("<%=localeMessage.getString("adapterTypeDetail.checkRequierd3")%>");
		return false;
	}else if($('input[name=addAdptrPrptyName]').val() == ""){
		alert("<%=localeMessage.getString("adapterTypeDetail.checkRequierd4")%>");
		return false;
	}else if($('input[name=addAdptrPrptyDesc]').val() == ""){
		alert("<%=localeMessage.getString("adapterTypeDetail.checkRequierd5")%>");
		return false;
	}
	//중복체크
	var data = $("#grid").getRowData();
	for ( var i = 0; i < data.length; i++) {
		if (data[i]['ADPTRIODSTCD'] == $('select[name=addAdptrIoDstCd]').val()
		    && data[i]['ADPTRPRPTYNAME'] == $('input[name=addAdptrPrptyName]').val()
		    ){
			alert("<%=localeMessage.getString("adapterTypeDetail.checkRequierd6")%>");
		    return false;
		}
	}
	
	return true;
}

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

function formatterFunction(cellvalue, options, rowObject){
	var rowId = options["rowId"];
	var adptrIoDstCd    = options["colModel"]["ADPTRIODSTCD"];
	var adptrPrptyName  = options["colModel"]["ADPTRPRPTYNAME"];
	return "<img id='btn_pop_delete' name='img_"+rowId+"' src=<c:url value='/images/bt/pop_delete.gif' /> />";
}

function gridRendering(){
	$('#grid').jqGrid({
		datatype:"local",
		loadonce: true,
		rowNum: 10000,
		editurl : "clientArray",
		//cellEdit: true,
		//cellsubmit : 'clientArray',
		colNames:['<%=localeMessage.getString("adapter.adptrIoDstCd")%> ',
                  '<%=localeMessage.getString("adapterTypeDetail.addAdptrPrptyName")%>',
                  '<%=localeMessage.getString("adapterTypeDetail.addAdptrPrptyDesc")%>',
                  '<%=localeMessage.getString("adapterTypeDetail.domainName")%>',
                  '<%=localeMessage.getString("adapterTypeDetail.deleteYn")%>'
                  ],
		colModel:[
				{ name : 'ADPTRIODSTCD'   , align:'center' ,width:'40' },
				{ name : 'ADPTRPRPTYNAME' , align:'left' ,width:'200' , editable : true },
				{ name : 'ADPTRPRPTYDESC' , align:'left' , editable : true },
				{ name : 'DMNKND' 		  , align:'left' ,width:'60', editable : true, edittype:'select', formatter:'select', editoptions: { value: domainName['value'] }},
				{ name : 'DELETEYN'       , align:'center' ,width:'50' ,unformat: unformatterFunction, formatter: formatterFunction}
				],
        jsonReader: {
             repeatitems:false
        },	   
        loadComplete : function () {
			
        },
        gridComplete : function(){
        	$("img[name*=img]").off('click').on('click', function(){
				var name = $(this).attr("name");
				var rowId =name.split("_")[1];
				$('#grid').jqGrid('delRowData',rowId);
			});
       		 //$(this).setColProp('DMNKND',{editoptions:domainName});
        },
        onSortCol : function(){
        	return 'stop';	//정렬 방지
        }, 
	    onSelectRow: function(rowid,status){
			if (lastsel2 !=undefined){
	            if($("#grid tr#" + lastsel2).attr("editable") == 1){ //editable=1 means row in edit mode
	        	  	$("#grid").saveRow(lastsel2,false,"clientArray");
				}
	    	}
	        $('#grid').restoreRow(lastsel2);       
	        $('#grid').editRow(rowid,true);	    	
		    lastsel2=rowid;	

	    },  
	    height: '500',
		autowidth: true,
		viewrecords: true
	});
	
	
	resizeJqGridWidth('grid','content_middle','1000');	
}
function init(key,callback){
	$.ajax({  
		type : "POST",
		url:url,
		dataType:"json",
		data:{cmd: 'LIST_INIT_COMBO'},
		success:function(json){
			new makeOptions("CODE","NAME").setObj($("select[name=addAdptrIoDstCd]")).setData(json.adptrIoDstcdRows).setFormat(codeName3OptionFormat).rendering();
			
			domainName['value']     = ": ;"+getGridSelectText("CODE","CODE",json.domainName);
			$("#grid").setColProp('DMNKND',{editoptions:domainName,editable:true,edittype:'select',formatter:'select'});
			if(typeof callback === 'function') {
				callback(key);
			}
		},
		error:function(e){
			alert(e.responseText);
		}
	});


	
}
function detail(key){
	if (!isDetail)return;
	$.ajax({  
		type : "POST",
		url:url,
		dataType:"json",
		data:{cmd: 'DETAIL'
		    , adptrCd : key
		    },
		success:function(json){
			var data = json;
			var detail = json.detail;
			//adapterType
			$("input[name=adptrCd]").attr('readonly',true).val(data['ADPTRCD']);
			$("input[name=adptrName]").val(data['ADPTRNAME']);
			//adapterTypeProp 
			
			var gridData = json['gridData'] || [];
			$("#grid").jqGrid('setGridParam', { data: gridData }).trigger('reloadGrid');
		},
		error: function(xhr, status, error) {
		    console.error('AJAX Error:', error);
		    console.error('Status:', status);
		    console.error('Response:', xhr.responseText);
		}
	});

}
$(document).ready(function() {	
	var returnUrl = getReturnUrlForReturn();
	var key ="${param.adptrCd}";
	
	if (key != "" && key !="null"){
		isDetail = true;
	}	
	
	gridRendering();

	init(key,detail);
	

	$("#btn_modify").click(function(){
		if (lastsel2 != undefined){
			if($("#grid tr#" + lastsel2).attr("editable") == 1){ //editable=1 means row in edit mode
				$("#grid").saveRow(lastsel2,false,"clientArray");
			}		
		}
		if (!isValid()) return;	
		var data      = $("#grid").getRowData();
		var gridData = new Array();
		for (var i = 0; i <data.length; i++) {
			gridData.push(data[i]);
		}
		if (lastsel2) {
			$('#grid').jqGrid('saveRow', lastsel2, false, 'clientArray');
		}
		var gridData = $("#grid").getRowData();
		//공통부만 form으로 구성
		var postData = $('#ajaxForm').serializeArray();
		
		postData.push({ name: "gridData" , value:JSON.stringify(gridData)});
		
		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(){
		if(confirm("<%= localeMessage.getString("common.confirmMsg")%>")){
			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);
				}
			});
		}else{
			return false;
		}
	});	
	$("#btn_previous").click(function(){
		goNav(returnUrl);//LIST로 이동
	});
	$("#btn_pop_input").click(function(){
		if (!isValidGrid()){
			return;
		}
	
		var data = new Object();
		data["ADPTRIODSTCD"]=$("select[name=addAdptrIoDstCd]").val(); 
		data["ADPTRPRPTYNAME"]=$("input[name=addAdptrPrptyName]").val(); 
		data["ADPTRPRPTYDESC"]=$("input[name=addAdptrPrptyDesc]").val(); 
	
		var rows = $("#grid")[0].rows;
		var index = Number($(rows[rows.length-1]).attr("id"));
		if (isNaN(index)) index=0;
	    var rowid = index + 1;
		$("#grid").jqGrid('addRow', {
           rowID : rowid,          
           initdata : data,
           position :"last",    //first, last
           useDefValues : false,
           useFormatter : false,
           addRowParams : {extraparam:{}}
		});
		$("#"+$('#grid').jqGrid('getGridParam','selrow')).focus();
		$("img[name=img_"+rowid+"]").click(function(){
			var name = $(this).attr("name");
			var rowId =name.split("_")[1];
			$('#grid').jqGrid('delRowData',rowId);
		});	
		
	});



	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" id="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("adapterTypeDetail.title")%></div>						
				
				<form id="ajaxForm">
					<table class="table_row" cellspacing="0">
						<!-- 공통-->
						<tr>
							<th style="width:180px;"><%=localeMessage.getString("adapterType.adptrCd")%></th>
							<td ><input type="text" name="adptrCd" /> </td>
						</tr>
						<tr>
							<th><%=localeMessage.getString("adapterType.adptrName")%></th><td ><input type="text" name="adptrName" /> </td>
						</tr>
					</table>
				</form>
				
				<table class="table_row" cellspacing="0">
					<tr>
						<th style="width:180px;"><%=localeMessage.getString("adapter.adptrIoDstCd")%></th>
						<td>
							<div class="select-style">
							<select name="addAdptrIoDstCd" />
							</div>
						</td>
						<th style="width:180px;"><%=localeMessage.getString("adapterTypeDetail.addAdptrPrptyName")%></th>
						<td><input type="text"  name="addAdptrPrptyName"/></td>
						<td style="width:60px" rowspan="2" > <!-- <img id="btn_pop_input" src="<c:url value="/img/btn_pop_input.png"/>" class="btn_img" /> -->
						<button type="button" class="cssbtn smallBtn2" id="btn_pop_input" style="vertical-align:middle; font-weight:bold;"><i class="material-icons">input</i><%= localeMessage.getString("button.input") %></button>
						</td>
					</tr>
					<tr>
						<th><%=localeMessage.getString("adapterTypeDetail.addAdptrPrptyDesc")%></th>
						<td colspan="3"><input type="text"  name="addAdptrPrptyDesc" /></td>
					</tr>
				</table>
				<!-- grid -->
				<table id="grid" ></table>
			</div><!-- end content_middle -->
		</div><!-- end right_box -->	
	</body>
</html>

