<%@ 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/apim/portalterms/portalTermsMan.json" />';
	var url_view = '<c:url value="/onl/apim/portalterms/portalTermsMan.view" />';

	function init() {
		$.ajax({
			type: "POST",
			url: url,
			dataType: "json",
			data: {cmd: 'LIST_INIT_COMBO'},
			success: function(json) {
				const comboConfigs = [
					{name: 'searchAgreementsType', data: json.termsTypeRows}
				];

				comboConfigs.forEach(function(config) {
					new makeOptions("CODE", "NAME")
							.setObj($("select[name=" + config.name + "]"))
							.setNoValueInclude(true)
							.setNoValue("", "<%=localeMessage.getString("combo.all")%>")
							.setData(config.data)
							.setFormat(nameOptionFormat)
							.rendering();
				});

				select_termsType = getGridSelectText("CODE", "NAME", json.termsTypeRows);
			}
		});
	}

	$(document).ready(function() {
		init();
		$('#grid').jqGrid({
			datatype:"json",
			mtype: 'POST',
			url: url,
			postData : { cmd : 'LIST',
				searchAgreementsType: $('select[name=searchAgreementsType]').val()},
			colNames:['No.', '약관타입', '종류', '버전', '약관시행일', '등록일', '등록자', '약관 수정일', '수정 등록자'],
			colModel:[
				{ name : 'rowNum'       		, align:'center', width:40 , sortable:false},
				{ name : 'agreementsType'       , align:'center', hidden:true},
				{ name : 'name'     			, align:'center', width:150 },
				{ name : 'revision'             , align:'center', width:40 },
				{ name : 'publishedOn'          , align:'center', width:100, formatter: gridDateFormat },
				{ name : 'createdDate'          , align:'center', width:120, formatter: timeStampFormat },
				{ name : 'createdByName' 		, align:'center', width:80 },
				{ name : 'lastModifiedDate'     , align:'center', width:120, formatter: timeStampFormat },
				{ name : 'lastModifiedByName'	, align:'center', width:80 }
				],
			jsonReader: {
				repeatitems:false
			},
			pager : $('#pager'),
			page : '${param.page}',
			rowNum : '${rmsDefaultRowNum}',
			autoheight: true,
			height: $("#container").height(),
			autowidth: true,
			viewrecords: true,
			rowList : eval('[${rmsDefaultRowList}]'),
			rowId: function(obj) {
				return obj.agreementsType + "_" + obj.revision;
			},
			loadComplete: function() {
				var records = $(this).getGridParam('records');
				var page = $(this).getGridParam('page');
				var rowNum = $(this).getGridParam('rowNum');
				var rows = $(this).getDataIDs();
				var colModel = $(this).getGridParam("colModel");

				for (var i = 0; i < rows.length; i++) {
					var number = ((page - 1) * rowNum + i) + 1;
					// var reverseNumber = records - ((page - 1) * rowNum + i);
					$(this).setCell(rows[i], 'rowNum', number);
				}
				for(var i = 0 ; i< colModel.length; i++){
					$(this).setColProp(colModel[i].name, {sortable : false});
				}
			},
			ondblClickRow: function(rowId) {
				var rowData = $(this).getRowData(rowId);
				var agreementsType = rowData.agreementsType;
				var revision = rowData.revision;

				var url2 = url_view;
				url2 += '?cmd=DETAIL';
				url2 += '&page='+$(this).getGridParam("page");
				url2 += '&returnUrl=' + getReturnUrl();
				url2 += '&menuId='+'${param.menuId}';
				url2 += '&agreementsType='+ agreementsType;
				url2 += '&revision='+ revision;
				goNav(url2);
			},
			loadError: function (jqXHR, textStatus, errorThrown) {
				var location ='<%=request.getContextPath()%>/';
				comloadError(jqXHR, textStatus, errorThrown ,location);
			}
		});

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

		// $("#btn_search").click(function(){
		// 	var postData = getSearchForJqgrid("cmd","LIST");
		// 	$("#grid").setGridParam({ url:url,postData: postData ,page:1 }).trigger("reloadGrid");
		// });

		$("select[name=searchAgreementsType]").change(function(){
			var postData = getSearchForJqgrid("cmd","LIST");
			$("#grid").setGridParam({ url:url, postData: postData, 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}';
			goNav(url2);
		});

		$("select[name=searchAgreementsType], 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">약관 목록</div>
		<form id="ajaxForm"  onsubmit="return false;">
			<div class="select-style " style="align-items: center; margin-bottom: 20px;">
				<select name="searchAgreementsType"></select>
			</div>
		</form>
		<table id="grid" ></table>
		<div id="pager"></div>

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