<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>
<link rel="stylesheet" href="/common/css/custom.css">
<style>
    table th {
        vertical-align: middle;
        line-height: 1.2;
    }

    .tbl_basic2 th {
        padding: 0 10px;
        height: 27px;
        line-height: 27px;
        vertical-align: middle;
        border: solid 1px #d8d9de;
        background-color: #eff0f4;
        letter-spacing: -1px;
        font-weight: normal;
        color: #5a5f69;
        text-align: left;
    }

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

    .ellipsis {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 180px;
    }

    /* GLightbox z-index 설정 */
    .glightbox-container {
        z-index: 9999 !important;
    }

    .table-scroll {
        max-height: 300px;
        overflow-y: scroll;
        min-height: 100px;
    }

    .swal2-cancel {
        background-color: #ff0000 !important;
        color: #fff !important;
    }

    .num {
        color: #b68556;
    }

    .table th,
    .table td {
        vertical-align: top;
        box-sizing: border-box;
    }
</style>

<div class="col-md-12 col-xl-12">
    <div class="col-lg-12">
        <div class="main-card mb-2 card">
            <div class="card-header bg-white border-bottom shadow-sm">
                <div class="d-flex flex-wrap align-items-center w-100 justify-content-between card-header-tab">
                    <h4 class="mb-0 fw-bold text-dark">홍보확인서 상세 내용</h4>
                    <div class="d-flex align-items-center flex-nowrap gap-4 ms-auto" style="white-space:nowrap;">
                        <?php if (($data['receiver'] ?? '') != "API"): ?>
                            <span class="text-muted me-2">발신번호:</span>
                            <span class="fw-bold text-primary fs-6 me-4"><?= esc(str_replace('-', '', $data['caller_no'] ?? '')) ?></span>
                        <?php endif; ?>
                        <span class="text-muted me-2">팩스 순번:</span>
                        <span class="fw-bold text-primary fs-6 me-4"><?= $data['fax_sq'] ?></span>
                        <?php if (($data['receiver'] ?? '') != "API"): ?>
                            <button type="button" class="btn btn-sm btn-outline-secondary"
                                onclick="faximage_rotate(90)">90˚</button>
                            <button type="button" class="btn btn-sm btn-outline-secondary"
                                onclick="faximage_rotate(180)">180˚</button>
                            <button type="button" class="btn btn-sm btn-outline-secondary"
                                onclick="faximage_rotate(270)">270˚</button>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            <div class="card-body">
                <!-- table 유지 + 반응형 -->
                <div class="table-responsive">
                    <table class="table table-bordered table-sm tbl_basic2 align-middle mb-0" style="min-width: 900px;">
                        <colgroup>
                            <col style="width: 60%;">
                            <col style="width: 40%;">
                        </colgroup>

                        <tbody>
                            <tr>
                                <!-- 좌: 이미지/문서 -->
                                <td class="p-2">
                                    <?php
                                    $arrImagePath = [
                                        $_SERVER['DOCUMENT_ROOT'] . '/upload/',
                                        '/home/confirms/test-admin.confirms.co.kr/upload/',
                                        '/home/confirms/upload/',
                                        '/home/www/admin.confirms.co.kr/upload/',
                                        '/home/www/upload/',
                                        '/image/confirms_upload/',
                                        '/misc/image/confirms_upload/',
                                        '/storage/web/admin.confirms.co.kr/src/upload/',
                                        '/storage/web/admin.confirms.co.kr/upload/',
                                    ];

                                    $filePath = $data['file_path'] ?? '';
                                    $fileName = $data['file_name'] ?? '';

                                    $image_path = str_replace($arrImagePath, '/upload/', $filePath);

                                    $ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
                                    ?>

                                    <?php if ($ext === 'pdf'): ?>
                                        <a href="<?= esc($filePath . $fileName) ?>" target="_blank" class="btn btn-primary">PDF 보기</a>
                                    <?php else: ?>
                                        <a href="<?= esc($image_path . $fileName) ?>?<?= esc(($data['img_width'] ?? '') . ($data['img_height'] ?? '')) ?>" 
                                            class="glightbox" 
                                            data-gallery="fax-image"
                                            data-title="팩스 이미지">
                                            <img id="fax_image"
                                                src="<?= esc($image_path . $fileName) ?>?<?= esc(($data['img_width'] ?? '') . ($data['img_height'] ?? '')) ?>"
                                                alt="fax" class="img-fluid" style="width: 100%; cursor: pointer;" />
                                        </a>
                                    <?php endif; ?>
                                </td>

                                <!-- 우: 상세 정보 -->
                                <td class="p-2 align-top" style="min-width:360px;">
                                    <?php $faxSq = esc($data['fax_sq'] ?? ''); ?>

                                    <!-- 1) 요약/조회 + 액션 -->
                                    <div class="main-card mb-3 card">
                                        <div class="card-header">
                                            <div class="d-flex align-items-center w-100">
                                                <span class="fw-semibold">기본 정보</span>
                                                <div class="ms-auto">
                                                    <span class="badge text-dark" id="span_current_stat"></span>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="card-body p-2">
                                            <table class="table table-sm table-bordered mb-2 tbl_basic2 align-middle">
                                                <colgroup>
                                                    <col style="width: 120px;">
                                                    <col>
                                                </colgroup>
                                                <tbody>
                                                    <tr>
                                                        <th>매물종류</th>
                                                        <td id="write_work_type">
                                                            <?php

                                                            switch ($data['work_type']) {
                                                                case "1":
                                                                    echo '<span class="tit"> ' . $article['typeOfProperty'] . ' </sapn>';
                                                                    break;
                                                                case "2":
                                                                    if ($article['vrfc_type_sub'] == 'D2') {
                                                                        $article['vrfc_type_nm'] = '홍보확인서 v2';
                                                                    }
                                                                    echo '<span class="tit"> 일반확인 - <span class="num">' . $article['vrfc_type_nm'] . '</sapn></sapn>';
                                                                    break;
                                                            }
                                                            ?>


                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <th>매물번호</th>
                                                        <td>
                                                            <form method="post" id="frmSearchArticle"
                                                                name="frmSearchArticle" onsubmit="return false;">
                                                                <input type="hidden" name="fax_sq"
                                                                    value="<?= $data['fax_sq'] ?>">
                                                                <input type="hidden" name="work_type"
                                                                    value="<?= $data['work_type'] ?>">
                                                                <input type="hidden" name="rlet_type_cd"
                                                                    id="rlet_type_cd"
                                                                    value="<?= $article['rlet_type_cd'] ?>">

                                                                <div class="input-group input-group-sm"
                                                                    style="max-width: 320px;">
                                                                    <input type="text" class="form-control"
                                                                        name="atcl_no" id="atcl_no"
                                                                        value="<?= $data['fax_sq'] ?>" maxlength="10"
                                                                        placeholder="매물번호 입력">
                                                                    <button type="button"
                                                                        class="btn btn-outline-primary"
                                                                        onclick="btnSilverAtclNo_onclick();">조회</button>
                                                                </div>
                                                            </form>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <th>CP ID</th>
                                                        <td><span class="num"
                                                                id="span_cpid"><?= $article['cpid'] ?></span></td>
                                                    </tr>
                                                    <tr>
                                                        <th>현재 상태</th>
                                                        <td><span class="num"
                                                                id="span_current_stat"><?= $article['current_stat'] ?></span>
                                                        </td>
                                                    </tr>
                                                </tbody>
                                            </table>

                                            <!-- 액션 버튼: 세로로 쌓이면 보기 좋아요 -->
                                            <div class="d-flex justify-content-end gap-1">
                                                <button type="button" class="btn btn-sm btn-success"
                                                    onclick="btnGreenBunyang_onclick('<?= $faxSq ?>');">현장확인</button>

                                                <button type="button" class="btn btn-sm btn-warning"
                                                    onclick="btnBrownNotArticle_onclick('<?= $faxSq ?>');">홍보확인서
                                                    아님</button>

                                                <button type="button" class="btn btn-sm btn-light"
                                                    onclick="btnSilverDup_onclick('<?= $faxSq ?>');">중복</button>

                                                <button type="button" class="btn btn-sm btn-info"
                                                    onclick="btnGreenBuBun_onclick('<?= $faxSq ?>');">부분수신</button>
                                            </div>
                                        </div>
                                    </div>

                                    <!-- 2) 공인중개사 정보 -->
                                    <div class="main-card mb-3 card">
                                        <div class="card-header">
                                            <span class="fw-semibold">공인중개사 정보</span>
                                        </div>
                                        <div class="card-body p-2">
                                            <table class="table table-sm table-bordered mb-0 tbl_basic2 align-middle">
                                                <colgroup>
                                                    <col style="width: 120px;">
                                                    <col>
                                                </colgroup>
                                                <tbody>
                                                    <tr>
                                                        <th>중개사명</th>
                                                        <td><span
                                                                id="span_realtor_nm"><?= $article['realtor_nm'] ?></span>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <th>대표전화/FAX</th>
                                                        <td><span id="span_realtor_tel_no">
                                                                <?= $article['agent_tel'] . (empty($article['agent_fax']) ? '' : ' / ' . $article['agent_fax']) ?>
                                                            </span></td>
                                                    </tr>
                                                    <tr>
                                                        <th>담당자전화</th>
                                                        <td><span id="span_realtor_tel_no">
                                                                <?= $article['realtor_tel_no'] ?>
                                                            </span></td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>

                                    <!-- 중개인 요청사항 -->
                                    <?php if ($data['work_type'] === '1'): ?>
                                        <div class="main-card mb-3 card">
                                            <div class="card-header">
                                                <span class="fw-semibold">중개인 요청사항</span>
                                            </div>
                                            <div class="card-body p-2">
                                                <table class="table table-sm table-bordered mb-0 tbl_basic2 align-middle"
                                                    id="info_table">
                                                    <colgroup>
                                                        <col style="width: 120px;">
                                                        <col>
                                                    </colgroup>
                                                    <tbody>
                                                        <tr>
                                                            <th>중개인 요청사항</th>
                                                            <td>
                                                                <textarea class="form-control" id="request_msg"
                                                                    name="request_msg"
                                                                    style="width: 100%;height: 100px;resize: none;"><?= $article['request_msg'] ?></textarea>
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                                <div class="d-flex justify-content-end gap-1">
                                                    <button type="button" class="btn btn-sm btn-outline-light"
                                                        id="btnSilverSaveResult" onclick="requestMessage();">저장</button>

                                                </div>
                                            </div>
                                        </div>
                                    <?php endif; ?>

                                    <!-- 3) 매물 정보 -->
                                    <div class="main-card mb-3 card">
                                        <div class="card-header">
                                            <span class="fw-semibold">매물 정보</span>
                                        </div>
                                        <div class="card-body p-2">
                                            <table class="table table-sm table-bordered mb-0 tbl_basic2 align-middle"
                                                id="info_table">
                                                <colgroup>
                                                    <col style="width: 120px;">
                                                    <col>
                                                </colgroup>
                                                <tbody>
                                                    <tr>
                                                        <th>등록일시</th>
                                                        <td><span id="span_rdate"><?= $article['rdate'] ?></span></td>
                                                    </tr>
                                                    <tr>
                                                        <th>의뢰인(매도자)</th>
                                                        <td><span
                                                                id="span_seller_nm"><?= $article['seller_nm'] ?></span>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <th>매물구분</th>
                                                        <td><span
                                                                id="span_rlet_type_nm"><?= $article['rlet_type_nm'] ?></span>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <th>지역구분</th>
                                                        <td><span id="span_address1"><?= $article['address1'] ?></span>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <th>단지명</th>
                                                        <td><span id="span_hscp_nm">
                                                                <?= $article['hscp_nm'] ?>
                                                            </span></td>
                                                    </tr>
                                                    <?php if (!empty($detail_hscp)):
                                                        $apt_rlet_type_cd = ['A01', 'A02', 'A03', 'A04', 'B01', 'B02', 'B03'];
                                                        $villa_rlet_type_cd = ['A05', 'A06'];
                                                        ?>
                                                        <tr>
                                                            <th>단지 정보</th>
                                                            <td>
                                                                <?php if (in_array($detail_hscp['aptType'], $apt_rlet_type_cd)): ?>
                                                                    <table width="100%">
                                                                        <tr>
                                                                            <th>단지번호</th>
                                                                            <td><?= $detail_hscp['aptNo'] ?></td>
                                                                            <th>단지명</th>
                                                                            <td><?= $detail_hscp['aptName'] ?></td>
                                                                        </tr>
                                                                        <tr>
                                                                            <th>단지타입</th>
                                                                            <td><?= $detail_hscp['aptType'] ?></td>
                                                                            <th>단지타입명</th>
                                                                            <td><?= $detail_hscp['aptTypeName'] ?></td>
                                                                        </tr>
                                                                        <tr>
                                                                            <th>사용승인일</th>
                                                                            <td><?= $detail_hscp['useAprvYmd'] ?></td>
                                                                            <th>단지 총 세대수</th>
                                                                            <td><?= $detail_hscp['totHsehCnt'] ?></td>
                                                                        </tr>
                                                                        <tr>
                                                                            <th>단지 총 동수</th>
                                                                            <td>
                                                                                <?= $detail_hscp['dongCount'] ?>
                                                                            </td>
                                                                            <th>단지 총 평형수</th>
                                                                            <td>
                                                                                <?= $detail_hscp['planTypeCount'] ?>
                                                                            </td>
                                                                        </tr>
                                                                    </table>
                                                                <?php endif; ?>
                                                                <?php if (in_array($detail_hscp['villaType'], $villa_rlet_type_cd)): ?>
                                                                    <table width="100%">
                                                                        <tr>
                                                                            <th>단지명</th>
                                                                            <td>
                                                                                <?= $detail_hscp['villaName']; ?>
                                                                            </td>
                                                                            <th>단지타입명</th>
                                                                            <td><?= $detail_hscp['villaTypeName']; ?>
                                                                            </td>
                                                                        </tr>
                                                                        <tr>
                                                                            <th>소재법정동번호</th>
                                                                            <td colspan="3">
                                                                                <?= $detail_hscp['cortarNo']; ?>
                                                                            </td>
                                                                        </tr>
                                                                        <tr>
                                                                            <th>소재 주소(전체)</th>
                                                                            <td colspan="3"><?= $detail_hscp['addr']; ?>
                                                                            </td>
                                                                        </tr>
                                                                        <tr>
                                                                            <th>주소(광역시/도)</th>
                                                                            <td><?= $detail_hscp['city']; ?></td>
                                                                            <th>주소(시군구)</th>
                                                                            <td><?= $detail_hscp['dvsn']; ?></td>
                                                                        </tr>
                                                                        <tr>
                                                                            <th>주소(읍면동)</th>
                                                                            <td><?= $detail_hscp['sec']; ?></td>
                                                                            <th>소재 동이하 상세주소</th>
                                                                            <td><?= $detail_hscp['dtlAddr']; ?></td>
                                                                        </tr>
                                                                        <tr>
                                                                            <th>단지 총 세대수</th>
                                                                            <td><?= $detail_hscp['totHsehCnt']; ?></td>
                                                                            <th>단지 총 동수</th>
                                                                            <td><?= $detail_hscp['totDongCnt']; ?></td>
                                                                        </tr>

                                                                    </table>
                                                                <?php endif; ?>
                                                            </td>
                                                        </tr>

                                                    <?php endif; ?>


                                                    <tr>
                                                        <th>리 주소</th>
                                                        <td>
                                                            <input type="text" class="form-control form-control-sm"
                                                                name="address2a" id="address2a"
                                                                value="<?= $article['address2a'] ?>" disabled>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <th>상세주소</th>
                                                        <td class="d-flex gap-1">
                                                            <input type="text" class="form-control form-control-sm"
                                                                name="address2b" id="address2b"
                                                                value="<?= $article['address2b'] ?>"
                                                                style="width: 100px;" disabled>
                                                            <input type="text" class="form-control form-control-sm"
                                                                name="address3" id="address3"
                                                                value="<?= $article['address3'] ?>" disabled>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <th>기타주소</th>
                                                        <td>
                                                            <input type="text" class="form-control form-control-sm"
                                                                name="address4" id="address4"
                                                                value="<?= $article['address4'] ?>" disabled>
                                                        </td>
                                                    </tr>

                                                    <tr>
                                                        <th>거래구분/가격</th>
                                                        <td>
                                                            <?php
                                                            $isSale = (substr($article['trade_type'] ?? '', 0, 1) === 'A'); // 매매(A*) 여부
                                                            
                                                            $display_sale = $isSale ? '' : 'style="display:none"';
                                                            $display_rent = $isSale ? 'style="display:none"' : '';
                                                            ?>

                                                            <div class="d-flex flex-column gap-2">

                                                                <!-- 1줄: 거래유형 + 버튼 -->
                                                                <div class="d-flex align-items-center gap-2">
                                                                    <select class="form-select form-select-sm"
                                                                        name="trade_type_list" id="trade_type_list"
                                                                        disabled style="max-width: 160px;">
                                                                        <option value="">-매물종류-</option>
                                                                        <?php foreach ($codes as $c): ?>
                                                                            <?php if ($c['category'] === "TRADE_TYPE"): ?>
                                                                                <option value="<?= $c['cd'] ?>"
                                                                                    <?= ($c['cd'] === ($article['trade_type'] ?? '')) ? 'selected' : '' ?>>
                                                                                    <?= $c['cd_nm'] ?>
                                                                                </option>
                                                                            <?php endif; ?>
                                                                        <?php endforeach; ?>
                                                                    </select>

                                                                    <div class="ms-auto d-flex gap-1">
                                                                        <button type="button"
                                                                            class="btn btn-sm btn-outline-secondary"
                                                                            id="btnSilverEditAddress"
                                                                            onclick="btnSilverEditAddress_onclick();">
                                                                            수정
                                                                        </button>
                                                                        <button type="button"
                                                                            class="btn btn-sm btn-success"
                                                                            id="btnGreenSaveAddress"
                                                                            onclick="btnGreenSaveAddress_onclick();"
                                                                            disabled>
                                                                            저장
                                                                        </button>
                                                                    </div>
                                                                </div>

                                                                <!-- 2줄: 금액 영역(매매/임대 분기) -->
                                                                <div class="d-flex flex-wrap align-items-center gap-2">

                                                                    <!-- 매매(A*) -->
                                                                    <div class="d-flex align-items-center gap-1 <?= $isSale ? '' : 'd-none' ?>"
                                                                        <?= $display_sale ?>>
                                                                        <span class="text-muted small">매매</span>
                                                                        <div class="input-group input-group-sm"
                                                                            style="max-width: 160px;">
                                                                            <input type="text"
                                                                                class="form-control text-end"
                                                                                id="span_deal_amt"
                                                                                value="<?= esc(str_replace(',', '', $article['deal_amt'] ?? '')) ?>"
                                                                                disabled>
                                                                            <span class="input-group-text">만원</span>
                                                                        </div>
                                                                    </div>

                                                                    <!-- 전/월/단기(비A*) -->
                                                                    <div class="d-flex flex-wrap align-items-center gap-2 <?= $isSale ? 'd-none' : '' ?>"
                                                                        <?= $display_rent ?>>
                                                                        <span class="text-muted small">보증금</span>
                                                                        <div class="input-group input-group-sm"
                                                                            style="max-width: 160px;">
                                                                            <input type="text"
                                                                                class="form-control text-end"
                                                                                id="span_wrrnt_amt"
                                                                                value="<?= esc(str_replace(',', '', $article['wrrnt_amt'] ?? '')) ?>"
                                                                                disabled>
                                                                            <span class="input-group-text">만원</span>
                                                                        </div>

                                                                        <span class="text-muted small">월세</span>
                                                                        <div class="input-group input-group-sm"
                                                                            style="max-width: 160px;">
                                                                            <input type="text"
                                                                                class="form-control text-end"
                                                                                id="span_lease_amt"
                                                                                value="<?= esc(str_replace(',', '', $article['lease_amt'] ?? '')) ?>"
                                                                                disabled>
                                                                            <span class="input-group-text">만원</span>
                                                                        </div>
                                                                    </div>

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

                                                    </tr>

                                                    <tr>
                                                        <th>분양가/프리미엄</th>
                                                        <td>
                                                            <div class="d-flex flex-wrap gap-1 align-items-center">
                                                                <span class="text-muted">분양가</span>
                                                                <input type="text" class="form-control form-control-sm"
                                                                    name="isale_amt" id="isale_amt"
                                                                    value="<?= str_replace(',', '', $article['isale_amt']) ?>"
                                                                    style="max-width: 100px;" disabled>
                                                                <span class="text-muted">만원</span>
                                                                <span class="text-muted">/ 프리미엄</span>
                                                                <input type="text" class="form-control form-control-sm"
                                                                    name="prem_amt" id="prem_amt"
                                                                    value="<?= str_replace(',', '', $article['prem_amt']) ?>"
                                                                    style="max-width: 100px;" disabled>
                                                                <span class="text-muted">만원</span>
                                                            </div>
                                                        </td>
                                                    </tr>

                                                    <?php
                                                    $chk_product_nm = ['C04', 'D03', 'D04', 'E01', 'E02', 'Z00'];

                                                    $isBasementLabel =
                                                        (($article['rlet_type_nm'] ?? '') === 'C03' && ($article['trade_type'] ?? '') === 'A1') ||
                                                        (($article['rlet_type_nm'] ?? '') === 'C05' && ($article['trade_type'] ?? '') === 'A1') ||
                                                        in_array(($article['rlet_type_nm'] ?? ''), $chk_product_nm, true);

                                                    $floorLabel = $isBasementLabel ? '지하층 / 지상층' : '층 / 총층';
                                                    ?>

                                                    <tr>
                                                        <th><?= esc($floorLabel) ?></th>

                                                        <!-- ⚠️ td에 d-flex 주면 table-cell 깨질 수 있어서 내부 div로 감싸는 걸 추천 -->
                                                        <td>
                                                            <div class="d-flex align-items-center gap-1">
                                                                <input type="text" class="form-control form-control-sm"
                                                                    name="span_floor" id="span_floor"
                                                                    value="<?= esc($article['floor'] ?? '') ?>"
                                                                    style="max-width:70px;" disabled>

                                                                <span class="text-muted">/</span>

                                                                <input type="text" class="form-control form-control-sm"
                                                                    name="span_floor2" id="span_floor2"
                                                                    value="<?= esc($article['floor2'] ?? '') ?>"
                                                                    style="max-width:70px;" disabled>
                                                            </div>
                                                        </td>
                                                    </tr>

                                                    <?php
                                                    if (!empty($article['vir_addr_yn'])):
                                                        $vir_style = "";
                                                        if ($article['vir_addr_yn'] == 'Y') {
                                                            $vir_style = "style='color:#F21E1E;font-weight: bold;'";
                                                        }
                                                        ?>
                                                        <tr>
                                                            <th>가주소 여부</th>
                                                            <td <?= $vir_style ?>>

                                                                <?= $article['vir_addr_yn'] ?>
                                                            </td>
                                                        </tr>
                                                    <?php endif; ?>
                                                    <tr>
                                                        <th>소유자명</th>
                                                        <td style="color:#FF0000;">
                                                            <?php
                                                            if ($article['stat_cd'] == '19' || $article['stat_cd'] == '60' || $article['stat_cd'] == '69') {
                                                                echo ('***');
                                                            } else {
                                                                ?>
                                                                <?php if (!empty($article['ownerNm'])): ?>
                                                                    <span id="span_ownerNm">
                                                                        <?= $article['ownerNm'] ?>
                                                                    </span>
                                                                <?php endif; ?>
                                                                <?php
                                                            }
                                                            ?>

                                                        </td>
                                                    </tr>
                                                    <?php
                                                    if (!empty($article['vrfc_type_sub'])):
                                                        if ($article['vrfc_type_sub'] == 'D2'): ?>
                                                            <tr>
                                                                <th>소유자명 확인</th>
                                                                <td>
                                                                    <select class="form-select" name="chkOwnerNm"
                                                                        id="chkOwnerNm" onchange="chkOwnerNm_onchange()">
                                                                        <option value="">-선택-</option>
                                                                        <option value="O">일치</option>
                                                                        <option value="X">불일치</option>
                                                                    </select>
                                                                </td>
                                                            </tr>

                                                        <?php endif; endif; ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>

                                    <!-- 4) 확인 폼 -->
                                    <form action="#" id="frmSave" name="frmSave" autocomplete="off">
                                        <?php if (empty($data['work_type'])) {
                                            $data['fax_sq'] = "2";
                                        } ?>
                                        <input type="hidden" name="work_type" id="work_type"
                                            value="<?= $data['work_type'] ?>">
                                        <input type="hidden" name="atcl_no" id="atcl_no_hidden"
                                            value="<?= $data['atcl_no'] ?>">
                                        <input type="hidden" name="vr_sq" id="vr_sq" value="<?= $data['vr_sq'] ?>">
                                        <input type="hidden" name="fax_sq" id="fax_sq" value="<?= $faxSq ?>">

                                        <div class="main-card mb-3 card">
                                            <div class="card-header">
                                                <span class="fw-semibold">확인 처리</span>
                                            </div>

                                            <div class="card-body p-2">
                                                <table
                                                    class="table table-sm table-bordered mb-2 tbl_basic2 align-middle">
                                                    <colgroup>
                                                        <col style="width: 120px;">
                                                        <col>
                                                    </colgroup>
                                                    <tbody>
                                                        <tr>
                                                            <th>확인담당자</th>
                                                            <td>
                                                                <?= session('usr_nm') . ' [' . session('usr_id') . ']'; ?>
                                                            </td>
                                                        </tr>

                                                        <tr>
                                                            <th>확인여부</th>
                                                            <td>
                                                                <select class="form-select form-select-sm"
                                                                    name="result_d11" id="result_d11"
                                                                    onchange="result_d11_onchange();">
                                                                    <?php foreach ($codes as $c): ?>
                                                                        <?php if ($c['category'] === "CONFIRM_RESULT_D11"): ?>
                                                                            <option value="<?= $c['cd'] ?>"
                                                                                <?= ($c['cd'] === ($confirm['code_d11'] ?? '')) ? 'selected' : '' ?>>
                                                                                <?= $c['cd_nm'] ?>
                                                                            </option>
                                                                        <?php endif; ?>
                                                                    <?php endforeach; ?>
                                                                </select>
                                                            </td>
                                                        </tr>

                                                        <tr>
                                                            <th>홍보확인서<br>미확인 상세</th>
                                                            <td>
                                                                <!-- 체크박스는 테이블 유지하되 bootstrap spacing만 -->
                                                                <table
                                                                    class="table table-borderless table-sm mb-0 tbl_basic2">
                                                                    <tbody>
                                                                        <tr>
                                                                            <?php
                                                                            $checks = explode('|', $confirm['comment_d11'] ?? '');

                                                                            $canEdit = in_array(($confirm['code_d11'] ?? ''), ['20011', '20012'], true);

                                                                            // onclick은 공통으로 두고, disabled만 조건처리
                                                                            $disabledAttr = $canEdit ? '' : ' disabled="disabled"';

                                                                            $nCnt = 0;

                                                                            // CONSULTANT_COMMENT만 필터
                                                                            $code_comment = [];
                                                                            foreach ($codes as $c) {
                                                                                if (($c['category'] ?? '') === 'CONSULTANT_COMMENT') {
                                                                                    $code_comment[] = $c;
                                                                                }
                                                                            }

                                                                            foreach ($code_comment as $idx => $row) {
                                                                                // 2개마다 줄바꿈
                                                                                if ($nCnt % 2 === 0 && $nCnt !== 0) {
                                                                                    echo '</tr><tr>';
                                                                                }

                                                                                // 체크 기준: 기존 코드는 $key를 checks랑 비교했는데,
                                                                                // 실제 저장된 값이 cd라면 cd로 비교하는 게 맞습니다.
                                                                                $cd = (string) ($row['cd'] ?? '');
                                                                                $isChecked = in_array($cd, $checks, true) ? ' checked' : '';

                                                                                $id = 'comment_d11_' . $cd;
                                                                                $label = $row['cd_nm'] ?? '';

                                                                                echo '<td class="p-0" style="border:0; padding:2px 0; height:18px;">';
                                                                                echo '  <div class="form-check d-flex align-items-center mb-0">';
                                                                                echo '      <input class="form-check-input mt-0" type="checkbox"'
                                                                                    . ' name="comment_d11[]"'
                                                                                    . ' id="' . esc($id) . '"'
                                                                                    . ' value="' . esc($cd) . '"'
                                                                                    . $isChecked
                                                                                    . $disabledAttr
                                                                                    . ' onclick="addString();">';
                                                                                echo '      <label class="form-check-label ms-1 small" for="' . esc($id) . '">'
                                                                                    . esc($label)
                                                                                    . '</label>';
                                                                                echo '  </div>';
                                                                                echo '</td>';

                                                                                $nCnt++;
                                                                            }

                                                                            ?>
                                                                        </tr>
                                                                    </tbody>
                                                                </table>
                                                            </td>
                                                        </tr>

                                                        <tr>
                                                            <th>확인내용</th>
                                                            <td>
                                                                <select class="form-select form-select-sm"
                                                                    name="fax_conf_yn_0" id="fax_conf_yn_0"
                                                                    onchange="fax_conf_yn_0_onchange();">
                                                                    <option value="" selected>-선택-</option>
                                                                    <option value="Y">전체일치</option>
                                                                    <option value="N">전체불일치</option>
                                                                </select>
                                                            </td>
                                                        </tr>

                                                        <tr>
                                                            <th>매물주소</th>
                                                            <td class="d-flex gap-1">
                                                                <?php
                                                                switch ($confirm['code_d12'] ?? '') {
                                                                    case '10000':
                                                                        $select = 'Y';
                                                                        break;

                                                                    case '20000':
                                                                        $select = 'N';
                                                                        break;

                                                                    default:
                                                                        $select = '';
                                                                        break;
                                                                }
                                                                ?>
                                                                <select class="form-select form-select-sm"
                                                                    name="fax_conf_yn_1" id="fax_conf_yn_1"
                                                                    onchange="fax_conf_yn_top_onchange();">
                                                                    <option value="">-선택-</option>
                                                                    <option value="Y" <?php if ($select === "Y") {
                                                                        echo "selected";
                                                                    } ?>>일치</option>
                                                                    <option value="N" <?php if ($select === "N") {
                                                                        echo "selected";
                                                                    } ?>>불일치</option>
                                                                </select>
                                                                <input type="text" class="form-control form-control-sm"
                                                                    name="fax_conf_info_1" id="fax_conf_info_1"
                                                                    value="<?= $confirm['comment_d12'] ?? '' ?>"
                                                                    placeholder="사유/메모">
                                                            </td>
                                                        </tr>

                                                        <tr>
                                                            <th>가격 거래구분</th>
                                                            <td class="d-flex gap-1">
                                                                <?php
                                                                switch ($confirm['code_d13'] ?? '') {
                                                                    case '10000':
                                                                        $select = 'Y';
                                                                        break;

                                                                    case '20000':
                                                                        $select = 'N';
                                                                        break;

                                                                    default:
                                                                        $select = '';
                                                                        break;
                                                                }
                                                                ?>
                                                                <select class="form-select form-select-sm"
                                                                    name="fax_conf_yn_2" id="fax_conf_yn_2"
                                                                    onchange="fax_conf_yn_top_onchange();">
                                                                    <option value="">-선택-</option>
                                                                    <option value="Y" <?php if ($select === "Y") {
                                                                        echo "selected";
                                                                    } ?>>일치</option>
                                                                    <option value="N" <?php if ($select === "N") {
                                                                        echo "selected";
                                                                    } ?>>불일치</option>
                                                                </select>
                                                                <input type="text" class="form-control form-control-sm"
                                                                    name="fax_conf_info_2" id="fax_conf_info_2"
                                                                    value="<?= $confirm['comment_d13'] ?? '' ?>"
                                                                    placeholder="사유/메모">
                                                            </td>
                                                        </tr>

                                                        <tr>
                                                            <th>의뢰인정보</th>
                                                            <td class="d-flex gap-1">
                                                                <?php
                                                                switch ($confirm['code_d14'] ?? '') {
                                                                    case '10000':
                                                                        $select = 'Y';
                                                                        break;

                                                                    case '20000':
                                                                        $select = 'N';
                                                                        break;

                                                                    default:
                                                                        $select = '';
                                                                        break;
                                                                }
                                                                ?>
                                                                <select class="form-select form-select-sm"
                                                                    name="fax_conf_yn_3" id="fax_conf_yn_3"
                                                                    onchange="fax_conf_yn_top_onchange();">
                                                                    <option value="">-선택-</option>
                                                                    <option value="Y" <?php if ($select === "Y") {
                                                                        echo "selected";
                                                                    } ?>>일치</option>
                                                                    <option value="N" <?php if ($select === "N") {
                                                                        echo "selected";
                                                                    } ?>>불일치</option>
                                                                </select>
                                                                <input type="text" class="form-control form-control-sm"
                                                                    name="fax_conf_info_3" id="fax_conf_info_3"
                                                                    value="<?= $confirm['comment_d14'] ?? '' ?>"
                                                                    placeholder="사유/메모">
                                                            </td>
                                                        </tr>

                                                        <?php if (!empty($article['memo'])): ?>
                                                            <tr>
                                                                <th>메모</th>
                                                                <td>
                                                                    <textarea class="form-control form-control-sm"
                                                                        name="memo_cal" id="memo_cal" rows="3"
                                                                        style="min-height: 80px;"><?= $article['memo'] ?></textarea>
                                                                </td>
                                                            </tr>
                                                        <?php endif; ?>

                                                        <tr id="res_tr" style="display:none">
                                                            <th>거주여부</th>
                                                            <td>
                                                                <?php $resYn = $article['resYn'] ?? ''; ?>
                                                                <select class="form-select form-select-sm" name="resYn"
                                                                    id="resYn" onchange="dbYn_change(this.value);">
                                                                    <option value="Y" <?php if ($resYn == 'Y')
                                                                        echo "selected"; ?>>Y</option>
                                                                    <option value="N" <?php if ($resYn == 'N')
                                                                        echo "selected"; ?>>N</option>
                                                                </select>
                                                            </td>
                                                        </tr>

                                                        <tr id="db_tr" style="display:none">
                                                            <th>
                                                                <div id="db_yn1">DB활용동의여부</div>
                                                            </th>
                                                            <td>
                                                                <select class="form-select form-select-sm"
                                                                    name="dbUsageAgrYn" id="dbUsageAgrYn">
                                                                    <option value="N">N</option>
                                                                    <option value="Y">Y</option>
                                                                </select>
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                </table>

                                                <!-- 하단 버튼 -->
                                                <div class="d-flex justify-content-end gap-1">
                                                    <?php
                                                    $display_none = "";
                                                    if ($article['stat_cd'] == '35' || $article['stat_cd'] == '39' || $article['stat_cd'] >= '40') {
                                                        $display_none = 'style="display:none;"';
                                                    }
                                                    ?>
                                                    <button type="button" class="btn btn-sm btn-outline-primary"
                                                        <?= $display_none ?> id="btnSilverSaveResult"
                                                        onclick="btnSilverSaveResult_onclick();">저장</button>
                                                    <button type="button" class="btn btn-sm btn-outline-secondary"
                                                        id="btnSilverNextAssign"
                                                        onclick="btnSilverNextAssign_onclick();">다음</button>
                                                </div>
                                            </div>
                                        </div>
                                    </form>
                                </td>

                            </tr>
                        </tbody>
                    </table>
                </div><!-- /table-responsive -->
            </div><!-- /card-body -->
        </div><!-- /card -->

        <div class="main-card mb-3 card">
            <div class="card-body ">
                <h5 class="card-title">정보변경 이력</h5>
                <div class="table-scroll" style="height: 250px;overflow-y: scroll;">
                    <table class="table table-bordered table-sm tbl_basic2 apt-info-table">
                        <tr>
                            <th width="150" style="text-align: center;">진행상태</th>
                            <th width="150" style="text-align: center;">변경내용</th>
                            <th width="90" style="text-align: center;">처리자(ID)</th>
                            <th width="150" style="text-align: center;">처리일시</th>
                            <th style="text-align: center;">세부내용</th>
                        </tr>
                        <?php if (!empty($history)) { ?>
                            <?php foreach ($history as $h) { ?>
                                <tr>
                                    <td style="text-align: center;">
                                        <?= $h['stat_cd_nm'] ?>
                                    </td>
                                    <td style="text-align: center;">
                                        <?= $h['chg_type'] ?>
                                    </td>
                                    <td style="text-align: center;">
                                        <?= $h['insert_id'] ?>
                                    </td>
                                    <td style="text-align: center;">
                                        <?= $h['insert_tm'] ?>
                                    </td>
                                    <td>
                                        <?= $h['memo'] ?>
                                    </td>

                                <?php } ?>
                            <?php } ?>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<?= $this->endSection() ?>

<?= $this->section('page_styles') ?>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/glightbox/dist/css/glightbox.min.css">
<?= $this->endSection() ?>

<?= $this->section('page_scripts') ?>
<script src="https://cdn.jsdelivr.net/npm/glightbox/dist/js/glightbox.min.js"></script>
<script type="text/javascript">

    $(function () {
        // GLightbox 초기화
        const lightbox = GLightbox({
            touchNavigation: true,
            loop: false,
            autoplayVideos: true,
            zoomable: true,
            draggable: true
        });
    });


    // 매물번호로 검색
    function btnSilverAtclNo_onclick() {
        var atclNo = $('#atcl_no').val();

        if (atclNo == "") {
            swal.fire({
                title: "매물번호를 입력해 주세요.",
                icon: "warning"
            });

            return;
        }

        location.replace("<?= site_url('m703/m703a/detail') ?>/" + atclNo);
    }

    // 현장확인
    function btnGreenBunyang_onclick(fax_sq) {
        swal.fire({
            text: "현재 이미지를 현장확인으로 저장하시겠습니까?",
            type: "warning",
            showCancelButton: true,
            confirmButtonText: "예",
            cancelButtonText: "아니오",
            closeOnConfirm: false,
            closeOnCancel: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
        }).then((result) => {
            if (result.isConfirmed) {
                $.ajax({
                    url: '/m703/m703a/saveBunyang',
                    contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
                    method: 'POST',
                    data: $('#frmSave').serialize(),
                    beforeSend: function () {
                        blockUI.blockPage({
                            message: tpl
                        })
                    },
                    complete: function () {
                        blockUI.unblockPage()
                    },
                    error: function (xhr, error, thrown) {
                        blockUI.unblockPage()
                        var msg = "";
                        if (xhr.responseText != null) {
                            msg = xhr.responseText
                        } else {
                            msg = "잠시후 다시 시도해 주세요."
                        }

                        Swal.fire({
                            title: msg,
                            icon: "error"
                        })
                    },
                    success: function (result) {
                        if (result.code == '0') {
                            Swal.fire({
                                title: '정상 처리되었습니다.',
                                icon: "success"

                            })

                            location.reload();
                        } else {
                            Swal.fire({
                                title: result.msg,
                                icon: "error"
                            })
                        }
                    }
                });
            }
        });
    }

    // 홍보확인서 아님
    function btnBrownNotArticle_onclick(fax_sq) {
        swal.fire({
            text: "현재 이미지를 홍보확인서가 아님으로 저장하시겠습니까?",
            type: "warning",
            showCancelButton: true,
            confirmButtonText: "예",
            cancelButtonText: "아니오",
            closeOnConfirm: false,
            closeOnCancel: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
        }).then((result) => {
            if (result.isConfirmed) {
                $.ajax({
                    url: '/m703/m703a/',
                    contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
                    method: 'POST',
                    data: $('#frmSave').serialize(),
                    beforeSend: function () {
                        blockUI.blockPage({
                            message: tpl
                        })
                    },
                    complete: function () {
                        blockUI.unblockPage()
                    },
                    error: function (xhr, error, thrown) {
                        blockUI.unblockPage()
                        var msg = "";
                        if (xhr.responseText != null) {
                            msg = xhr.responseText
                        } else {
                            msg = "잠시후 다시 시도해 주세요."
                        }

                        Swal.fire({
                            title: msg,
                            icon: "error"
                        })
                    },
                    success: function (result) {
                        if (result.code == '0') {
                            Swal.fire({
                                title: '정상 처리되었습니다.',
                                icon: "success"

                            })

                            location.reload();
                        } else {
                            Swal.fire({
                                title: result.msg,
                                icon: "error"
                            })
                        }
                    }
                });
            }
        });
    }

    // 중복저장
    function btnSilverDup_onclick(fax_sq) {
        var vr_sq = $('#frmSave > #vr_sq').val();
        var atcl_no = $('#frmSave > #atcl_no').val();

        swal.fire({
            text: "현재 이미지를 중복으로 저장하시겠습니까?",
            type: "warning",
            showCancelButton: true,
            confirmButtonText: "예",
            cancelButtonText: "아니오",
            closeOnConfirm: false,
            closeOnCancel: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
        }).then((result) => {
            if (result.isConfirmed) {
                $.ajax({
                    url: '/m703/m703a/',
                    contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
                    method: 'POST',
                    data: {
                        'fax_sq': fax_sq,
                        'vr_sq': vr_sq,
                        'atcl_no': atcl_no
                    },
                    beforeSend: function () {
                        blockUI.blockPage({
                            message: tpl
                        })
                    },
                    complete: function () {
                        blockUI.unblockPage()
                    },
                    error: function (xhr, error, thrown) {
                        blockUI.unblockPage()
                        var msg = "";
                        if (xhr.responseText != null) {
                            msg = xhr.responseText
                        } else {
                            msg = "잠시후 다시 시도해 주세요."
                        }

                        Swal.fire({
                            title: msg,
                            icon: "error"
                        })
                    },
                    success: function (result) {
                        if (result.code == '0') {
                            Swal.fire({
                                title: '정상 처리되었습니다.',
                                icon: "success"

                            })

                            location.reload();
                        } else {
                            Swal.fire({
                                title: result.msg,
                                icon: "error"
                            })
                        }
                    }
                });
            }
        });
    }

    // 부분 수신
    function btnGreenBuBun_onclick(fax_sq) {
        swal.fire({
            text: "현재 이미지를 부분수신으로 저장하시겠습니까?",
            type: "warning",
            showCancelButton: true,
            confirmButtonText: "예",
            cancelButtonText: "아니오",
            closeOnConfirm: false,
            closeOnCancel: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
        }).then((result) => {
            if (result.isConfirmed) {
                $.ajax({
                    url: '/m703/m703a/',
                    contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
                    method: 'POST',
                    data: {
                        'fax_sq': fax_sq,
                    },
                    beforeSend: function () {
                        blockUI.blockPage({
                            message: tpl
                        })
                    },
                    complete: function () {
                        blockUI.unblockPage()
                    },
                    error: function (xhr, error, thrown) {
                        blockUI.unblockPage()
                        var msg = "";
                        if (xhr.responseText != null) {
                            msg = xhr.responseText
                        } else {
                            msg = "잠시후 다시 시도해 주세요."
                        }

                        Swal.fire({
                            title: msg,
                            icon: "error"
                        })
                    },
                    success: function (result) {
                        if (result.code == '0') {
                            Swal.fire({
                                title: '정상 처리되었습니다.',
                                icon: "success"

                            })

                            location.reload();
                        } else {
                            Swal.fire({
                                title: result.msg,
                                icon: "error"
                            })
                        }
                    }
                });
            }
        });
    }

    // 매물정보 수정
    function btnSilverEditAddress_onclick() {
        var trade_type = $('#trade_type_list option:selected').val();
        var rlet_type_cd = $('#rlet_type_cd').val();

        $('#span_deal_amt').removeAttr("disabled");
        $('#span_wrrnt_amt').removeAttr("disabled");
        $('#span_lease_amt').removeAttr("disabled");

        if (trade_type == 'A1' && (rlet_type_cd == 'B01' || rlet_type_cd == 'B02' || rlet_type_cd == 'B03')) {
            $('#isale_amt').removeAttr("disabled");
            $('#prem_amt').removeAttr("disabled");
        }

        $("#btnGreenSaveAddress").removeAttr("disabled");
    }

    // 매물정보 저장
    function btnGreenSaveAddress_onclick() {
        var fax_sq = '<? $data['fax_sq'] ?>';
        var vr_sq = $('#frmSave > #vr_sq').val();
        var atcl_no = $('#frmSave > #atcl_no').val();
        var trade_type = $('#trade_type_list option:selected').val();
        var deal_amt = $('#span_deal_amt').val();
        var wrrnt_amt = $('#span_wrrnt_amt').val();
        var lease_amt = $('#span_lease_amt').val();
        var isale_amt = $('#isale_amt').val();
        var prem_amt = $('#prem_amt').val();

        var work_type = $('#frmSave > #work_type').val();
        if (work_type != '2') {
            swal.fire({
                title: "현장확인매물은 가격을 변경할 수 없습니다.",
                icon: "warning"
            });
            return;
        }

        if (vr_sq == '') {
            swal.fire({
                title: "매물을 조회하세요.",
                icon: "warning"
            });
            return;
        }


        swal.fire({
            text: "매물 정보를 변경하시겠습니까?",
            type: "warning",
            showCancelButton: true,
            confirmButtonText: "예",
            cancelButtonText: "아니오",
            closeOnConfirm: false,
            closeOnCancel: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
        }).then((result) => {
            if (result.isConfirmed) {
                $.ajax({
                    url: '/m703/m703a/saveFaxImgs',
                    contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
                    method: 'POST',
                    data: {
                        'fax_sq': fax_sq,
                        'vr_sq': vr_sq,
                        'atcl_no': atcl_no,
                        'trade_type': trade_type,
                        'deal_amt': deal_amt,
                        'wrrnt_amt': wrrnt_amt,
                        'lease_amt': lease_amt,
                        'isale_amt': isale_amt,
                        'prem_amt': prem_amt
                    },
                    beforeSend: function () {
                        blockUI.blockPage({
                            message: tpl
                        })
                    },
                    complete: function () {
                        blockUI.unblockPage()
                    },
                    error: function (xhr, error, thrown) {
                        blockUI.unblockPage()
                        var msg = "";
                        if (xhr.responseText != null) {
                            msg = xhr.responseText
                        } else {
                            msg = "잠시후 다시 시도해 주세요."
                        }

                        Swal.fire({
                            title: msg,
                            icon: "error"
                        })
                    },
                    success: function (result) {
                        if (result.code == '0') {
                            Swal.fire({
                                title: '정상 처리되었습니다.',
                                icon: "success"

                            })

                            location.reload();
                        } else {
                            Swal.fire({
                                title: result.msg,
                                icon: "error"
                            })
                        }
                    }
                });
            }
        });
    }



    // 중개사 메모 저장
    function requestMessage() {

        swal.fire({
            text: "중개사 요청사항을 저장하시겠습니까",
            type: "warning",
            showCancelButton: true,
            confirmButtonText: "예",
            cancelButtonText: "아니오",
            closeOnConfirm: false,
            closeOnCancel: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
        }).then((result) => {
            if (result.isConfirmed) {
                const data = {
                    'atcl_no': '<?= $article['atcl_no'] ?>',
                    'fax_sq': '<?= $data['fax_sq'] ?>',
                    'vr_sq': '<?= $data['vr_sq'] ?>',
                    'msg': $("#request_msg").val()
                };

                $.ajax({
                    url: '/m703/m703a/saveRequestMessage',
                    contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
                    method: 'POST',
                    data: data,
                    beforeSend: function () {
                        blockUI.blockPage({
                            message: tpl
                        })
                    },
                    complete: function () {
                        blockUI.unblockPage()
                    },
                    error: function (xhr, error, thrown) {
                        blockUI.unblockPage()
                        var msg = "";
                        if (xhr.responseText != null) {
                            msg = xhr.responseText
                        } else {
                            msg = "잠시후 다시 시도해 주세요."
                        }

                        Swal.fire({
                            title: msg,
                            icon: "error"
                        })
                    },
                    success: function (result) {
                        if (result.code == '0') {
                            Swal.fire({
                                title: '정상 처리되었습니다.',
                                icon: "success"

                            })

                            location.reload();
                        } else {
                            Swal.fire({
                                title: result.msg,
                                icon: "error"
                            })
                        }
                    }
                });
            }
        });

    }


    // 확인여부 onchange
    function result_d11_onchange() {
        var result_d11 = $("#result_d11 option:selected").val();

        if (result_d11 == '20011' || result_d11 == '20012') {
            $('input[id^="comment_d11_"]').removeAttr('disabled');
        } else {
            $('input[id^="comment_d11_"]').removeAttr('checked');
            $('input[id^="comment_d11_"]').attr('disabled', true);
        }
    }

    // 홍보확인서 체크박스 click
    function addString() {
        var chk1 = $("input:checkbox[id='comment_d11_01']").is(":checked");
        var chk2 = $("input:checkbox[id='comment_d11_05']").is(":checked");
        var chk3 = $("input:checkbox[id='comment_d11_08']").is(":checked");
        var chk4 = $("input:checkbox[id='comment_d11_14']").is(":checked");
        var chk5 = $("input:checkbox[id='comment_d11_07']").is(":checked");
        var chk6 = $("input:checkbox[id='comment_d11_15']").is(":checked");

        var text1 = $("#fax_conf_info_1").val();
        var text2 = $("#fax_conf_info_3").val();

        if (chk1 == true) {
            $("#fax_conf_info_1").val("주소불일치");
            $("#fax_conf_yn_1").val('N');
            fax_conf_yn_top_onchange();
        }

        if (chk2 == true) {
            $("#fax_conf_info_3").val("관계 불일치");
            $("#fax_conf_yn_3").val('N');
            fax_conf_yn_top_onchange();
        }

        if (chk3 == true) {
            $("#fax_conf_info_3").val("서명");
            $("#fax_conf_yn_3").val('N');
            fax_conf_yn_top_onchange();
        }

        if (chk4 == true) {
            $("#fax_conf_info_3").val("정보이용 미동의");
            $("#fax_conf_yn_3").val('N');
            fax_conf_yn_top_onchange();
        }

        if (chk5 == true) {
            $("#fax_conf_info_3").val("의뢰인 불일치");
            $("#fax_conf_yn_3").val('N');
            fax_conf_yn_top_onchange();
        }

        if (chk6 == true) {
            $("#fax_conf_info_3").val("공사주소증빙서류미수취");
            $('#fax_conf_yn_3').val('N');
            fax_conf_yn_top_onchange();
        }

    }


    function fax_conf_yn_top_onchange() {
        var fax1 = $("#fax_conf_yn_1 option:selected").val();
        var fax2 = $("#fax_conf_yn_2 option:selected").val();
        var fax3 = $("#fax_conf_yn_3 option:selected").val();

        if (!(fax1 == fax2 && fax2 == fax3 && fax1 == fax3)) {
            $("#fax_conf_yn_0").val('');
        }
    }


    // 다음매물
    function btnSilverNextAssign_onclick() {
        swal.fire({
            text: "다음 FAX 이미지를 불러오시겠습니까?",
            type: "warning",
            showCancelButton: true,
            confirmButtonText: "예",
            cancelButtonText: "아니오",
            closeOnConfirm: false,
            closeOnCancel: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
        }).then((result) => {
            if (result.isConfirmed) {
                const data = {
                    'curr_fax_sq': '<?= $data['fax_sq'] ?>'
                };

                $.ajax({
                    url: '/m703/m703a/getNextFaxImgs',
                    contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
                    method: 'POST',
                    data: data,
                    beforeSend: function () {
                        blockUI.blockPage({
                            message: tpl
                        })
                    },
                    complete: function () {
                        blockUI.unblockPage()
                    },
                    error: function (xhr, error, thrown) {
                        blockUI.unblockPage()
                        var msg = "";
                        if (xhr.responseText != null) {
                            msg = xhr.responseText
                        } else {
                            msg = "잠시후 다시 시도해 주세요."
                        }

                        Swal.fire({
                            title: "매물조회 실패",
                            icon: "error"
                        })
                    },
                    success: function (result) {
                        if (result.code == '0') {

                            console.log(result.data)
                            const fax_sq = result.data.fax_sq;

                            location.replace("<?= site_url('m703/m703a/detail') ?>/" + fax_sq);

                        } else {
                            Swal.fire({
                                title: result.msg,
                                icon: "error"
                            })
                        }
                    }
                });
            }
        });
    }

    // 결과 저장
    function btnSilverSaveResult_onclick() {

        var work_type = $('#frmSave > #work_type').val();
        var atcl_no = $('#frmSave > #atcl_no').val();
        var vr_sq = $('#frmSave > #vr_sq').val();
        if (work_type == '' || atcl_no == '' || vr_sq == '') {
            Swal.fire({
                title: "매물정보를 확인하세요.",
                icon: "warning"
            });
            return false;
        }


        if (work_type == '2') {
            if ($("#chkOwnerNm").length > 0) {	//소유자명 확인이 있으면 == 홍v2일때
                var nm = $('#chkOwnerNm').val();
                if (nm == '') {
                    Swal.fire({
                        title: "소유자명을 확인해야 합니다.",
                        icon: "warning"
                    });
                    return false;
                }
            }
            var result_d11 = $("#result_d11 option:selected").val();
            if (result_d11 == '') {
                Swal.fire({
                    title: "매물정보 확인여부를 선택하세요.",
                    icon: "warning"
                });
                $("#result_d11").focus();
                return false;
            }

            if (result_d11 == '20011' || result_d11 == '20012') {
                var cntChecked = $('input[name="comment_d11[]"]:checked').length;
                if (cntChecked <= 0) {
                    Swal.fire({
                        title: "홍보확인서 미확인여부 상세를 선택하세요.",
                        icon: "warning"
                    });
                    return false;
                }
            }

            var fax_conf_yn_1 = $("#fax_conf_yn_1 option:selected").val();
            if (fax_conf_yn_1 == '') {
                Swal.fire({
                    title: "매물주소 일치여부를 선택하세요.",
                    icon: "warning"
                });
                $("#fax_conf_yn_1").focus();
                return false;
            }
            if (fax_conf_yn_1 == 'N') {
                var fax_conf_info_1 = $.trim($('#fax_conf_info_1').val());
                if (fax_conf_info_1 == '') {
                    Swal.fire({
                        title: "매물주소 불일치 내용을 입력하세요.",
                        icon: "warning"
                    });
                    $("#fax_conf_info_1").focus();
                    return false;
                }
            }

            var fax_conf_yn_2 = $("#fax_conf_yn_2 option:selected").val();
            if (fax_conf_yn_2 == '') {
                Swal.fire({
                    title: "가격 거래구분 일치여부를 선택하세요.",
                    icon: "warning"
                });
                $("#fax_conf_yn_2").focus();
                return false;
            }
            if (fax_conf_yn_2 == 'N') {
                var fax_conf_info_2 = $.trim($('#fax_conf_info_2').val());
                if (fax_conf_info_2 == '') {
                    Swal.fire({
                        title: "가격 거래구분 불일치 내용을 입력하세요.",
                        icon: "warning"
                    });
                    $("#fax_conf_info_2").focus();
                    return false;
                }
            }

            var fax_conf_yn_3 = $("#fax_conf_yn_3 option:selected").val();
            if (fax_conf_yn_3 == '') {
                Swal.fire({
                    title: "의뢰인정보 일치여부를 선택하세요.",
                    icon: "warning"
                });
                $("#fax_conf_yn_3").focus();
                return false;
            }
            if (fax_conf_yn_3 == 'N') {
                var fax_conf_info_3 = $.trim($('#fax_conf_info_3').val());
                if (fax_conf_info_3 == '') {
                    Swal.fire({
                        title: "의뢰인정보 불일치 내용을 입력하세요.",
                        icon: "warning"
                    });
                    $("#fax_conf_info_3").focus();
                    return false;
                }
            }
        } else if (work_type == '1') {
            btnGreenBunyang_onclick('<?= $data['fax_sq'] ?>');
        }


        swal.fire({
            text: "확인결과를 저장하시겠습니까?",
            type: "warning",
            showCancelButton: true,
            confirmButtonText: "예",
            cancelButtonText: "아니오",
            closeOnConfirm: false,
            closeOnCancel: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
        }).then((result) => {
            if (result.isConfirmed) {
                $.ajax({
                    url: '/m703/m703a/saveResult',
                    contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
                    method: 'POST',
                    data: $('#frmSave').serialize(),
                    beforeSend: function () {
                        blockUI.blockPage({
                            message: tpl
                        })
                    },
                    complete: function () {
                        blockUI.unblockPage()
                    },
                    error: function (xhr, error, thrown) {
                        blockUI.unblockPage()
                        var msg = "";
                        if (xhr.responseText != null) {
                            msg = xhr.responseText
                        } else {
                            msg = "잠시후 다시 시도해 주세요."
                        }

                        Swal.fire({
                            title: msg,
                            icon: "error"
                        })
                    },
                    success: function (result) {
                        if (result.code == '0') {
                            Swal.fire({
                                title: '정상 처리되었습니다.',
                                icon: "success"

                            })

                            location.reload();
                        } else {
                            Swal.fire({
                                title: result.msg,
                                icon: "error"
                            })
                        }
                    }
                });
            }
        });

    }
</script>

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