{% 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 custom-container">
  <h1>Добавление участников лотереи: {{ lottery.name }}</h1>
  <p>{{ lottery.description }}</p>

  <!-- Форма фильтрации -->
  <form method="get" class="mb-4">
    <input type="hidden" name="lottery_id" value="{{ lottery.id }}">
    <div class="row">
      <div class="col-md-3">
        <label>Минимальная сумма депозита:</label>
        <input type="number" step="0.01" name="deposit_min" value="{{ request.GET.deposit_min }}" class="form-control">
      </div>
      <div class="col-md-3">
        <label>Максимальная сумма депозита:</label>
        <input type="number" step="0.01" name="deposit_max" value="{{ request.GET.deposit_max }}" class="form-control">
      </div>
      <div class="col-md-3">
        <label>Дата создания от:</label>
        <input type="date" name="created_after" value="{{ request.GET.created_after }}" class="form-control">
      </div>
      <div class="col-md-3">
        <label>Дата создания до:</label>
        <input type="date" name="created_before" value="{{ request.GET.created_before }}" class="form-control">
      </div>
    </div>
    <div class="mt-3">
      <button type="submit" class="btn btn-primary">Применить фильтр</button>
    </div>
  </form>

  <p><strong>Найдено подходящих счетов: {{ invoice_count }}</strong></p>

<!-- Форма добавления участников -->
<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>
          <th>Клиент</th>
          <th>Номер клиента</th>
          <th>Сумма счета</th>
          <th>Бонус</th>
          <th>ФД</th>
          <th>Депозит</th>
          <th>Примечание</th>
        </tr>
      </thead>
      <tbody>
        {% for invoice in form.fields.invoices.queryset %}
          <tr>
            <input type="checkbox" name="invoices" value="{{ invoice.id }}">
            <td>{{ invoice.created_at|date:"d.m.Y H:i" }}</td>
            <td>
              {% if invoice.closed_at %}
                {{ invoice.closed_at|date:"d.m.Y H:i" }}
              {% else %}
                —
              {% endif %}
            </td>
            <td>{{ invoice.ext_id|default:"—" }}</td>
            <td>{{ invoice.client.name|default:"Не указан" }}</td>
            <td>{{ invoice.client.club_card_number|default:"—" }}</td>
            <td>{{ invoice.sum|default:"—" }}</td>
            <td>{{ invoice.bonus|default:"—" }}</td>
            <td>{{ invoice.start_bonus|default:"—" }}</td>
            <td>{{ invoice.deposit_sum|default:"—" }}</td>
            <td>{{ invoice.notes|default:"—" }}</td>
          </tr>
        {% empty %}
          <tr>
            <td colspan="11" 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 %}
