<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 
<%@ page session="false" %>
<!doctype html>
<html lang="ko">
<head>
	<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1"/>
	<meta name="Description" content="사이트설명" />
	<meta name="Keywords" content="키워드" />
	
	<%@ include file="/WEB-INF/views/common.jsp" %>
	
	<link rel="stylesheet" type="text/css" href="${contextRoot }/css/common.css" />
	<link rel="stylesheet" type="text/css" href="${contextRoot }/css/main.css" />
	<link rel="stylesheet" type="text/css" href="${contextRoot }/css/popupLayout.css" />
	
	<title>SIGINFO_MAP</title>
</head>
<body>
	<div id="wrap">
		<!-- header -->
		<header id="header">
			<h1>신호센터 정보</h1>
		</header>
		<!-- header -->
		<!-- section -->
		<section id="contents" class="c_p">
		<article>
		<table class="table_type1">
		<colgroup>
			<col width="">
		</colgroup>
		<thead>
			<tr>
				<th>지역코드</th>
				<th>지역 센터</th>
				<th>아이디</th>
				<th>비밀번호</th>
				<th>지역센터ID</th>
				<th>파일 수정일자</th>
				<th>시뮬레이터</th>
				<th>X좌표</th>
				<th>Y좌표</th>
				<th>변경</th>
			</tr>
		</thead>
		<tbody id="centerList"></tbody>
		</table>
		<div class="t_c t20"><a class="btn_c" href="javascript:insertList()" style="margin-top:10px">센터추가</a></div>
		</article>

		<article>
		<div id="updateCon">
			<h2>신호센터정보 변경</h2>
			<table class="table_type1">
			<colgroup>
				<col width="9%">
				<col width="9%">
				<col width="9%">
				<col width="9%">
				<col width="9%">
				<col width="15%">
				<col width="10%">
				<col width="10%">
				<col width="10%">
				<col width="10%">
			</colgroup>
			<thead>
				<tr>
					<th>지역코드</th>
					<th>지역 센터</th>
					<th>아이디</th>
					<th>비밀번호</th>
					<th>지역센터ID</th>
					<th>파일 수정일자</th>
					<th>시뮬레이터</th>
					<th>X좌표</th>
					<th>Y좌표</th>
					<th>삭제</th>
				</tr>
			</thead>
			<tbody id="updateList">
				<tr>
					<td><input type="text" id="regionCd" /></td>
					<td><input type="text" id="regionNm" /></td>
					<td><input type="text" id="centerId" /></td>
					<td><input type="text" id="centerPw" /></td>
					<td><input type="text" id="regionId" /></td>
					<td><input type="text" id="updDt" /></td>
					<td>
						<select id="simulFlag" class="select">
							<option value="ON">ON</option>
							<option value="OFF">OFF</option>
						</select>
					</td>
					<td><input type="text" id="xCoord" /></td>
					<td><input type="text" id="yCoord" /></td>
					<td>
						<img alt="변경" src="images/btn_pop3.png" onClick="delCenter()">
					</td>
				</tr>
			</tbody>
			</table>
			<div class="t_c t20" style="margin-top:10px"><a class="btn_c" id="updateCenter" href="javascript:updateCenter1()">저장하기</a></div>
		</div>
		<div id="insertCon">
			<h2>신호센터정보 추가</h2>
			<table class="table_type1">
			<colgroup>
				<col width="10%">
				<col width="10%">
				<col width="10%">
				<col width="10%">
				<col width="10%">
				<col width="15%">
				<col width="15%">
				<col width="10%">
				<col width="10%">
			</colgroup>
			<thead>
				<tr>
					<th>지역코드</th>
					<th>지역 센터</th>
					<th>아이디</th>
					<th>비밀번호</th>
					<th>지역센터ID</th>
					<th>파일 수정일자</th>
					<th>시뮬레이터</th>
					<th>X좌표</th>
					<th>Y좌표</th>
				</tr>
			</thead>
			<tbody id="insertList">
				<tr>
					<td><input type="text" id="regionCdIn" /></td>
					<td><input type="text" id="regionNmIn" /></td>
					<td><input type="text" id="centerIdIn" /></td>
					<td><input type="text" id="centerPwIn" /></td>
					<td><input type="text" id="regionIdIn" /></td>
					<td><input type="text" id="updDtIn" /></td>
					<td>
						<select id="simulFlagIn" class="select">
							<option value="ON">ON</option>
							<option value="OFF">OFF</option>
						</select>
					</td>
					<td><input type="text" id="xCoordIn" /></td>
					<td><input type="text" id="yCoordIn" /></td>
				</tr>
			</tbody>
			</table>
			<div class="t_c t20" style="margin-top:10px"><a class="btn_c" id="insertCenter" href="javascript:insertCenter1()">저장하기</a></div>
		</div>
		</article>

		</section>
		
	</div>

<script src="${contextRoot }/js/common/common-all.js" type="text/javascript" ></script>
<script src="${contextRoot }/js/common/hashmap.js" type="text/javascript" ></script>
<script src="${contextRoot }/js/common/common.js" type="text/javascript" ></script>
<script src="${contextRoot }/js/signal.js" type="text/javascript" ></script>

<script type="text/javascript">

getCenterList();

//신호센터정보 가저오기
function getCenterList()
{
	var url = 'getCenterList.do';
	var param = '';

	requestService(url, param, getCenterListCallback);
}

function getCenterListCallback(data)
{
	var centerHtml = '';
	var regionCd;
	var regionNm;
	var centerId;
	var centerPw;
	var regionId;
	var updDt;
	var simulFlag;
	var xCoord;
	var yCoord;
	
	for(var ii = 0; ii < data.length; ii++)
	{
		regionCd  = data[ii].REGION_CD;
		regionNm  = data[ii].REGION_NM;
		centerId  = data[ii].REGION_CENTER_ID;
		centerPw  = data[ii].REGION_CENTER_PASSWD;
		regionId  = data[ii].REGION_ID;
		updDt     = data[ii].FILE_UPD_DT;
		simulFlag = data[ii].SIMUL_FLAG;
		xCoord    = data[ii].X_COORD;
		yCoord    = data[ii].Y_COORD;
		
		if(simulFlag == 'Y') simulFlag = 'ON';
		else simulFlag = 'OFF';
		
		if(updDt != '-') updDt = dateChange(updDt);
		
		centerHtml += '<tr>';
		centerHtml += '<td>'+regionCd+'</td>';
		centerHtml += '<td>'+regionNm+'</td>';
		centerHtml += '<td>'+centerId+'</td>';
		centerHtml += '<td>'+centerPw+'</td>';
		centerHtml += '<td>'+regionId+'</td>';
		centerHtml += '<td>'+updDt+'</td>';
		centerHtml += '<td>'+simulFlag+'</td>';
		centerHtml += '<td>'+xCoord+'</td>';
		centerHtml += '<td>'+yCoord+'</td>';
		centerHtml += '<td><img src="images/btn_pop2.png" alt="변경" onClick="updateView(\''+regionCd+'\',\''+regionNm+'\',\''+centerId+'\',\''+centerPw+'\',\''+regionId+'\',\''+updDt+'\',\''+simulFlag+'\',\''+xCoord+'\',\''+yCoord+'\')"></td>';
		centerHtml += '</tr>';
	}
	
	$('#centerList').empty().html(centerHtml);
}

function updateView(regionCd,regionNm,centerId,centerPw,regionId,updDt,simulFlag,xCoord,yCoord)
{
	var d = new Date();
	var curDate = dateToYYYYMMDD(d);
	
	$('#insertCon').hide();
	$('#updateCon').show();
	$('#regionCd').val(regionCd);
	$('#regionNm').val(regionNm);
	$('#centerId').val(centerId);
	$('#centerPw').val(centerPw);
	$('#regionId').val(regionId);
	$('#updDt').val(curDate);
	$('#simulFlag').val(simulFlag);
	$('#xCoord').val(xCoord);
	$('#yCoord').val(yCoord);
}

function updateCenter1()
{
	var regionCd = $('#regionCd').val();
	var regionNm = $('#regionNm').val();
	var centerId = $('#centerId').val();
	var centerPw = $('#centerPw').val();
	var updDt = $('#updDt').val();
	var simulFlag = $('#simulFlag').val();
	var xCoord = $('#xCoord').val();
	var yCoord = $('#yCoord').val();
	
	var updDt2 = dateToChar(updDt);
	
	if(regionCd == null || regionCd == '')
	{
		alert('지역코드를 입력하세요.');
		return;
	}
	if(regionNm == null || regionNm == '')
	{
		alert('지역명을 입력하세요.');
		return;
	}
	if(centerId == null || centerId == '')
	{
		alert('센터아이디을 입력하세요.');
		return;
	}
	
	if(simulFlag == 'ON') simulFlag = 'Y';
	else simulFlag = 'N';
	
	$('.centerNm_'+regionCd,opener.document).text(regionNm);
	
	var url = 'updateCenter.do';
	var param = 'regionCd='+regionCd+'&regionNm='+regionNm
	           +'&centerId='+centerId+'&centerPw='+centerPw
	           +'&updDt='+updDt2+'&simulFlag='+simulFlag
	           +'&xCoord='+xCoord+'&yCoord='+yCoord

	requestService(url, param, updateCenterCallback);
}

function updateCenterCallback()
{
	getCenterList();
	$('#updateCon').hide();
}

function delCenter()
{
	if(confirm("삭제하시겠습니까?") == true)
	{
		var regionCd = $('#regionCd').val();
		var url = 'delCenter.do';
		var param = 'regionCd='+regionCd

		requestService(url, param, delCenterCallback);
	}
	else return
}

function delCenterCallback()
{
	$('#updateCon').hide();
	getCenterList();
}

function insertList()
{
	$('#regionCdIn').val('');
	$('#regionNmIn').val('');
	$('#centerIdIn').val('');
	$('#centerPwIn').val('');
	$('#xCoordIn').val('');
	$('#yCoordIn').val('');
	
	$('#regionCdIn').focus();
	
	$('#updateCon').hide();
	$('#insertCon').show();
	
	var d = new Date();
	var curDate = dateToYYYYMMDD(d);
	
	$('#updDtIn').val(curDate);
}

function insertCenter1()
{
	var regionCd = $('#regionCdIn').val();
	var regionNm = $('#regionNmIn').val();
	var centerId = $('#centerIdIn').val();
	var centerPw = $('#centerPwIn').val();
	var regionId = $('#regionIdIn').val();
	var updDt    = $('#updDtIn').val();
	var simulFlag = $('#simulFlagIn').val();
	var xCoord   = $('#xCoordIn').val();
	var yCoord   = $('#yCoordIn').val();
	
	if(regionCd == null || regionCd == '')
	{
		alert('지역코드를 입력하세요.');
		return;
	}
	if(regionNm == null || regionNm == '')
	{
		alert('지역명을 입력하세요.');
		return;
	}
	if(centerId == null || centerId == '')
	{
		alert('아이디을 입력하세요.');
		return;
	}
	if(regionId == null || regionId == '')
	{
		alert('지역센터ID를 입력하세요.');
		return;
	}
	
	if(simulFlag == 'ON') simulFlag = 'Y';
	else simulFlag = 'N';
	
	var updDt2 = dateToChar(updDt);
	
	var url = 'insertCenter.do';
	var param = 'regionCd='+regionCd+'&regionNm='+regionNm+'&regionId='+regionId
	           +'&centerId='+centerId+'&centerPw='+centerPw
	           +'&updDt='+updDt2+'&simulFlag='+simulFlag
	           +'&xCoord='+xCoord+'&yCoord='+yCoord

	requestService(url, param, insertCenterCallback);
}

function insertCenterCallback()
{
	getCenterList();
	$('#insertCon').hide();
}

//데이트 포멧 
function dateToYYYYMMDD(date)
{
    function pad(num) {
        num = num + '';
        return num.length < 2 ? '0' + num : num;
    }
    return date.getFullYear().toString().substr(2,2) + '/' + pad(date.getMonth()+1) + '/' + pad(date.getDate()) + ' ' + pad(date.getHours()) + ':' + pad(date.getMinutes()) + ':' + pad(date.getSeconds());
}

function dateToChar(date)
{
	var pDate = date.replace('/','');
	var sDate = pDate.replace(/\s/g,'');
	var uDate = sDate.replace(/:/g,'');
	var oDate = uDate.replace('/','');
	
	return oDate;
}

function dateChange(date)
{
	var year = date.substr(0,2);
	var mon = date.substr(2,2);
	var day = date.substr(4,2);
	var hour = date.substr(6,2);
	var min = date.substr(8,2);
	var sec = date.substr(10,2);
	
	return year + '/' + mon + '/' + day + ' ' + hour + ':' + min + ':' +sec;
}

$(window).bind("beforeunload", function (e){
	opener.closeTopMenu('center');
});

</script>
</body>
</html>
