<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" session="false"%>
<%@ include file="/WEB-INF/jsp/include/taglib.jsp"%>

<!-- inner page html -->
<div class="content-wrapper">
	<div class="container-xxl flex-grow-1 px-0">
		<c:set var="pageKorName" scope="request">표지 정보 미확인 자료</c:set>
		<div class="card">
			<div class="wrapper-list">
				<div>
				    <div class="container-page-btn">
				        <button type="button" id="btnReset--${pageName}" class="btn btn-outline-dark" title="초기화">초기화</button>
				        <span class="container-window-btn-right">
 							<button type="button" id="btnSearch--${pageName}" class="btn btn-search" title="검색">검색</button>
				        </span>
				    </div>
				    <form id="frmSearch--${pageName}" name="frmSearch">
				        <div class="container-search">
		                    <div class="row g-1">
		                        <div class="col-6">
		                        	<label class="form-label fw-bold form-search-title">단속일자</label>
		                        	<span class="form-search-linebox">
		                                <input type="text" id="schCrdnYmdFrom--${pageName}" name="schCrdnYmdFrom" class="form-control form-date"
		                                	data-fmt-type="day"   title="시작 날짜 선택" />
		                                <button type="button" class="bx bx-sm bx-calendar bg-white"></button>
		                                ~
		                                <input type="text" id="schCrdnYmdTo--${pageName}" name="schCrdnYmdTo" class="form-control form-date"
		                                	data-fmt-type="day" title="종료 날짜 선택">
		                                <button type="button" class="bx bx-sm bx-calendar bg-white"></button>
		                            </span>
		                        </div>
		                        <div class="col-6">
		                        	<input type="hidden" id="by--${pageName}" name="by" />
									<input type="text" id="byOutput--${pageName}" class="form-control" readonly value="동적 검색" />
									<input type="hidden" id="mainOption--${pageName}" name="mainOption" />
									<input type="hidden" id="subOption--${pageName}" name="subOption" />
									<input type="text" id="term--${pageName}" name="term" class="form-control" />
		                        </div>
		                    </div>
				        </div>
				    </form>
				    <div>
				        <span class="container-page-btn">
				        	<div class="d-flex flex-row justify-content-between">
								<label id="crdnPaging--${pageName}PagingInfo" class="dataTables_info" role="status" aria-live="polite"></label>
				                 <ul id="crdnPaging--${pageName}" class="pagination pagination-primary">
				                 </ul>
							</div>
				            <span class="container-window-btn-right">
				                <button type="button" id="btnUpdateToConfirmData--${pageName}" class="btn btn-primary" title="개별 확인 자료로 수정">개별 확인 자료로 수정</button>
				            </span>
				        </span>
				    </div>

				    <div class="card-datatable text-nowrap">
				        <div id="DataTables_Table_0_wrapper--${pageName}" class="dataTables_wrapper dt-bootstrap5 no-footer">
							<div id="table-responsive--${pageName}" class="table-responsive"
								style="overflow-x: scroll;height:600px;overflow-y: scroll;">
								<table id="DataTables_Table_0--${pageName}"
									class="datatables-ajax table table-bordered dataTable no-footer" >
									<thead>
										<tr id="crdnTheadTr--${pageName}"
											data-by="by--${pageName}" data-by-output="byOutput--${pageName}"
											data-main-option="mainOption--${pageName}" data-sub-option="subOption--${pageName}">
											<th style="min-width: 80px;">No.</th>
											<th onclick="searchFromGridTitle('CRDN_INPT_SE_CD',this.innerText,'codeValue','FIM003');"
											style="min-width: 80px;">자료출처</th>
											<th onclick="searchFromGridTitle('CRDN_YMD','위반일자','ymd','');"
											style="min-width: 80px;">위반일시</th>
											<th onclick="searchFromGridTitle('VHRNO',this.innerText,'match','part');"
											style="min-width: 80px;">차량번호</th>
											<th onclick="searchFromGridTitle('CRDN_STDG_NM',this.innerText,'match','perfect');"
											style="min-width: 80px;">법정동</th>
											<th class="text-orange">잔액</th>
											<th onclick="searchFromGridTitle('ATCH_FILE_CNT',this.innerText,'match','perfect');"
											style="min-width: 80px;">사진건수</th>
											<th onclick="searchFromGridTitle('CRDN_SN',this.innerText,'match','perfect');"
											style="min-width: 80px;">발행번호</th>
										</tr>
				                    </thead>
				                    <tbody id="crdnTbody--${pageName}">
									</tbody>
									<template id="crdnRow--${pageName}">
										<tr data-key="{CRDN_ID}">
											<td onclick="{onclick}" ondblclick="{ondblclick}" class="text-end">{ROW_NUM}</td>
				                    		<td onclick="{onclick}" ondblclick="{ondblclick}" class="text-center">{CRDN_INPT_SE_CD}</td>
				                    		<td onclick="{onclick}" ondblclick="{ondblclick}" class="text-center">{CRDN_YMD_TM}</td>
				                    		<td onclick="{onclick}" ondblclick="{ondblclick}" class="text-center">{VHRNO}</td>
				                    		<td onclick="{onclick}" ondblclick="{ondblclick}" class="text-center">{CRDN_STDG_NM}</td>
				                    		<td onclick="{onclick}" ondblclick="{ondblclick}" class="text-end">{BLNC}</td>
				                    		<td onclick="{onclick}" ondblclick="{ondblclick}" class="text-end">{ATCH_FILE_CNT}</td>
				                    		<td onclick="{onclick}" ondblclick="{ondblclick}" class="text-end">{CRDN_SN}</td>
										</tr>
									</template>
									<template id="crdnNotFound--${pageName}">
										<tr>
			                    			<td valign="top" colspan="9" class="dataTables_empty text-center">단속 정보를 찾지 못했습니다.</td>
			                    		</tr>
									</template>
								</table>
							</div>
						</div>
				    </div>
				</div>
			</div>
		</div>

	</div>

	<jsp:include page="/WEB-INF/jsp/include/bottom.jsp" />

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


<script>

/**************************************************************************
 * Global Variable
 **************************************************************************/
var FIM003 = new CommonCodes(${FIM003});
var FIM010 = new CommonCodes(${FIM010});
pageObject["${pageName}"] = {};


$(document).ready(function(){

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

	/**************************************************************************
	 * DatasetControl
	 **************************************************************************/
	$P.crdnControl = new DatasetControl({
	    urls : {
	        load : wctx.url("/DPV/crdn/crdn03/030/list.do"),
	        getInfo : wctx.url("/DPV/sprt/sprt02/010/main.do"),
	        update: wctx.url("/DPV/crdn/crdn03/030/update.do")
	    },
	    dataGetter : obj => obj["List"], appendData:true,
	    keymapper : info => info ? info.CRDN_ID : "",
	    formats: {
	    	CRDN_INPT_SE_CD : FIM003,
	    	CRDN_YMD_TM 	: datetimeFormat,
	    	FFNLG_CRDN_AMT 	: numberFormat,
	    	CRDN_STTS_CD 	: FIM010,
	    	BLNC 			: numberFormat
		}
	});
	$P.crdnControl.defaultFetchSize = FETCH_XS;
	$P.crdnControl.untilPageNum = 0;
	$P.crdnControl.beforeCurrent = null;
	$P.crdnControl.tableRenderComplete = false;

	/**************************************************************************
	 * DatasetControl.on
	 **************************************************************************/
	$P.crdnControl.onDatasetChange = (obj) => {

		$P.renderCrdnList(obj["Total"]);

	    $("#crdnPaging--${pageName}").setPagingInfo({
	        list: $P.crdnControl.dataset,
	        prefix: "crdnPaging--${pageName}",
	        start: obj["Start"],
	        totalSize: obj["Total"],
	        fetchSize: obj["Fetch"],
	        func: "pageObject['${pageName}'].crdnControl.load({index})"
	    });

	    $P.crdnControl.tableRenderComplete = true;
	};

	$P.crdnControl.onCurrentChange = (item) => {

		if(!$P.crdnControl.tableRenderComplete){
			return;
		}

		$P.crdnControl.tableRenderComplete = false;
		if(!item){
			$P.clickCrdnList("", true);
		} else {
			$P.clickCrdnList($P.crdnControl.getCurrent()["CRDN_ID"], true);
		}
	};

	/**************************************************************************
	 * pageObject.function
	 **************************************************************************/
	$P.refreshCrdnList = () => {
		$P.crdnControl.untilPageNum = $P.crdnControl.query.pageNum;
	    $P.crdnControl.query.fetchSize = $P.crdnControl.defaultFetchSize * $P.crdnControl.query.pageNum;
	    $P.crdnControl.load(1);
	}

	 $P.searchCrdnList = () => {
		$P.crdnControl.query = $P.getParams();
	    $P.crdnControl.load(1);
	}

	$P.scrollEnd = (obj) => {
		if(obj.dataset.scrollStatus){
			return;
		}

		var el = $(obj);
    	if(el.scrollTop() == 0){
    		return;
    	}

    	if((el[0].scrollHeight - el.scrollTop() + VERTICAL_SCROLL_HEIGHT) <= el.outerHeight()){
    		obj.dataset.scrollStatus = "waiting";
    		$P.scrollCrdnList();
    	}
	}

	$P.scrollCrdnList = () => {
		$P.crdnControl.load($P.crdnControl.query.pageNum + 1);
	}

	$P.clickCrdnList = (dataKey, auto) => {
		if(dataKey == ""){
			return;
		}

		$("#crdnTbody--${pageName}").setCurrentRow(dataKey);

		if(!auto){
			$P.crdnControl.setCurrent(dataKey);
		}
	}

	$P.dblclickCrdnList = (dataKey) => {
		let params = {
			  callControlName : "pageObject['${pageName}'].crdnControl"
			, crdnId : dataKey
		}

		ajax.get({
			url : $P.crdnControl.urls.getInfo,
			data : params,
			success : resp => {

				dialog.open({
					id : "totalDialog--${pageName}",
					title : "개별총정보", size : "xxl", content : resp,
					init : () => {}
				});

			}
		});

	}

	$P.renderCrdnList = (total) => {

	    var crdnList = $P.crdnControl.dataset;
		var empty = crdnList.empty;
		var notFound = [document.getElementById("crdnNotFound--${pageName}").innerHTML];
		var found = document.getElementById("crdnRow--${pageName}").innerHTML;
		var replacer = (str, dataItem) => str
        	.replace(/{onclick}/gi, "pageObject['${pageName}'].clickCrdnList('" + dataItem.getValue("CRDN_ID") + "');")
    		.replace(/{ondblclick}/gi, "pageObject['${pageName}'].dblclickCrdnList('" + dataItem.getValue("CRDN_ID") + "');");

		var trs = empty ? notFound : crdnList.inStrings(found,replacer);
		$("#crdnTbody--${pageName}").html(trs.join());

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

	    if(total == crdnList.length){
			$("#table-responsive--${pageName}").attr("data-scroll-status", "complete");
		} else {
			$("#table-responsive--${pageName}").removeAttr("data-scroll-status");
		}

	    if($P.crdnControl.untilPageNum != 0){
			$P.crdnControl.query.fetchSize = $P.crdnControl.defaultFetchSize;
			$P.crdnControl.query.pageNum = $P.crdnControl.untilPageNum;
			$P.crdnControl.untilPageNum = 0;
		}
	}

	$P.getParams = () => {
		var formFields = new FimsFormFields("#frmSearch--${pageName}");
		var data = formFields.get();
		data.taskSeCd = "DPV";
		data.fetchSize = $P.crdnControl.defaultFetchSize;

		return data;
	}

	$P.fnReset = () => {
		var searchForm = $("#frmSearch--${pageName}");
		searchForm.find("input[type='radio']").not("[name='taskSeCd']").prop("checked", false);
		searchForm.find("input[type='checkbox']").prop("checked", false);
		searchForm.find("input[type='text']").val("");
		searchForm.find("input[type='hidden']").val("");
		searchForm.find("select").each(function(){ $(this).find("option:eq(0)").prop("selected", true); });

		$("#byOutput--${pageName}").val("동적 검색");
		$('#schCrdnYmdFrom--${pageName}').datepicker('setDate', DateUtil.getDateDay(-5475).date);
	    $('#schCrdnYmdTo--${pageName}').datepicker('setDate', new Date());
	    $P.crdnControl.setData([]);
	}

	/**************************************************************************
	 * element.on
	 **************************************************************************/
    $('#btnReset--${pageName}').on('click', () => $P.fnReset());
    $('#btnSearch--${pageName}').on('click', () => $P.searchCrdnList());
    $("#table-responsive--${pageName}").scroll(function(){ $P.scrollEnd(this); });
    $('#btnUpdateToConfirmData--${pageName}').on('click', () => {

    	if(!$P.crdnControl.getCurrent()) {
    		dialog.alert({content:"선택된 자료가 없습니다.",timeout: 0, onOK:()=>{}});
    		return;
    	};

    	dialog.alert({
    		content:"현 자료를 장애차량 확인된 자료로 변경하시겠습니까?",
    		onOK:() => {
    			$P.crdnControl.save({ 'crdnId' : $P.crdnControl.getCurrent()["CRDN_ID"] });
    		}
    	});
    });

    /**************************************************************************
	 * 초기화
	 **************************************************************************/
  	//달력 초기화
	initDatepicker("frmSearch--${pageName}");

    $P.fnReset();

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

});

</script>