<%@ 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/statistics/statisticinfo/osidInstiStatisticMan.json" />';
var url_view  ='<c:url value="/onl/statistics/statisticinfo/osidInstiStatisticMan.view" />';
var url_excel ='<c:url value="/onl/statistics/statisticinfo/osidInstiStatisticMan.excel" />';

function isValid(){
	
	if($("input[name=instiName]").val() ==""){
		alert("대외기관명을 입력하세요");
		$("input[name=instiName]").focus();
		return false;
	}

	return true;
}

function isValid2(){
	
	if($("input[name=apCode]").val() ==""){
		alert("업무구분코드명을 입력하세요");
		$("input[name=apCode]").focus();
		return false;
	}

	return true;
}

function dateFormater(cellvalue, options, rowObject){
	if (cellvalue==null|| cellvalue==undefined) return "";
	if (cellvalue.trim().length < 8) return "";
	var display = "";
	display=display+cellvalue.substr(0,4)+"-";
	display=display+cellvalue.substr(4,2)+"-";
	display=display+cellvalue.substr(6,2)+"";
	return display;
}	

// 업무코드를 추가 및 삭제 할 때 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_BLACK"); //jqgrid에서는 object 로
	postData["flag"] = "apcode";
	$("#targetGrid").setGridParam({ postData: postData ,page : "1"  }).trigger("reloadGrid");
}

// 블랙리스트업무코드 그리드 조회
function targetGrid()
{
	var gridPostData = getSearchForJqgrid("cmd","LIST_BLACK"); //jqgrid에서는 object 로
	gridPostData["flag"] = "apcode";
	
	$('#targetGrid').jqGrid({
		datatype:"json",
		mtype: 'POST',
		url: url,
		postData : gridPostData,
		colNames:[
		          '업무구분코드명',
		          '업무구분코드 한글명',
	              '신규등록일'
	  	],
		colModel:[
				{ name : 'APCODE'  		, align:'left' ,width:'50px'  , key :true },
				{ name : 'APCODENAME' 	, align:'left'   },
				{ name : 'BASEYMD' 		, align:'center' ,width:'50px'  ,formatter: dateFormater }
		],
        jsonReader: {
             repeatitems:false
        },	          
        multiselect:true,
        multiboxonly: false,
        rowNum: 10000,
	    height: '470',
		autowidth: true,
		viewrecords: 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});
			}
			
		},
        loadError: function (jqXHR, textStatus, errorThrown) {
        	var location ='<%=request.getContextPath()%>/';
        	comloadError(jqXHR, textStatus, errorThrown ,location);
    	},
		gridComplete:function (d){
			var colModel = $(this).getGridParam("colModel");
			for(var i = 0 ; i< colModel.length; i++){
				$(this).setColProp(colModel[i].name, {sortable : false});	//그리드 헤더 화살표 삭제(정렬X)		
			}
			$("#apcodeCnt").text(" [ "+$("#targetGrid").getGridParam("reccount") + " 건] ");
		},
		onSelectRow: function(rowId){
		}
	});
}

//대상 그리드 Reload.
function gridReload(){
	
	$("input[name=instiName]").val("");
	$("input[name=apCode]").val("");
	
	var postData = getSearchForJqgrid("cmd","LIST_BLACK"); //jqgrid에서는 object 로
	postData["flag"] = "company";
	$("#grid").setGridParam({ postData: postData ,page : "1"  }).trigger("reloadGrid");
}

$(document).ready(function() {
	
	// 대외기관 블랙리스트 조회
	var gridPostData = getSearchForJqgrid("cmd","LIST_BLACK"); //jqgrid에서는 object 로 
	gridPostData["flag"] = "company";
	
	$('#grid').jqGrid({
		datatype:"json",
		mtype: 'POST',
		url: url,
		postData : gridPostData,
		colNames:['대외기관명',
	              '신규등록일'
	 	 ],
		colModel:[
				{ name : 'INSTINAME'  	, align:'left'   , key :true },
				{ name : 'BASEYMD' 		, align:'center' ,formatter: dateFormater ,width:'50px' }
		],
        jsonReader: {
             repeatitems:false
        },	          
        multiselect:true,
        multiboxonly: false,
        rowNum: 10000,
	    height: '470',
		autowidth: true,
		viewrecords: 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) {
        	
        	var location ='<%=request.getContextPath()%>/';
        	comloadError(jqXHR, textStatus, errorThrown ,location);
        	
    	},
		gridComplete:function (d){
			
			var colModel = $(this).getGridParam("colModel");
			for(var i = 0 ; i< colModel.length; i++){
				$(this).setColProp(colModel[i].name, {sortable : false});	//그리드 헤더 화살표 삭제(정렬X)		
			}
			$("#instCnt").text(" [ "+$("#grid").getGridParam("reccount") + " 건] ");
			
			targetGridReload();
		},
	});
	
	// target Grid
	targetGrid();
	
	$("#btn_close").click(function(){
		window.close();
	});
	
	//블랙리스트 대외기관 등록
	$("#btn_newinst").click(function(){
		
		if (!isValid()){
			return;
		}
		
		if(confirm("대외기관을 등록하시겠습니까?") == false) 
			return ;
		
		var postData = $('#ajaxForm').serializeArray();
		postData.push({ name: "cmd"   	    , value:"TRANSACTION_BLACK_COMPANY"});
		
		$.ajax({
			type : "POST",
			url:url,
			data:postData,
			success:function(args){
				alert("<%= localeMessage.getString("common.saveMsg") %>");
				gridReload();
				
			},
			error:function(e){
				alert(e.responseText);
			}
		});
	});
	
	//블랙리스트 업무코드 등록
	$("#btn_newApcode").click(function(){
		
		if (!isValid2()){
			return;
		}
		
		if(confirm("업무코드를 등록하시겠습니까?") == false) 
			return ;
		
		var postData = $('#ajaxForm2').serializeArray();
		postData.push({ name: "cmd"   	    , value:"TRANSACTION_BLACK_APCODE"});
		
		$.ajax({
			type : "POST",
			url:url,
			data:postData,
			success:function(json){
				if(json.result == "success"){
					alert("<%= localeMessage.getString("common.saveMsg") %>");
					gridReload();
					
				}else{
					alert(json.result);
				}
				
			},
			error:function(e){
				alert(e.responseText);
			}
		});
	});
	
	//블랙리스트 대외기관 삭제
	$("#btn_delinst").click(function(){
		var params = new Array();
		var ids = $("#grid").jqGrid('getDataIDs');
		var chkcnt = 0;
		
		for(var i=0;i<ids.length;i++){
			if($("input:checkbox[id='jqg_grid_"+ids[i]+"']").is(":checked")){
				var rowObject = $("#grid").getRowData(ids[i]);
				params.push(rowObject);
				chkcnt++;
			}
		}
		
		if(chkcnt == 0){
			alert('삭제할 대외기관을 선택해 주세요.');
			return false;
		}
		
		if (confirm("대외기관을 삭제하시겠습니까?")==false){
			return;
		}
		
		var postData = [];
		postData.push({ name: "cmd" 		 , value:"DELETE_COMPANY"});
		postData.push({ name: "eaiSevrDstcd" , value:"X"});
		postData.push({ name: "gridData" 	 , value:JSON.stringify(params)});
		$.ajax({
			type : "POST",
			url : url,
			dataType : "json",
			data : postData,
			success : function(json) {
				if(json.result == "success"){
					alert("삭제 완료하였습니다");
					gridReload();
				}else{
					alert("삭제중 에러가 발생했습니다.");
				}
				
			},
			error : function(e) {
				alert("error"+e.responseText);
			}
		});
	
	});
	
	//블랙리스트 업무코드 삭제
	$("#btn_delApcode").click(function(){
		
		var params = new Array();
		var ids = $("#targetGrid").jqGrid('getDataIDs');
		var chkcnt = 0;
		
		for(var i=0;i<ids.length;i++){
			if($("input:checkbox[id='jqg_targetGrid_"+ids[i]+"']").is(":checked")){
				var rowObject = $("#targetGrid").getRowData(ids[i]);
				params.push(rowObject);
				chkcnt++;
			}
		}
		
		if(chkcnt == 0){
			alert('삭제할 업무구분코드를 선택해 주세요.');
			return false;
		}
		
		if (confirm("업무구분코드를 삭제하시겠습니까?")==false){
			return;
		}
		
		var postData = [];
		postData.push({ name: "cmd" 		 , value:"DELETE_APCODE"});
		postData.push({ name: "eaiSevrDstcd" , value:"X"});
		postData.push({ name: "gridData" 	 , value:JSON.stringify(params)});
		$.ajax({
			type : "POST",
			url : url,
			dataType : "json",
			data : postData,
			success : function(json) {
				if(json.result == "success"){
					alert("삭제 완료하였습니다");
					gridReload();
					
				}else{
					alert("삭제중 에러가 발생했습니다.");
				}
				
			},
			error : function(e) {
				alert("error"+e.responseText);
			}
		});
	
	});
	
	
	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 id="btn_close" src="<c:url value="/img/btn_close.png"/>"         level="R" status="DETAIL,NEW"/>
				</div>
				<div class="title">대외기관 및 업무 블랙리스트</div>
				<div class="popup_box">
				
					<div style="display:inline-block; width:calc(50% - 20px); vertical-align:top;">	
						<div style="text-align:right; font-size:12px;">
						<table width="100%">
						<tr>
							<td align="left"><span style="color:blue;" id="instCnt"></span></td>
							<td align="right">
								<img id="btn_delinst" src="<c:url value="/img/btn_delete.png" />"level="W" class="btn_img" />
								<img id="btn_newinst" src="<c:url value="/img/btn_regist.png"/>" level="W" class="btn_img" />
							</td>
						</tr>
						</table>	
						</div>
						<form id="ajaxForm" onsubmit="return false;">
							<span>* 신규등록  (대외기관명 입력후 [등록]버튼을 클릭해 주세요.)</span>
							<table class="table_row" cellspacing="0">
								<tr>
									<th style="width:20%;">대외기관명</th>
									<td><input type="text" name="instiName"/> </td>
								</tr>
							</table>
						</form>
						<div id="gridList" style="margin-top:10px;">
							<table id="grid"></table>
						</div>
					</div>	
						
					<div style="display:inline-block; width:30px;"></div>	
					
					<div style="display:inline-block; width:calc(50% - 20px); vertical-align:top;">	
					
					<div style="text-align:right; font-size:12px;">
					<table width="100%">
					<tr>
						<td align="left"><span style="color:blue;" id="apcodeCnt"></span></td>
						<td align="right">
							<img id="btn_delApcode" src="<c:url value="/img/btn_delete.png" />"level="W" class="btn_img" />
							<img id="btn_newApcode" src="<c:url value="/img/btn_regist.png"/>" level="W" class="btn_img" />
						</td>
					</tr>
					</table>	
					</div>
					<form id="ajaxForm2" onsubmit="return false;">
						<span>* 신규등록  (업무코드 입력후 [등록]버튼을 클릭해 주세요.)</span>
						<table class="table_row" cellspacing="0">
							<tr>
								<th style="width:20%;">업무구분코드</th>
								<td ><input type="text" name="apCode" maxlength="4"/> </td>
							</tr>
						</table>
					</form>
					<div id="treeList" style="margin-top:10px;">
						<table id="targetGrid" ></table>
						<div id="pager"></div>
					</div>
					</div>	
				</div>		


				<div id="pager"></div>
				
			</div><!-- end content_middle -->
		</div><!-- end right_box -->	
		<!-- button -->
	</body>
</html>

