{% extends 'base.html' %}
{% load static %}
{% block title %}SMS Messages - 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">SMS Messages</h1><p class="text-muted">All outgoing SMS records</p></div>
        <a href="{% url 'communication:sms_compose' %}" class="btn btn-primary"><i class="fas fa-sms me-2"></i>Compose SMS</a>
    </div>
    <div class="card shadow mb-4">
        <div class="card-body">
            <form method="get" class="row g-3 align-items-end">
                <div class="col-md-4"><label class="form-label">Type</label>
                    <select name="type" class="form-select">
                        <option value="">All Types</option>
                        {% for val, label in message_types %}<option value="{{ val }}" {% if request.GET.type == val %}selected{% endif %}>{{ label }}</option>{% endfor %}
                    </select>
                </div>
                <div class="col-md-4"><label class="form-label">Status</label>
                    <select name="status" class="form-select">
                        <option value="">All Statuses</option>
                        {% for val, label in status_choices %}<option value="{{ val }}" {% if request.GET.status == val %}selected{% endif %}>{{ label }}</option>{% endfor %}
                    </select>
                </div>
                <div class="col-md-4"><button type="submit" class="btn btn-primary w-100"><i class="fas fa-filter me-1"></i>Filter</button></div>
            </form>
        </div>
    </div>
    <div class="card shadow">
        <div class="card-body p-0">
            <table class="table table-hover mb-0">
                <thead class="table-dark"><tr><th>Type</th><th>Message Preview</th><th>Recipients</th><th>Sent</th><th>Status</th><th>Action</th></tr></thead>
                <tbody>
                    {% for sms in messages %}
                    <tr>
                        <td><span class="badge bg-info">{{ sms.get_message_type_display }}</span></td>
                        <td>{{ sms.message|truncatechars:60 }}</td>
                        <td>{{ sms.phone_numbers|length }} number{{ sms.phone_numbers|length|pluralize }}</td>
                        <td>{{ sms.sent_time|date:"d M Y H:i"|default:"Draft" }}</td>
                        <td><span class="badge bg-{% if sms.status == 'sent' %}success{% elif sms.status == 'failed' %}danger{% else %}secondary{% endif %}">{{ sms.get_status_display }}</span></td>
                        <td><a href="{% url 'communication:sms_detail' sms.pk %}" class="btn btn-sm btn-outline-primary"><i class="fas fa-eye"></i></a></td>
                    </tr>
                    {% empty %}
                    <tr><td colspan="6" class="text-center py-4 text-muted"><i class="fas fa-sms fa-2x mb-2 d-block"></i>No SMS messages yet.</td></tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock %}
