{% extends "admin/change_form.html" %}
{% load admin_urls i18n %}

{% block extrahead %}{{ block.super }}
{% if add and not original.client %}
{% if not original.contrat or original.contrat.client %}
<script type="text/javascript">
window.addEventListener("load", function(){
	let client_selector = document.getElementById("id_client");
	let client_selector_start_value = client_selector.value;
	client_selector.onchange = function() {
		if (client_selector.value && client_selector.value != client_selector_start_value) {
			let new_url = window.location.origin + "{% url "admin:eo_facture_facture_add" %}?client=" + client_selector.value;
			if (new_url != window.location) {
				window.location = new_url;
			}
		}
	};
});
</script>
{% endif %}
{% endif %}
{% endblock %}

{% block object-tools %}
{% if change %}{% if not is_popup %}
<ul class="object-tools">
	{% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}
	<li><a href="{{ history_url }}" class="historylink">{% trans "History" %}</a></li>
    {% if original.client %}
	<li><a href="{% url "admin:eo_facture_client_change" original.client.id %}" class="historylink">Client</a></li>
    {% endif %}
	{% if original.contrat %}
	<li><a href="{% url "admin:eo_facture_contrat_change" original.contrat.id %}" class="historylink">Contrat</a></li>
	{% endif %}
	<li><a href="{{history_url}}../view_pdf/{{ original.filename }}">Imprimer</a></li>
	{% if original.client.chorus_structure and not original.proforma %}
	<li><a href="{% url "admin:eo_facture_facture_send_to_chorus" original.id %}">Envoyer à Chorus</a></li>
	{% endif %}
	{% if not original.annulation and original.factures_avoir.count == 0 %}
	<li><a href="{% url "admin:eo_facture_facture_cancel" original.id %}">Annuler</a></li>
	{% endif %}
</ul>
{% endif %}{% endif %}
{% endblock %}

