<%@ 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/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" />';

var serverType = window.dialogArguments["serverType"];
var eaiSevrDstcd = window.dialogArguments["eaiSevrDstcd"];

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;
}	

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

	return true;
}


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

$(document).ready(function() {	
	
	$("input[name=baseYmd]").inputmask("yyyy-mm-dd",{'autoUnmask':true}).val(getToday());
	
	$('#grid').jqGrid({
		datatype:"json",
		mtype: 'POST',
		url: url,
		postData : { cmd : 'DETAIL' , serverType : serverType , sqlValue : eaiSevrDstcd ,flag : 'company'
		},
		colNames:['대외기관명',
	              '신규등록일'
	    ],
		colModel:[
				{ name : 'INSTINAME'  	, align:'left'   , key :true },
				{ name : 'BASEYMD' 		, align:'center' ,formatter: dateFormater  }
		],
		cmTemplate: {
			sortable: false,	
		},
        jsonReader: {
            repeatitems: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)		
			}
			$("#selectCnt").text(" [ "+$("#grid").getGridParam("reccount") + " 건] ");
			
		},		
        multiselect:true,
        multiboxonly: false,
        rowNum: 10000,
	    height: '580',
		autowidth: true,
		viewrecords: true,
		ondblClickRow: function(rowId) {
           
       }	
	    
	});
	

	$("#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"));
	});
	
	
	$("#btn_operate").click(function(){
		
		if (!isValid()){
			return;
		}
		
		if(confirm("등록하시겠습니까?") == false) 
			return ;
		
		var postData = $('#ajaxForm').serializeArray();
		postData.push({ name: "cmd"   	    , value:"TRANSACTION_COMPANY"});
		postData.push({ name: "serverType"  , value:serverType});
		postData.push({ name: "sqlValue"    , value:eaiSevrDstcd});
		
		$.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_delete").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: "serverType"   , value:serverType});
		postData.push({ name: "eaiSevrDstcd" , value:eaiSevrDstcd});
		postData.push({ name: "notIn" 		 , value:"Y"});
		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_clone").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:"TRANSACTION_MOVE_TO_BLACKLIST_COMPANY"});
		postData.push({ name: "eaiSevrDstcd" , value:eaiSevrDstcd});
		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_close").click(function(){
		window.returnValue = "ok";
		window.close();
	});
	
	
	$("#btn_excel").click(function(event) {
        event.stopPropagation(); // Do not propagate the event.
        // Create an object that will manage to download the file.
        var target = "excelDown" ; // iframe의 name
        //(url,cmd,gridObj,formObject,fileName,merge)
		gridJsonToExcelSubmit2(url_excel,"LIST_GRID_TO_EXCEL",$("#grid"),$("#ajaxForm"),'대외기관 리스트');
		return false;
	});
	
	
 	buttonControl();
	
});
 
</script>
</head>
	<body>
		<div class="popup_box">
			<div class="search_wrap">
 				<img src="<c:url value="/img/btn_clone1.png"/>" alt="" id="btn_clone"  level="W" /> 
				<img src="<c:url value="/img/btn_excel.png"/>" id="btn_excel"  level="W" status="DETAIL"/>					
				<img src="<c:url value="/img/btn_delete.png"/>" alt="" id="btn_delete" level="W" status="DETAIL,NEW"/>	
				<img src="<c:url value="/img/btn_regist.png"/>" alt="" id="btn_operate" level="W" status="DETAIL,NEW" />	
				<img src="<c:url value="/img/btn_close.png"/>" alt="" id="btn_close" level="R" />							
			</div>
			<div class="title">대외기관 리스트</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>
					<th>신규등록일</th>
					<td><input type="text"  name="baseYmd" /></td>
				</tr>
			</table>
			</form>
			<div style="height:20px;" align="right">&nbsp;<span style="color:blue;" id="selectCnt"></span>&nbsp;&nbsp;</div>	
			<table id="grid" ></table>
			<div id="pager"></div>
			
		</div><!-- end.popup_box -->
	</body>
</html>

