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

{% block content %}
<div class="page__header">
    <h1>포스트 관리</h1>
    <a href="/write" class="uk-button uk-button-primary">
        <span uk-icon="icon: plus"></span> 새 포스트 작성
    </a>
</div>

<!-- Filter Form -->
<div class="filter__form">
    <form method="get">
        <div class="uk-grid uk-grid-small uk-flex-middle" uk-grid>
            <div class="uk-width-1-5@m">
                <select class="uk-select" name="category">
                    <option value="">전체 카테고리</option>
                    <option value="IT" {% if request.args.get('category') == 'IT' %}selected{% endif %}>IT</option>
                    <option value="NEWS" {% if request.args.get('category') == 'NEWS' %}selected{% endif %}>NEWS</option>
                    <option value="ETC" {% if request.args.get('category') == 'ETC' %}selected{% endif %}>ETC</option>
                </select>
            </div>
            <div class="uk-width-1-5@m">
                <select class="uk-select" name="public_yn">
                    <option value="">공개 여부</option>
                    <option value="Y" {% if request.args.get('public_yn') == 'Y' %}selected{% endif %}>공개</option>
                    <option value="N" {% if request.args.get('public_yn') == 'N' %}selected{% endif %}>비공개</option>
                </select>
            </div>
            <div class="uk-width-1-5@m">
                <select class="uk-select" name="use_yn">
                    <option value="Y" {% if request.args.get('use_yn', 'Y') == 'Y' %}selected{% endif %}>사용중</option>
                    <option value="N" {% if request.args.get('use_yn') == 'N' %}selected{% endif %}>삭제됨</option>
                    <option value="" {% if request.args.get('use_yn') == '' %}selected{% endif %}>전체</option>
                </select>
            </div>
            <div class="uk-width-expand@m">
                <input class="uk-input" type="text" name="search" placeholder="제목 검색..." value="{{ request.args.get('search', '') }}">
            </div>
            <div class="uk-width-auto@m">
                <button class="uk-button uk-button-primary" type="submit">
                    <span uk-icon="icon: search"></span> 검색
                </button>
                <a href="/admin/posts" class="uk-button uk-button-default">초기화</a>
            </div>
        </div>
    </form>
</div>

<!-- Posts Table -->
<div class="admin__table">
    <table class="uk-table uk-table-striped uk-table-hover uk-table-middle">
        <thead>
            <tr>
                <th style="width: 60px;">ID</th>
                <th>제목</th>
                <th style="width: 100px;">카테고리</th>
                <th style="width: 100px;">작성자</th>
                <th style="width: 80px;">공개</th>
                <th style="width: 80px;">상태</th>
                <th style="width: 100px;">작성일</th>
                <th style="width: 120px;">관리</th>
            </tr>
        </thead>
        <tbody>
            {% if posts %}
                {% for post in posts %}
                <tr>
                    <td>{{ post.id }}</td>
                    <td>
                        <a href="/admin/posts/{{ post.id }}" class="post__title__link">
                            {{ post.title[:50] }}{% if post.title|length > 50 %}...{% endif %}
                        </a>
                    </td>
                    <td>{{ post.category }}</td>
                    <td>{{ post.mb_name }}</td>
                    <td>
                        {% if post.public_yn == 'Y' %}
                            <span class="uk-label uk-label-success">공개</span>
                        {% else %}
                            <span class="uk-label uk-label-warning">비공개</span>
                        {% endif %}
                    </td>
                    <td>
                        {% if post.use_yn == 'Y' %}
                            <span class="uk-label uk-label-success">사용중</span>
                        {% else %}
                            <span class="uk-label uk-label-danger">삭제됨</span>
                        {% endif %}
                    </td>
                    <td>{{ post.add_date.strftime('%Y-%m-%d') }}</td>
                    <td>
                        <a href="/post/{{ post.id }}" target="_blank" class="uk-icon-link" uk-icon="icon: link" title="사이트에서 보기"></a>
                        <a href="/admin/posts/{{ post.id }}" class="uk-icon-link" uk-icon="icon: pencil" title="수정"></a>
                        {% if post.use_yn == 'Y' %}
                            <a href="javascript:deletePost({{ post.id }})" class="uk-icon-link" uk-icon="icon: trash" title="삭제"></a>
                        {% else %}
                            <a href="javascript:restorePost({{ post.id }})" class="uk-icon-link uk-text-success" uk-icon="icon: refresh" title="복구"></a>
                        {% endif %}
                    </td>
                </tr>
                {% endfor %}
            {% else %}
                <tr>
                    <td colspan="8" class="uk-text-center uk-text-muted">
                        포스트가 없습니다.
                    </td>
                </tr>
            {% endif %}
        </tbody>
    </table>
</div>

<!-- Pagination -->
{% if pagination.total > 0 %}
<div class="admin__pagination">
    <div class="uk-flex uk-flex-between uk-flex-middle">
        <div class="uk-text-muted">
            총 {{ pagination.total }}개 중 {{ ((pagination.page - 1) * pagination.per_page) + 1 }} - {{ [pagination.page * pagination.per_page, pagination.total]|min }}개 표시
        </div>
        <ul class="uk-pagination uk-margin-remove">
            {% if pagination.has_prev %}
                <li>
                    <a href="?page={{ pagination.prev_num }}&category={{ request.args.get('category', '') }}&public_yn={{ request.args.get('public_yn', '') }}&use_yn={{ request.args.get('use_yn', 'Y') }}&search={{ request.args.get('search', '') }}">
                        <span uk-pagination-previous></span>
                    </a>
                </li>
            {% endif %}

            {% for page_num in range(1, pagination.total_pages + 1) %}
                {% if page_num == pagination.page %}
                    <li class="uk-active"><span>{{ page_num }}</span></li>
                {% elif page_num == 1 or page_num == pagination.total_pages or (page_num >= pagination.page - 2 and page_num <= pagination.page + 2) %}
                    <li>
                        <a href="?page={{ page_num }}&category={{ request.args.get('category', '') }}&public_yn={{ request.args.get('public_yn', '') }}&use_yn={{ request.args.get('use_yn', 'Y') }}&search={{ request.args.get('search', '') }}">
                            {{ page_num }}
                        </a>
                    </li>
                {% elif page_num == pagination.page - 3 or page_num == pagination.page + 3 %}
                    <li class="uk-disabled"><span>...</span></li>
                {% endif %}
            {% endfor %}

            {% if pagination.has_next %}
                <li>
                    <a href="?page={{ pagination.next_num }}&category={{ request.args.get('category', '') }}&public_yn={{ request.args.get('public_yn', '') }}&use_yn={{ request.args.get('use_yn', 'Y') }}&search={{ request.args.get('search', '') }}">
                        <span uk-pagination-next></span>
                    </a>
                </li>
            {% endif %}
        </ul>
    </div>
</div>
{% endif %}
{% endblock %}

{% block scripts %}
<script>
function deletePost(id) {
    Swal.fire({
        title: '포스트를 삭제하시겠습니까?',
        text: "삭제된 포스트는 복구할 수 있습니다.",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#901438',
        cancelButtonColor: '#6c757d',
        confirmButtonText: '삭제',
        cancelButtonText: '취소'
    }).then((result) => {
        if (result.isConfirmed) {
            $.ajax({
                url: "/admin/posts/" + id + "/delete",
                type: "DELETE",
                success: function(res) {
                    if (res.success) {
                        Swal.fire({
                            title: '삭제 완료',
                            text: res.message,
                            icon: 'success'
                        }).then(() => location.reload());
                    }
                },
                error: function() {
                    Swal.fire('오류', '삭제 중 오류가 발생했습니다.', 'error');
                }
            });
        }
    });
}

function restorePost(id) {
    Swal.fire({
        title: '포스트를 복구하시겠습니까?',
        icon: 'question',
        showCancelButton: true,
        confirmButtonColor: '#28a745',
        cancelButtonColor: '#6c757d',
        confirmButtonText: '복구',
        cancelButtonText: '취소'
    }).then((result) => {
        if (result.isConfirmed) {
            $.ajax({
                url: "/admin/posts/" + id + "/restore",
                type: "POST",
                success: function(res) {
                    if (res.success) {
                        Swal.fire({
                            title: '복구 완료',
                            text: res.message,
                            icon: 'success'
                        }).then(() => location.reload());
                    }
                },
                error: function() {
                    Swal.fire('오류', '복구 중 오류가 발생했습니다.', 'error');
                }
            });
        }
    });
}
</script>
{% endblock %}
