<!DOCTYPE html>
<html lang="en" xmlns:th=http://www.thymeleaf.org>
<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width,initial-scale=1"/>
    <meta name="theme-color" content="#000000"/>
    <meta name="description" content="포항시 교통정보센터입니다"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <title>포항시 교통정보센터</title>
    <th:block th:include="/include/head.html"></th:block>
    <link rel="stylesheet" th:href="@{/css/notice.css}">
</head>
<body id="body">
<th:block th:include="/include/header.html"></th:block>
<div class="noticeWrap">
    <div class="container view">
        <h2 class="header">공지사항</h2>
        <div class="content view">
            <div class="view-box">
                <div>
                    <input class="title" th:value="${notice.getBSubject()}" readonly>
                    <div class="b_content"></div>
                    <!--                    <textarea class="b_content" rows="19" th:text="${notice.getBContent()}" readonly></textarea>-->
                    <div class="attach-box">
                        <div class="attach">
                            <div th:if="${notice.getAttachFile() == '||' or #strings.isEmpty(notice.getAttachFile())}">첨부파일 없음</div>
                            <div class="attach-file" th:if="${notice.getAttachFile() != '||' and not #strings.isEmpty(notice.getAttachFile())}"
                                 th:each="item, i : ${#strings.arraySplit(notice.getAttachFile(), '|')}"
                                 th:text="${item}" th:title="${item + ' 다운로드'}"
                                 th:onclick="attachFileDownload([[${i.index}]], [[${notice}]], [[${item}]])"
                            ></div>
                        </div>
                        <div class="list-btn" onclick="movePath('/notice/list')">목록</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<th:block th:include="/include/footer.html"></th:block>
</body>
</html>
<script th:inline="javascript">
    // let isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
    //
    // if (isMobile) {
    //     $('.b_content').attr('rows', 10);
    // }
    const notice = [[${notice}]];
    $('.b_content').html(notice.b_content);

    function attachFileDownload(index, notice, fileName) {
        let attachFileId = notice.attach_file_id;
        if (attachFileId && attachFileId.split('|').length > 0) {
            attachFileId = attachFileId.split('|')[index];
        }
        const param = {
            boardNo : notice.board_no,
            fileId  : attachFileId,
        }
        getDataAsync('/api/notice/attach', 'POST', param, null, (jsonData)=>{
            const attachFile = jsonData.attach_file;
            const ext = fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length);
            if (attachFile.length > 0) {
                if (!ext) {
                    return alert("파일 확장자명이 잘못 되었습니다.");
                }
                const file = new Blob([attachFile], {type: "application/" + ext});
                const link = document.createElement('a');
                link.href = window.URL.createObjectURL(file);
                link.download = fileName;
                link.click();
            }
            else {
                alert(jsonData.message);
            }

        }, null);
    }
</script>