{% extends 'base.html' %}
{% load static %}
{% block title %}Compose SMS - 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">Compose SMS</h1><p class="text-muted">{{ recipient_count }} recipient{{ recipient_count|pluralize }} selected</p></div>
        <a href="{% url 'communication:sms_list' %}" class="btn btn-outline-secondary"><i class="fas fa-arrow-left me-2"></i>Back</a>
    </div>
    <div class="row">
        <div class="col-md-8">
            <div class="card shadow mb-4">
                <div class="card-header py-3 bg-primary text-white"><h6 class="m-0"><i class="fas fa-sms me-2"></i>Message</h6></div>
                <div class="card-body">
                    <form method="post">
                        {% csrf_token %}
                        <div class="mb-3">
                            <label class="form-label fw-bold">Recipient Type</label>
                            <div class="btn-group w-100" role="group">
                                <a href="?recipient_type=all" class="btn btn-{% if recipient_type == 'all' or not recipient_type %}primary{% else %}outline-primary{% endif %}">All Parents</a>
                                <a href="?recipient_type=staff" class="btn btn-{% if recipient_type == 'staff' %}primary{% else %}outline-primary{% endif %}">Staff</a>
                                <a href="?recipient_type=parents" class="btn btn-{% if recipient_type == 'parents' %}primary{% else %}outline-primary{% endif %}">SMS-Opted Parents</a>
                            </div>
                        </div>
                        <div class="mb-3">
                            <label class="form-label fw-bold">Message Type <span class="text-danger">*</span></label>
                            <select name="message_type" class="form-select" required>
                                {% for val, label in message_types %}<option value="{{ val }}">{{ label }}</option>{% endfor %}
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label fw-bold">Message <span class="text-danger">*</span></label>
                            <textarea name="message" class="form-control" rows="5" maxlength="160" required placeholder="Type your message (max 160 characters)..."></textarea>
                            <div class="text-end text-muted small mt-1"><span id="charCount">0</span>/160 characters</div>
                        </div>
                        <div class="mb-3">
                            <label class="form-label fw-bold">Select Recipients</label>
                            <div style="max-height:300px;overflow-y:auto;border:1px solid #dee2e6;border-radius:4px;padding:10px;">
                                <div class="mb-2">
                                    <button type="button" class="btn btn-sm btn-outline-secondary" onclick="selectAll(true)">Select All</button>
                                    <button type="button" class="btn btn-sm btn-outline-secondary ms-2" onclick="selectAll(false)">Deselect All</button>
                                </div>
                                {% for recipient in recipients %}
                                <div class="form-check">
                                    <input class="form-check-input recipient-check" type="checkbox" name="recipients" value="{{ recipient.pk }}" id="r{{ recipient.pk }}" checked>
                                    <label class="form-check-label" for="r{{ recipient.pk }}">
                                        {{ recipient.get_full_name }} — {{ recipient.phone }}
                                        {% if recipient.student %}<small class="text-muted">({{ recipient.student.get_full_name }})</small>{% endif %}
                                    </label>
                                </div>
                                {% empty %}
                                <p class="text-muted mb-0">No recipients available for this type.</p>
                                {% endfor %}
                            </div>
                        </div>
                        <div class="alert alert-info"><i class="fas fa-info-circle me-2"></i>In development mode, SMS messages are logged but not actually sent. Integration with an SMS gateway (e.g. Orange/MTN API) is required for production.</div>
                        <div class="d-flex justify-content-end gap-2">
                            <a href="{% url 'communication:sms_list' %}" class="btn btn-secondary">Cancel</a>
                            <button type="submit" class="btn btn-primary"><i class="fas fa-paper-plane me-2"></i>Send SMS</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card shadow"><div class="card-header py-3"><h6 class="m-0 font-weight-bold text-primary">SMS Tips</h6></div>
            <div class="card-body">
                <ul class="list-unstyled small">
                    <li class="mb-2"><i class="fas fa-check text-success me-2"></i>Keep messages under 160 characters</li>
                    <li class="mb-2"><i class="fas fa-check text-success me-2"></i>Use French for parent messages where possible</li>
                    <li class="mb-2"><i class="fas fa-check text-success me-2"></i>Avoid sending after 8PM</li>
                    <li class="mb-2"><i class="fas fa-check text-success me-2"></i>Always include school name in message</li>
                </ul>
            </div></div>
        </div>
    </div>
</div>
{% block extra_js %}
<script>
const msg = document.querySelector('[name="message"]');
const count = document.getElementById('charCount');
if (msg) msg.addEventListener('input', () => count.textContent = msg.value.length);
function selectAll(v) { document.querySelectorAll('.recipient-check').forEach(c => c.checked = v); }
</script>
{% endblock %}
{% endblock %}
