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

<!-- Page Body -->
<div class="content-pop" data-doctx="${pageName}">
    <div class="d-flex flex-row justify-content-evenly">
        <!-- 발송 정보 -->
        <div class="col-xl-12 mx-1">
            <div class="card">
                <!-- 업무 버튼 -->
                <div class="container-page-btn">
                    <label class="col-form-label bg-label-danger pe-2 w-px-120 text-sm-center">발송 이력</label>
                    <!-- 버튼 우측 정렬 -->
                    <span class="container-window-btn-right">
                        <button type="button" class="btn btn-primary w-px-120" name="btnCreateSndbk" title="개별 반송 등록" disabled>
                            반송 등록
                        </button>
                        <button type="button" class="btn btn-primary w-px-120" name="btnUpdateSndbk" title="개별 반송 수정" disabled>
                            반송 수정
                        </button>
                        <button type="button" class="btn btn-primary w-px-120" name="btnRemoveSndbk" title="개별 반송 삭제" disabled>
                            반송 삭제
                        </button>
                        <button type="button" class="btn btn-primary w-px-120" name="btnViewPostTrace" title="우편 종적 보기" disabled>
                            우편 종적 보기
                        </button>
                    </span> <!-- <span class="container-window-btn-right"> -->
                </div> <!-- <div class="container-page-btn"> -->

                <div class="row g-1">
                    <!-- DataTables(그리드) -->
                    <div class="card-datatable text-nowrap">
                        <div class="dataTables_wrapper dt-bootstrap5 no-footer">
                            <div class="table-responsive ox-scroll oy-scroll h-px-300" name="tableRspnsSndngDtl">
                                <table class="table-layout-fixed dataTable datatables-ajax table table-bordered no-footer" aria-describedby="DataTables_Table_0_info">
                                    <thead class="sticky-thead">
                                        <tr name="theadTrSndngDtl" data-search-target="[data-doctx='${pageName}']">
                                            <th class="cmn sorting" style="width: 120px" data-field="SNDNG_DTL_STTS_NM">발송상태</th>
                                            <th class="cmn sorting" style="width: 100px" data-field="SNDNG_RCVMT_NM">수납</th>
                                            <th class="cmn sorting" style="width: 100px" data-field="DIV_KB_NM">취급구분</th>
                                            <th class="cmn sorting" style="width: 140px" data-field="SNDNG_SE_NM">발송구분</th>
                                            <th class="cmn sorting" style="width: 120px" data-field="SNDNG_YMD">발송일자</th>
                                            <th class="cmn sorting" style="width: 120px" data-field="DUDT_YMD">납기일자</th>
                                            <th class="cmn sorting" style="width: 160px" data-field="RG_NO">등기번호</th>
                                            <th class="cmn sorting" style="width: 200px" data-field="RCPN_NM">성명</th>
                                            <th class="cmn sorting" style="width: 100px" data-field="RCPN_ZIP">우편번호</th>
                                            <th class="cmn sorting" style="width: 240px" data-field="RCPN_ADDR">주소</th>
                                            <th class="cmn sorting" style="width: 280px" data-field="RCPN_DTL_ADDR">상세주소</th>
                                            <th class="cmn sorting" style="width: 100px" data-field="FYR">회계연도</th>
                                            <th class="cmn sorting" style="width: 120px" data-field="LEVY_NO">부과번호</th>
                                            <th class="cmn sorting" style="width: 120px" data-field="LAST_AMT">최종금액</th>
                                            <th class="cmn sorting" style="width: 120px" data-field="DLVR_YMD">배달일자</th>
                                            <th class="cmn sorting" style="width: 100px" data-field="DLVR_TM">배달시각</th>
                                            <th class="cmn sorting" style="width: 160px" data-field="UNDLVR_RSN_NM">미배달사유</th>
                                            <th class="cmn sorting" style="width: 120px" data-field="ACTL_RCPN_NM">수령인</th>
                                            <th class="cmn sorting" style="width: 160px" data-field="RCPN_REL_NM">수령인관계</th>
                                            <th class="cmn sorting" style="width: 160px" data-field="SNDNG_DTL_ID">발송상세ID</th>
                                            <th class="cmn sorting" style="width: 180px" data-field="REG_DT">등록일시</th>
                                            <th class="cmn sorting" style="width: 140px" data-field="RGTR_NM">등록사용자</th>
                                            <th class="cmn sorting" style="width: 180px" data-field="MDFCN_DT">수정일시</th>
                                            <th class="cmn sorting" style="width: 140px" data-field="MDFR_NM">수정사용자</th>
                                            <th class="cmn dummy-th"></th>
                                        </tr>
                                    </thead>
                                    <tbody name="tbodySndngDtlList">
                                    </tbody>
                                    <template is="curly-brackets" name="tmpltSndngDtlRows">
                                        <tr data-key="{data-index}">
                                            <td class="text-center" onclick>{SNDNG_DTL_STTS_NM}</td>
                                            <td class="text-center" onclick>{SNDNG_RCVMT_NM}</td>
                                            <td class="text-center" onclick>{DIV_KB_NM}</td>
                                            <td class="text-center" onclick>{SNDNG_SE_NM}</td>
                                            <td class="text-center" onclick>{SNDNG_YMD}</td>
                                            <td class="text-center" onclick>{DUDT_YMD}</td>
                                            <td class="text-center" onclick>{RG_NO}</td>
                                            <td class="text-start"  onclick>{RCPN_NM}</td>
                                            <td class="text-center" onclick>{RCPN_ZIP}</td>
                                            <td class="text-start"  onclick>{RCPN_ADDR}</td>
                                            <td class="text-start"  onclick>{RCPN_DTL_ADDR}</td>
                                            <td class="text-center" onclick>{FYR}</td>
                                            <td class="text-center" onclick>{LEVY_NO}</td>
                                            <td class="text-end"    onclick>{LAST_AMT}</td>
                                            <td class="text-center" onclick>{DLVR_YMD}</td>
                                            <td class="text-center" onclick>{DLVR_TM}</td>
                                            <td class="text-start"  onclick>{UNDLVR_RSN_NM}</td>
                                            <td class="text-start"  onclick>{ACTL_RCPN_NM}</td>
                                            <td class="text-center" onclick>{RCPN_REL_NM}</td>
                                            <td class="text-center" onclick>{SNDNG_DTL_ID}</td>
                                            <td class="text-center" onclick>{REG_DT}</td>
                                            <td class="text-center" onclick>{RGTR_NM}</td>
                                            <td class="text-center" onclick>{MDFCN_DT}</td>
                                            <td class="text-center" onclick>{MDFR_NM}</td>
                                            <td class="cmn dummy-td"></td>
                                        </tr>
                                    </template>
                                    <template name="tmpltNotFoundSndngDtl">
                                        <tr class="odd">
                                            <td valign="top" colspan="25" class="dataTables_empty text-center">발송 정보를 찾지 못했습니다.</td>
                                        </tr>
                                    </template>
                                </table>
                            </div>
                        </div>
                    </div> <!-- DataTables(그리드) -->
                </div> <!-- <div class="row g-1"> -->
            </div> <!-- <div class="card"> -->
        </div> <!-- <div class="col-xl-12 mx-1"> -->
        <!-- 발송 정보 -->
    </div> <!-- <div class="d-flex flex-row justify-content-evenly"> -->

    <div class="d-flex flex-row justify-content-evenly">
        <!-- 가상계좌 정보 -->
        <div class="col-xl-3 mx-1">
            <!-- 입력 영역 -->
            <form name="frmEditSndngDtl">
                <!-- hidden -->
                <input type="hidden" name="sndngDtlId" data-map="SNDNG_DTL_ID" />
                <input type="hidden" name="sndngId" data-map="SNDNG_ID" />

                <div class="card">
                    <!-- 업무 버튼 -->
                    <div class="container-page-btn">
                        <label class="col-form-label bg-label-danger pe-2 w-px-120 text-sm-center">가상계좌 정보</label>
                    </div> <!-- <div class="container-page-btn"> -->
                    <div class="row g-1">
                        <!-- 가상계좌 -->
                        <div class="col-md-12">
                            <textarea is="id-textarea" type="text" class="form-control w-100" name="vrActno" cols="100%" rows="10" readonly></textarea>
                        </div>
                        <!-- 전자납부번호 -->
                        <div class="col-md-12">
                            <label is="name-label" class="col-form-label bg-lighter pe-2 w-px-120 text-sm-end" for="epaynoMask">전자납부번호</label>
                            <input is="id-input" type="text" class="form-control w-65" name="epaynoMask" data-map="EPAYNO_MASK" readonly />
                        </div>
                    </div>
                </div> <!-- <div class="card"> -->
            </form>
        </div> <!-- <div class="col-xl-3 mx-1"> -->
        <!-- 가상계좌 정보 -->

        <!-- 공시송달 이력 -->
        <div class="col-xl-9 mx-1">
            <div class="card">
                <!-- 업무 버튼 -->
                <div class="container-page-btn">
                    <label class="col-form-label bg-label-danger pe-2 w-px-120 text-sm-center">공시송달 이력</label>
                </div>
                <div class="row g-1">
                    <!-- DataTables(그리드) -->
                    <div class="card-datatable text-nowrap">
                        <div class="dataTables_wrapper dt-bootstrap5 no-footer">
                            <div class="table-responsive ox-scroll oy-scroll h-px-250" name="tableRspnsSvbtc">
                                <table class="table-layout-fixed dataTable datatables-ajax table table-bordered no-footer" aria-describedby="DataTables_Table_0_info">
                                    <thead class="sticky-thead">
                                        <tr name="theadTrSvbtc" data-search-target="[data-doctx='${pageName}']">
                                            <th class="cmn sorting" style="width: 140px" data-field="SNDNG_SE_NM">발송구분</th>
                                            <th class="cmn sorting" style="width: 120px" data-field="SNDNG_YMD">발송일자</th>
                                            <th class="cmn sorting" style="width: 120px" data-field="SVBTC_YMD">공시송달일자</th>
                                            <th class="cmn sorting" style="width: 180px" data-field="DOC_NO">문서번호</th>
                                            <th class="cmn sorting" style="width: 300px" data-field="TTL_NM">제목</th>
                                            <th class="cmn sorting" style="width: 300px" data-field="ETC_CN">기타내용</th>
                                            <th class="cmn sorting" style="width: 200px" data-field="SNDNG_DTL_ID">발송상세ID</th>
                                            <th class="cmn sorting" style="width: 180px" data-field="REG_DT">등록일시</th>
                                            <th class="cmn sorting" style="width: 140px" data-field="RGTR_NM">등록사용자</th>
                                            <th class="cmn dummy-th"></th>
                                        </tr>
                                    </thead>
                                    <tbody name="tbodySvbtcList">
                                    </tbody>
                                    <template is="curly-brackets" name="tmpltSvbtcRows">
                                        <tr data-key="{data-index}">
                                            <td class="text-center" onclick>{SNDNG_SE_NM}</td>
                                            <td class="text-center" onclick>{SNDNG_YMD}</td>
                                            <td class="text-center" onclick>{SVBTC_YMD}</td>
                                            <td class="text-start"  onclick>{DOC_NO}</td>
                                            <td class="text-start"  onclick>{TTL_NM}</td>
                                            <td class="text-start"  onclick>{ETC_CN}</td>
                                            <td class="text-center" onclick>{SNDNG_DTL_ID}</td>
                                            <td class="text-center" onclick>{REG_DT}</td>
                                            <td class="text-center" onclick>{RGTR_NM}</td>
                                            <td class="cmn dummy-td"></td>
                                        </tr>
                                    </template>
                                    <template name="tmpltNotFoundSvbtc">
                                        <tr class="odd">
                                            <td valign="top" colspan="10" class="dataTables_empty text-center">공시송달 정보를 찾지 못했습니다.</td>
                                        </tr>
                                    </template>
                                </table>
                            </div>
                        </div>
                    </div> <!-- DataTables(그리드) -->
                </div> <!-- <div class="row g-1"> -->
            </div> <!-- <div class="card"> -->
        </div> <!-- <div class="col-xl-9 mx-1"> -->
        <!-- 공시송달 이력 -->
    </div> <!-- <div class="d-flex flex-row justify-content-evenly"> -->
</div> <!-- <div class="content-pop"> -->

<script>
    LoadScript("sprt02040Script", wctx.url("/resources/js/fims/sprt/sprt02040.js?ver=${ver}"));

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

    /**************************************************************************
     * script 진입
     **************************************************************************/
    $(document).ready(function() {
        // pageObject
        let $P = pageObject["${pageName}"];

        // datasetControl
        $P.ctrlSndngDtl = newSprt02040ControlSD("${pageName}", "[data-doctx=${pageName}]", "${pageNameMain}");
        $P.ctrlSvbtc = newSprt02040ControlSV("${pageName}", "[data-doctx=${pageName}]", "${pageNameMain}");

        /**************************************************************************
         * 사용자 함수(function)
         **************************************************************************/
        // 정보 새로고침
        $P.fnRefresh = () => {
            let ctrlMain = pageObject["${pageNameMain}"].ctrlMain;

            // 단속 ID 가 없다면.. return
            let crdnId = ctrlMain.getValue("CRDN_ID");                          // 단속 ID
            if (!crdnId) return;

            let params = {
                  callPurpose: "refresh"
                , sggCd: ctrlMain.getValue("SGG_CD")
                , taskSeCd: ctrlMain.getValue("TASK_SE_CD")
                , crdnId: ctrlMain.getValue("CRDN_ID")
                , delYn: "N"
            };

            ajax.get({
                  url: wctx.url("/sprt/sprt02/040/info.do")
                , headers: { Accept: "application/json; charset=utf-8" }        // json
                , data: params || {}
                , success: (resp) => {
                    // 초기화
                    $P.findn("frmEditSndngDtl").reset();                        // 발송상세 화면 초기화
                    $P.ctrlSndngDtl.dataset.clear();                            // 발송상세 Dataset 초기화
                    $P.ctrlSvbtc.dataset.clear();                               // 공시송달 Dataset 초기화

                    // sprt02010-main 단속 정보
                    ctrlMain.setData([resp.totalInfo]);

                    // 발송 이력 정보
                    $P.ctrlSndngDtl.setData(resp.sndngDtls);
                    // 공시송달 이력 정보
                    $P.ctrlSvbtc.setData(resp.svbtcDtls);
                }
            });
        }

        /**************************************************************************
         * 버튼 clickEvent
         **************************************************************************/
        // 개별 반송 등록
        $P.fnCreateSndbk = (btnTitle) => {
            // 발송상세 ID 가 없다면.. return
            let sndngDtlId = $P.ctrlSndngDtl.getValue("SNDNG_DTL_ID");          // 발송 상세 ID
            if (!sndngDtlId) return;

            // 미배달 사유 확인
            let undlvrRsnNm = $P.ctrlSndngDtl.getValue("UNDLVR_RSN_NM");        // 미배달 사유 명
            if (undlvrRsnNm)
            	return dialog.alert({
                      content: "미배달 사유가 " + undlvrRsnNm + " (으)로 반송 등록 되어 있습니다."
                            + "<br><br>" + "[" + btnTitle + "]" + " 실행이 취소되었습니다."
                    , init: () => { AppSupport.setDialogZindex(); }
                    , onOK: () => { }
                });

            // 발송 상세 상태 코드
            let sndngDtlSttsCd = $P.ctrlSndngDtl.getValue("SNDNG_DTL_STTS_CD");
            let sndngDtlSttsNm = $P.ctrlSndngDtl.getValue("SNDNG_DTL_STTS_NM");

            // 발송상세 상태코드 확인 (FIM050 - 03:발송 04:발송확인)
            if (sndngDtlSttsCd != "03" && sndngDtlSttsCd != "04")
            	return dialog.alert({
                      content: "[" + btnTitle + "]" + " 작업은 발송상세 상태가 '발송' 또는 '발송확인' 자료만 처리 가능합니다."
                            + "<br>" + "발송상세상태 : " + sndngDtlSttsNm
                            + "<br><br>" + "[" + btnTitle + "]" + " 실행이 취소되었습니다."
                    , init: () => { AppSupport.setDialogZindex(); }
                    , onOK: () => { }
                });

            // 단속 ID 가 없다면.. return
            let ctrlMain = pageObject["${pageNameMain}"].ctrlMain;              // sprt02010 control
            let crdnId = ctrlMain.getValue("CRDN_ID");                          // 단속 ID
            if (!crdnId) return;

            let params = {
                  callPurpose: "create"
                , btnTitle: btnTitle
                , sggCd: ctrlMain.getValue("SGG_CD")                            // 시군구 코드
                , taskSeCd: ctrlMain.getValue("TASK_SE_CD")                     // 업무 구분 코드
                , sndngDtlId: $P.ctrlSndngDtl.getValue("SNDNG_DTL_ID")          // 발송 상세 ID
                , delYn: "N"
            };

            $P.ctrlSndngDtl.getInfo(params);
        }

        // 개별 반송 수정
        $P.fnUpdateSndbk = (btnTitle) => {
            // 발송상세ID 가 없다면.. return
        	let sndngDtlId = $P.ctrlSndngDtl.getValue("SNDNG_DTL_ID");          // 발송 상세 ID
            if (!sndngDtlId) return;

            // 미배달 사유 코드
            let undlvrRsnCd = $P.ctrlSndngDtl.getValue("UNDLVR_RSN_CD");
            let undlvrRsnNm = $P.ctrlSndngDtl.getValue("UNDLVR_RSN_NM");

            // 발송상세 상태코드 확인 (FIM050 - 05:반송)
            if (!undlvrRsnNm)
            	return dialog.alert({
                      content: "조회된 반송(미배달) 사유가 없습니다."
                            + "<br><br>" + "[" + btnTitle + "]" + " 실행이 취소되었습니다."
                    , init: () => { AppSupport.setDialogZindex(); }
                    , onOK: () => { }
                });

            let params = {
                  callPurpose: "update"
                , title: btnTitle
                , sggCd: $P.ctrlSndngDtl.getValue("SGG_CD")                     // 시군구 코드
                , taskSeCd: $P.ctrlSndngDtl.getValue("TASK_SE_CD")              // 업무 구분 코드
                , sndngDtlId: $P.ctrlSndngDtl.getValue("SNDNG_DTL_ID")          // 발송 상세 ID
                , sndngDtlSttsCd: $P.ctrlSndngDtl.getValue("SNDNG_DTL_STTS_CD") // 발송 상세 상태 코드
                , undlvrRsnNm: $P.ctrlSndngDtl.getValue("UNDLVR_RSN_NM")        // 미배달 사유 명
                , delYn : "N"
            };

            $P.ctrlSndngDtl.getInfo(params);
        }

        // 개별 반송 삭제
        $P.fnRemoveSndbk = (btnTitle) => {
            // 발송상세ID 가 없다면.. return
            let sndngDtlId = $P.ctrlSndngDtl.getValue("SNDNG_DTL_ID");          // 발송 상세 ID
            if (!sndngDtlId) return;

            // 발송상세 상태코드 확인 (FIM050 - 05:반송)
            let sndngDtlSttsCd = $P.ctrlSndngDtl.getValue("SNDNG_DTL_STTS_CD"); // 발송 상세 상태 코드
            let sndngDtlSttsNm = $P.ctrlSndngDtl.getValue("SNDNG_DTL_STTS_NM"); // 발송 상세 상태 명

            if (sndngDtlSttsCd != "05")
            	return dialog.alert({
                      content: "[" + btnTitle + "]" + " 작업은 발송상세 상태가 '반송' 자료만 처리 가능합니다."
                            + "<br>" + "발송상세상태 : " + sndngDtlSttsNm
                            + "<br><br>" + "[" + btnTitle + "]" + " 실행이 취소되었습니다."
                    , init: () => { AppSupport.setDialogZindex(); }
                    , onOK: () => { }
                });

            dialog.alert({
                  content: "선택한 반송 정보를 삭제하시겠습니까?"
                , init: () => { AppSupport.setDialogZindex(); }
                , onOK: () => {
                    let params = {
                          btnTitle: btnTitle
                        , callPurpose: "delete"
                        , sggCd: $P.ctrlSndngDtl.getValue("SGG_CD")             // 시군구 코드
                        , taskSeCd: $P.ctrlSndngDtl.getValue("TASK_SE_CD")      // 업무 구분 코드
                        , sndngDtlId: $P.ctrlSndngDtl.getValue("SNDNG_DTL_ID")  // 발송 상세 ID
                        , sndngDtlSttsCd: "03"                                  // 발송 상세 상태 코드(SNDNG_DTL_STTS_CD) FIM050 - 03 발송
                    };

                    $P.ctrlSndngDtl.remove(params);
                }
            });
        }

        // 우편 종적 보기
        $P.fnViewPostTrace = () => {
            let divKb = $P.ctrlSndngDtl.getValue("DIV_KB");                     // 취급 구분
            let rgNo = $P.ctrlSndngDtl.getValue("RG_NO");                       // 등기번호

            // 취급 구분이 일반이라면..
            if (typeof divKb == "undefined" || divKb == null || divKb == "" || divKb == "000") return;
            // 등기번호가 없다면..
            if (typeof rgNo == "undefined" || rgNo == null || rgNo == "") return;

            window.open("https://service.epost.go.kr/trace.RetrieveRegiPrclDeliv.postal?sid1=" + rgNo+"&displayHeader=N", "trace_view", "");
        }

        /**************************************************************************
         * 초기 셋팅
         **************************************************************************/
        // 이벤트 설정
        $P.setEvent = () => {
            // 업무 버튼 이벤트
            $P.$findn("btnCreateSndbk").on("click", function() {
                $P.fnCreateSndbk($P.$findn("btnCreateSndbk").attr("title"));        // 개별 반송 등록
            });
            $P.$findn("btnUpdateSndbk").on("click", function() {
                $P.fnUpdateSndbk($P.$findn("btnUpdateSndbk").attr("title"));        // 개별 반송 수정
            });
            $P.$findn("btnRemoveSndbk").on("click", function() {
                $P.fnRemoveSndbk($P.$findn("btnRemoveSndbk").attr("title"));        // 개별 반송 삭제
            });
            $P.$findn("btnViewPostTrace").on("click", function() {
                $P.fnViewPostTrace($P.$findn("btnViewPostTrace").attr("title"));    // 개별 반송 등록
            });

            // DataTables width 변경 조정
            Componentization.fnMakeResizableTable($P.findn("tableRspnsSndngDtl"));
            Componentization.fnMakeResizableTable($P.findn("tableRspnsSvbtc"));
        }

        // 초기 화면 설정
        $P.fnReset = () => {
            // 초기화
            $P.findn("frmEditSndngDtl").reset();                                // 발송상세 화면 초기화
            $P.ctrlSndngDtl.dataset.clear();                                    // 발송상세 Dataset 초기화
            $P.ctrlSvbtc.dataset.clear();                                       // 공시송달 Dataset 초기화

            // 화면 호출(callPurpose)이 view가 아니라면 버튼 활성화 disabled = false
            if (pageObject["${pageNameMain}"].callPurpose != "view") {
                $P.$findn("btnCreateSndbk").prop("disabled", false);
                $P.$findn("btnUpdateSndbk").prop("disabled", false);
                $P.$findn("btnRemoveSndbk").prop("disabled", false);
                $P.$findn("btnViewPostTrace").prop("disabled", false);
            }
        }

        /**************************************************************************
         * 최초 실행 함수
         **************************************************************************/
        // 1. 이벤트 설정
        $P.setEvent();

        // 2. 초기 화면 설정
        $P.fnReset();

        // 3. Dataset 설정
        $P.ctrlSndngDtl.setData(${sndngDtls});                                  // 발송상세 이력
        $P.ctrlSvbtc.setData(${svbtcDtls});                                     // 공시송달 이력
    });

</script>
