{% extends "base.html" %}

{% block title %}Messages - Admin - A+ Academy{% endblock %}

{% block extra_css %}
<style>
    .admin-sidebar {
        background: white;
        border-radius: 20px;
        box-shadow: var(--soft-shadow);
        padding: 1.5rem;
        height: calc(100vh - 140px);
        position: sticky;
        top: 100px;
    }
    .admin-nav-link {
        display: flex;
        align-items: center;
        gap: 10px;
        padding: 12px 16px;
        color: var(--text-dark);
        text-decoration: none;
        border-radius: 10px;
        margin-bottom: 8px;
        transition: all 0.3s ease;
    }
    .admin-nav-link:hover, .admin-nav-link.active {
        background: rgba(37, 99, 235, 0.1);
        color: var(--primary-blue);
    }
    .message-row {
        transition: all 0.3s ease;
    }
    .message-row:hover {
        background: rgba(37, 99, 235, 0.02);
    }
    .message-row.unread {
        background: rgba(37, 99, 235, 0.05);
        font-weight: 500;
    }
</style>
{% endblock %}

{% block content %}
<section style="padding: 120px 0 40px;">
    <div class="container">
        <div class="row g-4">
            <!-- Sidebar -->
            <div class="col-lg-3">
                <div class="admin-sidebar">
                    <div class="d-flex align-items-center gap-2 mb-4 pb-3 border-bottom">
                        <i class="bi bi-person-circle text-primary fs-4"></i>
                        <div>
                            <div class="fw-bold">{{ current_user.username }}</div>
                            <small class="text-muted">Administrator</small>
                        </div>
                    </div>

                    <nav>
                        <a href="{{ url_for('admin.dashboard') }}" class="admin-nav-link">
                            <i class="bi bi-speedometer2"></i> Dashboard
                        </a>
                        <a href="{{ url_for('admin.messages') }}" class="admin-nav-link active">
                            <i class="bi bi-envelope"></i> Messages
                        </a>
                        <a href="{{ url_for('admin.content') }}" class="admin-nav-link">
                            <i class="bi bi-file-text"></i> Site Content
                        </a>
                        <a href="{{ url_for('admin.admin_subjects') }}" class="admin-nav-link">
                            <i class="bi bi-book"></i> Subjects
                        </a>
                        <div class="border-top my-3"></div>
                        <a href="{{ url_for('admin.logout') }}" class="admin-nav-link text-danger">
                            <i class="bi bi-box-arrow-right"></i> Logout
                        </a>
                    </nav>
                </div>
            </div>

            <!-- Main Content -->
            <div class="col-lg-9">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h4 class="mb-0">Contact Messages</h4>
                    <div class="btn-group">
                        <a href="{{ url_for('admin.messages', status='all') }}" class="btn btn-sm {{ 'btn-primary' if status == 'all' else 'btn-outline-primary' }}">All</a>
                        <a href="{{ url_for('admin.messages', status='unread') }}" class="btn btn-sm {{ 'btn-primary' if status == 'unread' else 'btn-outline-primary' }}">Unread</a>
                        <a href="{{ url_for('admin.messages', status='unreplied') }}" class="btn btn-sm {{ 'btn-primary' if status == 'unreplied' else 'btn-outline-primary' }}">Pending</a>
                    </div>
                </div>

                <div class="soft-card p-0 overflow-hidden">
                    {% if messages.items %}
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead class="bg-light">
                                <tr>
                                    <th>From</th>
                                    <th>Subject</th>
                                    <th>Message</th>
                                    <th>Date</th>
                                    <th>Status</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for msg in messages.items %}
                                <tr class="message-row {{ 'unread' if not msg.is_read }}">
                                    <td>
                                        <div class="fw-semibold">{{ msg.name }}</div>
                                        <small class="text-muted">{{ msg.email }}</small>
                                    </td>
                                    <td>
                                        <span class="badge bg-secondary">{{ msg.subject }}</span>
                                    </td>
                                    <td>
                                        <small class="text-muted">{{ msg.message[:50] }}...</small>
                                    </td>
                                    <td>
                                        <small>{{ msg.created_at.strftime('%d %b %Y') }}</small>
                                    </td>
                                    <td>
                                        {% if msg.is_replied %}
                                            <span class="badge bg-success">Replied</span>
                                        {% elif msg.is_read %}
                                            <span class="badge bg-info">Read</span>
                                        {% else %}
                                            <span class="badge bg-warning">New</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <a href="{{ url_for('admin.view_message', message_id=msg.id) }}" class="btn btn-sm btn-primary">
                                            <i class="bi bi-eye"></i>
                                        </a>
                                        {% if not msg.is_replied %}
                                        <a href="{{ url_for('admin.reply_message', message_id=msg.id) }}" class="btn btn-sm btn-success">
                                            <i class="bi bi-reply"></i>
                                        </a>
                                        {% endif %}
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>

                    <!-- Pagination -->
                    {% if messages.pages > 1 %}
                    <nav class="p-3 border-top">
                        <ul class="pagination justify-content-center mb-0">
                            {% if messages.has_prev %}
                            <li class="page-item">
                                <a class="page-link" href="{{ url_for('admin.messages', page=messages.prev_num, status=status) }}">Previous</a>
                            </li>
                            {% endif %}

                            {% for page_num in messages.iter_pages(left_edge=1, right_edge=1, left_current=1, right_current=2) %}
                                {% if page_num %}
                                    <li class="page-item {{ 'active' if page_num == messages.page else '' }}">
                                        <a class="page-link" href="{{ url_for('admin.messages', page=page_num, status=status) }}">{{ page_num }}</a>
                                    </li>
                                {% else %}
                                    <li class="page-item disabled"><span class="page-link">...</span></li>
                                {% endif %}
                            {% endfor %}

                            {% if messages.has_next %}
                            <li class="page-item">
                                <a class="page-link" href="{{ url_for('admin.messages', page=messages.next_num, status=status) }}">Next</a>
                            </li>
                            {% endif %}
                        </ul>
                    </nav>
                    {% endif %}

                    {% else %}
                    <div class="text-center py-5">
                        <i class="bi bi-inbox text-muted fs-1"></i>
                        <p class="mt-2 text-muted">No messages found</p>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</section>
{% endblock %}
