<%@ 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"/>
<style>
.badge-use { display:inline-block; padding:3px 10px; border-radius:12px; font-size:11px; font-weight:500; }
.badge-use.active { background:rgba(0,128,128,0.15); color:rgba(0,128,128,1); border:1px solid rgba(0,128,128,0.3); }
.badge-use.inactive { background:#f5f5f5; color:#999; border:1px solid #ddd; }
.threshold-display { font-size:12px; color:#666; }
.threshold-display .num { color:rgba(0,128,128,1); font-weight:600; }
.threshold-display .unit { color:#555; }
.threshold-display .code { background:#e8f4f4; color:rgba(0,128,128,0.9); padding:1px 5px; border-radius:3px; font-family:Consolas,monospace; font-size:10px; margin-left:4px; }
.datetime-display { font-size:11px; color:#666; }
.datetime-display .date { color:#333; }
.datetime-display .time { color:rgba(0,128,128,0.9); font-weight:500; }
.group-id-display { font-family:Consolas, monospace; font-size:11px; color:#666; }
</style>
<script language="javascript" >
var url      ='<c:url value="/onl/admin/inflow/inflowGroupControlMan.json" />';
var url_view ='<c:url value="/onl/admin/inflow/inflowGroupControlMan.view" />';

function useYnBadgeFormatter(cellValue, options, rowObject) {
	if (cellValue === '1') {
		return '<span class="badge-use active">사용</span>';
	} else {
		return '<span class="badge-use inactive">미사용</span>';
	}
}

function perSecondFormatter(cellValue, options, rowObject) {
	if (!cellValue) return '<span style="color:#ccc;">-</span>';
	return '<span class="threshold-display"><span class="num">' + Number(cellValue).toLocaleString() + '</span>건 / <span class="unit">초</span></span>';
}

function dateTimeFormatter(cellValue, options, rowObject) {
	if (!cellValue) return '<span style="color:#ccc;">-</span>';
	// 2025-12-10 16:34:16 형식 파싱
	var parts = cellValue.split(' ');
	if (parts.length < 2) return cellValue;
	var datePart = parts[0].split('-');
	var timePart = parts[1].substring(0, 5); // HH:mm만
	if (datePart.length < 3) return cellValue;
	var year = datePart[0];
	var month = parseInt(datePart[1], 10);
	var day = parseInt(datePart[2], 10);
	var currentYear = new Date().getFullYear().toString();
	var dateStr = (year === currentYear) ? month + '월 ' + day + '일' : year.substring(2) + '년 ' + month + '월 ' + day + '일';
	return '<span class="datetime-display"><span class="date">' + dateStr + '</span> <span class="time">' + timePart + '</span></span>';
}

function thresholdFormatter(cellValue, options, rowObject) {
	var threshold = rowObject.THRESHOLD;
	var timeUnit = rowObject.THRESHOLDTIMEUNIT;

	if (!threshold || !timeUnit) return '<span style="color:#ccc;">-</span>';

	var unitLabels = {
		'SEC': '초',
		'MIN': '분',
		'HOU': '시간',
		'DAY': '일',
		'MON': '월'
	};
	var label = unitLabels[timeUnit] || timeUnit;
	return '<span class="threshold-display"><span class="num">' + Number(threshold).toLocaleString() + '</span>건 / <span class="unit">' + label + '</span><span class="code">' + timeUnit + '</span></span>';
}

function groupIdFormatter(cellValue, options, rowObject) {
	if (!cellValue) return '<span style="color:#ccc;">-</span>';
	return '<span class="group-id-display">' + cellValue + '</span>';
}

$(document).ready(function() {
	$('#grid').jqGrid({
		datatype:"json",
		mtype: 'POST',
		url: url,
		postData : { cmd : 'LIST', searchGroupName: $('input[name=searchGroupName]').val(), searchUseYn: $('select[name=searchUseYn]').val()},
		colNames:['그룹 ID',
                  '그룹명',
                  '초당 임계치',
                  '추가 임계치',
                  '사용여부',
                  '수정일시'
        ],
		colModel:[
			{ name : 'GROUPID'				, align:'left'		, width:'200'	, sortable:false },
			{ name : 'GROUPNAME'			, align:'left'		, width:'280'},
			{ name : 'THRESHOLDPERSECOND'	, align:'center'	, width:'100'	, formatter:perSecondFormatter },
			{ name : 'THRESHOLD' 			, align:'center'	, width:'120'	, formatter:thresholdFormatter },
			{ name : 'USEYN'				, align:'center'	, width:'70'	, formatter:useYnBadgeFormatter },
			{ name : 'MODIFIEDAT'			, align:'center'	, width:'100'	, formatter:dateTimeFormatter }
		],
        jsonReader: {
             repeatitems:false
        },
		pager : $('#pager'),
		page : '${param.page}',
		rowNum : '${rmsDefaultRowNum}',
	    autoheight: true,
	    height: $("#container").height(),
		autowidth: true,
		viewrecords: true,
		rowList : eval('[${rmsDefaultRowList}]'),
		ondblClickRow: function(rowId) {
			var rowData = $(this).getRowData(rowId);
            var groupId = rowData['GROUPID'];
            var url2 = url_view;
            url2 += '?cmd=DETAIL';
            url2 += '&page='+$(this).getGridParam("page");
            url2 += '&returnUrl='+getReturnUrl();
            url2 += '&menuId='+'${param.menuId}';
			//검색값
            url2 += '&searchGroupName='+$("input[name=searchGroupName]").val();
            url2 += '&searchUseYn='+$("select[name=searchUseYn]").val();
            //key값
            url2 += '&groupId='+groupId;
            goNav(url2);

        },
		gridComplete:function (d){
			var colModel = $(this).getGridParam("colModel");
			for(var i = 0 ; i< colModel.length; i++){
				$(this).setColProp(colModel[i].name, {sortable : false});
			}

		}
	});


	resizeJqGridWidth('grid','content_middle','1000');

	$("#btn_search").click(function(){
		$("#grid").setGridParam({ postData: { searchGroupName: $('input[name=searchGroupName]').val(), searchUseYn: $('select[name=searchUseYn]').val()}, page:1 }).trigger("reloadGrid");
	});
	$("#btn_new").click(function(){
		var url2 = url_view;
		url2 += '?cmd=DETAIL';
		url2 += '&page='+$("#grid").getGridParam("page");
		url2 += '&returnUrl='+getReturnUrl();
        url2 += '&menuId='+'${param.menuId}';
		//검색값
        url2 += '&searchGroupName=';

        goNav(url2);
	});

	$("input[name^=search]").keydown(function(key){
		if (key.keyCode == 13){
			$("#btn_search").click();
		}
	});

	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">
					<button type="button" class="cssbtn" id="btn_new" level="W"><i class="material-icons">add</i> <%= localeMessage.getString("button.new") %></button>
					<button type="button" class="cssbtn" id="btn_search" level="R"><i class="material-icons">search</i> <%= localeMessage.getString("button.search") %></button>
				</div>
				<div class="title">유량제어 그룹 관리<span class="tooltip">유량제어 그룹 관리</span></div>

				<table class="search_condition" cellspacing=0;>
					<tbody>
						<tr>
							<th style="width:180px;">그룹명</th>
							<td><input type="text" name="searchGroupName" value="${param.searchGroupName}"></td>
							<th style="width:180px;">사용여부</th>
							<td>
								<div class="select-style">
									<select name="searchUseYn">
										<option value="">전체</option>
										<option value="1">사용함</option>
										<option value="0">사용안함</option>
									</select>
								</div>
							</td>
						</tr>
					</tbody>
				</table>

				<table id="grid" ></table>
				<div id="pager"></div>

			</div><!-- end content_middle -->
		</div><!-- end right_box -->
	</body>
</html>
