<%@ 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/adapter/snaAdapterMan.json" />';
var adptrbzwkname = window.dialogArguments["adptrbzwkname"];
var adptrbzwkgroupname = window.dialogArguments["adptrbzwkgroupname"];
var eaiSevrInstncName = window.dialogArguments["eaiSevrInstncName"];

var arrGridData = new Array();


//업무코드를 추가 및 삭제 할 때 validation 체크
function isGridValid(gridName)
{
	var gridCnt	= 0;
	
	gridCnt = $(gridName).jqGrid("getGridParam", "selarrrow").length;
	if(gridCnt <= 0)
	{
		alert("<%= localeMessage.getString("userPop.checkRequierd1") %>");
		return false;
	}
	
	return true;
}


//대상 그리드 Reload.
function targetGridReload(){
	var postData = getSearchForJqgrid("cmd","LIST_ADAPTER_BIZ"); //jqgrid에서는 object 로
	postData["searchPrptygroupName"] = adptrbzwkname;
	postData["targetGridYn"] = "Y";
	$("#targetGrid").setGridParam({ postData: postData ,page : "1"  }).trigger("reloadGrid");
}

// 대상 그리드 조회
function targetGrid()
{
	var gridPostData = getSearchForJqgrid("cmd","LIST_ADAPTER_BIZ"); //jqgrid에서는 object 로 
	gridPostData["searchPrptygroupName"] = adptrbzwkname;
	gridPostData["targetGridYn"] = "Y";
	
	$('#targetGrid').jqGrid({
		datatype:"json",
		mtype: 'POST',
		url: url,
		loadui: "disable",
		postData : gridPostData,
		colNames:['<%= localeMessage.getString("unifbwk.bizCode") %> ',
                  '<%= localeMessage.getString("unifbwk.bizName") %>',
                  'CHK'
                  ],
		colModel:[
				{ name : 'BIZCODE'   , align:'left'		,key:true	,sortable:false  },
				{ name : 'BIZNAME'   , align:'left'  },
				{ name : 'CHK'   , hidden:true  }
				],
        jsonReader: {
             repeatitems:false
        },	          
	    autoheight: true,
	    height: $("#container").height(),
	    height: 430,
		autowidth: true,
		viewrecords: true,
		gridview: true,						// jqGrid의 성능 향상 - treeGrid, subGrid, afterInsertRow event의 경우 제외.
		scroll: true,						// 스크롤 사용여부 설정.(default: false)
		rowNum : 10000,						// Grid에 표시될 레코드 수 설정 (-1은 조회된 데이터 전부 그리드에 셋팅).
		multiselect : true,

		loadComplete:function (d){
			var targetGridCnt	= $("#targetGrid").getGridParam("reccount");
			var colModel = $(this).getGridParam("colModel");
			
			for(var i = 0 ; i< colModel.length; i++){
				$(this).setColProp(colModel[i].name, {sortable : false});
			}
			
			arrGridData = $("#targetGrid").getRowData();
			
			// 사용자 업무코드 List Count
			$("input[name=selectCnt]").val(targetGridCnt);
			
			// 검색조건의 데이터가 있으면 해당 row 선택.
			var searchName = $("input[name=searchBzwkName]").val().split(",");
			for(var idx in searchName){
				var val = searchName[idx];
				$("#targetGrid").jqGrid("setSelection",val, true);
			}
		},
        loadError: function (jqXHR, textStatus, errorThrown) {
        	
        	var location ='<%=request.getContextPath()%>/';
        	comloadError(jqXHR, textStatus, errorThrown ,location);
        	
    	},
		gridComplete:function (d){
			$("input[name=selectCnt]").val($("#targetGrid").getGridParam("reccount"));
		},
		onSelectRow: function(rowId){
			// 선택된 row로 이동
			$("#"+$("#targetGrid").jqGrid("getGridParam", "selrow")).focus();
		}
	});
}

//저장
function modify(modifyType, grid)
{
	
	if (confirm("<%= localeMessage.getString("common.checkModify")%>") != true)
	return;
	
	var gridData = new Array();

	if(modifyType == "add")
	{
		gridData = grid;
	}
	
	if(modifyType == "delete")
	{
		var data = $(grid).getRowData();
		for ( var i = 0; i < data.length; i++) {
			if(data[i]['CHK'] =='true'){
				gridData.push(data[i]);
			}
		}
	}
	
	var codeArray = "";
	for (var i = 0; i <gridData.length; i++) {
		codeArray += (codeArray==""?"":",") + gridData[i]['BIZCODE'].trim();
	}
	
	var postData = new Array();
	postData.push({ name: "cmd"      			, value:"UPDATE_ADAPTER_BIZ"});
	postData.push({ name: "prptygroupName"   	, value:adptrbzwkname});
	postData.push({ name: "adptrbzwkgroupname"  , value:adptrbzwkgroupname});
	postData.push({ name: "eaiSevrInstncName"   , value:eaiSevrInstncName});
	postData.push({ name: "gridData" 			, value:codeArray});
	
	$.ajax({
		type : "POST",
		url:url,
		data:postData,
		success : function(json) {
			if(json.message == "success"){
				alert("<%= localeMessage.getString("common.saveMsg") %>");
				window.returnValue = "ok";
				$("#btn_close").trigger("click");
			}else{
				alert(json.message);
			}
		},
		
		error:function(e){
			alert(e.responseText);
		}
	});
	
	
}



$(document).ready(function() {
	// 업무코드 List Count
	//init();
	
	// 전체 업무코드 조회
	var gridPostData = getSearchForJqgrid("cmd","LIST_ADAPTER_BIZ"); //jqgrid에서는 object 로 
	gridPostData["searchPrptygroupName"] = adptrbzwkname;
	
	$('#grid').jqGrid({
		datatype:"json",
		mtype: 'POST',
		url: url,
		postData : gridPostData,
		colNames:['<%= localeMessage.getString("unifbwk.bizCode") %> ',
                  '<%= localeMessage.getString("unifbwk.bizName") %>',
                  'CHK'
                  ],
		colModel:[
				{ name : 'BIZCODE'   , align:'left'	,sortable:false  },
				{ name : 'BIZNAME'   , align:'left'  },
				{ name : 'CHK'   , align:'left' 	,hidden:true }
				],
        jsonReader: {
             repeatitems:false
        },	          
	    autoheight: true,
	    height: $("#container").height(),
	    height: 430,
		autowidth: true,
		viewrecords: true,
		gridview: true,						// jqGrid의 성능 향상 - treeGrid, subGrid, afterInsertRow event의 경우 제외.
		scroll: true,						// 스크롤 사용여부 설정.(default: false)
		rowNum : 10000,						// Grid에 표시될 레코드 수 설정 (-1은 조회된 데이터 전부 그리드에 셋팅).
		multiselect : true,

		loadComplete:function (d){
			var colModel = $(this).getGridParam("colModel");
			for(var i = 0 ; i< colModel.length; i++){
				$(this).setColProp(colModel[i].name, {sortable : false});
			}
		},
        loadError: function (jqXHR, textStatus, errorThrown) {
    	},
		gridComplete:function (d){
			//targetGridReload();
		},
	});
	
	// target Grid
	targetGrid();
	
	$("#chkCheck").click(function(e){
	   e = e || event;/* get IE event ( not passed ) */
	   e.stopPropagation ? e.stopPropagation() : e.cancelBubble = false;
	   $("input[id*=chkCHK]").prop("checked",$(this).is(":checked"));
	});
	
	// 조회된 그리드에서 대상 그리드로 업무코드 추가.
	$("input[name=add]").click(function(){
		
		if(!isGridValid("#grid")) return;

	 	var selRowIds = $("#grid").jqGrid("getGridParam", "selarrrow");

		for(var i=0;i<selRowIds.length;i++){
		    $("#grid").jqGrid("setCell", selRowIds[i], "CHK", "true");
		    arrGridData.push($("#grid").getRowData(selRowIds[i]));
		}
		
		modify("add", arrGridData);

	});
	
	// 대상그리드에서 선택된 업무코드 삭제
	$("input[name=remove]").click(function(){
		if(!isGridValid("#targetGrid")) return;
		
		var targetGrid			= $("#targetGrid");
		var selTargetGridIds	= targetGrid.jqGrid("getGridParam", "selarrrow");
		
		for(var i=0;i<selTargetGridIds.length;i++){
		    $("#targetGrid").jqGrid("setCell", selTargetGridIds[i], "CHK", "false");
		}
		
		var deleteData = $("#targetGrid").getRowData();
		modify("delete", "#targetGrid");

	});
	$("#btn_close").click(function(){
		window.close();
	});
	
	$("input[name^=search]").keydown(function(key){
		if (key.keyCode == 13){
			$("#btn_search").click();
		}
	});
	
	$("#btn_search").click(function(){
		var postData = getSearchForJqgrid("cmd","LIST_ADAPTER_BIZ"); //jqgrid에서는 object 로
		$("#grid").setGridParam({ postData: postData ,page : "1"  }).trigger("reloadGrid");
	});
		
	
	buttonControl();
	
});

</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">
					<img src="<c:url value="/img/btn_search.png"/>" alt="" id="btn_search" level="R" />	
					<img id="btn_close" src="<c:url value="/img/btn_close.png"/>"         level="R" status="DETAIL,NEW"/>
				</div>
				<div class="title">[프라퍼티] APP_CODE</div>
				<table class="search_condition" cellspacing=0;>
					<tbody>
						<tr>
							<th style="width:180px;"><%=localeMessage.getString("unifbwk.bizCode")%></th>
							<td>
								<input type="text" name="searchBzwkName" value="${param.searchBzwkName}">
							</td>							
						</tr>						
					</tbody>
				</table>
				
				<table width="98%" style="margin-left:10px;">
					<tr>
						<td width="50%">
							<div id="grid_div"><table id="grid" ></table></div>
						</td>
						<td>
							<input type=button name="add" value=">" style="width:40px"/><br><br>
							<input type=button name="remove" value="<" style="width:40px"/><br><br>
						</td>
						<td width="50%">
							<div id="targetGrid_div"><table id="targetGrid" ></table></div>
						</td>
					</tr>
				</table> 
				<div id="pager"></div>
			</div><!-- end content_middle -->
		</div><!-- end right_box -->	

	</body>
</html>

