{% extends "admin/base_site.html" %}
{% load static %}
{% load i18n %}

{% block extrahead %}
  {{ block.super }}
  <style>
    .custom-container {
      margin-top: 20px;
    }
  </style>
  <script>
    document.addEventListener("DOMContentLoaded", function(){
      const selectAllCheckbox = document.getElementById("select-all");
      if (selectAllCheckbox) {
        selectAllCheckbox.addEventListener("click", function(){
          const checkboxes = document.querySelectorAll("input[name='invoices']");
          checkboxes.forEach(chk => chk.checked = selectAllCheckbox.checked);
        });
      }
    });
  </script>
{% endblock extrahead %}

{% block content %}
<div class="container-fluid">
  <h1>Добавление участников лотереи: {{ lottery.name }}</h1>
  <p>{{ lottery.description }}</p>
  
  <!-- Форма фильтрации -->
  <form method="get" class="form-inline mb-3">
    <input type="hidden" name="lottery_id" value="{{ lottery.id }}">
    <div class="form-group mr-3">
      <label for="id_deposit_min" class="mr-2">Минимальный депозит:</label>
      <input type="number" step="0.01" name="deposit_min" id="id_deposit_min" class="form-control">
    </div>
    <div class="form-group mr-3">
      <label for="id_deposit_max" class="mr-2">Максимальный депозит:</label>
      <input type="number" step="0.01" name="deposit_max" id="id_deposit_max" class="form-control">
    </div>
    <button type="submit" class="btn btn-info">Фильтровать</button>
  </form>
  
  <!-- Форма добавления участников -->
  <form method="post">
    {% csrf_token %}
    <div class="table-responsive">
      <table class="table table-striped table-bordered table-sm">
        <thead>
          <tr>
            <th><input type="checkbox" id="select-all" /></th>
            <th>Счет</th>
            <th>Владелец счета</th>
            <th>Депозит</th>
          </tr>
        </thead>
        <tbody>
          {% for invoice in form.fields.invoices.queryset %}
            <tr>
              <td>
                <input type="checkbox" name="invoices" value="{{ invoice.id }}" />
              </td>
              <td>{{ invoice.ext_id }}</td>
              <td>
                {% if invoice.client_name %}
                  {{ invoice.client_name }}
                {% else %}
                  Не указан
                {% endif %}
              </td>
              <td>{{ invoice.deposit_sum }}</td>
            </tr>
          {% empty %}
            <tr>
              <td colspan="5" class="text-center">Нет доступных счетов</td>
            </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
    <button type="submit" class="btn btn-primary">Добавить выбранные счета</button>
  </form>
  
  <a href="{% url 'admin:draw_lotteryparticipant_changelist' %}" class="btn btn-secondary mt-3">Вернуться к списку участников</a>
</div>
{% endblock content %}
