<!DOCTYPE html>
<html lang="ko">

<head>
    <?php $this->load->view('adm/layout/head')?>
</head>

<body>
    <!-- start wrapper -->
    <div class="wrapper">
        <!-- start nav -->
        <?php $this->load->view('adm/layout/nav')?>
        <!-- end nav -->
        <!-- start content_wrapper -->
        <div class="content_wrapper">
            <!-- start header -->
            <?php $this->load->view('adm/layout/header')?>
            <!-- end header -->
            <div class="contents">
                <h3 style="font-size:17px;font-weight:bold;color:#aaa">컨텐츠관리 > 등록</h3>
                <!-- 여기까지 레이아웃 상단입니다 -->
                <div class="box">
                    <div class="box-table" style="overflow:visible">
                        <form method="post" action="" enctype="multipart/form-data">
                            <input type="hidden" name="contentId" value="<?php if(isset($post)) echo $post->idx?>">
                            <table class="table table-bordered">
                                <tbody>
                                    <tr>
                                        <th width="150">카테고리</th>
                                        <td>
                                            <select name="category" class="form-control" style="width:200px">
                                                <option value="">카테고리</option>
                                                <?php foreach($lifeCategories as $category) { echo '<option value="'.$category->idx.'" '.($post->category_idx == $category->idx ? 'selected':'').'>'. $category->category_name. '</option>';}?>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>에디터</th>
                                        <td>
                                            <select name="editor" class="form-control" style="width:200px">
                                                <option value="">에디터</option>
                                                <?php foreach($graceEditors as $editor) { echo '<option value="'.$editor->editorId.'" '.($post->partner_idx == $editor->editorId ? 'selected':'').'>'. $editor->editorName. '</option>';}?>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>제목</th>
                                        <td>
                                            <input type="text" name="title" class="form-control" value="<?php if(isset($post)) echo convertEmoji($post->title)?>">
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>메인이미지</th>
                                        <td>
                                            <input type="hidden" name="main_img" value="<?php if(isset($post)) echo $post->main_img?>">
                                            <input type="file" class="cm-main-img" name="uploadfile">
                                            <img src="<?php if(isset($post)) echo imgSrc($post->mainImg)?>" width="200" style="margin-top:5px;<?php if(isset($post)) echo $post ? '':'display:none;'?>">
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>본문</th>
                                        <td>
                                            <!-- <textarea id="content" name="content"></textarea> -->
                                            <textarea name="content" class="js-st-instance"
                                                style="width:100%; height:500px;"><?php if(isset($post)) echo contentImgSrc(convertEmoji($post->content))?></textarea>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>공개설정</th>
                                        <td>
                                            <div class="input-group">
                                                <select name="is_display" style="width:200px" class="cm-display-select">
                                                    <option value="">공개설정</option>
                                                    <option value="N" <?php if(isset($post) && $post->is_display == 'N') echo ' selected';?>>비공개</option>
                                                    <option value="Y" <?php if(isset($post) && $post->is_display == 'Y') echo ' selected';?>>공개</option>
                                                    <option value="R" <?php if(isset($post) && $post->is_display == 'N' && strtotime($post->display_date) > time()) echo ' selected';?>>예약</option>
                                                </select>
                                                <div style="margin-top:5px;">
                                                    <input type="text" name="display_time" class="datetimepicker form-control cm-reserve-datetime"
                                                        value="<?php if(isset($post->display_date)) echo $post->display_date?>"
                                                        style="width:200px;display:<?php if(isset($post) && $post->is_display == 'N' && strtotime($post->display_date) > time()) echo 'block'; else echo 'none';?>;">
                                                </div>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                            <div style="text-align:center">
                                <a class="btn btn-danger cm-contentedit-cancel">취소</a>
                                <button type="submit" class="btn btn-success">저장</button>
                            </div>
                        </form>
                    </div>
                </div>
                <!-- 여기부터 레이아웃 하단입니다 -->
            </div>
        </div>
        <!-- end content_wrapper -->
    </div>
    <!-- end wrapper -->
    <?php $this->load->view('adm/layout/footer')?>
    <script src="/assets/adm/js/tinymce/tinymce.min.js"></script>
    <script>
    tinyMCE.init({
        selector: '.js-st-instance',
        plugins: ['link image code fullscreen lineheight',
            'autolink lists link anchor table',
            'textcolor colorpicker textpattern'
        ],
        file_browser_callback: RoxyFileBrowser,
        language: 'ko_KR',
        image_advtab: true,
        toolbar: 'codesample | bold italic sizeselect fontselect fontsizeselect | hr alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | insertfile undo redo | forecolor backcolor emoticons | code | lineheightselect | table tabledelete | tableprops tablerowprops tablecellprops | tableinsertrowbefore tableinsertrowafter tabledeleterow | tableinsertcolbefore tableinsertcolafter tabledeletecol',
        fontsize_formats: "8pt 10pt 12pt 14pt 18pt 24pt 36pt",
        font_formats: '나눔고딕=나눔고딕코딩,NanumGothicCoding,sans-serif; Andale Mono=andale mono,times; Arial=arial,helvetica,sans-serif; Arial Black=arial black,avant garde; Book Antiqua=book antiqua,palatino; Comic Sans MS=comic sans ms,sans-serif; Courier New=courier new,courier; Georgia=georgia,palatino; Helvetica=helvetica; Impact=impact,chicago; Symbol=symbol; Tahoma=tahoma,arial,helvetica,sans-serif; Terminal=terminal,monaco; Times New Roman=times new roman,times; Trebuchet MS=trebuchet ms,geneva; Verdana=verdana,geneva; Webdings=webdings; Wingdings=wingdings,zapf dingbats',
        lineheight_formats: "1pt 2pt 3pt 4pt 5pt 6pt 7pt 8pt 9pt 10pt 11pt 12pt 14pt 16pt 18pt 20pt 22pt 24pt 26pt 36pt 48pt",
        setup: function(ed) {
            ed.on('init', function() {
                // this.getDoc().body.style.fontSize = '12';
                this.getDoc().body.style.fontFamily = '나눔고딕';
            });
            ed.on('Change', function(e) {
                contentEdited = true;
            });
        }
    });

    function RoxyFileBrowser(field_name, url, type, win) {
        var roxyFileman = '/fileman/index.html?integration=tinymce4';
        if (roxyFileman.indexOf("?") < 0) {
            roxyFileman += "?type=" + type;
        } else {
            roxyFileman += "&type=" + type;
        }
        roxyFileman += '&input=' + field_name + '&value=' + win.document.getElementById(field_name).value;
        if (tinyMCE.activeEditor.settings.language) {
            roxyFileman += '&langCode=' + tinyMCE.activeEditor.settings.language;
        }
        tinyMCE.activeEditor.windowManager.open({
            file: roxyFileman,
            title: 'Crossmap File Manager',
            width: 850,
            height: 650,
            resizable: "yes",
            plugins: "media",
            inline: "yes",
            close_previous: "no"
        }, {
            window: win,
            input: field_name
        });
        return false;
    }
    $('.datetimepicker').datetimepicker({
        format: 'YYYY-MM-DD HH:mm:ss'
    });

    $('.cm-display-select').on('change', function() {
        $('.cm-reserve-datetime').hide();
        if (this.value == 'R') {
            $('.cm-reserve-datetime').show();
        }
    })

    var contentEdited = false;
    $(function() {
        $('input,select').on('change', function() {
            contentEdited = true;
        })

        $('.cm-contentedit-cancel').on('click', function() {
            if (contentEdited) {
                if (confirm('정말로 취소하시겠습니까?')) {
                    location.href = '/adm/content'
                }
            } else location.href = '/adm/content';
        })
    })
    </script>
</body>

</html>