{% extends 'buybackprogram/page.html' %}
{% load i18n %}
{% load humanize %}
{% load django_bootstrap5 %}
{% load static %}

{% block body %}
<div class="card">
  <div class="card-header">
    Featured
  </div>
  <div class="card-body">
    <form class="form" action="{% url 'buybackprogram:program_edit' program_pk=program.id %}" method="POST">
      {% csrf_token %}
      {% bootstrap_form form %}
      <button type="submit" class="btn btn-primary">{% trans "Edit Program" %}</button>
    </form>
  </div>
</div>
{% endblock %}

{% block extra_javascript %}
  <script type="text/javascript" src="{% static 'buybackprogram/js/bootstrap-autocomplete.min.js' %}"></script>
  <script type="text/javascript" src="{% static 'buybackprogram/js/autocomplete.js' %}"></script>
</div>
<script>
const walletUrl = "{% url 'buybackprogram:wallet_autocomplete' %}";
function loadWallets(ownerId, selectedId) {
    const walletSelect = document.getElementById('id_wallet');
    if (!walletSelect) return;
    walletSelect.innerHTML = '<option value="">---------</option>';
    if (!ownerId) return;
    fetch(walletUrl + '?owner_id=' + ownerId)
        .then(r => r.json())
        .then(data => {
            data.forEach(w => {
                const opt = document.createElement('option');
                opt.value = w.id;
                opt.textContent = w.name;
                if (selectedId && w.id == selectedId) opt.selected = true;
                walletSelect.appendChild(opt);
            });
        })
        .catch(err => console.error('Wallet fetch error:', err));
}
document.addEventListener('DOMContentLoaded', function () {
    const ownerSelect = document.getElementById('id_owner');
    if (!ownerSelect) return;
    const currentWallet = document.getElementById('id_wallet')?.value;
    ownerSelect.addEventListener('change', function () {
        loadWallets(this.value, null);
    });
    if (ownerSelect.value) {
        loadWallets(ownerSelect.value, currentWallet);
    }
});
</script>
{% endblock %}

{% block extra_css %}
{% include "buybackprogram/bundles/buybackprogram-css-light.html" %}
{% endblock %}

{% block extra_script %}
{% endblock %}
