<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" session="false"%>
<%@ include file="/WEB-INF/jsp/include/taglib.jsp"%>
<c:set var="prefixName" scope="request">코드 그룹</c:set>
              <!-- Page Body -->
    <div class="d-flex flex-column flex-grow-1">
        <div id="DataTables_Table_0_wrapper" class="dataTables_wrapper dt-bootstrap5 no-footer">
        
			<div class="d-flex flex-row justify-content-evenly">
				<div style="width:49%;">
				<h5 class="mt-3">${prefixName}</h5>
				<div class="d-flex flex-row justify-content-between p-3">
					<%--div>
						<div class="input-group" id="${infoPrefix}-query">
							<select id="${infoPrefix}by" name="by" onchange="document.getElementById('${infoPrefix}term').focus();" aria-controls="DataTables_Table_0" class="form-select">
								<option value="groupName">이름</option>
								<option value="groupID">아이디</option>
							</select>
							<input id="${infoPrefix}term" name="term" autofocus type="text" placeholder="조회 조건을 입력하십시오." class="form-control">
						</div>
					</div--%>
					<div>
						<%--button onclick="searchGroups();" class="btn btn-primary">찾기</button--%>
						<button onclick="codeControl.groups.newInfo();" class="btn btn-primary">+ 추가</button>
						<button id="btnRemoveGroups" onclick="removeGroups();" class="btn btn-primary">- 제거</button>
					</div>
				</div>
				<table class="datatables-ajax table table-bordered dataTable no-footer" id="DataTables_Table_0" aria-describedby="DataTables_Table_0_info">
					<thead id="groupThead">
						<tr><th tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" style="text-align:center;"><input onchange="codeControl.groups.select(this.checked);" type="checkbox" class="form-check-input"></th>
							<th class="sorting sorting_asc" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-sort="ascending" aria-label="Full name: activate to sort column descending">아이디</th>
							<th class="sorting" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="Email: activate to sort column ascending">이름</th>
							<th class="sorting" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="Start date: activate to sort column ascending">등록일자</th>
						</tr>
                    </thead>
                    <tbody id="groupList">
					</tbody>
                   	<template id="groupRow">
                   	<tr data-key="{GRP_ID}"{notUsed}>
                       <td style="text-align:center;"><input value="{GRP_ID}" onchange="codeControl.groups.select('{GRP_ID}', this.checked);" type="checkbox" class="form-check-input"></td>
                       <td onclick="codeControl.groups.setCurrent('{GRP_ID}')" ondblclick="codeControl.groups.getInfo({})">{GRP_ID}</td>
                       <td onclick="codeControl.groups.setCurrent('{GRP_ID}')" ondblclick="codeControl.groups.getInfo({})">{GRP_NM}</td>
                       <td onclick="codeControl.groups.setCurrent('{GRP_ID}')" ondblclick="codeControl.groups.getInfo({})">{REG_DT}</td>
                    </tr>
                    </template>
                   	<template id="groupNotFound">
					<tr class="odd">
						<td valign="top" colspan="4" class="dataTables_empty text-center">${prefixName} 정보를 찾지 못했습니다.</td>
					</tr>
                    </template>
				</table>
				<div class="d-flex flex-row p-3 justify-content-between">
					<label id="groupPagingInfo" class="dataTables_info" role="status" aria-live="polite"></label>
	                 <ul id="groupPaging" class="pagination pagination-primary">
	                 </ul>
				</div>
				</div>
				<div style="width:49%;">
				<h5 class="mt-3">공통 코드</h5>
				<div class="d-flex flex-row justify-content-end p-3">
					<div>
						<button id="btnAddCode" onclick="codeControl.newCode();" class="btn btn-primary">+ 추가</button>
						<button id="btnRemoveCodes" onclick="removeCodes();" class="btn btn-primary">- 제거</button>
					</div>
				</div>
				<table class="datatables-ajax table table-bordered dataTable no-footer" id="DataTables_Table_0" aria-describedby="DataTables_Table_0_info">
					<thead id="codeThead">
						<tr><th tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" style="text-align:center;"><input id="codeToggler" onchange="codeControl.codes.select(this.checked);" type="checkbox" class="form-check-input"></th>
							<th class="sorting" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="Email: activate to sort column ascending">코드</th>
							<th class="sorting" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="Email: activate to sort column ascending">코드값</th>
							<th class="sorting" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="Start date: activate to sort column ascending">등록일자</th>
						</tr>
                    </thead>
                    <tbody id="codeList">
					</tbody>
                   	<template id="codeRow">
                   	<tr data-key="{CODE}"{notUsed}>
                       <td style="text-align:center;"><input value="{CODE}" onchange="codeControl.codes.select('{CODE}', this.checked);" type="checkbox" class="form-check-input"></td>
                       <td onclick="codeControl.codes.setCurrent('{CODE}')" ondblclick="codeControl.codes.getInfo({})">{CODE}</td>
                       <td onclick="codeControl.codes.setCurrent('{CODE}')" ondblclick="codeControl.codes.getInfo({})">{CODE_VAL}</td>
                       <td onclick="codeControl.codes.setCurrent('{CODE}')" ondblclick="codeControl.codes.getInfo({})">{REG_DT}</td>
                    </tr>
                    </template>
                   	<template id="codeNotFound">
					<tr class="odd">
						<td valign="top" colspan="4" class="dataTables_empty text-center">코드 정보를 찾지 못했습니다.</td>
					</tr>
                    </template>
				</table>
				<div class="d-flex flex-row p-3 justify-content-between">
					<label id="codePagingInfo" class="dataTables_info" role="status" aria-live="polite"></label>
	                 <ul id="codePaging" class="pagination pagination-primary">
	                 </ul>
				</div>
				</div>
			</div>
		</div>
    </div>
              <!--/ Page Body -->
<script >
var codeControl = new CodeControl();

function searchGroups() {
	codeControl.groups.query = {
		by:$("#${infoPrefix}-query [name='by']").val(),
		term:$("#${infoPrefix}-query [name='term']").val()
	};
	codeControl.groups.load(1);
}

function removeGroups() {
	dialog.alert({
		content:"선택한 ${prefixName} 정보를 제거하시겠습니까?",
		onOK:() => {
			codeControl.groups.remove();
		}
	});
}

function removeCodes() {
	dialog.alert({
		content:"선택한 코드를 삭제하시겠습니까?",
		onOK:() => {
			codeControl.codes.remove();
		}
	});
}

function renderGroupList() {
	let groupList = codeControl.groups.dataset;
	let empty = groupList.empty;
	
	let trs = empty ?
		[document.getElementById("groupNotFound").innerHTML] : <%-- from template#groupNotFound --%>
		groupList.inStrings(
				document.getElementById("groupRow").innerHTML,
				(tmpl, item) => tmpl.replace(/{notUsed}/gi, item.getValue("USE_YN") == "N" ? "class=\"not-used\"" : "")
			); <%-- from template#groupRow --%>
	$("#groupList").html(trs.join());
	$("#groupThead").find("input[type='checkbox']").prop("checked", false);
	$("#codeThead").find("input[type='checkbox']").prop("checked", false);
}

codeControl.onGroupListChange = obj => {
	renderGroupList();

	$("#groupPaging").setPaging({
		list:codeControl.groups.dataset,
		prefix:codeControl.groups.prefix,
		start:obj.groupStart,
		totalSize:obj.groupTotal,
		fetchSize:obj.groupFetch,
		func:"codeControl.groups.load({index})"
	});
};

codeControl.onCurrentGroupChange = item => {
	$("#btnAddCode").prop("disabled", !item);
	if (!item) return;
	
	let key = item.data.GRP_ID;
	$("#groupList").setCurrentRow(key);
};

codeControl.onGroupSelect = selected => {
	let groupList = codeControl.groups.dataset;
	let keys = selected.map(e => groupList.getKey(e));
	$("#groupList input[type='checkbox']").each(function() {
		let checkbox = $(this);
		checkbox.prop("checked", keys.includes(checkbox.val()));
	});
	
	$("#btnRemoveGroups").prop("disabled", keys.length < 1);
};

function renderCodeList() {
	let codeList = codeControl.codes.dataset;
	let empty = codeList.empty;
	
	let trs = empty ?
		[document.getElementById("codeNotFound").innerHTML] : <%-- from template#groupNotFound --%>
		codeList.inStrings(
				document.getElementById("codeRow").innerHTML,
				(tmpl, item) => tmpl.replace(/{notUsed}/gi, item.getValue("USE_YN") == "N" ? "class=\"not-used\"" : "")
			); <%-- from template#groupRow --%>
	$("#codeList").html(trs.join());
	$("#codeToggler").prop("checked", false);
}

codeControl.onCodeListChange = obj => {
	renderCodeList();
	
	$("#codePaging").setPaging({
		list:codeControl.codes.dataset,
		prefix:codeControl.codes.prefix,
		start:obj ? obj.codeStart : -1,
		totalSize:obj ? obj.codeTotal : 0,
		fetchSize:obj ? obj.codeFetch : 10,
		func:"codeControl.codes.load({index})"
	});
};

codeControl.onCurrentCodeChange = item => {
	if (!item) return;
	
	let data = item.data;
	let key = data.CODE;
	$("#codeList").setCurrentRow(key);
};

codeControl.onCodeSelect = selected => {
	let codeList = codeControl.codes.dataset;
	let keys = selected.map(e => codeList.getKey(e));
	$("#codeList input[type='checkbox']").each(function() {
		let checkbox = $(this);
		checkbox.prop("checked", keys.includes(checkbox.val()));
	});
	
	$("#btnRemoveCodes").prop("disabled", selected.length < 1);
};

$("#${infoPrefix}term").onEnterPress(searchGroups);

$(function(){
	${onload}
	codeControl.groups.setData({
		groupList:${groupList},
		groupStart:${groupStart},
		groupFetch:${groupFetch},
		groupTotal:${groupTotal}
	});
});
//# sourceURL=code-main.jsp
</script>