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

<style>
    table th {
        vertical-align: middle;
        line-height: 1.2;
    }

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

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

<div class="col-md-12 col-xl-12">
    <div class="col-lg-12">
        <div class="main-card mb-3 card">
            <div class="card-header bg-white border-bottom shadow-sm">
                <div class="d-flex flex-wrap align-items-center gap-3">
                    <div class="flex-grow-1">
                        <h4 class="mb-0 fw-bold text-dark">홍보확인서 상세 내용</h4>
                    </div>
                    <div class="d-flex flex-wrap gap-3">
                        <div><span class="tit">매물ID:</span> <span class="num"><?= $data['atcl_no'] ?></span></div>
                        <div><span class="tit">CP ID:</span> <span class="num"><?= $article['cpid'] ?? '' ?></span></div>
                        <div><span class="tit">현재 상태:</span> <span class="num"><?= $article['current_stat'] ?? '' ?></span></div>
                    </div>
                </div>
            </div>
            <div class="card-body">
                <h5 class="card-title">팩스 이미지 및 기본 정보</h5>
                <form action="" id="frmSave" name="frmSave" onsubmit="return false;">
                    <input type="hidden" name="work_type" id="work_type" value="<?= $data['work_type'] ?>" />
                    <input type="hidden" name="atcl_no" value="<?= $data['atcl_no'] ?>" />
                    <input type="hidden" name="vr_sq" id="vr_sq" value="<?= $data['vr_sq'] ?>" />
                    <input type="hidden" name="fax_sq" id="fax_sq" value="<?= $data['fax_sq'] ?>" />
                    <input type="hidden" name="file_type" id="file_type" value="" />
                </form>
                <?php if (($data['receiver'] ?? '') != "API"): ?>
                    <div class="d-flex justify-content-end gap-1 mb-2">
                        <span>
                            발신번호 : <?= esc(str_replace('-', '', $data['caller_no'] ?? '')) ?>
                        </span>
                        <button type="button" class="btn btn-sm btn-outline-secondary"
                            onclick="faximage_rotate(90)">90˚</button>
                        <button type="button" class="btn btn-sm btn-outline-secondary"
                            onclick="faximage_rotate(180)">180˚</button>
                        <button type="button" class="btn btn-sm btn-outline-secondary"
                            onclick="faximage_rotate(270)">270˚</button>
                    </div>
                <?php endif; ?>
                <!-- table 유지 + 반응형 -->
                <div class="table-responsive">
                    <table class="table table-bordered table-sm tbl_basic2 align-middle mb-0" style="min-width: 900px;">
                        <colgroup>
                            <col style="width: 60%;">
                            <col style="width: 40%;">
                        </colgroup>

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

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

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

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

                                    <?php if ($ext === 'pdf'): ?>
                                        <a href="<?= esc($filePath . $fileName) ?>" class="embed"></a>
                                    <?php else: ?>
                                        <a onclick="fn_preview('<?= esc($image_path . $fileName) ?>?<?= esc(($data['img_width'] ?? '') . ($data['img_height'] ?? '')) ?>')"
                                            rel="lightbox">
                                            <img id="fax_image"
                                                src="<?= esc($image_path . $fileName) ?>?<?= esc(($data['img_width'] ?? '') . ($data['img_height'] ?? '')) ?>"
                                                alt="fax" class="img-fluid" style="width: 100%;" />
                                        </a>
                                    <?php endif; ?>
                                </td>

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

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

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

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


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

                                                                <div class="input-group input-group-sm"
                                                                    style="max-width: 320px;">
                                                                    <input type="text" class="form-control"
                                                                        name="atcl_no" id="atcl_no"
                                                                        value="<?= $data['fax_sq'] ?>" maxlength="10"
                                                                        placeholder="매물번호 입력">
                                                                    <button type="button"
                                                                        class="btn btn-outline-primary"
                                                                        onclick="btnSilverAtclNo_onclick();">조회</button>
                                                                </div>
                                                            </form>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <th>CP ID</th>
                                                        <td><span class="num"
                                                                id="span_cpid"><?= $article['cpid'] ?></span></td>
                                                    </tr>
                                                    <tr>
                                                        <th>현재 상태</th>
                                                        <td><span class="num"
                                                                id="span_current_stat"><?= $article['current_stat'] ?></span>
                                                        </td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                        <div class="card-footer d-flex justify-content-end gap-1">
                                            <button type="button" class="btn btn-sm btn-warning"
                                                onclick="btnBrownNotArticle_onclick('<?= $faxSq ?>');">홍보확인서 아님</button>

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

                                            <button type="button" class="btn btn-sm btn-success"
                                                onclick="btnSilverSave_mobile_onclick('<?= $faxSq ?>');">모바일
                                                분양권</button>

                                            <button type="button" class="btn btn-sm btn-info"
                                                onclick="btnSilverSave_bunyang_onclick('<?= $faxSq ?>');">분양권</button>
                                        </div>
                                    </div>

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

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

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

                                    <!-- 3) 매물 정보 -->
                                    <div class="main-card mb-3 card">
                                        <div class="card-header">
                                            <span class="fw-semibold">매물 정보</span>
                                        </div>
                                        <div class="card-body p-2">
                                            <table class="table table-bordered table-sm tbl_basic2 align-middle mb-0"
                                                id="info_table">
                                                <colgroup>
                                                    <col style="width: 120px;">
                                                    <col>
                                                </colgroup>
                                                <tbody>
                                                    <tr>
                                                        <th>등록일시</th>
                                                        <td><span id="span_rdate"><?= $article['rdate'] ?></span></td>
                                                    </tr>
                                                    <tr>
                                                        <th>의뢰인(매도자)</th>
                                                        <td><span
                                                                id="span_seller_nm"><?= $article['seller_nm'] ?></span>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <th>매물구분</th>
                                                        <td><span
                                                                id="span_rlet_type_nm"><?= $article['rlet_type_nm'] ?></span>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <th>지역구분</th>
                                                        <td><span id="span_address1"><?= $article['address1'] ?></span>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <th>단지명</th>
                                                        <td><span id="span_hscp_nm">
                                                                <?= $article['hscp_nm'] ?>
                                                            </span></td>
                                                    </tr>
                                                    <tr>
                                                        <th>리 주소</th>
                                                        <td>
                                                            <input type="text" class="form-control form-control-sm"
                                                                name="address2a" id="address2a"
                                                                value="<?= $article['address2a'] ?>" disabled>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <th>상세주소</th>
                                                        <td class="d-flex text-nowrap gap-1">
                                                            <?php if (empty($article['address2b'])): ?>
                                                                <input type="text" class="form-control form-control-sm"
                                                                    name="address2b" id="address2b"
                                                                    value="<?= $article['address2b'] ?>" disabled />
                                                                <?php if (!empty($article['address3'])): ?>
                                                                    <input type="text" class="form-control form-control-sm"
                                                                        name="address3" id="address2b"
                                                                        value="<?= $article['address3'] ?>" disabled />
                                                                <?php else: ?>
                                                                    <input type="text" class="form-control form-control-sm"
                                                                        name="address3" id="address3"
                                                                        value="<?= $article['rcpt_ho'] ?? '' ?>" disabled>
                                                                <?php endif; ?>
                                                                <button type="button"
                                                                    class="btn btn-sm btn-outline-light">매물확인</button>
                                                            <?php else: ?>
                                                                <input type="text" class="form-control form-control-sm"
                                                                    name="address2b" id="address2b"
                                                                    value="<?= $article['address2b'] ?>" disabled />
                                                                <?php if (empty($article['rcpt_ho'])): ?>
                                                                    <input type="text" class="form-control form-control-sm"
                                                                        name="address3" id="address3"
                                                                        value="<?= $article['rcpt_etc_addr'] ?>" disabled>
                                                                <?php else: ?>
                                                                    <input type="text" class="form-control form-control-sm"
                                                                        name="address3" id="address3"
                                                                        value="<?= $article['rcpt_ho'] ?? '' ?>" disabled>
                                                                <?php endif; ?>
                                                            <?php endif; ?>
                                                            <button type="button"
                                                                class="btn btn-sm btn-outline-light">매물확인</button>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <th>기타주소</th>
                                                        <td>
                                                            <input type="text" class="form-control form-control-sm"
                                                                name="address4" id="address4"
                                                                value="<?= $article['address4'] ?>" disabled>
                                                        </td>
                                                    </tr>

                                                    <tr>
                                                        <th>거래구분/가격</th>
                                                        <td class="d-flex text-nowrap align-items-center gap-1">
                                                            <select class="form-select" name="span_trade_type_nm"
                                                                id="span_trade_type_nm" disabled="disabled"
                                                                style="width: 100px;">
                                                                <option value="">-거래구분-</option>
                                                                <option value="A1" <?php if ($article['trade_type_nm'] == '매매') {
                                                                    echo ('selected');
                                                                } ?>> 매매 </option>
                                                                <option value="B1" <?php if ($article['trade_type_nm'] == '전세') {
                                                                    echo ('selected');
                                                                } ?>> 전세 </option>
                                                                <option value="B2" <?php if ($article['trade_type_nm'] == '월세') {
                                                                    echo ('selected');
                                                                } ?>> 월세 </option>
                                                            </select>

                                                            <?php if ($article['trade_type'] == "B1"): ?>
                                                                <span
                                                                    id="span_junse_amt"><?= number_format($article['wrrnt_amt']) ?>
                                                                    만원 </span>
                                                                <span id="span_deal_amt" style="display: none;"></span>
                                                                <span id="span_lease_amt" style="display: none;"></span>
                                                            <?php elseif ($article['trade_type'] == "A1"): ?>
                                                                <span id="span_junse_amt" style="display: none;"></span>
                                                                <span
                                                                    id="span_deal_amt"><?= number_format($article['deal_amt']) ?>
                                                                    만원</span>
                                                                <span id="span_lease_amt" style="display: none;"></span>
                                                            <?php else: ?>
                                                                <span id="span_junse_amt" style="display: none;"></span>
                                                                <span id="span_deal_amt" style="display: none;"></span>
                                                                <span
                                                                    id="span_lease_amt"><?= number_format($article['wrrnt_amt']) ?>
                                                                    만원 / <?= number_format($article['lease_amt']) ?>
                                                                    만원</span>
                                                            <?php endif; ?>
                                                        </td>

                                                    </tr>


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

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

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

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

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

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

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

                                    <!-- 거주여부 -->
                                    <div class="main-card mb-3 card">
                                        <div class="card-header">
                                            <span class="fw-semibold">거주 여부</span>
                                        </div>
                                        <div class="card-body p-2">
                                            <table class="table table-bordered table-sm tbl_basic2 align-middle mb-0">
                                                <colgroup>
                                                    <col style="width: 120px;">
                                                    <col>
                                                </colgroup>
                                                <tbody>
                                                    <tr>
                                                        <th>거주여부</th>
                                                        <td>
                                                            <select class="form-select" name="resYn" id="resYn"
                                                                onchange="dbYn_change(this.value);">
                                                                <option value="Y" <?php if ($article['resYn'] == 'Y')
                                                                    echo "selected"; ?>>Y</option>
                                                                <option value="N" <?php if ($article['resYn'] == 'N')
                                                                    echo "selected"; ?>>N</option>
                                                            </select>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <th>DB활용동의여부</th>
                                                        <td>
                                                            <select class="form-select" name="dbUsageAgrYn"
                                                                id="dbUsageAgrYn">
                                                                <option value="N" <?php if ($article['dbUsageAgrYn'] == 'N')
                                                                    echo "selected"; ?>>N</option>
                                                                <option value="Y" <?php if ($article['dbUsageAgrYn'] == 'Y')
                                                                    echo "selected"; ?>>
                                                                    Y</option>
                                                            </select>
                                                        </td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>

                                    <div class="main-card card">
                                        <div class="card-body d-flex justify-content-end gap-1">
                                            <button class="btn btn-sm btn-outline-success"
                                                onclick="btnGreenResultUpload_onclick('I1')">홍보확인서</button>
                                            <button class="btn btn-sm btn-outline-success"
                                                onclick="btnGreenResultUpload_onclick('I10')">촬영동의서</button>
                                            <button class="btn btn-sm btn-outline-success"
                                                onclick="btnGreenBunyangUpload_onclick('<?= $data['fax_sq'] ?>')">분양계약서</button>
                                            <button class="btn btn-sm btn-outline-light"
                                                onclick="btnSilverNextAssign_onclick();">다음</button>
                                        </div>

                                    </div>
                                </td>

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



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

                                <?php } ?>
                            <?php } ?>
                    </table>
                </div>
            </div>
        </div>

    </div>
</div>


<?= $this->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">
            </div>
        </div>
    </div>
</div>
<?= $this->endSection() ?>

<script type="text/javascript">

    $(function () {
        $('#atcl_no').keydown(function (e) {
            if (e.keyCode == 13) {
                btnSilverAtclNo_onclick();
            }
        })

    });


    // 이미지회전
    function faximage_rotate(degress) {
        swal.fire({
            text: "현재 이미지를 회전시켜 저장하시겠습니까?",
            type: "warning",
            showCancelButton: true,
            confirmButtonText: "예",
            cancelButtonText: "아니오",
            closeOnConfirm: false,
            closeOnCancel: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
        }).then((result) => {
            if (result.isConfirmed) {
                const data = {
                    'vr_sq': '<?= $data['vr_sq'] ?>',
                    'degress': degress,
                };

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

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

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


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

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

            return;
        }

        location.replace("<?= site_url('m708/m708a/detail') ?>/" + atclNo);

        /*
        var atclNo = $('#atcl_no').val();

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

            return;
        }

        $.getJSON("/m708/m708a/getArticleInfo?&atcl_no=" + atclNo, function (res) {

            if (res.code == '0') {
                if (res.vr_sq != null) {
                    location.replace("<?= site_url('m708/m708a/detail') ?>/" + vr_sq);
    } else {
        swal.fire({
            title: "매물 조회에 실패했습니다.",
            icon: "warning"
        });
    }
            } else {
        swal.fire({
            title: "잠시후 다시 시도해 주세요.",
            icon: "warning"
        });
    }

        });
        */
    }

    // 매물확인
    function view201detail(atcl_no) {
        var type = <?= $data['work_type'] ?? '' ?>;

        if (type == '1') {	//현장확인매물            
            window.open('/article/receipt/detail/' + atcl_no);
        } else {
            alert('현장확인 매물이 아닙니다.');
            return;
        }
    }

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

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

                            })

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

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

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

                            })

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

    // 중복저장
    function btnSilverDup_onclick(fax_sq) {
        var vr_sq = '<?= $article['vr_sq'] ?>';
        var atcl_no = '<?= $article['atcl_no'] ?>';

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

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

                            })

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

    // 모바일 분양권
    function btnSilverSave_mobile_onclick(fax_sq) {
        var vr_sq = '<?= $article['vr_sq'] ?>';
        var atcl_no = '<?= $article['atcl_no'] ?>';

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

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

                            })

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

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

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

                            })

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

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

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

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


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

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

                            })

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



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

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

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

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

                            })

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

    }


    // 
    function btnGreenResultUpload_onclick(fileType) {
        var work_type = $('#frmSave > #work_type').val();
        var atcl_no = $('#frmSave > #atcl_no').val();
        var vr_sq = $('#frmSave > #vr_sq').val();

        if (work_type == '' || atcl_no == '' || vr_sq == '' || fileType == '') {
            Swal.fire({
                title: '매물정보를 확인하세요.',
                icon: "success"

            });
            return false;
        }

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

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

                            })

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

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

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

                        Swal.fire({
                            title: msg,
                            icon: "error"
                        })
                    },
                    success: function (result) {
                        if (result.code == '0') {
                            if (parseInt(result.cnt) >= 4) {
                                Swal.fire({
                                    title: '분양계약서가 업로드 되어 있습니다.',
                                    icon: "warning"

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

    // 분양계약서 업로드
    function upload_bunyang() {
        var vr_sq = $('#frmSave > #vr_sq').val();
        var work_type = $('#frmSave > #work_type').val();
        var atcl_no = $('#frmSave > #atcl_no').val();

        if (work_type == '' || atcl_no == '' || vr_sq == '') {
            swal.fire({
                title: "매물정보를 확인하세요.",
                icon: "warning"
            });
            return false;
        } else {
            swal.fire({
                text: "확인결과를 저장하시겠습니까?",
                type: "warning",
                showCancelButton: true,
                confirmButtonText: "예",
                cancelButtonText: "아니오",
                closeOnConfirm: false,
                closeOnCancel: true,
                confirmButtonColor: "#3085d6",
                cancelButtonColor: "#d33",
            }).then((result) => {
                if (result.isConfirmed) {
                    const data = {
                        'curr_fax_sq': '<?= $data['fax_sq'] ?>'
                    };

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

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

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

    }

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

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

                        Swal.fire({
                            title: msg,
                            icon: "error"
                        })
                    },
                    success: function (result) {
                        if (result.code == '0') {

                            const fax_sq = result.data.fax_sq;

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

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



    // 이미지 프리뷰
    function fn_preview(src) {
        const $img = $('#imgPreview');

        // 이미지 표시
        $img.attr('src', src).show();

        $('#previewTitle').text('이미지 미리보기');

        const modal = new bootstrap.Modal(document.getElementById('previewModal'));
        modal.show();
    }
</script>

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