{% extends "base.html" %}
{% comment %}
SPDX-License-Identifier: GPL-3.0-or-later
{% endcomment %}
{% load i18n perms crispy_forms_tags %}
{% load render_table from django_tables2 %}
{% load static django_tables2 i18n %}

{% block content %}
<h1 class="text-white">{{ title }}</h1>
{% include "activity/includes/activity_info.html" %}

{% if activity.activity_type.manage_entries and ".change__opener"|has_perm:activity %}
    <div class="card bg-white mb-3">
        <h3 class="card-header text-center">
            {% trans "Openers" %}
        </h3>
        <div class="card-body">
            <form class="input-group" method="POST" id="form_opener">
                {% csrf_token %}
                <input type="hidden" name="activity" value="{{ object.pk }}">
                {%include "autocomplete_model.html" %}
                <div class="input-group-append">
                    <input type="submit" class="btn btn-success" value="{% trans "Add" %}">
                </div>
            </form>
        </div>
        {% render_table opener %}
    </div>
{% endif %}

{% if guests.data %}
<div class="card bg-white mb-3">
    <h3 class="card-header text-center">
        {% trans "Guests list" %}
    </h3>
    <div id="guests_table">
        {% render_table guests %}
    </div>
    {% if export %}
    <div class="card-footer text-center">
	<a href="{% url 'activity:guest_pdf' activity_pk=activity.pk %}" data-turbolinks="false">
            <button class="btn btn-block btn-danger"><i class="fa fa-file-pdf-o"></i> {% trans "Export to PDF" %}</button>
        </a>
    </div>
    <div class="card-body">
	<form action="{% url 'activity:guest_pdf' activity_pk=activity.pk %}" method="post">
	  {% csrf_token %}
	  {{ email_form|crispy }}
	  <button class="btn btn-primary" type="submit">{% trans "Share" %}</button>
	</form>
    </div>
    {% endif %}
</div>
{% endif %}
{% endblock %}

{% block extrajavascript %}
<script src="{% static "activity/js/opener.js" %}"></script>
<script src="{% static "js/autocomplete_model.js" %}"></script>
<script>
    function remove_guest(guest_id) {
        $.ajax({
         url:"/api/activity/guest/" + guest_id + "/",
         method:"DELETE",
         headers: {"X-CSRFTOKEN": CSRF_TOKEN}
     })
      .done(function() {
          addMsg('{% trans "Guest deleted" %}', 'success');
          $("#guests_table").load(location.pathname + " #guests_table");
      })
      .fail(function(xhr, textStatus, error) {
          errMsg(xhr.responseJSON);
      });
    }

    $("#open_activity").click(function() {
        $.ajax({
            url: "/api/activity/activity/{{ activity.pk }}/",
            type: "PATCH",
            dataType: "json",
            headers: {
                "X-CSRFTOKEN": CSRF_TOKEN
            },
            data: {
                open: {{ activity.open|yesno:'false,true' }}
            }
        }).done(function () {
            reloadWithTurbolinks();
        }).fail(function (xhr) {
            errMsg(xhr.responseJSON);
        });
    });

    $("#validate_activity").click(function () {
        $.ajax({
            url: "/api/activity/activity/{{ activity.pk }}/",
            type: "PATCH",
            dataType: "json",
            headers: {
                "X-CSRFTOKEN": CSRF_TOKEN
            },
            data: {
                valid: {{ activity.valid|yesno:'false,true' }}
            }
        }).done(function () {
            reloadWithTurbolinks();
        }).fail(function (xhr) {
            errMsg(xhr.responseJSON);
        });
    });
    $("#delete_activity").click(function () {
        if (!confirm("{% trans 'Are you sure you want to delete this activity?' %}")) {
            return;
        }

        $.ajax({
            url: "/api/activity/activity/{{ activity.pk }}/",
            type: "DELETE",
            headers: {
                "X-CSRFTOKEN": CSRF_TOKEN
            }
        }).done(function () {
            addMsg("{% trans 'Activity deleted' %}", "success");
            window.location.href = "/activity/";  // Redirige vers la liste des activités
        }).fail(function (xhr) {
            errMsg(xhr.responseJSON);
        });
    });
    {% if mail %}
    var mails = {{ mail|safe }};
    for (const mail of mails) {
      addMsg(gettext("An email has been sent to") + " " +  mail, "success");
    }
    {% endif %}
</script>
{% endblock %}
