<?= $this->extend('layouts/main') ?>

<?= $this->section('content') ?>
<style>
    th {
        font-size: 11px;
    }

    #resultList tbody tr {
        cursor: pointer;
    }

    .blockUI {
        z-index: 1500 !important;
    }

    .swal2-cancel {
        background-color: #ff0000 !important;
        color: #fff !important;
    }
</style>

<h1>확인매물 일자별실적</h1>

<div class="row">
    <div class="col-md-12 col-xl-12">
        <div class="main-card mb-3 card">
            <div class="card-body">
                <form class="row align-items-end" id="frm_srch_info" onsubmit="return false;">

                    <!-- 1줄 -->
                    <div class="row mb-3">
                        <!-- 기준일자 -->
                        <div class="col-md-3">
                            <label class="form-label mb-1">기준일자</label>
                            <div class="row g-2">
                                <div class="col-5">
                                    <input type="date" class="form-control" id="sdate" name="sdate">
                                </div>
                                <div class="col-2 d-flex align-items-center justify-content-center">~</div>
                                <div class="col-5">
                                    <input type="date" class="form-control" id="edate" name="edate">
                                </div>
                            </div>
                        </div>

                        <div class="col-md-1">
                            <label class="form-label mb-1">검증방식</label>
                            <select class="form-control" name="vrfcreq_way">
                                <option value="">선택</option>
                                <?php foreach ($code_vrfcreq_way as $cd => $cd_nm): ?>
                                    <option value="<?= esc($cd) ?>"><?= esc($cd_nm) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>

                        <div class="col-md-1">
                            <label class="form-label mb-1">매체사(CP)</label>
                            <select class="form-control" name="cp_id">
                                <option value="">선택</option>
                                <?php foreach ($code_cp_id as $cd => $cd_nm): ?>
                                    <option value="<?= esc($cd) ?>"><?= esc($cd_nm) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>

                        <div class="col-md-1 d-grid align-items-end">
                            <button type="button" class="btn btn-primary" id="btnSearch">검색</button>
                        </div>
                    </div>


                </form>

            </div>
        </div>
    </div>


    <div class="col-md-12 col-xl-12">
        <div class="main-card mb-3 card">
            <div class="card-header d-flex align-items-center">
                <h3 class="card-title mb-0">사용자 목록</h3>
                <div class="ms-auto d-flex align-items-center gap-3">
                    <button class="mb-2 me-2 border-0 btn-transition btn btn-shadow btn-outline-success"
                        id="excel-download">
                        <i class="fa fa-fw" aria-hidden="true" title="file-excel-o"></i>엑셀다운로드
                    </button>
                </div>
            </div>
            <div class="card-body">
                <table id="resultList" class="table table-hover table-striped table-bordered">
                    <thead>
                        <tr>
                            <th rowspan="2">일자</th>
                            <th rowspan="2">접수</th>
                            <th rowspan="2">미배정</th>
                            <th rowspan="2">일반확인배정</th>
                            <th rowspan="2">서류확인<br />완료</th>
                            <th rowspan="2">전화확인<br />완료</th>
                            <th rowspan="2">접수취소<br />(홍보확인서<br />미수신)</th>
                            <th colspan="5" style="text-align: center;">전화/서류 확인실패</th>
                            <th rowspan="2">1차 재검증</th>
                            <th rowspan="2">등기부등본<br />확인완료</th>
                            <th colspan="2" style="text-align: center;">등기부등본<br />확인실패</th>
                            <th rowspan="2">검증완료</th>
                        </tr>
                        <tr>
                            <th>개인정보<br />제공동의</th>
                            <th>매물정보</th>
                            <th>가격정보</th>
                            <th>권리자</th>
                            <th>합계</th>
                            <th>매물정보</th>
                            <th>권리자</th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>

                    <tfoot class="table-light">
                        <tr class="fw-bold">
                            <td>합계</td>
                            <td class="sum-rcpt_cnt"></td>
                            <td class="sum-notassign_cnt"></td>
                            <td class="sum-assign_cnt"></td>
                            <td class="sum-paper_cnt"></td>
                            <td class="sum-phone_cnt"></td>
                            <td class="sum-cancel_cnt"></td>
                            <td class="sum-priv_cnt"></td>
                            <td class="sum-info_cnt"></td>
                            <td class="sum-price_cnt"></td>
                            <td class="sum-owner_cnt"></td>
                            <td class="sum-fail_cnt"></td>
                            <td class="sum-retry_cnt"></td>
                            <td class="sum-rgbk_cnt"></td>
                            <td class="sum-reg_info_cnt"></td>
                            <td class="sum-reg_owner_cnt"></td>
                            <td class="sum-complete_cnt"></td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>
</div>


<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.13.6/css/jquery.dataTables.min.css" />
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script defer src="/architectui/assets/js/datatable.kor.js"></script>
<script type="text/javascript">

    // const tpl = document.querySelector('.my-loader-template');

    let date = new Date();

    $(function () {


        let table = $('#resultList').DataTable({
            language: lang_kor,
            processing: true,
            ajax: {
                url: '/m409/m409a/getResultList',
                type: 'GET',
                beforeSend: function () {
                    blockUI.blockPage({
                        message: tpl
                    })
                },
                complete: function () {
                    blockUI.unblockPage()
                },
                data: function (d) {
                    d.sdate = $("#frm_srch_info [name=sdate]").val()
                    d.edate = $("#frm_srch_info [name=edate]").val()
                    d.vrfcreq_way = $("#frm_srch_info [name=vrfcreq_way]").val()
                    d.cp_id = $("#frm_srch_info [name=cp_id]").val()

                    d.start = d.start || 0
                    d.length = d.length || 10
                }
            },
            "columnDefs": [
                { 'targets': '_all', "defaultContent": "" },
                { className: 'text-center', targets: '_all' },
            ],
            columns: [
                { data: 'ref_date', "width": "100px" },
                { data: 'rcpt_cnt' },
                { data: 'notassign_cnt' },
                { data: 'assign_cnt' },
                { data: 'paper_cnt' },
                { data: 'phone_cnt' },
                { data: 'cancel_cnt' },
                { data: 'priv_cnt' },
                { data: 'info_cnt' },
                { data: 'price_cnt' },
                { data: 'owner_cnt' },
                { data: 'fail_cnt' },
                { data: 'retry_cnt' },
                { data: 'rgbk_cnt' },
                { data: 'reg_info_cnt' },
                { data: 'reg_owner_cnt' },
                { data: 'complete_cnt' },
            ],
            // 옵션들 예시
            paging: false,
            searching: false,
            ordering: false,
            serverSide: true,
        });


        $('#resultList').on('xhr.dt', function (e, settings, json) {
            const rows = json?.data || [];

            const fields = [
                'rcpt_cnt', 'notassign_cnt', 'assign_cnt', 'paper_cnt', 'phone_cnt',
                'cancel_cnt', 'priv_cnt', 'info_cnt', 'price_cnt', 'owner_cnt',
                'fail_cnt', 'retry_cnt', 'rgbk_cnt', 'reg_info_cnt', 'reg_owner_cnt',
                'complete_cnt'
            ];

            const toNum = (v) => {
                if (v == null || v === '') return 0;
                return Number(String(v).replace(/,/g, '')) || 0; // "18" 같은 문자열 대응
            };

            // 합계 객체 생성
            const sum = {};
            fields.forEach(f => sum[f] = 0);

            // rows 합산
            rows.forEach(r => {
                fields.forEach(f => sum[f] += toNum(r[f]));
            });

            // footer에 주입
            fields.forEach(f => {
                $('#resultList tfoot .sum-' + f).text(sum[f].toLocaleString());
            });
        });



        $('#resultList tbody').on('click', 'tr', function () {
            const row = table.row(this).data()
            if (!row) return

            // const modalEl = document.getElementById('userModal');
            // const myModal = new bootstrap.Modal(modalEl);

            // $("#frm_user_info")[0].reset()

            // $("#frm_user_info [name=usr_sq]").val(row.usr_sq)
            // $("#frm_user_info [name=type]").val("update")

            // $("#frm_user_info [name=addUserNm]").val(row.usr_nm)
            // $("#frm_user_info [name=addUserDept]").val(row.dept_sq)
            // $("#frm_user_info [name=addUserId]").val(row.usr_id)
            // $("#frm_user_info [name=addUserId]").prop("readonly", true)

            // $("#frm_user_info [name=addUserLevel]").val(row.usr_level)
            // $("#frm_user_info [name=addUserPosition]").val(row.usr_position)
            // $("#frm_user_info [name=addUserTel1]").val(row.usr_tel1)
            // $("#frm_user_info [name=addUserTel2]").val(row.usr_tel2)
            // $("#frm_user_info [name=addSmsYn]").val(row.sms_auth_yn)
            // $("#frm_user_info [name=addUserAddr1]").val(row.usr_addr1)
            // $("#frm_user_info [name=addUserAddr2]").val(row.usr_addr2)
            // $("#frm_user_info [name=addUseYn]").val(row.use_yn)

            // myModal.show();

        });

        // [검색] 버튼 눌렀을 때 다시 조회
        $('#btnSearch').on('click', function () {
            table.ajax.reload()
        });


        // 유저 등록 모달
        $("#addUser").on("click", function () {
            // $("#frm_user_info")[0].reset()

            // $("#frm_user_info [name=usr_sq]").val("")
            // $("#frm_user_info [name=type]").val("create")
            // $("#frm_user_info [name=addUserId]").prop("readonly", false)

            // const modalEl = document.getElementById('userModal');
            // const myModal = new bootstrap.Modal(modalEl);
            // myModal.show();
        });


        // 엑셀다운 click
        $("#excel-download").on("click", function () {

            $.ajax({
                url: "/m409/m409a/excel",
                method: "GET",
                dataType: "json",
                data: $("#frm_srch_info").serialize(),
                beforeSend: function () {
                    blockUI.blockPage({
                        message: tpl
                    })
                },
                complete: function () {
                    blockUI.unblockPage()
                },
                success: function (result) {
                    // downloadExcel(result.data);
                    downloadExcelWithHeader(result.data);
                }
            });

        });

    });

    // 엑셀 다운로드
    function downloadExcelWithHeader(dataRows) {

        /* =======================
         * 1) 헤더
         * ======================= */
        const header1 = [
            "일자",
            "접수",
            "미배정",
            "일반확인배정",
            "서류확인완료",
            "전화확인완료",
            "접수취소(홍보확인서 미수신)",
            "전화/서류 확인실패", "", "", "", "",
            "1차 재검증",
            "등기부등본 확인완료",
            "등기부등본 확인실패", "",
            "검증완료"
        ];

        const header2 = [
            "", "", "", "", "", "", "",
            "개인정보 제공동의",
            "매물정보",
            "가격정보",
            "권리자",
            "합계",
            "",
            "",
            "매물정보",
            "권리자",
            ""
        ];

        /* =======================
         * 2) 바디 (컬럼 순서 정확히)
         * ======================= */
        const body = dataRows.map(r => ([
            r.ref_date,
            r.rcpt_cnt,
            r.notassign_cnt,
            r.assign_cnt,
            r.paper_cnt,
            r.phone_cnt,
            r.cancel_cnt,
            r.priv_cnt,
            r.info_cnt,
            r.price_cnt,
            r.owner_cnt,
            r.fail_cnt,
            r.retry_cnt,
            r.rgbk_cnt,
            r.reg_info_cnt,
            r.reg_owner_cnt,
            r.complete_cnt
        ]));

        const aoa = [header1, header2, ...body];

        const ws = XLSX.utils.aoa_to_sheet(aoa);

        /* =======================
         * 3) 병합 설정
         * ======================= */
        ws["!merges"] = [
            // rowspan 2
            { s: { r: 0, c: 0 }, e: { r: 1, c: 0 } },  // 일자
            { s: { r: 0, c: 1 }, e: { r: 1, c: 1 } },  // 접수
            { s: { r: 0, c: 2 }, e: { r: 1, c: 2 } },  // 미배정
            { s: { r: 0, c: 3 }, e: { r: 1, c: 3 } },
            { s: { r: 0, c: 4 }, e: { r: 1, c: 4 } },
            { s: { r: 0, c: 5 }, e: { r: 1, c: 5 } },
            { s: { r: 0, c: 6 }, e: { r: 1, c: 6 } },
            { s: { r: 0, c: 12 }, e: { r: 1, c: 12 } }, // 1차 재검증
            { s: { r: 0, c: 13 }, e: { r: 1, c: 13 } }, // 등기부 완료
            { s: { r: 0, c: 16 }, e: { r: 1, c: 16 } }, // 검증완료

            // 전화/서류 확인실패 (colspan 5)
            { s: { r: 0, c: 7 }, e: { r: 0, c: 11 } },

            // 등기부등본 확인실패 (colspan 2)
            { s: { r: 0, c: 14 }, e: { r: 0, c: 15 } }
        ];

        /* =======================
         * 4) 컬럼 너비
         * ======================= */
        ws["!cols"] = [
            { wpx: 100 },   // 일자
            { wpx: 50 },
            { wpx: 50 },
            { wpx: 50 },
            { wpx: 50 },
            { wpx: 50 },
            { wpx: 50 },
            { wpx: 50 },
            { wpx: 50 },
            { wpx: 50 },
            { wpx: 50 },
            { wpx: 50 },
            { wpx: 50 },
            { wpx: 50 },
            { wpx: 50 },
            { wpx: 50 },
            { wpx: 50 }
        ];

        /* =======================
         * 5) 저장
         * ======================= */
        const wb = XLSX.utils.book_new();
        XLSX.utils.book_append_sheet(wb, ws, "sheet1");

        XLSX.writeFile(
            wb,
            "확인매물_일자별실적_" + getDateTimeString() + ".xlsx"
        );
    }




    // 엑셀 다운로드
    function downloadExcel(data) {
        const ws = XLSX.utils.json_to_sheet(data);
        ws['!cols'] = [
            { wpx: 80 },
            { wpx: 80 },
            { wpx: 80 },
            { wpx: 80 },
            { wpx: 80 },
            { wpx: 80 },
            { wpx: 80 },
            { wpx: 80 },
            { wpx: 80 },
            { wpx: 80 },
            { wpx: 80 },
            { wpx: 80 },
            { wpx: 80 },
            { wpx: 80 },
            { wpx: 80 },
            { wpx: 80 },
        ];

        const wb = XLSX.utils.book_new();
        XLSX.utils.book_append_sheet(wb, ws, "Sheet1");

        const wbout = XLSX.write(wb, { bookType: 'xlsx', type: 'array' });

        const blob = new Blob([wbout], { type: 'application/octet-stream' });

        const link = document.createElement("a");
        link.href = URL.createObjectURL(blob);
        link.download = "현장확인인원별배정현황" + getDateTimeString() + ".xlsx";
        link.click();
        URL.revokeObjectURL(link.href);
    }

    function getDateTimeString() {
        const d = new Date();
        const yyyy = d.getFullYear();
        const mm = String(d.getMonth() + 1).padStart(2, '0');
        const dd = String(d.getDate()).padStart(2, '0');
        const hh = String(d.getHours()).padStart(2, '0');
        const mi = String(d.getMinutes()).padStart(2, '0');
        const ss = String(d.getSeconds()).padStart(2, '0');
        return `${yyyy}${mm}${dd}${hh}${mi}${ss}`;
    }

    document.addEventListener("DOMContentLoaded", function () {
        const today = new Date().toISOString().slice(0, 10);
        document.getElementById("sdate").value = today;
        document.getElementById("edate").value = today;
    });

</script>
<?= $this->endSection() ?>