{% extends 'admin/base_admin.html' %}

{% block content %}
<div class="page__header">
    <h1>포스트 수정</h1>
    <div>
        <a href="/post/{{ post.id }}" target="_blank" class="uk-button uk-button-default">
            <span uk-icon="icon: link"></span> 사이트에서 보기
        </a>
        <a href="/admin/posts" class="uk-button uk-button-default">
            <span uk-icon="icon: arrow-left"></span> 목록으로
        </a>
    </div>
</div>

<div class="admin__form">
    <form action="/admin/posts/{{ post.id }}" method="post" class="uk-form-stacked">
        <div class="uk-margin">
            <label class="uk-form-label" for="title">제목</label>
            <div class="uk-form-controls">
                <input class="uk-input" id="title" type="text" name="title" value="{{ post.title }}" required>
            </div>
        </div>

        <div class="uk-grid uk-child-width-1-2@m" uk-grid>
            <div>
                <div class="uk-margin">
                    <label class="uk-form-label" for="category">카테고리</label>
                    <div class="uk-form-controls">
                        <select class="uk-select" id="category" name="category" required>
                            <option value="IT" {% if post.category == 'IT' %}selected{% endif %}>IT</option>
                            <option value="NEWS" {% if post.category == 'NEWS' %}selected{% endif %}>NEWS</option>
                            <option value="ETC" {% if post.category == 'ETC' %}selected{% endif %}>ETC</option>
                        </select>
                    </div>
                </div>
            </div>
            <div>
                <div class="uk-margin">
                    <label class="uk-form-label">공개 설정</label>
                    <div class="uk-form-controls uk-margin-small-top">
                        <label>
                            <input class="uk-checkbox" type="checkbox" name="public" {% if post.public_yn == 'Y' %}checked{% endif %}>
                            이 글을 외부에 공개합니다
                        </label>
                    </div>
                </div>
            </div>
        </div>

        <div class="uk-margin">
            <label class="uk-form-label">작성 정보</label>
            <div class="uk-form-controls">
                <div class="uk-text-muted" style="padding: 10px 0;">
                    <span uk-icon="icon: user"></span> {{ post.mb_name }} ({{ post.mb_id }})
                    &nbsp;&nbsp;|&nbsp;&nbsp;
                    <span uk-icon="icon: clock"></span> {{ post.add_date.strftime('%Y-%m-%d %H:%M') }}
                </div>
            </div>
        </div>

        <div class="uk-margin">
            <label class="uk-form-label" for="contents">내용</label>
            <div class="uk-form-controls">
                <textarea class="summernote" id="contents" name="contents" required>{{ post.contents }}</textarea>
            </div>
        </div>

        <div class="uk-margin uk-margin-large-top">
            <button type="submit" class="uk-button uk-button-primary uk-button-large">
                <span uk-icon="icon: check"></span> 저장
            </button>
            <a href="/admin/posts" class="uk-button uk-button-default uk-button-large">취소</a>
        </div>
    </form>
</div>
{% endblock %}

{% block scripts %}
<script>
$(document).ready(function() {
    $('#contents').summernote({
        height: 400,
        lang: 'ko-KR',
        toolbar: [
            ['style', ['style']],
            ['font', ['bold', 'underline', 'clear']],
            ['fontname', ['fontname']],
            ['color', ['color']],
            ['para', ['ul', 'ol', 'paragraph']],
            ['table', ['table']],
            ['insert', ['link', 'picture', 'video']],
            ['view', ['fullscreen', 'codeview', 'help']]
        ],
        callbacks: {
            onImageUpload: function(files) {
                var $editor = $(this);
                var data = new FormData();
                data.append("file", files[0]);
                $.ajax({
                    url: "/upload_image",
                    method: "POST",
                    data: data,
                    processData: false,
                    contentType: false,
                    success: function(response) {
                        if (response.success) {
                            $editor.summernote("insertImage", response.fileUrl);
                        } else {
                            Swal.fire('오류', response.message, 'error');
                        }
                    },
                    error: function() {
                        Swal.fire('오류', '이미지 업로드에 실패했습니다.', 'error');
                    }
                });
            }
        }
    });
});
</script>
{% endblock %}
