{% extends 'base.html' %}
{% load static %}

{% block title %}Dashboard - Rapha-Bethel BNPS{% endblock %}

{% block extra_css %}
<style>
    .stat-card {
        transition: transform 0.3s;
        border-radius: 10px;
        overflow: hidden;
    }
    .stat-card:hover {
        transform: translateY(-5px);
        box-shadow: 0 10px 20px rgba(0,0,0,0.15) !important;
    }
    .stat-icon {
        font-size: 2.5rem;
        opacity: 0.8;
    }
    .progress {
        height: 10px;
        border-radius: 5px;
    }
    .birthday-badge {
        background: linear-gradient(45deg, #FF9800, #FF5722);
        color: white;
        padding: 3px 10px;
        border-radius: 20px;
        font-size: 0.8rem;
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h3 mb-0 text-gray-800">Dashboard</h1>
        <div class="btn-group">
            <button class="btn btn-primary">
                <i class="fas fa-plus"></i> Quick Action
            </button>
        </div>
    </div>

    <!-- Stats Cards -->
    <div class="row">
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card border-left-primary shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">
                                Total Students
                            </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">{{ total_students }}</div>
                            <div class="mt-2 mb-0 text-muted text-xs">
                                <span class="text-success mr-2">
                                    <i class="fas fa-arrow-up"></i> 12%
                                </span>
                                <span>Since last month</span>
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-user-graduate stat-icon text-primary"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card border-left-success shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                Today's Attendance
                            </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">{{ today_attendance }}</div>
                            <div class="mt-2">
                                <div class="progress">
                                    <div class="progress-bar bg-success" role="progressbar" 
                                         style="width: {{ attendance_rate }}%">
                                    </div>
                                </div>
                                <small class="text-muted">{{ attendance_rate }}% Present</small>
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-clipboard-check stat-icon text-success"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card border-left-warning shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">
                                Pending Invoices
                            </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">{{ pending_invoices }}</div>
                            <div class="mt-2 mb-0 text-muted text-xs">
                                {% if overdue_invoices > 0 %}
                                <span class="text-danger">
                                    <i class="fas fa-exclamation-triangle"></i> {{ overdue_invoices }} overdue
                                </span>
                                {% else %}
                                <span class="text-success">All invoices are current</span>
                                {% endif %}
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-file-invoice-dollar stat-icon text-warning"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card border-left-info shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-info text-uppercase mb-1">
                                Total Revenue
                            </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">
                                {{ total_revenue|floatformat:0 }} XAF
                            </div>
                            <div class="mt-2 mb-0 text-muted text-xs">
                                <span class="text-success mr-2">
                                    <i class="fas fa-arrow-up"></i> 8%
                                </span>
                                <span>Since last month</span>
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-money-bill-wave stat-icon text-info"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Charts and Recent Data -->
    <div class="row">
        <!-- Attendance Chart -->
        <div class="col-xl-8 col-lg-7">
            <div class="card shadow mb-4">
                <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                    <h6 class="m-0 font-weight-bold text-primary">Weekly Attendance Trend</h6>
                </div>
                <div class="card-body">
                    <div class="chart-area">
                        <canvas id="attendanceChart" height="300"></canvas>
                    </div>
                </div>
            </div>
        </div>

        <!-- Upcoming Birthdays -->
        <div class="col-xl-4 col-lg-5">
            <div class="card shadow mb-4">
                <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                    <h6 class="m-0 font-weight-bold text-primary">Upcoming Birthdays</h6>
                </div>
                <div class="card-body">
                    {% if upcoming_birthdays %}
                    <div class="list-group list-group-flush">
                        {% for student in upcoming_birthdays %}
                        <div class="list-group-item d-flex align-items-center">
                            <div class="mr-3">
                                <div class="icon-circle bg-warning">
                                    <i class="fas fa-birthday-cake text-white"></i>
                                </div>
                            </div>
                            <div class="flex-grow-1">
                                <div class="font-weight-bold">{{ student.first_name }} {{ student.last_name }}</div>
                                <div class="text-muted small">
                                    {{ student.date_of_birth|date:"F d" }} • {{ student.current_class|default:"No Class" }}
                                </div>
                            </div>
                            <span class="birthday-badge">{{ student.date_of_birth|date:"d M" }}</span>
                        </div>
                        {% endfor %}
                    </div>
                    {% else %}
                    <p class="text-muted text-center py-4">No upcoming birthdays</p>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <!-- Recent Students & Announcements -->
    <div class="row">
        <!-- Recent Students -->
        <div class="col-lg-6 mb-4">
            <div class="card shadow">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">Recent Admissions</h6>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>Student</th>
                                    <th>Admission Date</th>
                                    <th>Class</th>
                                    <th>Status</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for student in recent_students %}
                                <tr>
                                    <td>
                                        <strong>{{ student.first_name }} {{ student.last_name }}</strong><br>
                                        <small class="text-muted">{{ student.student_id }}</small>
                                    </td>
                                    <td>{{ student.admission_date|date:"d M Y" }}</td>
                                    <td>
                                        {% if student.current_class %}
                                        <span class="badge badge-info">{{ student.current_class }}</span>
                                        {% else %}
                                        <span class="badge badge-secondary">Not Assigned</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if student.status == 'active' %}
                                        <span class="badge badge-success">Active</span>
                                        {% else %}
                                        <span class="badge badge-warning">{{ student.status|title }}</span>
                                        {% endif %}
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!-- Recent Announcements -->
        <div class="col-lg-6 mb-4">
            <div class="card shadow">
                <div class="card-header py-3 d-flex justify-content-between align-items-center">
                    <h6 class="m-0 font-weight-bold text-primary">Recent Announcements</h6>
                    <a href="/admin/communication/announcement/add/" class="btn btn-sm btn-primary">
                        <i class="fas fa-plus"></i> New
                    </a>
                </div>
                <div class="card-body">
                    {% for announcement in recent_announcements %}
                    <div class="mb-3 pb-3 border-bottom">
                        <h6 class="font-weight-bold">{{ announcement.title }}</h6>
                        <p class="text-muted small mb-2">{{ announcement.content|truncatewords:20 }}</p>
                        <div class="d-flex justify-content-between align-items-center">
                            <small class="text-muted">
                                <i class="far fa-clock"></i> {{ announcement.publish_date|timesince }} ago
                            </small>
                            <span class="badge badge-{{ announcement.priority }}">
                                {{ announcement.get_priority_display }}
                            </span>
                        </div>
                    </div>
                    {% empty %}
                    <p class="text-muted text-center py-4">No announcements yet</p>
                    {% endfor %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
// Attendance Chart
document.addEventListener('DOMContentLoaded', function() {
    const ctx = document.getElementById('attendanceChart').getContext('2d');
    const attendanceChart = new Chart(ctx, {
        type: 'line',
        data: {
            labels: {{ attendance_labels|safe }},
            datasets: [{
                label: 'Attendance',
                data: {{ attendance_data }},
                borderColor: '#4e73df',
                backgroundColor: 'rgba(78, 115, 223, 0.05)',
                pointRadius: 3,
                pointBackgroundColor: '#4e73df',
                pointBorderColor: '#4e73df',
                pointHoverRadius: 5,
                fill: true,
                tension: 0.4
            }]
        },
        options: {
            maintainAspectRatio: false,
            scales: {
                x: {
                    grid: {
                        display: false
                    }
                },
                y: {
                    beginAtZero: true,
                    ticks: {
                        precision: 0
                    }
                }
            },
            plugins: {
                legend: {
                    display: false
                }
            }
        }
    });
    
    // Add animation to stats cards
    document.querySelectorAll('.stat-card').forEach(card => {
        card.addEventListener('mouseenter', function() {
            this.style.transform = 'translateY(-5px)';
        });
        card.addEventListener('mouseleave', function() {
            this.style.transform = 'translateY(0)';
        });
    });
});
</script>
{% endblock %}