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

{% block title %}Parents & Guardians - Rapha-Bethel BNPS{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1 class="h3 mb-0">Parents & Guardians</h1>
            <p class="text-muted">{{ total_parents }} contact{{ total_parents|pluralize }} on record</p>
        </div>
        <a href="{% url 'students:parent_add' %}" class="btn btn-primary">
            <i class="fas fa-user-plus me-2"></i>Add Parent/Guardian
        </a>
    </div>

    <!-- Search -->
    <div class="card shadow mb-4">
        <div class="card-body">
            <form method="get" class="row g-3 align-items-end">
                <div class="col-md-5">
                    <input type="text" name="q" class="form-control" placeholder="Search by name or phone..." value="{{ request.GET.q }}">
                </div>
                <div class="col-md-3">
                    <select name="relationship" class="form-control">
                        <option value="">All Relationships</option>
                        <option value="father" {% if request.GET.relationship == "father" %}selected{% endif %}>Father</option>
                        <option value="mother" {% if request.GET.relationship == "mother" %}selected{% endif %}>Mother</option>
                        <option value="guardian" {% if request.GET.relationship == "guardian" %}selected{% endif %}>Guardian</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary w-100"><i class="fas fa-search me-1"></i>Search</button>
                </div>
                <div class="col-md-2">
                    <a href="{% url 'students:parent_list' %}" class="btn btn-outline-secondary w-100">Clear</a>
                </div>
            </form>
        </div>
    </div>

    <!-- Table -->
    <div class="card shadow">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="table-dark">
                        <tr>
                            <th>Name</th>
                            <th>Relationship</th>
                            <th>Phone</th>
                            <th>Student(s)</th>
                            <th>Primary Contact</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for parent in parents %}
                        <tr>
                            <td>
                                <div class="fw-bold">{{ parent.get_full_name }}</div>
                                {% if parent.email %}<small class="text-muted">{{ parent.email }}</small>{% endif %}
                            </td>
                            <td><span class="badge bg-secondary">{{ parent.get_relationship_display }}</span></td>
                            <td>
                                <i class="fas fa-phone text-success me-1"></i>{{ parent.phone }}
                                {% if parent.alternate_phone %}<br><small class="text-muted">{{ parent.alternate_phone }}</small>{% endif %}
                            </td>
                            <td>
                                <a href="{% url 'students:student_detail' parent.student.pk %}" class="text-decoration-none">
                                    {{ parent.student.get_full_name }}
                                </a>
                                <br><small class="text-muted">{{ parent.student.current_class }}</small>
                            </td>
                            <td>
                                {% if parent.is_primary_contact %}
                                <span class="badge bg-success"><i class="fas fa-star me-1"></i>Primary</span>
                                {% else %}
                                <span class="text-muted">—</span>
                                {% endif %}
                            </td>
                            <td>
                                <a href="{% url 'students:parent_detail' parent.pk %}" class="btn btn-sm btn-outline-primary me-1">
                                    <i class="fas fa-eye"></i>
                                </a>
                                <a href="{% url 'students:parent_edit' parent.pk %}" class="btn btn-sm btn-outline-warning">
                                    <i class="fas fa-edit"></i>
                                </a>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="6" class="text-center py-4 text-muted">
                                <i class="fas fa-users fa-2x mb-2 d-block"></i>No parents/guardians found.
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
{% endblock %}
