<?= $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">
                <form class="form-control" id="frm_board_info" enctype="multipart/form-data">
                    <div class="position-relative mb-3">
                        <label for="subject" class="form-label">제목</label>
                        <input name="subject" id="subject" type="text" class="form-control">
                    </div>
                    <div class="position-relative mb-3">
                        <label for="subject" class="form-label">내용</label>
                        <textarea class="form-control" name="content" id="content" rows="10" cols="100"></textarea>
                    </div>
                    <div class="position-relative mb-3">
                        <label for="file" class="form-label">첨부파일</label>
                        <input name="file" id="file" type="file" class="form-control">
                    </div>
                </form>

            </div>
            <div class="card-footer">
                <button class="mb-2 me-2 btn btn-primary" id="btn_save">저장</button>

            </div>
        </div>

    </div>
</div>
<!-- <script src="https://cdn.ckeditor.com/4.12.1/standard/ckeditor.js"></script> -->
<link href="https://cdn.jsdelivr.net/npm/froala-editor@latest/css/froala_editor.pkgd.min.css" rel="stylesheet"
    type="text/css" />
<script type="text/javascript"
    src="https://cdn.jsdelivr.net/npm/froala-editor@latest/js/froala_editor.pkgd.min.js"></script>
<script type="text/javascript">
    const editorKey = '5OA4gF4D3I3G3B6C4D-13TMIBDIa2NTMNZFFPFZe2a1Id1f1I1fA8D6C4F4G3H3I2A18A15A6==';
    // const tpl = document.querySelector('.my-loader-template');

    $(function () {
        editor = new FroalaEditor("#content", {
            fileUpload: true
            , 'key': editorKey
            , 'height': 150
            , toolbarButtons: [
                'bold', 'italic', 'underline', 'strikeThrough', 'subscript', 'superscript', 'fontFamily', 'fontSize',
                'color', 'paragraphFormat', 'align', 'formatOL', 'formatUL', 'outdent', 'indent', 'quote', 'insertHR',
                'undo', 'redo', 'clearFormatting', 'selectAll', 'html'
            ]
            , toolbarButtonsXS: [
                'bold', 'italic', 'underline', 'strikeThrough', 'subscript', 'superscript', 'fontFamily', 'fontSize',
                'color', 'paragraphFormat', 'align', 'formatOL', 'formatUL', 'outdent', 'indent', 'quote', 'insertHR',
                'undo', 'redo', 'clearFormatting', 'selectAll', 'html'
            ]
        })


        $("#btn_save").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/actWrite',
                        method: 'POST',
                        data: new FormData($("#frm_board_info")[0]),
                        processData: false,
                        contentType: false,
                        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() ?>