<!-- templates/academics/class_detail.html -->
{% extends 'base.html' %}
{% load static %}

{% block title %}{{ class.name }} - Class Details{% endblock %}

{% block extra_css %}
<style>
    .stat-card {
        transition: transform 0.3s, box-shadow 0.3s;
        border-radius: 10px;
        overflow: hidden;
    }
    .stat-card:hover {
        transform: translateY(-5px);
        box-shadow: 0 10px 20px rgba(0,0,0,0.15);
    }
    .teacher-card {
        border-left: 4px solid #4CAF50;
        transition: all 0.3s;
        padding: 15px;
        margin-bottom: 15px;
        background-color: #f8f9fc;
        border-radius: 8px;
    }
    .teacher-card:hover {
        background-color: #e8f0fe;
        transform: translateX(5px);
    }
    .student-row:hover {
        background-color: #f8f9fc;
        cursor: pointer;
    }
    .class-header {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        padding: 30px;
        border-radius: 15px;
        margin-bottom: 25px;
        box-shadow: 0 10px 30px rgba(102, 126, 234, 0.3);
    }
    .info-label {
        font-weight: 600;
        color: #495057;
        width: 150px;
    }
    .avatar-circle {
        width: 50px;
        height: 50px;
        border-radius: 50%;
        background-color: #4CAF50;
        color: white;
        display: flex;
        align-items: center;
        justify-content: center;
        font-weight: bold;
        font-size: 1.2rem;
        box-shadow: 0 4px 10px rgba(76, 175, 80, 0.3);
    }
    .small-avatar {
        width: 35px;
        height: 35px;
        font-size: 0.9rem;
    }
    .badge-subject {
        background-color: #e3f2fd;
        color: #1976d2;
        padding: 5px 10px;
        border-radius: 20px;
        font-size: 0.8rem;
        font-weight: 500;
    }
    .progress {
        height: 10px;
        border-radius: 5px;
    }
    .section-title {
        position: relative;
        padding-bottom: 10px;
        margin-bottom: 20px;
    }
    .section-title:after {
        content: '';
        position: absolute;
        bottom: 0;
        left: 0;
        width: 50px;
        height: 3px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border-radius: 3px;
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- Class Header -->
    <div class="class-header d-flex justify-content-between align-items-center">
        <div>
            <h1 class="mb-2">{{ class.name }}</h1>
            <p class="mb-0">
                <i class="fas fa-code me-2"></i> {{ class.code }} |
                <i class="fas fa-calendar ms-3 me-2"></i> {{ class.academic_year }} |
                <i class="fas fa-users ms-3 me-2"></i> {{ total_students }} Students |
                <i class="fas fa-chalkboard-teacher ms-3 me-2"></i> {{ total_teachers }} Teachers
            </p>
        </div>
        <div>
            <a href="{% url 'academics:class_list' %}" class="btn btn-light me-2">
                <i class="fas fa-arrow-left"></i> Back
            </a>
            <a href="/admin/academics/schoolclass/{{ class.id }}/change/" class="btn btn-warning">
                <i class="fas fa-edit"></i> Edit
            </a>
        </div>
    </div>

    <!-- Quick Stats Cards -->
    <div class="row mb-4">
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card border-left-primary shadow h-100">
                <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="text-muted small mt-2">
                                <i class="fas fa-user-plus text-success me-1"></i> Capacity: {{ class.capacity|default:"40" }}
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-users fa-2x text-gray-300"></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">
                <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">
                                Teachers
                            </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">{{ total_teachers }}</div>
                            <div class="text-muted small mt-2">
                                <i class="fas fa-chalkboard-teacher text-info me-1"></i> {{ total_subjects }} Subjects
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-chalkboard-teacher fa-2x text-gray-300"></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">
                <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">
                                Today's Attendance
                            </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">{{ today_attendance|default:"0" }}</div>
                            <div class="progress mt-2">
                                <div class="progress-bar bg-info" role="progressbar" 
                                     style="width: {{ attendance_rate|default:'0' }}%"></div>
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-calendar-check fa-2x text-gray-300"></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">
                <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">
                                Section
                            </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">{{ class.get_section_type_display }}</div>
                            <div class="text-muted small mt-2">
                                <i class="fas fa-door-open me-1"></i> Room: {{ class.room_number|default:"TBD" }}
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-door-open fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <!-- Left Column - Class Info & Teachers -->
        <div class="col-lg-5">
            <!-- Class Information Card -->
            <div class="card shadow mb-4">
                <div class="card-header py-3 d-flex justify-content-between align-items-center">
                    <h6 class="m-0 font-weight-bold text-primary">Class Information</h6>
                    <span class="badge bg-{{ class.section_type }}">{{ class.get_section_type_display }}</span>
                </div>
                <div class="card-body">
                    <table class="table table-borderless">
                        <tr>
                            <td class="info-label">Code:</td>
                            <td><span class="badge bg-secondary">{{ class.code }}</span></td>
                        </tr>
                        <tr>
                            <td class="info-label">Level:</td>
                            <td>{{ class.get_level_display }}</td>
                        </tr>
                        <tr>
                            <td class="info-label">Section:</td>
                            <td>{{ class.section|default:"A" }}</td>
                        </tr>
                        <tr>
                            <td class="info-label">Academic Year:</td>
                            <td>{{ class.academic_year }}</td>
                        </tr>
                        <tr>
                            <td class="info-label">Room Number:</td>
                            <td>{{ class.room_number|default:"<span class='text-muted'>Not assigned</span>" }}</td>
                        </tr>
                        <tr>
                            <td class="info-label">Capacity:</td>
                            <td>
                                <div class="d-flex align-items-center">
                                    <span>{{ total_students }}/{{ class.capacity|default:"40" }}</span>
                                    <div class="progress flex-grow-1 ms-3" style="height: 8px;">
                                        {% with percentage=total_students|divisibleby:class.capacity|default:40|floatformat:0 %}
                                        <div class="progress-bar bg-{% if percentage > 90 %}danger{% elif percentage > 75 %}warning{% else %}success{% endif %}" 
                                             style="width: {% widthratio total_students class.capacity|default:40 100 %}%"></div>
                                        {% endwith %}
                                    </div>
                                </div>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>

            <!-- Class Teacher Card -->
            {% if class_teacher %}
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-success">
                        <i class="fas fa-star"></i> Class Teacher
                    </h6>
                </div>
                <div class="card-body">
                    <div class="d-flex align-items-center">
                        <div class="avatar-circle me-3">
                            {{ class_teacher.user.first_name|first|upper }}{{ class_teacher.user.last_name|first|upper }}
                        </div>
                        <div>
                            <h5 class="mb-1">{{ class_teacher.user.get_full_name }}</h5>
                            <p class="text-muted small mb-1">
                                <i class="fas fa-phone"></i> {{ class_teacher.phone_number|default:"No phone" }}
                            </p>
                            <p class="text-muted small mb-0">
                                <i class="fas fa-envelope"></i> {{ class_teacher.user.email }}
                            </p>
                        </div>
                        <div class="ms-auto">
                            <a href="{% url 'staff:staff_detail' class_teacher.pk %}" class="btn btn-sm btn-outline-primary">
                                <i class="fas fa-eye"></i>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
            {% endif %}

            <!-- Subject Teachers Card -->
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-info">
                        <i class="fas fa-chalkboard-teacher"></i> Subject Teachers ({{ total_teachers }})
                    </h6>
                </div>
                <div class="card-body">
                    {% if teachers %}
                        {% for teacher_data in teachers %}
                        <div class="teacher-card">
                            <div class="d-flex justify-content-between align-items-start">
                                <div>
                                    <h6 class="mb-2">{{ teacher_data.teacher.user.get_full_name }}</h6>
                                    <div class="mb-2">
                                        {% for subject in teacher_data.subjects %}
                                        <span class="badge-subject me-1 mb-1">
                                            <i class="fas fa-book me-1"></i>{{ subject.name }}
                                        </span>
                                        {% endfor %}
                                    </div>
                                    <small class="text-muted">
                                        <i class="fas fa-phone"></i> {{ teacher_data.teacher.phone_number|default:"No phone" }}
                                    </small>
                                </div>
                                <a href="{% url 'staff:staff_detail' teacher_data.teacher.pk %}" 
                                   class="btn btn-sm btn-outline-primary">
                                    <i class="fas fa-eye"></i>
                                </a>
                            </div>
                        </div>
                        {% endfor %}
                    {% else %}
                        <div class="text-center py-4">
                            <i class="fas fa-chalkboard-teacher fa-2x text-muted mb-3"></i>
                            <p class="text-muted">No teachers assigned yet</p>
                            <a href="/admin/academics/classsubject/add/?class={{ class.id }}" 
                               class="btn btn-sm btn-primary">
                                <i class="fas fa-plus"></i> Assign Teacher
                            </a>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>

        <!-- Right Column - Students List -->
        <div class="col-lg-7">
            <div class="card shadow mb-4">
                <div class="card-header py-3 d-flex justify-content-between align-items-center">
                    <h6 class="m-0 font-weight-bold text-primary">
                        <i class="fas fa-users"></i> Students in {{ class.name }} ({{ total_students }})
                    </h6>
                    <div>
                        <a href="{% url 'students:student_add' %}?class={{ class.id }}" class="btn btn-sm btn-success">
                            <i class="fas fa-plus"></i> Add Student
                        </a>
                        <a href="{% url 'students:import_students_csv' %}?class={{ class.id }}" 
                           class="btn btn-sm btn-info">
                            <i class="fas fa-upload"></i> Import
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    {% if students %}
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Student</th>
                                    <th>Gender</th>
                                    <th>Age</th>
                                    <th>Parent Contact</th>
                                    <th>Status</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for student in students %}
                                <tr class="student-row" onclick="window.location='{% url 'students:student_detail' student.pk %}'">
                                    <td><small>{{ student.student_id }}</small></td>
                                    <td>
                                        <div class="d-flex align-items-center">
                                            <div class="avatar-circle small-avatar me-2">
                                                {{ student.first_name|first|upper }}{{ student.last_name|first|upper }}
                                            </div>
                                            <strong>{{ student.first_name }} {{ student.last_name }}</strong>
                                        </div>
                                    </td>
                                    <td>
                                        <span class="badge bg-{% if student.gender == 'M' %}info{% else %}warning{% endif %}">
                                            {{ student.get_gender_display }}
                                        </span>
                                    </td>
                                    <td>{{ student.get_age|default:"-" }}</td>
                                    <td>
                                        {% with parent=student.parents.first %}
                                            {% if parent %}
                                                <small>{{ parent.phone }}</small>
                                            {% else %}
                                                <span class="text-muted">—</span>
                                            {% endif %}
                                        {% endwith %}
                                    </td>
                                    <td>
                                        <span class="badge bg-{% if student.status == 'active' %}success{% else %}secondary{% endif %}">
                                            {{ student.get_status_display }}
                                        </span>
                                    </td>
                                    <td>
                                        <a href="{% url 'students:student_detail' student.pk %}" 
                                           class="btn btn-sm btn-outline-primary" 
                                           onclick="event.stopPropagation();"
                                           title="View">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        <a href="{% url 'students:student_edit' student.pk %}" 
                                           class="btn btn-sm btn-outline-warning"
                                           onclick="event.stopPropagation();"
                                           title="Edit">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <div class="text-center py-5">
                        <i class="fas fa-users fa-3x text-muted mb-3"></i>
                        <h5 class="text-muted">No students in this class</h5>
                        <p class="text-muted mb-3">Add students to {{ class.name }} to see them here</p>
                        <a href="{% url 'students:student_add' %}?class={{ class.id }}" class="btn btn-primary">
                            <i class="fas fa-user-plus"></i> Add Student
                        </a>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Assign Teacher Modal -->
<div class="modal fade" id="assignTeacherModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-success text-white">
                <h5 class="modal-title">Assign Teacher to {{ class.name }}</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form method="post" action="{% url 'academics:assign_teacher' class.id %}">
                {% csrf_token %}
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Select Teacher</label>
                        <select name="teacher_id" class="form-control" required>
                            <option value="">-- Choose a teacher --</option>
                            {% for teacher in all_teachers %}
                            <option value="{{ teacher.id }}">
                                {{ teacher.user.get_full_name }} ({{ teacher.get_role_display }})
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Select Subject</label>
                        <select name="subject_id" class="form-control" required>
                            <option value="">-- Choose a subject --</option>
                            {% for subject in all_subjects %}
                            <option value="{{ subject.id }}">{{ subject.name }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Hours per week</label>
                        <input type="number" name="hours_per_week" class="form-control" value="2" min="1" max="10">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-success">Assign Teacher</button>
                </div>
            </form>
        </div>
    </div>
</div>

{% endblock %}

{% block extra_js %}
<script>
    // Add click handlers or any additional JS here
    document.addEventListener('DOMContentLoaded', function() {
        // Tooltip initialization if needed
        var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
        var tooltipList = tooltipTriggerList.map(function(tooltipTriggerEl) {
            return new bootstrap.Tooltip(tooltipTriggerEl)
        });
    });
</script>
{% endblock %}