{% extends 'base.html' %}
{% load static %}
{% block title %}{{ title }} - 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">{{ title }}</h1></div>
        <a href="{% url 'finance:payment_list' %}" class="btn btn-outline-secondary"><i class="fas fa-arrow-left me-2"></i>Back</a>
    </div>
    <div class="row justify-content-center">
        <div class="col-md-8">
            {% if selected_invoice %}
            <div class="alert alert-info mb-4">
                <i class="fas fa-file-invoice me-2"></i>Recording payment for invoice <strong>{{ selected_invoice.invoice_number }}</strong>
                — {{ selected_invoice.student.get_full_name }} &bull; Balance: <strong>{{ selected_invoice.balance|floatformat:0 }} XAF</strong>
            </div>
            {% endif %}
            <div class="card shadow">
                <div class="card-header py-3 bg-success text-white"><h6 class="m-0"><i class="fas fa-money-bill me-2"></i>Payment Information</h6></div>
                <div class="card-body">
                    <form method="post">
                        {% csrf_token %}
                        {% if form.errors %}<div class="alert alert-danger">Please correct the errors below.</div>{% endif %}

                        {% if not object %}
                        <div class="mb-3">
                            <label class="form-label fw-bold">Invoice <span class="text-danger">*</span></label>
                            {{ form.invoice }}
                            {% if form.invoice.errors %}<div class="text-danger small">{{ form.invoice.errors }}</div>{% endif %}
                        </div>
                        {% endif %}

                        <div class="row mb-3">
                            <div class="col-md-6">
                                <label class="form-label fw-bold">Amount (XAF) <span class="text-danger">*</span></label>
                                {{ form.amount }}
                                {% if form.amount.errors %}<div class="text-danger small">{{ form.amount.errors }}</div>{% endif %}
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold">Payment Date <span class="text-danger">*</span></label>
                                {{ form.payment_date }}
                            </div>
                        </div>
                        <div class="row mb-3">
                            <div class="col-md-6">
                                <label class="form-label fw-bold">Payment Method <span class="text-danger">*</span></label>
                                {{ form.payment_method }}
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold">Status</label>
                                {{ form.status }}
                            </div>
                        </div>

                        <div id="mobilemoneyFields" class="row mb-3" style="display:none;">
                            <div class="col-md-6">
                                <label class="form-label fw-bold">Provider</label>
                                {{ form.mobile_money_provider }}
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold">Mobile Money Number</label>
                                {{ form.mobile_money_number }}
                            </div>
                        </div>
                        <div id="bankFields" class="row mb-3" style="display:none;">
                            <div class="col-md-6">
                                <label class="form-label fw-bold">Bank Name</label>
                                {{ form.bank_name }}
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold">Cheque Number</label>
                                {{ form.cheque_number }}
                            </div>
                        </div>

                        <div class="mb-3">
                            <label class="form-label fw-bold">Transaction ID / Reference</label>
                            {{ form.transaction_id }}
                        </div>
                        <div class="mb-3">
                            <label class="form-label fw-bold">Notes</label>
                            {{ form.notes }}
                        </div>

                        <div class="d-flex justify-content-end gap-2 mt-4">
                            <a href="{% url 'finance:payment_list' %}" class="btn btn-secondary">Cancel</a>
                            <button type="submit" class="btn btn-success"><i class="fas fa-save me-2"></i>{{ object|yesno:"Update Payment,Record Payment" }}</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{% block extra_js %}
<script>
document.querySelectorAll('input:not(.form-check-input):not([type="submit"]), select, textarea').forEach(el => {
    el.classList.add(el.tagName === 'SELECT' ? 'form-select' : 'form-control');
});
const methodSelect = document.querySelector('[name="payment_method"]');
function toggleFields() {
    const v = methodSelect ? methodSelect.value : '';
    document.getElementById('mobilemoneyFields').style.display = v === 'mobile_money' ? 'flex' : 'none';
    document.getElementById('bankFields').style.display = ['bank_transfer','cheque'].includes(v) ? 'flex' : 'none';
}
if (methodSelect) { methodSelect.addEventListener('change', toggleFields); toggleFields(); }
</script>
{% endblock %}
{% endblock %}
