<%@ 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="pageKorName" scope="request">장비단속레이아웃 관리</c:set>
<div class="content-wrapper" data-doctx="${pageName}">
	<div class="container-xxl flex-grow-1 px-0">
		<div class="card wrapper-list">

		    <div class="container-page-btn">
		        <button type="button" name="btnReset" class="btn btn-outline-dark w-px-120" title="초기화">초기화</button>
		        <span class="container-window-btn-right">
		            <button type="button" name="btnSearch" class="btn btn-search w-px-120" title="검색">검색</button>
		        </span>
		    </div>

		    <form name="frmSearch">
		        <div class="container-search">
                    <div class="row g-1">
                        <div class="col-6">
                            <span class="form-label fw-bold form-search-title">업무구분</span>
                            <span class="form-search-linebox">
								<c:forEach items="${TaskListForSgg}" var="item">
									<label>
										<input name="taskSeCd" type="radio" value="${item.code}"
										class="form-check-input" alt="업무구분"
										onchange="pageObject['${pageName}'].fnResetAndChangeBiz(this.value);">
										${item.value}
									</label>
								</c:forEach>
                            </span>
                        </div>
                        <div class="col-6"></div>
                        <div class="col-6"></div>
                        <div class="col-6"></div>
                        <div class="col-6"></div>
                        <div class="col-6"></div>
                    </div>
		        </div>
		    </form>

		    <div>
		        <div name="gridbuttonArea" class="container-page-btn">
		            <div class="d-flex flex-row justify-content-between">
						<span is="paging-info" name="layoutPagingInfo" class="dataTables_info"></span>
		                <ul name="layoutPaging" class="pagination pagination-primary"></ul>
					</div>

					<span class="container-window-btn-right">
						<button type="button" class="btn btn-primary w-px-120" name="btnAdd" title="추가">추가</button>
					</span>
		        </div>
		    </div>

		    <div class="card-datatable text-nowrap">
		        <div class="dataTables_wrapper dt-bootstrap5 no-footer">
					<div name="table-responsive" class="table-responsive ox-scroll oy-scroll h-px-500">
						<table name="layoutTable" class="table-layout-fixed datatables-ajax table table-bordered dataTable no-footer">
							<thead class="sticky-thead">
								<tr><th style="width: 80px;">No.</th>
									<th style="width: 160px;">업무명</th>
									<th style="width: 240px;">부서명</th>
									<th style="width: 240px;">파일형식</th>
									<th style="width: 160px;">업체명</th>
									<th style="width: 160px;">업체명(연계)</th>
									<th class="cmn dummy-th"></th>
								</tr>
							</thead>
		                    <tbody name="layoutTbody">
		                    </tbody>
							<template is="curly-brackets" name="layoutRow">
								<tr data-index="DATA-INDEX">
									<td onclick ondblclick class="text-end">ROW_NUM</td>
									<td onclick ondblclick class="text-center">TASK_SE_NM</td>
		                    		<td onclick ondblclick class="text-center">DEPT_NM</td>
		                    		<td onclick ondblclick class="text-center">DATA_FILE_SE_NM</td>
		                    		<td onclick ondblclick class="text-center">ENT_NM</td>
		                    		<td onclick ondblclick class="text-center">LINK_ENT_NM</td>
		                    		<td class="dummy-td cmn"></td>
	                    		</tr>
							</template>
							<template name="layoutNotFound">
								<tr><td valign="top" colspan="7" class="dataTables_empty text-center">
									레이아웃 정보를 찾지 못했습니다.
									</td>
								</tr>
							</template>
						</table>
					</div>
				</div>
		    </div>

		</div>

	</div>

  	<div class="content-backdrop fade"></div>
</div>

<script>
LoadScript("crdn01010Script",wctx.url("/resources/js/fims/crdn/crdn01010.js?ver=${ver}"));

/**************************************************************************
* Global Variable
**************************************************************************/
pageObject["${pageName}"] = newDoctxFinder("${pageName}");

$(document).ready(function(){

	var $P = pageObject["${pageName}"];

	/**************************************************************************
	* DatasetControl, DatasetSupport, FormFields
	**************************************************************************/
	var ctrl = newCrdn01010Control($P);
	ctrl.defaultFetchSize = FETCH_XS;
	ctrl.query = { pageNum : 1, fetchSize : ctrl.defaultFetchSize };

	ctrl.dataset.onDatasetChange = (obj, option) => {
		var t = $P.getGridTemplate();
		var trs = Apply.fromDataset.getTbody(ctrl.dataset, t.found, t.notFound, t.replacer);
		$P.renderLayoutList(obj?.Paging?.totalSize, ctrl.dataset.length, trs, option);

		$P.pagingSupport.setPagingInfo(obj);
	};

	ctrl.dataset.onCurrentChange = (dataItem) => {
		Apply.fromDataset.currentRow(ctrl.dataset, dataItem, $P.findn("layoutTbody"));
	}

	$P.pagingSupport = new FimsPagingSupport({
		doq: $P,
        linkContainer: "[name='layoutPaging']"
	});

	/**************************************************************************
	* pageObject.function
	**************************************************************************/
	$P.fnResetAndChangeBiz = (taskSeCd) => {
		ctrl.urls.load = wctx.url("/crdn/crdn01/010/list.do");
		ctrl.urls.getInfo = wctx.url("/crdn/crdn01/020/info.do");

		$P.fnReset();
	}

	$P.fnReset = () => {
		var searchForm = $P.$findn("frmSearch");
		searchForm.find("input[type='radio']").not("[name='taskSeCd']").prop("checked", false);
		searchForm.find("input[type='checkbox']").prop("checked", false);
		searchForm.find("input[type='hidden']").val("");
		searchForm.find("input[type='text']").val("");

		ctrl.dataset.setData([]);
	}

	$P.getParams = () => {
		var formFields = new FimsFormFields($P.selectorn("frmSearch"));
		var data = formFields.get();
		data.fetchSize = ctrl.defaultFetchSize;

		return data;
	}

	$P.searchLayoutList = () => {
		ctrl.query = $P.getParams();
	    ctrl.load(1);
	}

	$P.scrollLayoutList = () => {
		ctrl.load(ctrl.query.pageNum + 1);
	}

	$P.refreshLayoutList = () => {
		if(ctrl.query.pageNum == null){
			return;
		}

        ctrl.reload({all : true});
    }

	$P.getGridTemplate = () => {
		var notFound = [$P.findn("layoutNotFound").innerHTML];
		var found = $P.findn("layoutRow").innerHTML;
		var replacer = (str, dataItem) => str
			.replace(/{onclick}/gi, "pageObject['${pageName}'].clickLayoutList('{DATA-INDEX}');")
			.replace(/{ondblclick}/gi, "pageObject['${pageName}'].dblclickLayoutList('{FILE_LAYOUT_ID}');");

		return {
			found : found,
			notFound : notFound,
			replacer : replacer
		};
	}

	$P.renderLayoutList = (total, listLength, trs, option) => {

		let noMore = (listLength >= total);
		var initScroll = (ctrl.query.pageNum < 2);
		if(option != null && option.reloaded){
			initScroll = false;
		}

		$P.findn("table-responsive").changeContent(trs, initScroll, noMore);

	    fn_securityModeToggle($("#securityMode--top").is(":checked")); //보안모드
	}

	$P.clickLayoutList = (dataIndex) => {
		if(dataIndex == ""){
			return;
		}

		$P.$findn("layoutTbody").setCurrentRow(dataIndex);

		Apply.toDataset.current(ctrl.dataset, dataIndex);
	}

	$P.dblclickLayoutList = (dataKey) => {
		$P.getInfo(dataKey);
	}

	$P.createLayout = () => {
		$P.getInfo();
	}

	$P.getInfo = (fileLayoutId) => {
		var params = {};
		if(fileLayoutId != null){
			params.fileLayoutId = fileLayoutId;
		}

		ajax.get({
			url : ctrl.urls.getInfo,
			data : params,
			success : (resp) => {

				let dialogName = "layoutInfoDialog";
				let dialogId = dialogName+"-"+uuid();

				dialog.open({
					id : dialogId,
					title : "레이아웃 정보",
					size : "xl",
					content : resp,
					init : () => {
						$("#"+dialogId).attr("name", dialogName);
						$("#"+dialogId).attr("data-ref-doctx","${pageName}");

						AppSupport.setDialogZindex();

						var parentRes = new Object();
						var childReq = pageObject.childReq.pop();

						for(var reqKey in childReq) {
						    if($P.provide[reqKey]){
						    	parentRes[reqKey] = $P.provide[reqKey];
						    } else {
						    	parentRes[reqKey] = function(){};
						    }
						}

						pageObject.parentRes.push(parentRes);

					}
				});

			}
		});
	}

	$P.provide = {
		"refreshList" : function(){
    		$P.refreshLayoutList();
    	},
	};

	/**************************************************************************
	* element.on
	**************************************************************************/

	$P.$findn("btnReset").on('click', () => $P.fnReset());
	$P.$findn("btnSearch").on('click', () => $P.searchLayoutList());
	$P.$findn("btnAdd").on('click', () => $P.createLayout());

	/**************************************************************************
	* 초기화
	**************************************************************************/
	Componentization.fnMakeResizableTable($P.findn("table-responsive"));
	Componentization.fnMakeScrollableTable($P.findn("table-responsive"), $P.scrollLayoutList);

	var defaultBizValue = $("#layout-navbar input[name='taskSeCd']:checked").val();
	$P.$findn("frmSearch").find("input[name='taskSeCd'][value='" + defaultBizValue + "']").prop("checked",true);
	$P.fnResetAndChangeBiz(defaultBizValue);

    fn_securityModeToggle($("#securityMode--top").is(":checked")); //보안모드

    $P.$findn("frmSearch").find("input[name='taskSeCd']").each(function(i) {
		$(this).prop("disabled", "true");
	});
});

</script>