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

{% block content %}
<div id="wrapped_tables">
{% if tables|length > 0 %}
<div class="card bg-light mb-3">
    <h3 class="card-header text-center">
        {% trans "My wrapped" %}
    </h3>
    {% render_table tables.1 %}
</div>
{% endif %}

{% if tables|length > 0 %}
<div class="card bg-light mb-3">
    <h3 class="card-header text-center">
        {% trans "Public wrapped" %}
    </h3>
    {% render_table tables.0 %}
</div>
{% endif %}
</div>
{% endblock %}

{% block extrajavascript %}
<script type="text/javascript">
	let club_not_public = {{ club_not_public }};
	if (club_not_public) { (addMsg("{% trans "Do not forget to ask permission to people who are in your wrapped before to make them public" %}", 'warning'));}
   function refreshTable() {
	$("#wrapped_tables").load(location.pathname + " #wrapped_tables");
   }

   function copylink(id) {
	   navigator.clipboard.writeText({{ request.get_full_path }} + id)
	    .then(() => { addMsg("{% trans "Link copied" %}", 'success', 1000);});
	}

   function makepublic(id, isprivate) {
	const makepublic_obj = $('#makepublic_'+id)

	if (makepublic_obj.data('pending'))
	// The button is already clicked
	{ return }
	
	makepublic_obj.html('<strong style="font-size: 16pt;">⟳</strong>')
	makepublic_obj.data('pending', true)
	
	$.ajax({
	    url: '/api/wrapped/wrapped/' + id + '/',
	    type: 'PATCH',
	    dataType: 'json',
	    headers: {
		'X-CSRFTOKEN': CSRF_TOKEN
	    },
	    data: {
		public: isprivate
	    },
	    success: function() {
		if(!isprivate)
		    addMsg("{% trans "Wrapped is private" %}", 'success', 2000)
		else addMsg("{% trans "Wrapped is public" %}", 'success', 2000)
		refreshTable()
	    },
	    error: function (err) {
		addMsg("{% trans "An error occured" %}", 'danger')
		refreshTable()
	    }
	})
    }
</script>
{% endblock %}
