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

<?= $this->section('content') ?>

<h1>공지사항</h1>
<div class="row">

    <div class="col-md-12 col-xl-12">
        <div class="main-card mb-3 card">
            <div class="card-body">
                <table class="table table-bordered">
                    <tbody>
                        <tr>
                            <th style="width:150px;">제목</th>
                            <td colspan="5"><?= $notice['subject'] ?></td>
                        </tr>
                        <tr>
                            <th>작성자</th>
                            <td><?= $notice['insert_nm'] ?></td>
                            <th style="width:150px;">작성일</th>
                            <td><?= $notice['insert_tm'] ?></td>
                            <th>조회수</th>
                            <td><?= $notice['hit'] ?></td>
                        </tr>
                        <tr>
                            <th>내용</th>
                            <td colspan="5" style="min-height: 200px;">
                                <?= $notice['content'] ?>
                            </td>
                        </tr>
                        <?php if (!empty($files)): ?>
                            <tr>
                                <th>첨부파일</th>
                                <td colspan="5" style="min-height: 200px;">
                                    <div>
                                        <?php if ($files['cloud_upload_yn'] === "Y"): ?>
                                            <?php $link = NCLOUD_OBJECT_STORAGE_URL . $files['file_path'] . $files['file_name']; ?>
                                            <a href="<?= $link ?>" download="<?= $files['orig_name'] ?>">
                                                <?= esc($files['orig_name']) ?>
                                            </a>
                                        <?php else: ?>
                                            <a href="<?= site_url('/board/notice/download/' . $files['file_sq']) ?>">
                                                <?= esc($files['orig_name']) ?>
                                            </a>
                                        <?php endif; ?>
                                    </div>
                                </td>
                            </tr>

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

            </div>
            <div class="card-footer">
                <button class="mb-2 me-2 btn-transition btn btn-outline-primary"
                    onclick="location.href='/board/notice/lists'">목록</button>
                <button class="mb-2 me-2 btn-transition btn btn-outline-primary"
                    onclick="location.href='/board/notice/modify/<?= $notice['bbs_sq'] ?>'">수정</button>
                <button class="mb-2 me-2 btn-transition btn btn-outline-primary" id="btn_remove">삭제</button>
            </div>
        </div>

    </div>
</div>
<script type="text/javascript">
    // const tpl = document.querySelector('.my-loader-template');

    $(function () {

        $("#btn_remove").on("click", function () {

            swal.fire({
                text: "삭제 하시겠습니까?",
                type: "warning",
                showCancelButton: true,
                confirmButtonText: "예",
                cancelButtonText: "아니오",
                closeOnConfirm: false,
                closeOnCancel: true,
                confirmButtonColor: "#3085d6",
                cancelButtonColor: "#d33",
            }).then((result) => {
                if (result.isConfirmed) {
                    $.ajax({
                        url: '/board/notice/remove',
                        contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
                        method: 'POST',
                        data: { bbs_sq: <?= $notice['bbs_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",
                                draggable: true
                            })
                        },
                        success: function (result) {

                            if (result.code == '0') {
                                location.replace('/board/notice/lists')
                            } else {
                                Swal.fire({
                                    title: result.msg,
                                    icon: "error",
                                    draggable: true
                                })
                            }
                        }
                    });
                }
            })

        });

    });

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