<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.io.*"%>
<%@ page import="com.eactive.eai.rms.common.login.SessionManager"%>
<%@ 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");
	request.setCharacterEncoding("UTF-8");
	String SERVICE_NAME = "";
	if (com.eactive.eai.common.util.SystemUtil.isDevServer()) {
		SERVICE_NAME = "deai";
	} else if (com.eactive.eai.common.util.SystemUtil.isStagingServer()) {
		SERVICE_NAME = "seai";
	}
%>
<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"/>
<style type="text/css">
.ui-jqgrid-hbox, .ui-jqgrid-bdiv { overflow-x: hidden; }
.contents { display: table; }
.arrow { display: table-cell; vertical-align: middle; width: 150px; text-align: center; }
.arrow > img { display: block; margin: auto; cursor: pointer; }
.arrow > span { font-weight: bold; font-size: 12pt; }
.gridcount { display: inline-block; float: left; margin-top: 12px; }
.gridtitle { display: block; text-align: center; font-size: 15pt; font-weight: bold; }
.search_wrap { float: left; width: 100%; text-align: center; text-indent: 44.5em }
</style>
<script type="text/javascript">
var url = '<c:url value="/onl/admin/service/eaiMsgCmFtp.json"/>';
$(document).ready(function() {
	$('.gridtitle:eq(0)').html('<%=localeMessage.getString("eaimsgcmftp.devSys")%>');
	$('.gridtitle:eq(1)').html('<%=localeMessage.getString("eaimsgcmftp.prdSys")%>');
	
//	if ('<%=SERVICE_NAME%>' == 'deai') {
//		$('.gridtitle:eq(0)').html('개발시스템');
//		$('.gridtitle:eq(1)').html('스테이징시스템');
//	} else if ('<%=SERVICE_NAME%>' == 'seai'){
//		$('.gridtitle:eq(0)').html('스테이징시스템');
//		$('.gridtitle:eq(1)').html('운영시스템');
//	}
	$('#grid1').jqGrid({
		datatype : "json",
		mtype : 'POST',
		url : url,
		postData : { cmd: 'LIST_LOCAL_FILES' },
		colNames : [ '<%=localeMessage.getString("eaimsgcmftp.cmList")%>' ],
		colModel : [ { name: 'FILE_NM', align: 'center' } ],
		jsonReader : {
	    	root : "cmList",
			repeatitems : false
		},
		width: '400',
		height: '600',
		rowNum: 10000,
		multiselect: true,
		multiboxonly: true,
		loadComplete: function(data) {
			$('#grid1cnt').html($('#grid1').getGridParam("reccount"));
		},
		loadError: function(xhr,status,error){
			$('#grid1cnt').html(0);
	    },
	    ondblClickRow: function(rowId) {
	    	var rowData = $(this).getRowData(rowId);
	    	var url2 = '<c:url value="/onl/admin/service/eaiMsgCmFtp.view"/>';
	    	var args = new Object();
	    	url2 += '?cmd=POPUP';
	    	args['flag'] = 'LOCAL';
	    	args['fileName'] = rowData.FILE_NM;
	    	showModal(url2, args, 550, 800, null);
	    }
	});
	$('#grid2').jqGrid({
		datatype : "json",
		mtype : 'POST',
		url : url,
		postData : { cmd: 'LIST_FTP_FILES' },
		colNames : [ '<%=localeMessage.getString("eaimsgcmftp.appTime")%>', '<%=localeMessage.getString("eaimsgcmftp.man")%>', '<%=localeMessage.getString("eaimsgcmftp.sys")%>', 'CM 리스트' ],
		colModel : [
			{ name: 'DATE', align: 'center', width: '200' },
			{ name: 'USER', align: 'center' },
			{ name: 'SYSTEM', align: 'center' },
			{ name: 'FILE_NM', hidden: true }
		],
		jsonReader : {
	    	root : "ftpList",
			repeatitems : false
		},
		width: '600',
		height: '600',
		rowNum: 10000,
		multiselect: true,
		multiboxonly: true,
		loadComplete: function(data) {
			$('#grid2cnt').html($('#grid2').getGridParam("reccount"));
		},
		loadError: function(xhr,status,error){
			$('#grid2cnt').html(0);
	    },
	    ondblClickRow: function(rowId) {
	    	var rowData = $(this).getRowData(rowId);
	    	var url2 = '<c:url value="/onl/admin/service/eaiMsgCmFtp.view"/>';
	    	var args = new Object();
	    	url2 += '?cmd=POPUP';
	    	args['flag'] = 'FTP';
	    	args['fileName'] = rowData.FILE_NM;
	    	showModal(url2, args, 550, 800, null);
	    }
	});
	
	$('#btn_ftp').click(onclickBtnFtp);
	$('#btn_delete').click(onclickBtnDelete);
	$('#btn_refresh').click(onclickBtnRefresh);
});

function onclickBtnFtp() {
	var sel = $('#grid1').jqGrid('getGridParam', 'selarrrow');
	if (sel.length == 0) {
		alert('전송할 파일을 체크하세요.');
		return;
	}
	var localFiles = jQuery.map(sel, function(i, index) {
		return $('#grid1').getCell(i, 'FILE_NM');
	});
	if (confirm('전송하시겠습니까?')) {
		var params = new Object();
		params['cmd'] = 'UPLOAD_FTP';
		params['LocalFiles'] = localFiles.join('&');
		$.ajax({  
			dataType:"json",
			type : "POST",
			url  : '<c:url value="/onl/admin/service/eaiMsgCmFtp.json" />',
			data : params,
			success:function(json){
				alert(json.message);
				$('#btn_refresh').trigger('click');
			},
			error:function(xhr){
				if (xhr.responseText != null) {
					try {
						alert(JSON.parse(xhr.responseText).errorMsg);	
					} catch (e) {
						alert(xhr.responseText);
					}
				}
			}
		});
	}
}

function onclickBtnDelete() {
	var localsels = $('#grid1').jqGrid('getGridParam', 'selarrrow');
	var ftpsels = $('#grid2').jqGrid('getGridParam', 'selarrrow');
	if (localsels.length == 0 && ftpsels.length == 0) {
		alert('삭제할 파일을 체크하세요.');
		return;
	}
	var localFiles = jQuery.map(localsels, function(i, index) {
		return $('#grid1').getCell(i, 'FILE_NM');
	});
	var ftpFiles = jQuery.map(ftpsels, function(i, index) {
		return $('#grid2').getCell(i, 'FILE_NM');
	});
	if (confirm('파일을 삭제하시겠습니까?')) {
		var params = new Object();
		params['cmd'] = 'DELETE_FILES';
		params['LocalFiles'] = localFiles.join('&');
		params['FtpFiles'] 	 = ftpFiles.join('&');
		$.ajax({  
			dataType:"json",
			type : "POST",
			url  : '<c:url value="/onl/admin/service/eaiMsgCmFtp.json" />',
			data : params,
			success:function(json){
				alert(json.message);
				$('#btn_refresh').trigger('click');
			},
			error:function(xhr){
				if (xhr.responseText != null) {
					try {
						alert(JSON.parse(xhr.responseText).errorMsg);	
					} catch (e) {
						alert(xhr.responseText);
					}
				}
			}
		});
	}
}

function onclickBtnRefresh() {
	$('#grid1').trigger('reloadGrid');
	$('#grid2').trigger('reloadGrid');
}

</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"><i class="material-icons">delete</i> <%= localeMessage.getString("button.delete") %></button>
				<button type="button" class="cssbtn" id="btn_refresh" level="W"><i class="material-icons">refresh</i> <%= localeMessage.getString("button.refresh") %></button>
<%-- 				<img src="<c:url value="/img/btn_delete.png"/>" id="btn_delete"  level="W"/> --%>
<%-- 				<img src="<c:url value="/img/btn_refresh.png"/>" id="btn_refresh"  level="W"/> --%>
			</div>
			<div class="title"><%=localeMessage.getString("eaimsgcmftp.title")%><span class="tooltip"><%=localeMessage.getString("eaimsgcmftp.tooltip")%></span></div>
			<div class="contents">
				<div>
					<span class="gridcount"><%=localeMessage.getString("eaimsgcmftp.total")%>:&nbsp;&nbsp;<span id="grid1cnt">0</span>&nbsp;<%=localeMessage.getString("eaimsgcmftp.unit")%></span>
					<span class="gridtitle"></span>
					<table id="grid1"></table>
				</div>
				<div class="arrow">
					<img id="btn_ftp" src="<c:url value="/images/common/arrow.gif"/>" level="W"/>
					<span><%=localeMessage.getString("eaimsgcmftp.trans")%></span>
				</div>
				<div>
					<span class="gridcount"><%=localeMessage.getString("eaimsgcmftp.total")%>:&nbsp;&nbsp;<span id="grid2cnt">0</span>&nbsp;<%=localeMessage.getString("eaimsgcmftp.unit")%></span>
					<span class="gridtitle"></span>
					<table id="grid2"></table>
				</div>
			</div>
		</div>
	</div>
</body>
</html>