<?php
$usr_level = session('usr_level');

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

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

  .num {
    font-family: Tahoma;
    color: #b68556;
    font-size: 17px;
  }

  .table-scroll {
    max-height: 300px;
    overflow-y: scroll;
  }
</style>

<form id="rcptFrm" enctype="multipart/form-data" onsubmit="return false;">
  <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;">
          <span class="text-muted me-2">매물ID:</span>
          <span class="fw-bold text-primary fs-6 me-4"><?= esc($data['rcpt_atclno'] ?? '') ?></span>
          <span class="text-muted me-2">CP ID:</span>
          <span class="fw-bold text-primary fs-6 me-4"><?= esc($data['rcpt_cpid'] ?? '') ?></span>
          <!-- 필요시 상태 등 추가 가능 -->
        </div>
      </div>
    </div>
    <div class="card-body">
      <h5 class="card-title">공인 중개사 정보</h5>

        <table class="table table-bordered table-sm tbl_basic2 apt-info-table">
          <colgroup>
            <col width="15%" />
            <col width="35%" />
            <col width="15%" />
            <col width="35%" />
          </colgroup>
          <tbody>
            <tr>
              <th>중개사ID</th>
              <td><?= esc($data['agent_id'] ?? '') ?></td>
              <th>중개사명</th>
              <td><?= esc($data['agent_nm'] ?? '') ?></td>
            </tr>
            <tr>
              <th>대표전화</th>
              <td><?= esc($data['agent_head_tel'] ?? '') ?></td>
              <th>담당자전화</th>
              <td><?= esc($data['agent_contact_tel'] ?? '') ?></td>
            </tr>
            <tr>
              <th>검증방식</th>
              <td>
                <?= (($data['isSiteVRVerification'] ?? '') === 'Y')
                  ? "<span style='color:red;'>현장V2</span>"
                  : "현장"; ?>
              </td>
              <th>연락가능전화</th>
              <td>
                <?php $agent_tel = str_replace("-", "", $data['agent_tel'] ?? ''); ?>
                <div class="d-flex align-items-center gap-2">
                  <input type="text" id="agent_tel" name="agent_tel" class="form-control form-control-sm" maxlength="16"
                    value="<?= esc($agent_tel) ?>" style="max-width: 220px;" />
                  <button type="button" class="btn btn-sm btn-outline-light" onclick="fn_save_tel();">저장</button>
                </div>
              </td>
            </tr>
          </tbody>
        </table>

      </div>
    </div>

    <!-- 매물정보 -->
    <form id="rcptFrm" enctype="multipart/form-data" onsubmit="return false;">
      <input type="hidden" name="rcpt_sq" value="<?= $data['rcpt_sq'] ?>" />
      <input type="hidden" name="rsrv_sq" value="<?= $data['rsrv_sq'] ?>" />
      <input type="hidden" name="rcpt_atclno" value="<?= $data['rcpt_atclno'] ?>" />
      <input type="hidden" name="rcpt_key" value="<?= $data['rcpt_key'] ?>" />
      <input type="hidden" name="rcpt_product" id="rcpt_product" value="<?= $data['rcpt_product'] ?>" />
      <div class="main-card mb-3 card">
        <div class="card-body">
          <h5 class="card-title">매물 정보</h5>

          <table class="table table-bordered table-sm tbl_basic2 apt-info-table">
            <colgroup>
              <col width="15%" />
              <col width="35%" />
              <col width="15%" />
              <col width="35%" />
            </colgroup>
            <tbody>
              <tr>
                <th>등록일</th>
                <td>
                  <?= $data['insert_tm'] ?>
                </td>
                <th>예약(촬영)요청일</th>
                <td>
                  <?= $data['rsrv_date'] ?>
                  <?= $data['rsrv_tm_ap'] ?>
                  <?= $data['rsrv_tm_hour'] ?>
                </td>
              </tr>
              <tr>
                <th>현장확인(촬영)일자</th>
                <td>
                  <?= $data['photo_save_dt'] ?>
                </td>
                <th>가주소 여부</th>
                <td>
                  <?php if ($data['virAddr_yn'] == 'Y') { ?><span style="font-weight: bold;color: red;">가주소</span>
                  <?php } else {
                    echo $data['virAddr_yn'];
                  } ?>
                </td>
              </tr>
              <tr>
                <th>매물구분</th>
                <td><?= $data['rcpt_product_nm'] ?></td>
                <th>거래구분</th>
                <td>
                  <select class="form-select" id="trade_type" onchange="trade_type_onchange();" disabled>
                    <option value="">거래구분</option>
                    <?php foreach ($codes as $c): ?>
                      <?php if ($c['category'] === "TRADE_TYPE"): ?>
                        <option value="<?= $c['cd'] ?>" <?php if ($c['cd'] === $data['trade_type']) {
                            echo "selected";
                          } ?>>
                          <?= $c['cd_nm'] ?>
                        </option>
                      <?php endif; ?>
                    <?php endforeach; ?>
                  </select>
                </td>
              </tr>
              <tr>
                <th rowspan="3">지역구분</th>
                <td rowspan="3">
                  <?= $data['addr'] ?>
                </td>
                <th>리 주소</th>
                <td>
                  <input type="hidden" class="form-control" name="rcpt_dtl_addr" id="rcpt_dtl_addr"
                    value="<?= $data['rcpt_dtl_addr'] ?>" size="50" disabled="disabled" />
                  <input type="text" class="form-control" name="rcpt_dtl_addr1" id="rcpt_dtl_addr1"
                    value="<?= $data['rcpt_li_addr'] ?>" size="50" disabled="disabled" /><br />
                </td>
              </tr>
              <tr>
                <th>상세주소</th>
                <td class="d-flex gap-1">
                  <?php if (empty($data['rcpt_jibun_addr'])) { ?>
                    <input type="hidden" class="form-control" name="rcpt_dtl_addr2a" id="rcpt_dtl_addr2a"
                      value="<?= $data['rcpt_jibun_addr'] ?>" size="50" disabled="disabled" />
                    <input type="hidden" class="form-control" name="rcpt_dtl_addr2b" id="rcpt_dtl_addr2b"
                      value="<?= $data['rcpt_etc_addr'] ?>" size="50" disabled="disabled" />
                    <input type="text" class="form-control" name="rcpt_dtl_addr2" id="rcpt_dtl_addr2"
                      value="<?= $data['rcpt_dtl_addr'] ?>" size="50" disabled="disabled" /><br />
                    <input type="text" class="form-control" name="rcpt_dtl_addr3" id="rcpt_dtl_addr3"
                      value="<?= $data['rcpt_ho'] ?>" size="50" disabled="disabled" />
                    <input type="hidden" class="form-control" name="rcpt_ho" id="rcpt_ho" value="<?= $data['rcpt_ho'] ?>"
                      size="50" disabled="disabled" />
                  <?php } else { ?>
                    <input type="hidden" class="form-control" name="rcpt_dtl_addr2" id="rcpt_dtl_addr2"
                      value="<?= $data['rcpt_dtl_addr'] ?>" size="50" disabled="disabled" />
                    <input type="hidden" class="form-control" name="rcpt_dtl_addr3" id="rcpt_dtl_addr3"
                      value="<?= $data['rcpt_ho'] ?>" size="50" disabled="disabled" />
                    <input type="text" class="form-control" name="rcpt_dtl_addr2a" id="rcpt_dtl_addr2a"
                      value="<?= $data['rcpt_jibun_addr'] ?>" size="50" disabled="disabled" /><br />
                    <input type="text" class="form-control" name="rcpt_dtl_addr2b" id="rcpt_dtl_addr2b"
                      value="<?= $data['rcpt_etc_addr'] ?>" size="50" disabled="disabled" />
                    <input type="hidden" class="form-control" name="rcpt_ho" id="rcpt_ho" value="<?= $data['rcpt_ho'] ?>"
                      size="50" disabled="disabled" />
                  <?php } ?>
                </td>
              </tr>
              <tr>
                <th>기타주소</th>
                <td>
                  <input type="text" class="form-control" name="rcpt_ref_addr" id="rcpt_ref_addr"
                    value="<?= $data['rcpt_ref_addr'] ?>" size="50" disabled="disabled" />
                </td>
              </tr>
              <tr>
                <th>단지명</th>
                <td></td>
                <th>가격</th>
                <td>
                  <?php
                  // 숫자 콤마 제거(표시용)
                  $v2 = str_replace(',', '', $data['rcpt_product_info2'] ?? '');
                  $v3 = str_replace(',', '', $data['rcpt_product_info3'] ?? '');
                  $v4 = str_replace(',', '', $data['rcpt_product_info4'] ?? '');
                  $v5 = str_replace(',', '', $data['rcpt_product_info5'] ?? '');

                  // 분양가/프리미엄 노출 조건
                  $chk_array = ['B01', 'B02', 'B03'];
                  $show_sale_premium = (in_array(($data['rcpt_product'] ?? ''), $chk_array) || ($data['trade_type'] ?? '') === 'A1');

                  ?>

                  <!-- 보증금/금액 -->
                  <div class="d-flex flex-column gap-1">
                    <div class="d-flex align-items-center gap-1 flex-wrap">
                      <input type="text" class="form-control form-control-sm" name="rcpt_product_info2"
                        id="rcpt_product_info2" value="<?= esc($v2) ?>" style="width: 110px;" disabled />
                      <span class="small text-nowrap">만원</span>
                    </div>

                    <!-- 월세(월) -->
                    <div id="div_trade_type_price_monthly">
                      <div class="d-flex align-items-center gap-1 flex-wrap">
                        <input type="text" class="form-control form-control-sm" name="rcpt_product_info3"
                          id="rcpt_product_info3" value="<?= esc($v3) ?>" style="width: 110px;" disabled />
                        <span class="small text-nowrap">만원 (월)</span>
                      </div>
                    </div>

                    <!-- 분양가 / 프리미엄 -->
                    <?php if ($show_sale_premium): ?>
                      <div class="d-flex align-items-center gap-2 flex-wrap mt-1">
                        <span class="small text-nowrap">분양가</span>
                        <input type="text" class="form-control form-control-sm" name="rcpt_product_info4"
                          id="rcpt_product_info4" value="<?= esc($v4) ?>" style="width: 110px;" disabled />
                        <span class="small text-nowrap">만원</span>

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

                        <span class="small text-nowrap">프리미엄</span>
                        <input type="text" class="form-control form-control-sm" name="rcpt_product_info5"
                          id="rcpt_product_info5" value="<?= esc($v5) ?>" style="width: 110px;" disabled />
                        <span class="small text-nowrap">만원</span>
                      </div>
                    <?php endif; ?>

                    <!-- 버튼 -->
                    <div class="d-flex align-items-center justify-content-end gap-1 mt-2">
                      <button type="button" class="btn btn-sm btn-outline-light btn-edit"
                        onclick="editPriceInfo();">수정</button>
                      <button type="button" class="btn btn-sm btn-outline-success btn-save"
                        onclick="modifyPriceInfo();">가격수정</button>
                    </div>
                  </div>
                </td>
              </tr>

              <tr>
                <th>평형</th>
                <td></td>
                <?php
                $chk_product_nm = [
                  'C04',				// 전원주택
                  'D03',				// 빌딩,건물
                  'D04',				// 상가,건물
                  'E01',				// 숙박,콘도
                  'E02',				// 공장,창고
                  'Z00'				// 기타		
                ];

                if (in_array($data['rcpt_product'], array('C03', 'C05', 'C06')) && $data['rcpt_product_info1'] == '매매') {
                  echo ("<th>지하층 / 지상층</th>");
                } else if (in_array($data['rcpt_product'], $chk_product_nm)) {
                  echo ("<th>지하층 / 지상층</th>");
                } else {
                  echo ("<th>층 / 총층</th>");
                }
                ?>
                <td class="d-flex gap-1">
                  <input type="text" class="form-control" name="rcpt_floor" id="rcpt_floor"
                    value="<?= $data['rcpt_floor'] ?>" size="8" maxlength="3" disabled="disabled"
                    style="width: 100px;" />
                  <input type="text" class="form-control" name="rcpt_floor2" id="rcpt_floor2"
                    value="<?= $data['rcpt_floor2'] ?>" size="8" maxlength="3" disabled="disabled"
                    style="width: 100px;" />
                </td>
              </tr>
              <tr class="spc">
                <th></th>
                <td></td>
                <th>면적확인파일1</th>
                <td>
                  <?php
                  $arrI6 = [];
                  $arrI7 = [];
                  foreach ($images as $key => $img) {
                    switch ($img['img_type']) {
                      case "I6":
                        $arrI6[] = $img;
                        unset($images[$key]);
                        break;

                      case "I7":
                        $arrI7[] = $img;
                        unset($images[$key]);
                        break;
                    }
                  }

                  if (!empty($arrI6)) { ?>
                    <?php foreach ($arrI6 as $img) {
                      $sq = $img['img_sq'];

                      $link = $img['img_path'] . $img['img_filenm'];
                      $temp2 = explode(".", $img['img_filenm']);
                      //$name = $temp2[0]."_thumb.".$temp2[1];
                      $name = $temp2[0] . "_thumb.jpg";
                      $thumblink = $img['img_path'] . $name;

                      if ($img['cloud_upload_yn'] == 'Y') {
                        $thumblink = NCLOUD_OBJECT_STORAGE_URL . $thumblink;
                        $link = NCLOUD_OBJECT_STORAGE_URL . $link;
                      }

                      ?>
                      <div id="img_file_dis_I6" style="padding: 6px 0;">
                        <a onclick="fn_preview('<?= $link ?>')">
                          <img id="photo-display2_I6" src="<?= $thumblink ?>" alt="Image" class="lazy" />
                        </a>
                      </div>
                    <?php } ?>
                  <?php } else { ?>
                    <div id="img_file_dis_I6" style="padding: 10px 0;">
                      <img id="photo-display2_I6" src="/plugin/img/photo.gif" alt="No Image" />
                    </div>
                  <?php } ?>
                </td>
              </tr>
              <tr class="spc">
                <th>방</th>
                <td></td>
                <th>면적확인파일2</th>
                <td>
                  <?php if (!empty($arrI7)) { ?>
                    <?php foreach ($arrI7 as $img) {
                      $sq = $img['img_sq'];

                      $link = $img['img_path'] . $img['img_filenm'];
                      $temp2 = explode(".", $img['img_filenm']);
                      //$name = $temp2[0]."_thumb.".$temp2[1];
                      $name = $temp2[0] . "_thumb.jpg";
                      $thumblink = $img['img_path'] . $name;

                      if ($img['cloud_upload_yn'] == 'Y') {
                        $thumblink = NCLOUD_OBJECT_STORAGE_URL . $thumblink;
                        $link = NCLOUD_OBJECT_STORAGE_URL . $link;
                      }

                      ?>
                      <div id="img_file_dis_I6" style="padding: 6px 0;">
                        <a onclick="fn_preview('<?= $link ?>')">
                          <img id="photo-display2_I6" src="<?= $thumblink ?>" alt="Image" class="lazy" />
                        </a>
                      </div>
                    <?php } ?>
                  <?php } else { ?>
                    <div id="img_file_dis_I6" style="padding: 10px 0;">
                      <img id="photo-display2_I6" src="/plugin/img/photo.gif" alt="No Image" />
                    </div>
                  <?php } ?>

                </td>
              </tr>
              <tr>
                <th>지도좌표</th>
                <td class="d-flex text-nowrap gap-1">
                  <div class="d-flex flex-nowrap gap-1">

                    <!-- 좌표 입력 -->
                    <div class="d-flex align-items-center gap-2 flex-wrap">
                      <div class="d-flex align-items-center gap-1">
                        <span class="small text-nowrap">경도</span>
                        <input type="text" class="form-control form-control-sm" name="rcpt_x" id="rcpt_x"
                          value="<?= esc($data['rcpt_x'] ?? '') ?>" style="width: 140px;" disabled />
                      </div>

                      <div class="d-flex align-items-center gap-1">
                        <span class="small text-nowrap">위도</span>
                        <input type="text" class="form-control form-control-sm" name="rcpt_y" id="rcpt_y"
                          value="<?= esc($data['rcpt_y'] ?? '') ?>" style="width: 140px;" disabled />
                      </div>
                    </div>

                    <!-- 버튼 -->
                    <div class="d-flex align-items-center gap-1 flex-wrap">
                      <button type="button" class="btn btn-sm btn-outline-light" id="btnSilverReadLatLng"
                        onclick="ajax_get_geocode();" disabled>
                        주소좌표
                      </button>

                      <button type="button" class="btn btn-sm btn-outline-light" id="btnSilverModifyMap"
                        onclick="modifyMap();" disabled>
                        지도좌표
                      </button>
                    </div>

                  </div>
                </td>
                <th></th>
                <td colspan="2" class="d-flex gap-1 justify-content-end">
                  <button type="button" class="btn btn-sm btn-outline-light" id="btnSilverReadLatLng" onclick="">
                    수정
                  </button>

                  <button type="button" class="btn btn-sm btn-outline-light" id="btnSilverModifyMap" onclick="">
                    저장
                  </button>
                </td>
              </tr>
            </tbody>
          </table>

        </div>
      </div>

      <!-- 거주여부 -->
      <div class="main-card mb-3 card">
        <div class="card-body">
          <h5 class="card-title">거주여부</h5>
          <table class="table table-bordered table-sm tbl_basic2 apt-info-table">
            <tbody>
              <tr>
                <th>거주여부</th>
                <td id="db_yn0">
                  <select class="form-select" name="resYn" id="resYn" onchange="dbYn_change(this.value);">
                    <option value="Y">Y</option>
                    <option value="N" selected="">N</option>
                  </select>
                </td>
                <th>
                  <div id="db_yn1">DB활용동의여부</div>
                </th>
                <td>
                  <div id="db_yn2">
                    <select class="form-select" name="dbUsageAgrYn" id="dbUsageAgrYn">
                      <option value="N" selected="">N</option>
                      <option value="Y">Y</option>
                    </select>
                  </div>
                </td>
                <td style="text-align: center;">
                  <button type="button" class="btn btn-sm btn-outline-light" onclick="saveResDbYn();">저장</button>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>

      <!-- 평면도요청 -->
      <div class="main-card mb-3 card">
        <div class="card-body">
          <h5 class="card-title">평면도요청</h5>
          <table class="table table-bordered table-sm tbl_basic2 apt-info-table">
            <tbody>
              <tr>
                <th style="width: 100px;">
                  <div id="ground_plan">평면도요청</div>
                </th>
                <td>
                  <select class="form-select" name="ground_plan" id="ground_plan" style="width: 100px;">
                    <option value="N">N</option>
                    <option value="Y" selected="">Y</option>
                  </select>
                </td>
                <th style="width: 200px;">평면도 등록 여부</th>
                <td>
                  <?php
                  $intAptChk = 0;
                  $typeArray = ['아파트', '주상복합', '오피스텔'];
                  if (empty($apt_ground))
                    $apt_ground = [];
                  foreach ($apt_ground as $gval) {


                    $addr = $gval['addr2'];
                    $hscp_no = $gval['hscp_no'];

                    if (in_array($data['rcpt_product_nm'], $typeArray)) {
                      if (strpos($data['rcpt_atclno'], $hscp_no) !== false) {
                        $intAptChk++;
                      }
                    } else {

                      $strAddrArray = explode(" ", $data['rcpt_dtl_addr']);

                      if ($addr == $strAddrArray['0']) {
                        $intAptChk++;

                      }
                    }
                  }

                  if ($intAptChk > 0) {
                    echo "Y";
                  } else {
                    echo "N";
                  }
                  ?>
                </td>
                <td style="text-align: center;">
                  <button type="button" class="btn btn-sm btn-outline-light" onclick="res_ground();">저장</button>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>

      <!-- 예약확정 -->
      <div class="main-card mb-3 card">
        <div class="card-body">
          <h5 class="card-title">예약확정</h5>
          <table class="table table-bordered table-sm tbl_basic2 apt-info-table">
            <colgroup>
              <col width="15%" />
              <col width="35%" />
              <col width="15%" />
              <col width="35%" />
            </colgroup>
            <tbody>
              <tr>
                <th>방문희망일시</th>
                <td class="d-flex gap-1" colspan="3">
                  <input type="text" name="rsrv_date" id="rsrv_date" class="form-control"
                    value="<?= $data['rsrv_date'] ?>" size="13" readonly />
                  <select class="form-select" name="rsrv_tm_ap" onchange="rsrv_tm_ap_onchange(this.value)">
                    <option value="">오전/오후</option>
                    <option value="00" <?php if (empty($data['rsrv_tm_ap']))
                      echo "selected"; ?>>무관</option>
                    <option value="AM" <?php if ($data['rsrv_tm_ap'] == 'AM')
                      echo "selected"; ?>>오전</option>
                    <option value="PM" <?php if ($data['rsrv_tm_ap'] == 'PM')
                      echo "selected"; ?>>오후</option>
                  </select>
                  <select class="form-select" name="rsrv_tm_hour" id="rsrv_tm_hour">
                    <option value="">-시-</option>
                    <option value="00" <? if ('00' == $data['rsrv_tm_hour'])
                      echo "selected"; ?>>무관</option>
                    <?php for ($i = 1; $i <= 24; $i++) {
                      $hh = sprintf("%02s", $i);
                      ?>
                      <option value="<?= $hh ?>" <? if ($hh == $data['rsrv_tm_hour'])
                          echo "selected"; ?>>
                        <?= $hh ?>시
                      </option>
                    <?php } ?>
                  </select>
                </td>
              </tr>
              <tr>
                <th>방문날짜</th>
                <td>
                  <?= $data['rsrv_date'] ?>
                  <?= $data['rsrv_tm_ap'] ?>
                  <?php if (!empty($data['rsrv_tm_hour']))
                    echo $data['rsrv_tm_hour'] . "시"; ?>
                </td>
                <th>담당자</th>
                <td class="d-flex gap-1">
                  <select class="form-select" id="bonbu">
                    <option value="">본부선택</option>
                    <?php

                    foreach ($bonbu as $b): ?>
                      <option value="<?= $b['dept_sq'] ?>">
                        <?= $b['dept_nm'] ?>
                      </option>
                    <?php endforeach; ?>
                  </select>
                  <select class="form-select" id="team">
                    <option value="">팀선택</option>
                    <?php
                    $team_dept = $data['dept_sq'];
                    if (empty($data['dept_sq'])) {
                      $team_dept = $data['region_dept_sq'];
                    }
                    foreach ($team as $t): ?>
                      <option value="<?= $t['dept_sq'] ?>" <?php if ($t['dept_sq'] === $team_dept) {
                          echo "selected";
                        } ?>>
                        <?= $t['dept_nm'] ?>
                      </option>
                    <?php endforeach; ?>
                  </select>
                  <select class="form-select" id="user">
                    <option value="">담당자선택</option>
                  </select>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="card-footer justify-content-end">
          <button type="button" class="btn btn-sm btn-outline-success">저장</button>
        </div>
      </div>

      <!-- 동영상촬영여부 -->
      <div class="main-card mb-3 card">
        <div class="card-body">
          <h5 class="card-title">동영상촬영여부</h5>
          <table class="table table-bordered table-sm tbl_basic2 apt-info-table">
            <tbody>
              <tr>
                <th style="width: 200px;">
                  동영상촬영여부
                </th>
                <td>
                  <select class="form-select" name="exp_movie_yn" id="exp_movie_yn" style="width: 150px;">\
                    <option value="">-선택-</option>
                    <option value="N" <?php if ($data['exp_movie_yn'] == 'N')
                      echo "selected"; ?>>N</option>
                    <option value="Y" <?php if ($data['exp_movie_yn'] == 'N')
                      echo "selected"; ?>>Y</option>
                  </select>
                </td>
                <td style="text-align: center;">
                  <button type="button" class="btn btn-sm btn-outline-light" onclick="requestMovie();">저장</button>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>

      <!-- 중개인 요청사항 -->
      <div class="main-card mb-3 card">
        <div class="card-body">
          <h5 class="card-title">중개인 요청사항</h5>
          <table class="table table-bordered table-sm tbl_basic2 apt-info-table">
            <tbody>
              <tr>
                <th style="width: 200px;">
                  중개인 요청사항
                </th>
                <td>
                  <textarea class="form-control" id="request_msg" style="height:100px;resize: none;">
                    <?= $data['request_msg'] ?>
                  </textarea>
                </td>
                <td style="text-align: center;">
                  <button type="button" class="btn btn-sm btn-outline-light" onclick="requestMessage();">저장</button>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>

      <!-- 시간대별 통계 -->
      <div class="main-card mb-3 card">
        <div class="card-body">
          <h5 class="card-title">
            담당자 예약현황 (<?= $data['rsrv_date'] ?>)
          </h5>
          <table class="table table-bordered table-sm tbl_basic2 apt-info-table">
            <tbody>
              <tr>
                <?php if ($tmCount): ?>
                  <?php foreach ($tmCount as $tm): ?>
                    <th>
                      <?= $tm['rsrv_tm_ap'] ?>
                      <?= $tm['rsrv_tm_hour'] ?> 시
                    </th>
                  <?php endforeach; ?>
                <?php endif; ?>
              </tr>
              <tr>
                <?php if ($tmCount): ?>
                  <?php foreach ($tmCount as $tm): ?>
                    <td><?= $tm['cnt'] ?> 건</td>
                  <?php endforeach; ?>
                <?php endif; ?>
              </tr>
            </tbody>
          </table>
        </div>
      </div>

      <!-- 취소 -->
      <div class="main-card mb-3 card">
        <div class="card-body">
          <h5 class="card-title">취소</h5>
          <table class="table table-bordered table-sm tbl_basic2 apt-info-table">
            <tbody>
              <tr>
                <th style="width: 100px;">
                  <div id="ground_plan">취소</div>
                </th>
                <td class="d-flex gap-1">
                  <select class="form-select" name="result_cd2" id="result_cd2" style="width: 100px;">
                    <option value="">분류1</option>
                    <?php foreach ($codes as $c): ?>
                      <?php if ($c['category'] === "RECEIPT_STATUS2"): ?>
                        <?php if (substr($c['cd'], 0, 2) == "90"): ?>
                          <option value="<?= $c['cd'] ?>" <?php if ($c['cd'] === $data['result_cd2']) {
                              echo "selected";
                            } ?>>
                            <?= $c['cd_nm'] ?>
                          </option>
                        <?php endif; ?>
                      <?php endif; ?>
                    <?php endforeach; ?>
                  </select>
                  <select class="form-select" name="result_cd3" id="result_cd3" style="width: 100px;">
                    <option value="">분류2</option>
                  </select>
                </td>
                <th style="width: 150px;">취소사유</th>
                <td>
                  <input type="text" class="form-control" id="result_msg" value="<?= $data['result_msg'] ?>" />
                </td>
                <td style="text-align: center;">
                  <button type="button" class="btn btn-sm btn-outline-light" onclick="rsrvcancel();">저장</button>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>

      <!-- 매물 상태 정보 -->
      <div class="main-card mb-3 card">
        <div class="card-body">
          <h5 class="card-title">매물 상태 정보
            <div class="d-flex justify-content-end">
              <?php
              if (in_array($usr_level, ['1'])): ?>
                <button type="button" class="btn btn-outline-warning">검수지연으로 상태변경</button>
              <?php endif; ?>
            </div>

          </h5>
          <table class="table table-bordered table-sm tbl_basic2 apt-info-table">
            <?php
            $stat1 = $data['rsrv_delay_dt_dt'] . "<br/>" . $data['rsrv_delay_dt_tm'];		//예약지연
            $stat2 = $data['rsrv_cplt_dt_dt'] . "<br/>" . $data['rsrv_cplt_dt_tm'];		//에약확인
            if ($data['req_rec_yn'] == "Y") {												//촬영
              $stat3 = $data['photo_save_dt_dt'] . "<br/>" . $data['photo_save_dt_tm'] . "<br/>녹취필요";
            } else {
              $stat3 = $data['photo_save_dt_dt'] . "<br/>" . $data['photo_save_dt_tm'];
            }
            $stat4 = $data['check_delay_dt_dt'] . "<br/>" . $data['check_delay_dt_tm'];	//검수지연
            $stat5 = $data['check_fail_dt_dt'] . "<br/>" . $data['check_fail_dt_tm'];	//검수실패
            $stat6 = $data['check_dt_dt'] . "<br/>" . $data['check_dt_tm'];				//검수
            $stat7 = $data['check_cplt_dt_dt'] . "<br/>" . $data['check_cplt_dt_tm'];		//검수완료
            $stat8 = $data['cancel_dt_dt'] . "<br/>" . $data['cancel_dt_tm'];				//취소
            
            $currStat = substr($data['rcpt_stat'], 0, 2);
            $currStat2 = substr($data['rcpt_stat'], 2, 2);

            if ($currStat == 15) {		//예약지연
              $stat1 = "<b><font color='red'>" . $stat1 . "</font></b>";
            } else if ($currStat == 20) {		//예약확인
              $stat2 = "<b><font color='red'>" . $stat2 . "</font></b>";
            } else if ($currStat == 40) {		//촬영
              $stat3 = "<b><font color='red'>" . $stat3 . "</font></b>";
            } else if ($currStat == 50) {		//검수
              $stat6 = "<b><font color='red'>" . $stat6 . "</font></b>";
            } else if ($currStat == 60) {		//검수완료
              $stat7 = "<b><font color='red'>" . $stat7 . "</font></b>";
            } else if ($currStat == 70) {		//검수지연
              $stat4 = "<b><font color='red'>" . $stat4 . "<br/>" . $data['rcpt_stat_nm'] . "</font></b>";
            }
            if ($currStat == 90 && $currStat2 != 50) {		//취소
              $stat8 = "<b><font color='red'>" . $stat8 . "<br/>" . $data['result_cd2_nm'] . "</font></b>";
            } else if ($currStat == 90 && $currStat2 == 50) {		//검수실패
              $stat5 = "<b><font color='red'>" . $stat5 . "<br/>" . $data['rcpt_stat_nm'] . "</font></b>";
            }
            ?>
            <tr>
              <th rowspan="2" width="100" style="text-align:center">현장확인<br />진행상황</th>
              <th style="text-align:center">예약지연</th>
              <th style="text-align:center">예약확인</th>
              <th style="text-align:center">촬영</th>
              <th style="text-align:center">검수지연</th>
              <th style="text-align:center">검수실패</th>
              <th style="text-align:center">검수</th>
              <th style="text-align:center">검수완료</th>
              <th style="text-align:center">취소</th>
            </tr>
            <tr>
              <td style="text-align:center; height: 50px;">
                <?= $stat1 ?>
              </td>
              <td style="text-align:center">
                <?= $stat2 ?>
              </td>
              <td style="text-align:center">
                <?= $stat3 ?>
              </td>
              <td style="text-align:center">
                <?= $stat4 ?>
              </td>
              <td style="text-align:center">
                <?= $stat5 ?>
              </td>
              <td style="text-align:center">
                <?= $stat6 ?>
              </td>
              <td style="text-align:center">
                <?= $stat7 ?>
              </td>
              <td style="text-align:center">
                <?= $stat8 ?>
              </td>
            </tr>
            <?php if (!in_array($usr_level, [])): ?>
              <tr>
                <th>상태변경</th>
                <?php if ($currStat == 10) { ?>
                  <td height="33" style="text-align:center">
                    <button type="button" class="btn btn-sm btn-success" onclick="chgStatus(150000);">예약지연</button>
                  </td>
                  <td style="text-align:center">
                    <button type="button" class="btn btn-sm btn-success" onclick="chgStatus(200000);">예약확인</button>
                  </td>
                  <td style="text-align:center">
                    <button type="button" class="btn btn-sm btn-success" disabled>촬영</button>
                  </td>
                  <td style="text-align:center">
                    <button type="button" class="btn btn-sm btn-success" disabled>검수지연</button>
                  </td>
                  <td style="text-align:center">
                  </td>
                  <td style="text-align:center">
                    <button type="button" class="btn btn-sm btn-success" disabled>검수</button>
                  </td>
                  <td style="text-align:center"></td>
                  <td style="text-align:center"></td>
                <?php } else if ($currStat == 15) { ?>
                    <td height="33" style="text-align:center">
                      <button type="button" class="btn btn-sm btn-success" disabled>예약지연</button>
                    </td>
                    <td style="text-align:center">
                      <button type="button" class="btn btn-sm btn-success" onclick="chgStatus(200000);">예약확인</button>
                    </td>
                    <td style="text-align:center">
                      <button type="button" class="btn btn-sm btn-success" disabled>촬영</button>
                    </td>
                    <td style="text-align:center">
                      <button type="button" class="btn btn-sm btn-success" disabled>검수지연</button>
                    </td>
                    <td style="text-align:center">
                    </td>
                    <td style="text-align:center">
                      <button type="button" class="btn btn-sm btn-success" disabled>검수</button>
                    </td>
                    <td style="text-align:center"></td>
                    <td style="text-align:center"></td>
                <?php } else if ($currStat == 20) { ?>
                      <td height="33" style="text-align:center">
                        <button type="button" class="btn btn-sm btn-success" disabled>예약지연</button>
                      </td>
                      <td style="text-align:center">
                        <button type="button" class="btn btn-sm btn-success" disabled>예약확인</button>
                      </td>
                      <td style="text-align:center">
                        <button type="button" class="btn btn-sm btn-success" onclick="chgStatus(400000);">촬영</button>
                      </td>
                      <td style="text-align:center">
                        <button type="button" class="btn btn-sm btn-success" disabled>검수지연</button>
                      </td>
                      <td style="text-align:center">
                      </td>
                      <td style="text-align:center">
                        <button type="button" class="btn btn-sm btn-success" disabled>검수</button>
                      </td>
                      <td style="text-align:center"></td>
                      <td style="text-align:center"></td>
                <?php } else if ($currStat == 40) { ?>
                        <td height="33" style="text-align:center">
                          <button type="button" class="btn btn-sm btn-success" disabled>예약지연</button>
                        </td>
                        <td style="text-align:center">
                          <button type="button" class="btn btn-sm btn-success" disabled>예약확인</button>
                        </td>
                        <td style="text-align:center">
                          <button type="button" class="btn btn-sm btn-success" disabled>촬영</button>
                        </td>
                        <td style="text-align:center">
                          <button type="button" class="btn btn-sm btn-success" onclick="chgStatus(700000);">검수지연</button>
                        </td>
                        <td style="text-align:center">
                        </td>
                        <td style="text-align:center">
                          <button type="button" class="btn btn-sm btn-success"
                            onclick="chgStatus(500000, '<?= $data['rcpt_product'] ?>', '<?= $data['chg_floor_yn'] ?>');">검수</button>
                        </td>
                        <td style="text-align:center"></td>
                        <td style="text-align:center"></td>
                <?php } else if ($currStat == 70) { ?>
                          <td height="33" style="text-align:center">
                            <button type="button" class="btn btn-sm btn-success" disabled>예약지연</button>
                          </td>
                          <td style="text-align:center">
                            <button type="button" class="btn btn-sm btn-success" disabled>예약확인</button>
                          </td>
                          <td style="text-align:center">
                            <button type="button" class="btn btn-sm btn-success" disabled>촬영</button>
                          </td>
                          <td style="text-align:center">
                            <button type="button" class="btn btn-sm btn-success" disabled>검수지연</button>
                          </td>
                          <td style="text-align:center">
                          </td>
                          <td style="text-align:center">
                            <button type="button" class="btn btn-sm btn-success"
                              onclick="chgStatus(500000, '<?= $data['rcpt_product'] ?>', '<?= $data['chg_floor_yn'] ?>');">검수</button>
                          </td>
                          <td style="text-align:center;"> </td>
                          <td style="text-align:center"></td>
                <?php } else if ($currStat == 50) { ?>
                            <td height="33" style="text-align:center">
                              <button type="button" class="btn btn-sm btn-success" disabled>예약지연</button>
                            </td>
                            <td style="text-align:center">
                              <button type="button" class="btn btn-sm btn-success" disabled>예약확인</button>
                            </td>
                            <td style="text-align:center">
                              <button type="button" class="btn btn-sm btn-success" onclick="chgStatus(400000);">촬영</button>
                            </td>
                            <td style="text-align:center">
                              <button type="button" class="btn btn-sm btn-success" onclick="chgStatus(700000);">검수지연</button>
                            </td>
                            <td style="text-align:center">
                            </td>
                            <td style="text-align:center">
                              <button type="button" class="btn btn-sm btn-success" disabled>검수</button>
                            </td>
                            <td style="text-align:center"></td>
                            <td style="text-align:center"></td>
                <?php } else if ($currStat == 60) {	//검수완료 ?>
                              <td height="33" style="text-align:center">
                                <button type="button" class="btn btn-sm btn-success" disabled>예약지연</button>
                              </td>
                              <td style="text-align:center">
                                <button type="button" class="btn btn-sm btn-success" disabled>예약확인</button>
                              </td>
                              <td style="text-align:center">
                                <button type="button" class="btn btn-sm btn-success" disabled>촬영</button>
                              </td>
                              <td style="text-align:center">
                                <button type="button" class="btn btn-sm btn-success" disabled>검수지연</button>
                              </td>
                              <td style="text-align:center">
                              </td>
                              <td style="text-align:center">
                                <button type="button" class="btn btn-sm btn-success" disabled>검수</button>
                              </td>
                              <td style="text-align:center"></td>
                              <td style="text-align:center"></td>
                <?php }
                if ($currStat == 90 && $currStat2 != 50) { 	//취소 ?>
                  <td height="33" style="text-align:center">
                    <button type="button" class="btn btn-sm btn-success" disabled>예약지연</button>
                  </td>
                  <td style="text-align:center">
                    <button type="button" class="btn btn-sm btn-success" disabled>예약확인</button>
                  </td>
                  <td style="text-align:center">
                    <button type="button" class="btn btn-sm btn-success" disabled>촬영</button>
                  </td>
                  <td style="text-align:center">
                    <button type="button" class="btn btn-sm btn-success" disabled>검수지연</button>
                  </td>
                  <td style="text-align:center">
                  </td>
                  <td style="text-align:center">
                    <button type="button" class="btn btn-sm btn-success" disabled>검수</button>
                  </td>
                  <td style="text-align:center"></td>
                  <td style="text-align:center"></td>
                <?php } else if ($currStat == 90 && $currStat2 == 50) {	//검수실패 ?>
                    <td height="33" style="text-align:center">
                      <button type="button" class="btn btn-sm btn-success" disabled>예약지연</button>
                    </td>
                    <td style="text-align:center">
                      <button type="button" class="btn btn-sm btn-success" disabled>예약확인</button>
                    </td>
                    <td style="text-align:center">
                      <button type="button" class="btn btn-sm btn-success" disabled>촬영</button>
                    </td>
                    <td style="text-align:center">
                      <button type="button" class="btn btn-sm btn-success" disabled>검수지연</button>
                    </td>
                    <td style="text-align:center">
                    </td>
                    <td style="text-align:center">
                      <button type="button" class="btn btn-sm btn-success"
                        onclick="chgStatus(500000, '<?= $data['rcpt_product'] ?>', '<?= $data['chg_floor_yn'] ?>');">검수</button>
                    </td>
                    <td style="text-align:center"></td>
                    <td style="text-align:center"></td>
                <?php } ?>
              </tr>
            <?php endif; ?>
          </table>
        </div>
      </div>

      <!-- SMS 보내기 -->
      <div class="main-card mb-3 card">
        <div class="card-body">
          <h5 class="card-title">SMS 보내기</h5>
          <table class="table table-bordered table-sm tbl_basic2 apt-info-table">
            <?php
            $smsList = [];
            foreach ($codes as $c) {
              if ($c['category'] === "SMS_MSG_TYPE") {
                array_push($smsList, $c);
              }
            }

            $smsTot = sizeof($smsList);
            $rowspan = ceil($smsTot / 6);
            ?>
            <tr>
              <th rowspan="<?= $rowspan ?>" style="text-align:center">
                상황별<br />SMS선택
              </th>
              <?php
              $smsCnt = 1;
              foreach ($smsList as $sms1):
                if (!in_array($sms1['cd'], ["S10", "S3", "S7", "S15"])) {
                  continue;
                }
                ?>
                <td style="text-align:center">
                  <a href="javascript:viewSmsPop('<?= $sms1['cd'] ?>');">
                    <?= $sms1['category_nm'] ?>
                  </a>
                </td>
                <?php
                if ($smsCnt % 6 == 0) {
                  echo "</tr><tr>";
                }
                $smsCnt++;
              endforeach; ?>
            </tr>
          </table>
        </div>
      </div>

      <!-- 매물 위치 -->
      <div class="main-card mb-3 card">
        <div class="card-body">
          <h5 class="card-title">매물 위치</h5>
          <table class="table table-bordered table-sm tbl_basic2 apt-info-table">
            <colgroup>
              <col width="25%">
            </colgroup>

            <tr>
              <td colspan="2" style="padding:5px;">
                <div id="mapArea" style="width:100%;height:300px;"></div>
              </td>
            </tr>
          </table>
        </div>
      </div>

      <!-- 확인 정보 및 사진 정보 -->
      <div class="main-card mb-3 card">
        <div class="card-body">
          <h5 class="card-title">확인 정보 및 사진 정보</h5>
          <div class="row g-3">

            <!-- 홍보확인서 -->
            <div class="col-12 col-lg-10p">
              <div class="border rounded-3 p-2 ">
                <div class="d-flex justify-content-between align-items-center mb-2">
                  <div class="fw-semibold">홍보확인서</div>

                  <!-- 업로드 버튼 -->
                  <button type="button" class="btn btn-sm btn-outline-secondary"
                    onclick="viewFilePop('I1', '')">파일</button>
                </div>

                <div class="ratio ratio-4x3 bg-light rounded-2 overflow-hidden">
                  <?php
                  $arrI1 = array();
                  reset($images);
                  foreach ($images as $key => $img) {
                    if ($img['img_type'] == 'I1') {
                      $arrI1[] = $img;
                      unset($images[$key]);
                    }
                  }
                  if (!empty($arrI1)) {
                    foreach ($arrI1 as $img) {
                      list($filename, $ext) = explode(".", $img['img_filenm']);
                      $thumbname = $filename . "_thumb.jpg";

                      $imgPath = $img['img_path'] . $thumbname;
                      $link = $img['img_path'] . $img['img_filenm'];

                      $link = realFilePath($link);
                      $imgPath = realFilePath($imgPath);

                      if ($img['cloud_upload_yn'] == 'Y') {
                        $imgPath = NCLOUD_OBJECT_STORAGE_URL . $img['img_path'] . $thumbname;
                        $link = NCLOUD_OBJECT_STORAGE_URL . $img['img_path'] . $img['img_filenm'];
                      }

                      $sq = $img['img_sq'];
                    }
                    ?>
                    <a onclick="fn_preview('<?= $link ?>')">
                      <img id="photo-display2_I1" src="<?= $link ?>" alt="홍보확인서" class="w-100  object-fit-contain">
                    </a>
                  <?php } else { ?>
                    <img id="photo-display2_I1" src="/plugin/img/photo.gif" alt="홍보확인서" class="w-100  object-fit-contain">
                  <?php } ?>
                </div>
              </div>
            </div>

            <!-- 촬영동의서 -->
            <div class="col-12 col-lg-10p">
              <div class="border rounded-3 p-2 ">
                <div class="d-flex justify-content-between align-items-center mb-2">
                  <div class="fw-semibold">촬영동의서</div>
                  <button type="button" class="btn btn-sm btn-outline-secondary"
                    onclick="viewFilePop('I10', '')">파일</button>
                </div>

                <div class="ratio ratio-4x3 bg-light rounded-2 overflow-hidden">
                  <?php
                  if (isset($imgs_count['I10']) && $imgs_count['I10'] != 0) {
                    foreach ($images as $img) {
                      if ($img['img_type'] == 'I10') {
                        $temp2 = explode(".", $img['img_filenm']);
                        //$name = $temp2[0]."_thumb.".$temp2[1];
                        $name = $temp2[0] . "_thumb.jpg";

                        $image_name = $img['img_path'] . $img['img_filenm'];
                        $thumbnail_image_name = $img['img_path'] . $name;
                        $agreement_image = realFilePath($image_name);
                        $agreement_image_thumbnail = realFilePath($thumbnail_image_name);

                        if ($img['cloud_upload_yn'] == 'Y') {
                          $agreement_image_thumbnail = NCLOUD_OBJECT_STORAGE_URL . $thumbnail_image_name;
                          $agreement_image = NCLOUD_OBJECT_STORAGE_URL . $img['img_path'] . $img['img_filenm'];
                        }
                        ?>

                        <a onclick="fn_preview('<?= $agreement_image ?>')">
                          <img src="<?= $agreement_image_thumbnail ?>" alt="촬영동의서" class="w-100  object-fit-contain">
                        </a>
                        <?php
                      }
                    }
                  } else {
                    ?>
                    <img src="/plugin/img/photo.gif" alt="촬영동의서" class="w-100  object-fit-contain">
                  <?php } ?>
                </div>
              </div>
            </div>

            <!-- 현장확인내역서 -->
            <div class="col-12 col-lg-10p">
              <div class="border rounded-3 p-2 ">
                <div class="d-flex justify-content-between align-items-center mb-2">
                  <div class="fw-semibold">현장확인내역서</div>
                  <button type="button" class="btn btn-sm btn-outline-secondary"
                    onclick="viewFilePop('I2', '')">파일</button>
                </div>

                <div class="ratio ratio-4x3 bg-light rounded-2 overflow-hidden">
                  <?php
                  if (isset($imgs_count['I2']) && $imgs_count['I2'] != 0) {
                    foreach ($images as $img) {
                      if ($img['img_type'] == 'I2') {
                        $temp2 = explode(".", $img['img_filenm']);
                        //$name = $temp2[0]."_thumb.".$temp2[1];
                        $name = $temp2[0] . "_thumb.jpg";
                        $image_name = $img['img_path'] . $img['img_filenm'];
                        $thumbnail_image_name = $img['img_path'] . $name;
                        $agreement_image = realFilePath($image_name);
                        $agreement_image_thumbnail = realFilePath($thumbnail_image_name);

                        if ($img['cloud_upload_yn'] == 'Y') {
                          $agreement_image_thumbnail = NCLOUD_OBJECT_STORAGE_URL . $thumbnail_image_name;
                          $agreement_image = NCLOUD_OBJECT_STORAGE_URL . $img['img_path'] . $img['img_filenm'];
                        }
                        ?>
                        <a onclick="fn_preview('<?= $agreement_image ?>')">
                          <img src="<?= $agreement_image_thumbnail ?>" alt="현장확인내역서" class="w-100  object-fit-contain">
                        </a>
                        <?php
                      }
                    }
                  } else {
                    ?>
                    <img src="/plugin/img/photo.gif" alt="현장확인내역서" class="w-100  object-fit-contain">
                  <?php } ?>
                </div>
              </div>
            </div>
          </div>

          <!-- 분양권(썸네일 여러개 + 파일 버튼) -->
          <div class="my-3">
            <div class="border rounded-3 p-2">
              <div class="d-flex justify-content-between align-items-center mb-2">
                <div class="fw-semibold">분양권 (최대 5장)</div>
                
                <!-- 버튼 그룹 -->
                <div class="d-flex gap-1">
                  <button type="button" class="btn btn-sm btn-outline-secondary"
                    onclick="viewFilePop('I8', '0')">파일</button>
                  <button type="button" class="btn btn-sm btn-outline-success"
                    onclick="downloadImagesByType('I8', '분양권')">일괄다운로드</button>
                  <button type="button" class="btn btn-sm btn-outline-danger"
                    onclick="deleteAllImagesByType('I8', '분양권')">일괄삭제</button>
                </div>
              </div>

              <div class="d-flex flex-wrap gap-2">
                <!-- NOTE: 기존 코드가 id 중복(photo-display2_I8)이라 반드시 고쳐야 함 -->
                <?php
                $arrI8 = array();
                reset($images);
                foreach ($images as $key => $img) {
                  if ($img['img_type'] == 'I8') {
                    $arrI8[] = $img;
                    unset($images[$key]);
                  }
                }
                if (!empty($arrI8)) {

                  foreach ($arrI8 as $img) {
                    list($filename, $ext) = explode(".", $img['img_filenm']);
                    $thumbname = $filename . "_thumb.jpg";

                    $imgPath = $img['img_path'] . $thumbname;
                    $link = $img['img_path'] . $img['img_filenm'];
                    
                    $agreement_image = realFilePath($link);
                    $agreement_image_thumbnail = realFilePath($imgPath);

                    if ($img['cloud_upload_yn'] == 'Y') {
                      $agreement_image_thumbnail = NCLOUD_OBJECT_STORAGE_URL . $imgPath;
                      $agreement_image = NCLOUD_OBJECT_STORAGE_URL . $link;
                    }
                    ?>
                    <div style="width: 150px;">
                      <div class="ratio ratio-4x3 bg-light rounded-2 overflow-hidden">
                        <a onclick="fn_preview('<?= $agreement_image ?>')" class="d-flex align-items-center justify-content-center">
                          <img src="<?= $agreement_image_thumbnail ?>" alt="분양권" class="w-100 h-100 object-fit-contain">
                        </a>
                      </div>
                    </div>
                  <?php } ?>
                <?php } else { ?>
                  <div style="width: 150px;">
                    <div class="ratio ratio-4x3 bg-light rounded-2 overflow-hidden">
                      <img src="/plugin/img/photo.gif" alt="분양권" class="w-100 h-100 object-fit-contain">
                    </div>
                  </div>
                <?php } ?>
              </div>
            </div>
          </div>

          <!-- 매물사진 -->
          <div class="my-3">
            <div class="border rounded-3 p-2">
              <div class="d-flex justify-content-between align-items-center mb-2">
                <div class="fw-semibold">매물사진 (최대 15장)</div>

                <div class="d-flex gap-1 flex-wrap">
                  <button type="button" class="btn btn-sm btn-outline-secondary" onclick="viewFilePop('I4', '')">파일</button>
                  <button type="button" class="btn btn-sm btn-outline-primary"
                    onclick="savePropertyImageOrder()">순서저장</button>
                  <button type="button" class="btn btn-sm btn-outline-success"
                    onclick="downloadImagesByType('I4', '매물사진')">일괄다운로드</button>
                  <button type="button" class="btn btn-sm btn-outline-danger"
                    onclick="deleteAllImagesByType('I4', '매물사진')">일괄삭제</button>
                </div>
              </div>

              <div id="property-images-container" class="d-flex flex-wrap gap-2">
              <!-- 기존 li+table 구조 대신 썸네일 카드로 -->
              <!-- 필요 개수만큼 반복 -->
              <?php
              if (isset($imgs_count['I4']) && $imgs_count['I4'] != 0) {
                $cnt = 0;
                foreach ($images as $img) {
                  if ($img['img_type'] == 'I4') {

                    $link = $img['img_path'] . $img['img_filenm'];
                    $temp2 = explode(".", $img['img_filenm']);
                    //$name = $temp2[0]."_thumb.".$temp2[1];
                    $thumbname = $temp2[0] . "_thumb.jpg";
                    $thumblink = $img['img_path'] . $thumbname;

                    $agreement_image = realFilePath($link);
                    $agreement_image_thumbnail = realFilePath($thumblink);

                    if ($img['cloud_upload_yn'] == 'Y') {
                      $agreement_image_thumbnail = NCLOUD_OBJECT_STORAGE_URL . $thumblink;
                      $agreement_image = NCLOUD_OBJECT_STORAGE_URL . $link;
                    }

                    $cnt++;
                    ?>
                    <div class="property-image-item" style="width: 150px; position: relative; cursor: move;" data-img-sq="<?= $img['img_sq'] ?>" data-view-order="<?= $img['view_odr'] ?>">
                      <div class="image-order-badge" style="position: absolute; top: 1px; left: 1px; background: #6c9ee8; color: white; min-width: 24px; height: 24px; padding: 0 6px; border-radius: 12px; display: inline-flex; align-items: center; justify-content: center; font-size: 13px; font-weight: 500; z-index: 10; box-shadow: 0 2px 4px rgba(0,0,0,0.2);"><?= $cnt ?></div>
                      <div class="ratio ratio-4x3 bg-light rounded-2 overflow-hidden">
                        <a onclick="fn_preview('<?= $agreement_image ?>')" class="d-flex align-items-center justify-content-center">
                          <img src="<?= $agreement_image_thumbnail ?>" alt="매물사진" class="w-100 h-100 object-fit-contain">
                        </a>
                      </div>
                    </div>
                  <?php }
                } ?>
              <?php } else { ?>
                <div class="property-image-item" style="width: 150px; position: relative;">
                  <div class="ratio ratio-4x3 bg-light rounded-2 overflow-hidden">
                    <img src="/plugin/img/photo.gif" alt="매물사진" class="w-100 h-100 object-fit-contain">
                  </div>
                </div>
              <?php } ?>
              </div>
            </div>
          </div>

          <!-- 동영상 / 평면도 / 체크리스트 -->
          <div class="row g-3">
          <!-- 동영상 -->
          <div class="col-12 col-lg-10p">
            <div class="border rounded-3 p-2 ">
              <div class="d-flex justify-content-between align-items-center mb-2">
                <div class="fw-semibold">동영상</div>
                <button type="button" class="btn btn-sm btn-outline-secondary"
                  onclick="viewFilePop('V1', '')">파일</button>
              </div>
              <div class="ratio ratio-4x3 bg-light rounded-2 overflow-hidden">
                  <?php
                  if (isset($imgs_count['V1']) && $imgs_count['V1'] != 0) {
                    foreach ($images as $img) {
                      if ($img['img_type'] == 'V1') {

                        $link = $img['img_path'] . $img['img_filenm'];
                        $temp2 = explode(".", $img['img_filenm']);
                        //$name = $temp2[0]."_thumb.".$temp2[1];
                        $name = $temp2[0] . "_thumb.jpg";
                        $thumblink = $img['img_path'] . $name;

                        if ($img['cloud_upload_yn'] == 'Y') {
                          $thumblink = NCLOUD_OBJECT_STORAGE_URL . $thumblink;
                          $link = NCLOUD_OBJECT_STORAGE_URL . $link;
                        }
                        ?>

                        <a onclick="fn_preview('<?= $link ?>', 'vdo')">
                          <img src="/plugin/img/video.png" alt="동영상" class="w-100 object-fit-contain">
                        </a>

                        <?php
                      }
                    }
                  } else {
                    ?>
                    <img src="/plugin/img/photo.gif" alt="동영상" class="w-100 object-fit-contain">
                  <?php } ?>
                </div>
              </div>
            </div>

          <!-- 평면도 -->
          <div class="col-12 col-lg-10p">
            <div class="border rounded-3 p-2 ">
              <div class="d-flex justify-content-between align-items-center mb-2">
                <div class="fw-semibold">평면도</div>
                  <button type="button" class="btn btn-sm btn-outline-secondary"
                    onclick="viewFilePop('I5', '')">파일</button>
                </div>
                <div class="ratio ratio-4x3 bg-light rounded-2 overflow-hidden">
                  <?php
                  if (isset($imgs_count['I5']) && $imgs_count['I5'] != 0) {
                    foreach ($images as $img) {
                      if ($img['img_type'] == 'I5') {
                        $link = $img['img_path'] . $img['img_filenm'];
                        $temp2 = explode(".", $img['img_filenm']);
                        $name = $temp2[0] . "_thumb.jpg";
                        $thumblink = $img['img_path'] . $name;

                        if ($img['cloud_upload_yn'] == 'Y') {
                          $thumblink = NCLOUD_OBJECT_STORAGE_URL . $thumblink;
                          $link = NCLOUD_OBJECT_STORAGE_URL . $link;
                        }
                        ?>
                        <a onclick="fn_preview('<?= $link ?>')">
                          <img id="photo-display2_I1" src="<?= $thumblink ?>" alt="평면도" class="w-100  object-fit-contain">
                        </a>
                      <?php }
                    }
                  } else { ?>

                    <img src="/plugin/img/photo.gif" alt="평면도" class="w-100  object-fit-contain">
                  <?php } ?>
                </div>
              </div>
            </div>

          <!-- 체크리스트 -->
          <div class="col-12 col-lg-10p">
            <div class="border rounded-3 p-2 ">
              <div class="d-flex justify-content-between align-items-center mb-2">
                <div class="fw-semibold">체크리스트</div>
                  <button type="button" class="btn btn-sm btn-outline-secondary"
                    onclick="viewFilePop('I11', '')">파일</button>
                </div>
                <div class="ratio ratio-4x3 bg-light rounded-2 overflow-hidden">
                  <?php
                  if (isset($imgs_count['I11']) && $imgs_count['I11'] != 0) {
                    foreach ($images as $img) {
                      if ($img['img_type'] == 'I11') {
                        $link = $img['img_path'] . $img['img_filenm'];
                        $temp2 = explode(".", $img['img_filenm']);
                        $name = $temp2[0] . "_thumb.jpg";
                        $thumblink = $img['img_path'] . $name;

                        if ($img['cloud_upload_yn'] == 'Y') {
                          $thumblink = NCLOUD_OBJECT_STORAGE_URL . $thumblink;
                          $link = NCLOUD_OBJECT_STORAGE_URL . $link;
                        }
                        ?>
                        <a onclick="fn_preview('<?= $link ?>')">
                          <img id="photo-display2_I1" src="<?= $thumblink ?>" alt="체크리스트" class="w-100  object-fit-contain">
                        </a>
                      <?php }
                    }
                  } else { ?>

                    <img src="/plugin/img/photo.gif" alt="체크리스트" class="w-100  object-fit-contain">
                  <?php } ?>
                </div>
              </div>
            </div>
          </div>

          <!-- 평면도중복 -->
          <?php if (!empty($dupleGroundPlan)): ?>
            <div class="border rounded-3 p-3 mb-3 bg-white">
              <div class="d-flex align-items-center justify-content-between mb-2">
                <div class="fw-semibold">평면도 중복</div>
                <span class="badge bg-secondary"><?= count($dupleGroundPlan ?? []) ?>건</span>
              </div>

              <div class="text-muted small py-2">중복된 평면도가 없습니다.</div>

              <div class="vstack gap-2">
                <?php $numbering = 1;
                foreach ($dupleGroundPlan as $row): ?>
                  <div class="border rounded-3 p-2 bg-light">
                    <div class="d-flex align-items-center justify-content-between">
                      <div class="fw-semibold">
                        <span class="badge bg-dark me-2">중복 <?= $numbering ?></span>
                        <span class="text-muted small">평면도 중복</span>
                      </div>
                    </div>

                    <div class="row g-2 mt-1 small">
                      <div class="col-12 col-md-6">
                        <div class="d-flex align-items-center gap-2">
                          <span class="text-muted">매물번호</span>
                          <span class="fw-semibold"><?= esc($row['rcpt_key'] ?? '') ?></span>
                        </div>
                      </div>
                      <div class="col-12 col-md-6">
                        <div class="d-flex align-items-center gap-2">
                          <span class="text-muted">촬영일자</span>
                          <span class="fw-semibold"><?= esc($row['photo_save_dt'] ?? '') ?></span>
                        </div>
                      </div>
                    </div>
                  </div>
                  <?php $numbering++; endforeach; ?>
              </div>
            </div>
          <?php endif; ?>

          <!-- 매물현장확인 체크리스트 -->
          <?php if ($data['exp_photo_yn'] == 'N'): ?>
            <div class="border rounded-3 p-3 mb-3 bg-white">
              <div class="d-flex align-items-center justify-content-between mb-2">
                <div class="fw-semibold">매물현장확인 체크리스트</div>
              </div>

              <div class="row g-2 align-items-center">
                <div class="col-1 col-md-1">
                  <label class="form-label mb-0">엘리베이터</label>
                </div>

                <div class="col-12 col-md-9">
                  <div class="d-flex gap-3">
                    <div class="form-check form-check-inline mb-0">
                      <input class="form-check-input" type="radio" name="elevator_yn" id="elevator_y" value="Y" <?php if ($result_check['elevator_yn'] == 'Y') {
                        echo 'checked="checked"';
                      } ?>>
                      <label class="form-check-label" for="elevator_y">있음</label>
                    </div>

                    <div class="form-check form-check-inline mb-0">
                      <input class="form-check-input" type="radio" name="elevator_yn" id="elevator_n" value="N" <?php if ($result_check['elevator_yn'] == 'N') {
                        echo 'checked="checked"';
                      } ?>>
                      <label class="form-check-label" for="elevator_n">없음</label>
                    </div>
                  </div>
                </div>
              </div>

              <div class="row g-2 align-items-center">
                <div class="col-1 col-md-1">
                  <label class="form-label mb-0">관리비/월</label>
                </div>

                <div class="col-12 col-md-9">
                  <div class="d-flex gap-3">
                    <input type="text" class="form-control" name="maintenance_fee" id="maintenance_fee"
                      value="<?= $result_check['maintenance_fee'] ?>" size="10" maxlength="10" style="width: 150px;" />
                  </div>
                </div>
              </div>

              <div class="row g-2 align-items-center">
                <div class="col-1 col-md-1">
                  <label class="form-label mb-0">엘리베이터CCTV</label>
                </div>

                <div class="col-12 col-md-9">
                  <div class="d-flex gap-3">
                    <div class="form-check form-check-inline mb-0">
                      <input class="form-check-input" type="radio" name="elevator_cctv_yn" id="elevator_cctv_y" value="Y"
                        <?php if ($result_check['elevator_cctv_yn'] == 'Y') {
                          echo 'checked="checked"';
                        } ?>>
                      <label class="form-check-label" for="elevator_cctv_y">있음</label>
                    </div>

                    <div class="form-check form-check-inline mb-0">
                      <input class="form-check-input" type="radio" name="elevator_cctv_yn" id="elevator_cctv_n" value="N"
                        <?php if ($result_check['elevator_cctv_yn'] == 'N') {
                          echo 'checked="checked"';
                        } ?>>
                      <label class="form-check-label" for="elevator_cctv_n">없음</label>
                    </div>
                  </div>
                </div>
              </div>

              <div class="row g-2 align-items-center">
                <div class="col-1 col-md-1">
                  <label class="form-label mb-0">건물내부CCTV</label>
                </div>

                <div class="col-12 col-md-9">
                  <div class="d-flex gap-3">
                    <div class="form-check form-check-inline mb-0">
                      <input class="form-check-input" type="radio" name="building_in_cctv_yn" id="building_in_cctv_y"
                        value="Y" <?php if ($result_check['building_in_cctv_yn'] == 'Y') {
                          echo 'checked="checked"';
                        } ?>>
                      <label class="form-check-label" for="building_in_cctv_y">있음</label>
                    </div>

                    <div class="form-check form-check-inline mb-0">
                      <input class="form-check-input" type="radio" name="building_in_cctv_yn" id="building_in_cctv_n"
                        value="N" <?php if ($result_check['building_in_cctv_yn'] == 'N') {
                          echo 'checked="checked"';
                        } ?>>
                      <label class="form-check-label" for="building_in_cctv_n">없음</label>
                    </div>
                  </div>
                </div>
              </div>

              <div class="row g-2 align-items-center">
                <div class="col-1 col-md-1">
                  <label class="form-label mb-0">건물외부CCTV</label>
                </div>

                <div class="col-12 col-md-9">
                  <div class="d-flex gap-3">
                    <div class="form-check form-check-inline mb-0">
                      <input class="form-check-input" type="radio" name="building_ou_cctv_yn" id="building_ou_cctv_y"
                        value="Y" <?php if ($result_check['building_ou_cctv_yn'] == 'Y') {
                          echo 'checked="checked"';
                        } ?>>
                      <label class="form-check-label" for="building_ou_cctv_y">있음</label>
                    </div>

                    <div class="form-check form-check-inline mb-0">
                      <input class="form-check-input" type="radio" name="building_ou_cctv_yn" id="building_ou_cctv_n"
                        value="N" <?php if ($result_check['building_ou_cctv_yn'] == 'N') {
                          echo 'checked="checked"';
                        } ?>>
                      <label class="form-check-label" for="building_ou_cctv_n">없음</label>
                    </div>
                  </div>
                </div>
              </div>

              <div class="row g-2 align-items-center">
                <div class="col-1 col-md-1">
                  <label class="form-label mb-0">난방방식</label>
                </div>

                <div class="col-12 col-md-9">
                  <div class="d-flex gap-3">
                    <input type="text" class="form-control" name="heating_type" id="heating_type"
                      value="<?= $result_check['heating_type'] ?>" size="10" maxlength="10" style="width: 150px;" />
                  </div>
                </div>
              </div>

              <div class="row g-2 align-items-center">
                <div class="col-1 col-md-1">
                  <label class="form-label mb-0">경비실</label>
                </div>

                <div class="col-12 col-md-9">
                  <div class="d-flex gap-3">
                    <div class="form-check form-check-inline mb-0">
                      <input class="form-check-input" type="radio" name="security_dept_yn" id="security_dept_y" value="Y"
                        <?php if ($result_check['security_dept_yn'] == 'Y') {
                          echo 'checked="checked"';
                        } ?>>
                      <label class="form-check-label" for="security_dept_y">있음</label>
                    </div>

                    <div class="form-check form-check-inline mb-0">
                      <input class="form-check-input" type="radio" name="security_dept_yn" id="security_dept_n" value="N"
                        <?php if ($result_check['security_dept_yn'] == 'N') {
                          echo 'checked="checked"';
                        } ?>>
                      <label class="form-check-label" for="security_dept_n">없음</label>
                    </div>
                  </div>
                </div>
              </div>


              <div class="row g-2 align-items-center">
                <div class="col-1 col-md-1">
                  <label class="form-label mb-0">주차장</label>
                </div>

                <div class="col-12 col-md-9">
                  <div class="d-flex gap-3">
                    <div class="form-check form-check-inline mb-0">
                      <input class="form-check-input" type="radio" name="parking_lot_yn" id="parking_lot_y" value="Y"
                        <?php if ($result_check['parking_lot_yn'] == 'Y') {
                          echo 'checked="checked"';
                        } ?>>
                      <label class="form-check-label" for="parking_lot_y">있음</label>
                    </div>

                    <div class="form-check form-check-inline mb-0">
                      <input class="form-check-input" type="radio" name="parking_lot_yn" id="parking_lot_n" value="N"
                        <?php if ($result_check['parking_lot_yn'] == 'N') {
                          echo 'checked="checked"';
                        } ?>>
                      <label class="form-check-label" for="parking_lot_n">없음</label>
                    </div>
                  </div>
                </div>
              </div>

              <div class="row g-2 align-items-center">
                <div class="col-1 col-md-1">
                  <label class="form-label mb-0">인터폰</label>
                </div>

                <div class="col-12 col-md-9">
                  <div class="d-flex gap-3">
                    <div class="form-check form-check-inline mb-0">
                      <input class="form-check-input" type="radio" name="interphone_yn" id="interphone_y" value="Y" <?php if ($result_check['interphone_yn'] == 'Y') {
                        echo 'checked="checked"';
                      } ?>>
                      <label class="form-check-label" for="interphone_y">있음</label>
                    </div>

                    <div class="form-check form-check-inline mb-0">
                      <input class="form-check-input" type="radio" name="interphone_yn" id="interphone_n" value="N" <?php if ($result_check['interphone_yn'] == 'N') {
                        echo 'checked="checked"';
                      } ?>>
                      <label class="form-check-label" for="interphone_n">없음</label>
                    </div>
                  </div>
                </div>
              </div>

              <div class="row g-2 align-items-center">
                <div class="col-1 col-md-1">
                  <label class="form-label mb-0">출입카드키</label>
                </div>

                <div class="col-12 col-md-9">
                  <div class="d-flex gap-3">
                    <div class="form-check form-check-inline mb-0">
                      <input class="form-check-input" type="radio" name="building_security_yn" id="building_security_y"
                        value="Y" <?php if ($result_check['building_security_yn'] == 'Y') {
                          echo 'checked="checked"';
                        } ?>>
                      <label class="form-check-label" for="building_security_y">있음</label>
                    </div>

                    <div class="form-check form-check-inline mb-0">
                      <input class="form-check-input" type="radio" name="building_security_yn" id="building_security_n"
                        value="N" <?php if ($result_check['building_security_yn'] == 'N') {
                          echo 'checked="checked"';
                        } ?>>
                      <label class="form-check-label" for="building_security_n">없음</label>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          <?php endif; ?>

          <!-- 검수사항 -->
          <div class="my-3">
            <div class="border rounded-3 p-2">
              <div class="fw-semibold mb-2">검수사항</div>
            <div class="form-check">
              <input class="form-check-input" type="checkbox" name="chkPromote" id="chkPromote" value="Y">
              <label class="form-check-label" for="chkPromote">홍보확인서 확인</label>
            </div>
            <?php if ($data['rcpt_product'] == 'B01' || $data['rcpt_product'] == 'B02' || $data['rcpt_product'] == 'B03'): ?>
              <div class="form-check">
                <input class="form-check-input" type="checkbox" name="chkBunyang" id="chkBunyang" value="Y">
                <label class="form-check-label" for="chkBunyang">분양권 확인</label>
              </div>
            <?php endif; ?>
            </div>
          </div>

          <!-- 360이미지 / 촬영위치 -->
          <div class="my-3">
            <div class="border rounded-3 p-2">
              <div class="d-flex justify-content-between align-items-center mb-2">
                <div class="fw-semibold">360이미지 (최대 5장) / 촬영위치</div>
              <div class="d-flex gap-1">
                <button type="button" class="btn btn-sm btn-outline-secondary" onclick="viewFilePop('I9', '')">파일</button>
                <button type="button" class="btn btn-sm btn-outline-primary" onclick="save360ImageLocations()">촬영위치 저장</button>
                <button type="button" class="btn btn-sm btn-outline-success"
                  onclick="downloadImagesByType('I9', '360이미지')">일괄다운로드</button>
              </div>
            </div>

            <div class="d-flex flex-wrap gap-2">
              <?php
              if (isset($imgs_count['I9']) && $imgs_count['I9'] != 0) {
                $cnt = 0;
                foreach ($images as $img) {
                  if ($img['img_type'] == 'I9') {
                    $link = $img['img_path'] . $img['img_filenm'];
                    $temp2 = explode(".", $img['img_filenm']);
                    $name = $temp2[0] . "_thumb.jpg";
                    $thumblink = $img['img_path'] . $name;

                    if ($img['cloud_upload_yn'] == 'Y') {
                      $thumblink = NCLOUD_OBJECT_STORAGE_URL . $thumblink;
                      $link = NCLOUD_OBJECT_STORAGE_URL . $link;
                    }
                    $cnt++;
                    ?>
                    <div class="thumb-card">
                      <div class="ratio ratio-4x3 bg-light rounded-2 overflow-hidden mb-1">
                        <a onclick="fn_preview('<?= $link ?>')" class="d-flex align-items-center justify-content-center">
                          <img src="<?= $thumblink ?>" alt="360이미지" class="w-100 h-100 object-fit-contain" 
                            onerror="console.error('360이미지 로드 실패:', this.src); this.style.border='2px solid red';"
                            onload="console.log('360이미지 로드 성공:', this.src);">
                        </a>
                      </div>
                      <input class="form-control form-control-sm" type="text" name="img_location_<?= $img['img_sq'] ?>"
                        id="img_location_<?= $img['img_sq'] ?>" value="<?= $img['img_location'] ?>" size="11"
                        style="width: 160px;">
                    </div>
                  <?php }
                }
              } else { ?>
                <div class="thumb-card">
                  <div class="ratio ratio-4x3 bg-light rounded-2 overflow-hidden mb-1">
                    <img src="/plugin/img/photo.gif" alt="360이미지" class="w-100 h-100 object-fit-contain">
                  </div>
                  <input class="form-control form-control-sm" type="text" placeholder="촬영위치" style="width: 160px;">
                </div>
              <?php } ?>
            </div>
            </div>
          </div>
        </div>
      </div>


      <!-- 거주인 정보 및 녹취 내용 -->
      <div class="main-card mb-3 card">
        <div class="card-body">
          <h5 class="card-title">중개인 요청사항</h5>

          <?php
          // 전화번호 분해
          $arrRecTel = ["", "", ""];
          if (!empty($data['rec_tel'])) {
            $tmp = explode("-", $data['rec_tel']);
            $arrRecTel[0] = $tmp[0] ?? "";
            $arrRecTel[1] = $tmp[1] ?? "";
            $arrRecTel[2] = $tmp[2] ?? "";
          }
          ?>

          <table class="table table-bordered table-sm tbl_basic2 apt-info-table mb-0">
            <colgroup>
              <col width="15%" />
              <col width="35%" />
              <col width="15%" />
              <col width="35%" />
            </colgroup>

            <tbody>
              <!-- 1) 거주자 정보 -->
              <tr>
                <th class="align-middle">거주자 전화번호</th>
                <td class="align-middle">
                  <div class="input-group input-group-sm" style="max-width: 340px;">
                    <input type="text" class="form-control" name="rec_tel1" maxlength="4" placeholder="010"
                      value="<?= esc($arrRecTel[0]) ?>" />
                    <span class="input-group-text">-</span>
                    <input type="text" class="form-control" name="rec_tel2" maxlength="4" placeholder="1234"
                      value="<?= esc($arrRecTel[1]) ?>" />
                    <span class="input-group-text">-</span>
                    <input type="text" class="form-control" name="rec_tel3" maxlength="4" placeholder="5678"
                      value="<?= esc($arrRecTel[2]) ?>" />
                  </div>
                </td>

                <th class="align-middle">거주인 이름</th>
                <td class="align-middle">
                  <input type="text" class="form-control form-control-sm" name="rec_nm" style="max-width: 220px;"
                    value="<?= esc($data['rec_nm'] ?? '') ?>" />
                </td>
              </tr>

              <!-- 2) 음성파일 -->
              <tr>
                <th class="align-middle">음성파일</th>
                <td colspan="3" class="align-middle">
                  <div class="d-flex align-items-center flex-wrap gap-2">

                    <!-- 현재 파일 정보 -->
                    <?php if (!empty($record)): ?>
                      <div class="d-flex align-items-center gap-2 flex-wrap">
                        <a class="fw-semibold text-decoration-none"
                          href="/article/receipt/download_file?record_sq=<?= $record['record_sq'] ?>">
                          <?= esc($record['record_orignm']) ?>
                        </a>
                        <span class="badge bg-light text-dark border"><?= esc($record['record_size']) ?> KB</span>
                        <span class="badge bg-light text-dark border"><?= esc($record['insert_tm']) ?></span>
                      </div>
                    <?php else: ?>
                      <span class="text-muted small">등록된 파일 없음</span>
                    <?php endif; ?>

                    <div class="vr"></div>

                    <!-- 업로드 버튼 + 파일명 -->
                    <label class="btn btn-sm btn-outline-secondary mb-0">
                      파일 업로드
                      <input type="file" id="rec_file" name="rec_file" accept=".wav" class="d-none"
                        onchange="showFileName(this);">
                    </label>
                    <span id="file_name" class="text-muted small"></span>

                    <div class="vr"></div>

                    <!-- 체크박스 -->
                    <div class="form-check mb-0">
                      <input class="form-check-input" type="checkbox" name="chk_record" id="chk_record" value="Y"
                        <?= empty($record) ? 'disabled' : 'checked' ?>>
                      <label class="form-check-label" for="chk_record">
                        녹취파일 확인
                      </label>
                    </div>
                  </div>
                </td>
              </tr>

              <!-- 3) 거주인 요청사항 -->
              <tr>
                <th class="align-middle">거주인 요청사항</th>
                <td colspan="3">
                  <textarea class="form-control form-control-sm" name="rec_remark" rows="2"
                    placeholder="요청사항을 입력하세요"><?= esc($data['remark'] ?? '') ?></textarea>
                </td>
              </tr>
            </tbody>
          </table>
        </div>

        <div class="card-footer d-flex justify-content-end gap-1">
          <?php if (!in_array($usr_level, [])): ?>
            <button type="button" class="btn btn-sm btn-outline-primary" onclick="saveRecInfo();">저장</button>
          <?php endif; ?>
          <button type="button" class="btn btn-sm btn-outline-focus" onclick="viewSmsPop('S14');">완료안내</button>
        </div>
      </div>
    </form>

    <!-- 정보변경 이력 -->
    <div class="main-card mb-3 card">
      <div class="card-body ">
        <h5 class="card-title">정보변경 이력 
          <small class="text-muted">(총 <?= count($history ?? []) ?>건)</small>
        </h5>
        <div class="table-scroll">
          <table id="history-table" class="table table-bordered table-sm tbl_basic2 apt-info-table">
            <thead>
              <tr>
                <th style="text-align: center;">진행상태</th>
                <th style="text-align: center;">변경내용</th>
                <th style="text-align: center;">처리자(ID)</th>
                <th style="text-align: center;">처리일시</th>
                <th style="text-align: center;">세부내용</th>
              </tr>
            </thead>
            <tbody>
              <?php if (!empty($history)) { ?>
                <?php foreach ($history as $h) { ?>
                  <tr>
                    <td style="text-align: center;">
                      <?= $h['rcpt_stat_nm'] ?>
                    </td>
                    <td style="text-align: center;">
                      <?= $h['changed_type_nm'] ?>
                    </td>
                    <td style="text-align: center;">
                      <?= $h['changed_id'] ?>
                    </td>
                    <td style="text-align: center;">
                      <?= $h['changed_tm'] ?>
                    </td>
                    <td>
                      <?= $h['remark'] ?>
                    </td>
                  </tr>
                <?php } ?>
              <?php } else { ?>
                <tr>
                  <td colspan="5" style="text-align: center;">이력이 없습니다.</td>
                </tr>
              <?php } ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>

  </div>
</div>

<?= $this->section('modals') ?>
<div class="modal" id="previewModal" tabindex="-1">
  <div class="modal-dialog modal-xl">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">미리보기</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body p-0">
        <img id="imgPreview" src="" alt="미리보기" width="100%" height="auto">
        <video id="vdoPreview" controls playsinline preload="metadata" style="display: none;height: 500px;width: 100%;">
          <source id="videoSource" src="" type="video/mp4">
          브라우저가 video 태그를 지원하지 않습니다.
        </video>
      </div>
    </div>
  </div>
</div>

<div class="modal fade" id="uploadModal" tabindex="-1">
  <div class="modal-dialog modal-xl">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">파일 업로드</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body p-0">
        <form id="frm_file_info" method="post" enctype="multipart/form-data" onsubmit="return false;">
          <input type="hidden" name="rcpt_key" value="<?= $data['rcpt_key'] ?? '' ?>">
          <input type="hidden" name="rsrv_sq" value="<?= $data['rsrv_sq'] ?? '' ?>">
          <input type="hidden" name="rcpt_sq" value="<?= $data['rcpt_sq'] ?? '' ?>">
          <input type="hidden" name="img_type" value="">
          <input type="hidden" name="img_sub_type" value="">
          <input type="hidden" name="vr_sq" value="">

          <!-- 버튼 툴바 -->
          <div class="d-flex justify-content-end gap-2 mb-2" style="padding: 10px 10px 0 0;">
            <button type="button" class="btn btn-sm btn-primary" id="uploadPick">
              <i class="pe-7s-up-arrow"></i> 파일선택
            </button>

            <button type="button" class="btn btn-sm btn-success" id="btnUpload">
              <i class="pe-7s-up-arrow"></i> 파일업로드
            </button>

            <button type="button" class="btn btn-sm btn-danger" id="btnRemove">
              <i class="pe-7s-less"></i> 업로드취소
            </button>
          </div>

          <!-- Dropzone 영역 -->
          <div id="myDropzone" class="dropzone border rounded-3 p-2" style="max-height: 520px;overflow-y: auto; overflow-x: hidden; min-height: 250px;">
            <div class="dz-message dz-message-fixed needsclick text-center py-3">
              <i class="pe-7s-upload" style="font-size:28px; color: #6c757d;"></i><br>
              <span style="font-size: 13px; color: #6c757d;">파일을 드래그하거나 위 [파일선택] 버튼을 클릭하세요</span>
            </div>
          </div>

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

<div class="modal fade" id="smsModal" tabindex="-1" aria-hidden="true">
  <div class="modal-dialog modal-md">
    <div class="modal-content">

      <div class="modal-header">
        <h5 class="modal-title">SMS 보내기</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>

      <form id="smsForm" onsubmit="return false;">
        <input type="hidden" name="rcpt_key" value="<?= $data['rcpt_key'] ?>" />
        <input type="hidden" name="rcpt_sq" value="<?= $data['rcpt_sq'] ?>" />
        <input type="hidden" name="rsrv_sq" value="<?= $data['rsrv_sq'] ?>" />
        <input type="hidden" name="cd" />
        <div class="modal-body">
          <div class="mb-3">
            <label for="smsPhone" class="form-label">수신번호</label>
            <input type="tel" class="form-control" id="smsPhone" name="phone" placeholder="010-0000-0000" required>
          </div>

          <div class="mb-3">
            <label for="smsContent" class="form-label">내용</label>
            <textarea class="form-control" id="smsContent" name="content" rows="5" placeholder="메시지 내용을 입력하세요"
              style="resize: none;" required></textarea>
          </div>

        </div>

        <div class="modal-footer justify-content-center gap-1">
          <button type="button" class="btn btn-sm btn-outline-secondary" data-bs-dismiss="modal">
            닫기
          </button>
          <button type="submit" class="btn btn-sm btn-outline-primary" id="btnSendSms">
            보내기
          </button>
        </div>
      </form>

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

<script type="text/javascript" src="https://oapi.map.naver.com/openapi/v3/maps.js?ncpKeyId=dtounkwjc5"></script>
<script src="https://unpkg.com/dropzone@6.0.0-beta.1/dist/dropzone-min.js"></script>
<link href="https://unpkg.com/dropzone@6.0.0-beta.1/dist/dropzone.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.jsdelivr.net/npm/sortablejs@1.15.0/Sortable.min.js"></script>
<style>
  /* Dropzone 커스텀 스타일 */
  .dropzone {
    display: grid;
    grid-template-columns: repeat(8, 1fr); /* 한 줄에 8개 고정 */
    gap: 6px;
    align-items: flex-start;
    cursor: default !important; /* 클릭 커서 제거 */
    box-sizing: border-box;
  }

  .dropzone .dz-preview {
    margin: 0;
    min-height: auto !important;
  }

  .dropzone .dz-preview.dz-image-preview {
    background: #fff;
    border: 2px solid #e1e4e8;
    border-radius: 6px;
    padding: 5px;
    width: 100%;
    cursor: move;
    transition: all 0.2s;
  }

  .dropzone .dz-preview.dz-image-preview:hover {
    border-color: #007bff;
    box-shadow: 0 2px 8px rgba(0,123,255,0.2);
  }

  .dropzone .dz-preview.sortable-ghost {
    opacity: 0.4;
  }

  /* 이미지 영역 */
  .dropzone .dz-preview .dz-image {
    width: 100%;
    aspect-ratio: 1;
    border-radius: 4px;
    overflow: visible; /* 배지가 보이도록 변경 */
    margin-bottom: 5px;
    position: relative;
  }

  .dropzone .dz-preview .dz-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 4px;
  }

  /* 파일 정보 영역 */
  .dropzone .dz-preview .dz-details {
    position: relative;
    padding: 3px 0;
    text-align: center;
    opacity: 1 !important;
  }

  .dropzone .dz-preview .dz-filename {
    display: block !important;
    opacity: 1 !important;
  }

  .dropzone .dz-preview .dz-filename span {
    display: block;
    width: 100%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 10px;
    color: #586069;
    padding: 0 2px;
    line-height: 1.3;
  }

  .dropzone .dz-preview .dz-size {
    font-size: 9px;
    color: #959da5;
    margin-top: 2px;
  }

  /* 프로그레스 바 */
  .dropzone .dz-preview .dz-progress {
    height: 3px;
    border-radius: 2px;
    margin: 5px 0 3px 0;
    overflow: hidden;
    background: #e1e4e8;
    opacity: 1 !important;
    display: none; /* 기본적으로 숨김 */
  }

  .dropzone .dz-preview.dz-processing .dz-progress {
    display: block; /* 업로드 중일 때만 표시 */
  }

  .dropzone .dz-preview .dz-progress .dz-upload {
    background: linear-gradient(90deg, #0366d6 0%, #0969da 100%);
    height: 100%;
    transition: width 0.3s ease;
  }

  /* 성공/에러 표시 */
  .dropzone .dz-preview .dz-success-mark,
  .dropzone .dz-preview .dz-error-mark {
    display: none !important;
  }

  .dropzone .dz-preview.dz-success .dz-image::before {
    content: "✓";
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(40, 167, 69, 0.9);
    color: white;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    font-weight: bold;
    z-index: 5;
  }

  .dropzone .dz-preview.dz-error .dz-image::before {
    content: "✕";
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(220, 53, 69, 0.9);
    color: white;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    font-weight: bold;
    z-index: 5;
  }

  /* 버튼 영역 */
  .dropzone .dz-preview .file-buttons {
    display: flex;
    gap: 3px;
    margin-top: 5px;
  }

  /* 삭제 버튼 */
  .dropzone .dz-preview .dz-remove {
    display: block !important;
    flex: 1;
    padding: 3px 6px;
    font-size: 9px;
    text-align: center;
    border: 1px solid #dc3545;
    border-radius: 3px;
    background: white;
    color: #dc3545;
    text-decoration: none;
    transition: all 0.2s;
  }

  .dropzone .dz-preview .dz-remove:hover {
    background: #dc3545;
    color: white;
  }

  /* 교체 버튼 */
  .dropzone .dz-preview .dz-replace {
    display: block !important;
    flex: 1;
    padding: 3px 6px;
  
  /* 매물사진 순서 배지 스타일 */
  .property-image-item {
    position: relative;
  }
  
  .image-order-badge {
    position: absolute;
    top: 1px;
    left: 1px;
    background: #6c9ee8;
    color: white;
    min-width: 24px;
    height: 24px;
    padding: 0 6px;
    border-radius: 12px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 13px;
    font-weight: 500;
    z-index: 10;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  }
  
  .property-image-item.sortable-ghost {
    opacity: 0.4;
  }
  
  .property-image-item.sortable-drag {
    opacity: 0.8;
  }
  
  .dropzone .dz-preview .dz-replace {
    font-size: 9px;
    text-align: center;
    border: 1px solid #007bff;
    border-radius: 3px;
    background: white;
    color: #007bff;
    text-decoration: none;
    transition: all 0.2s;
    cursor: pointer;
  }

  .dropzone .dz-preview .dz-replace:hover {
    background: #007bff;
    color: white;
  }

  /* 순서 번호 배지 */
  .file-order-badge {
    position: absolute;
    top: -6px;
    left: -6px;
    background: #007bff;
    color: white;
    border-radius: 50%;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: bold;
    z-index: 10;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
    border: 2px solid white;
  }

  /* 에러 메시지 */
  .dropzone .dz-preview .dz-error-message {
    display: none !important;
  }
  
  /* 메시지 영역 간격 조정 */
  .dropzone .dz-message {
    margin: 1em 0 !important;
    grid-column: 1 / -1; /* 전체 너비 차지 */
    pointer-events: none; /* 클릭 이벤트 차단 */
  }
  
  /* 파일선택 버튼만 클릭 가능하도록 */
  .dropzone.dz-clickable {
    cursor: default !important;
  }
  
  .dropzone.dz-clickable * {
    cursor: default !important;
  }
  
  .dropzone .dz-message * {
    pointer-events: none;
  }
</style>
<script type="text/javascript">

  window.rcpt_hscp_nm = <?= json_encode($data['rcpt_hscp_nm'] ?? '') ?>;
  window.spc_stat = <?= json_encode($data['spc_stat'] ?? '') ?>;
  window.lat = parseFloat("<?= esc($data['rcpt_y'] ?? '0') ?>");
  window.lng = parseFloat("<?= esc($data['rcpt_x'] ?? '0') ?>");
  window.smsArr = <?= json_encode($sms ?? [], JSON_UNESCAPED_UNICODE) ?>;

</script>
<script src="/js/article/receipt/detail.js"></script>

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