{% extends "base.html" %}

{% block title %}Admin Dashboard - 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);
    }
    .stat-card {
        background: white;
        border-radius: 16px;
        padding: 1.5rem;
        box-shadow: var(--soft-shadow);
        border-left: 4px solid var(--primary-blue);
    }
    .stat-card.warning { border-left-color: #f59e0b; }
    .stat-card.success { border-left-color: #10b981; }
    .stat-card.info { border-left-color: #3b82f6; }
</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 active">
                            <i class="bi bi-speedometer2"></i> Dashboard
                        </a>
                        <a href="{{ url_for('admin.messages') }}" class="admin-nav-link">
                            <i class="bi bi-envelope"></i> Messages
                            {% if unread_messages > 0 %}
                                <span class="badge bg-danger ms-auto">{{ unread_messages }}</span>
                            {% endif %}
                        </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">
                <h4 class="mb-4">Dashboard Overview</h4>

                <!-- Stats Row -->
                <div class="row g-3 mb-4">
                    <div class="col-md-3">
                        <div class="stat-card">
                            <div class="text-muted small mb-1">Total Messages</div>
                            <div class="fs-3 fw-bold">{{ total_messages }}</div>
                            <i class="bi bi-envelope text-primary opacity-25 fs-1 position-absolute" style="right: 20px; top: 20px;"></i>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="stat-card warning">
                            <div class="text-muted small mb-1">Unread</div>
                            <div class="fs-3 fw-bold">{{ unread_messages }}</div>
                            <i class="bi bi-envelope-open text-warning opacity-25 fs-1 position-absolute" style="right: 20px; top: 20px;"></i>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="stat-card info">
                            <div class="text-muted small mb-1">Pending Reply</div>
                            <div class="fs-3 fw-bold">{{ unreplied_messages }}</div>
                            <i class="bi bi-reply text-info opacity-25 fs-1 position-absolute" style="right: 20px; top: 20px;"></i>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="stat-card success">
                            <div class="text-muted small mb-1">Active Subjects</div>
                            <div class="fs-3 fw-bold">{{ active_subjects }}</div>
                            <i class="bi bi-book text-success opacity-25 fs-1 position-absolute" style="right: 20px; top: 20px;"></i>
                        </div>
                    </div>
                </div>

                <!-- Recent Messages -->
                <div class="soft-card p-4">
                    <div class="d-flex justify-content-between align-items-center mb-4">
                        <h5 class="mb-0">Recent Messages</h5>
                        <a href="{{ url_for('admin.messages') }}" class="btn btn-sm btn-outline-primary">View All</a>
                    </div>

                    {% if recent_messages %}
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>Name</th>
                                    <th>Subject</th>
                                    <th>Date</th>
                                    <th>Status</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for msg in recent_messages %}
                                <tr>
                                    <td>
                                        <div class="fw-semibold">{{ msg.name }}</div>
                                        <small class="text-muted">{{ msg.email }}</small>
                                    </td>
                                    <td>{{ msg.subject }}</td>
                                    <td>{{ msg.created_at.strftime('%Y-%m-%d') }}</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>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <div class="text-center py-5 text-muted">
                        <i class="bi bi-inbox fs-1"></i>
                        <p class="mt-2">No messages yet</p>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</section>
{% endblock %}
