{% extends 'admin/kmuhelper/change_form.html' %}

{% block extrahead %}
    {{ block.super }}

    <script>
        function openURL(url, doPrint) {
            var $ = django.jQuery;

            // Check if the form has been changed
            var formChanged = $('form').serialize() != window.initialData;
            if (formChanged) {
                if (!confirm('Achtung: Es liegen ungespeicherte Änderungen vor. Die PDF-Datei wird mit den aktuell gespeicherten Daten generiert. Trotzdem fortfahren?')) {
                    return false;
                }
            }

            // Open the URL in a new window
            var win = window.open(url, '_blank');

            // (Try to) automatically print the document
            if (doPrint) {
                setTimeout(function () {
                    win.print();
                }, 1000);
            }

            // Prevent the default action
            return false;
        }
    </script>

{% endblock extrahead %}

{% block object-tools-items %}
    {% if change %}
        <li class="dropdown">
            <a href="#">PDF Generieren</a>
            <div>
                <b>PDF generieren</b>
                <hr>
                <i>Rechnung</i>
                <a href="{% url 'admin:kmuhelper_order_pdf' object_id %}?preset=invoice" target="_blank"
                   onclick="return openURL(this.href, false);" rel="noopener">Digital</a>
                <a href="{% url 'admin:kmuhelper_order_pdf' object_id %}?preset=invoice&print" target="_blank"
                   onclick="return openURL(this.href, true);" rel="noopener">Druck</a>
                <hr>
                <i>Zahlungserinnerung</i>
                <a href="{% url 'admin:kmuhelper_order_pdf' object_id %}?preset=payment-reminder" target="_blank"
                   onclick="return openURL(this.href, false);" rel="noopener">Digital</a>
                <a href="{% url 'admin:kmuhelper_order_pdf' object_id %}?preset=payment-reminder&print" target="_blank"
                   onclick="return openURL(this.href, true);" rel="noopener">Druck</a>
                <hr>
                <i>Mehr</i>
                <a href="{% url 'admin:kmuhelper_order_pdf' object_id %}?preset=delivery-note" target="_blank"
                   rel="noopener">Lieferschein</a>
                <a href="{% url 'admin:kmuhelper_order_pdf_form' object_id %}">Benutzerdefiniert</a>
            </div>
        </li>

        {% if perms.kmuhelper.view_email or perms.kmuhelper.add_email %}
            {% load kmuhelper_tags %}
            {% kmuhelper_email_show_buttons as show_email_buttons %}
            {% if show_email_buttons %}

                <li class="dropdown">
                    <a href="#">E-Mails</a>
                    <div>
                        <b>E-Mails</b>
                        <hr>

                        {% if perms.kmuhelper.view_emailtemplate and perms.kmuhelper.add_email %}
                            <abbr title="Speichere die Daten dieser Bestellung im Zwischenspeicher und gehe zu E-Mail-Vorlagen">
                                <a href="{% url 'admin:kmuhelper_emailtemplate_savevars' %}?{{ original.to_query_string }}">Zu
                                    E-Mail-Vorlage</a>
                            </abbr>
                            <hr>
                        {% endif %}
                        {% if original.addr_billing_email %}
                            {% if original.email_link_invoice and perms.kmuhelper.view_email %}
                                <a href="{% url 'admin:kmuhelper_email_change' original.email_link_invoice.pk %}">Rechnungsemail
                                    ansehen</a>
                            {% endif %}
                            {% if perms.kmuhelper.add_email and perms.kmuhelper.change_order %}
                                <a href="{% url 'admin:kmuhelper_order_email_invoice' object_id %}">Rechnungsemail
                                    generieren</a>
                            {% endif %}
                        {% else %}
                            <a href="#" class="disabled">Rechnungsemail</a>
                        {% endif %}
                        <hr>
                        {% if original.addr_shipping_email and original.is_shipped %}
                            {% if original.email_link_shipped and perms.kmuhelper.view_email %}
                                <a href="{% url 'admin:kmuhelper_email_change' original.email_link_shipped.pk %}">Lieferungsemail
                                    ansehen</a>
                            {% endif %}
                            {% if perms.kmuhelper.add_email and perms.kmuhelper.change_order %}
                                <a href="{% url 'admin:kmuhelper_order_email_shipped' object_id %}">Lieferungsemail
                                    generieren</a>
                            {% endif %}
                        {% else %}
                            <a href="#" class="disabled">Lieferungsemail</a>
                        {% endif %}
                    </div>
                </li>

            {% endif %}
        {% endif %}

        {% if perms.kmuhelper.change_order and original.woocommerceid %}
            <li>
                <abbr title="Achtung: Hier abgeänderte Werte werden überschrieben, sofern diese in WooCommerce gespeichert werden können!">
                    <a href="{% url 'kmuhelper:wc-update-order' object_id %}">Von WooCommerce importieren und
                        ersetzen</a>
                </abbr>
            </li>
        {% endif %}

        {% if perms.kmuhelper.add_supply and original.is_shipped %}
            <li>
                <attr title="Die Produkte dieser Bestellung zu einer Lieferung kopieren">
                    <a href="{% url 'admin:kmuhelper_order_copy_to_supply' object_id %}" rel="noopener">Rücksendung</a>
                </attr>
            </li>
        {% endif %}

        {% if original.tracking_number %}
            <li><a target="_blank" href="{{ original.tracking_link }}" rel="noopener">Tracking</a></li>
        {% endif %}

        {% if has_change_permission and not original.is_shipped %}
            <li><a href="#id_addr_shipping_first_name" onclick="copyform();">Rechnungsadresse -&gt; Lieferadresse</a>
            </li>
            <script>
                function copyform() {
                    document.getElementById("id_addr_shipping_first_name").value = document.getElementById("id_addr_billing_first_name").value;
                    document.getElementById("id_addr_shipping_last_name").value = document.getElementById("id_addr_billing_last_name").value;
                    document.getElementById("id_addr_shipping_company").value = document.getElementById("id_addr_billing_company").value;
                    document.getElementById("id_addr_shipping_address_1").value = document.getElementById("id_addr_billing_address_1").value;
                    document.getElementById("id_addr_shipping_address_2").value = document.getElementById("id_addr_billing_address_2").value;
                    document.getElementById("id_addr_shipping_city").value = document.getElementById("id_addr_billing_city").value;
                    document.getElementById("id_addr_shipping_state").value = document.getElementById("id_addr_billing_state").value;
                    document.getElementById("id_addr_shipping_postcode").value = document.getElementById("id_addr_billing_postcode").value;
                    document.getElementById("id_addr_shipping_country").value = document.getElementById("id_addr_billing_country").value;
                    document.getElementById("id_addr_shipping_email").value = document.getElementById("id_addr_billing_email").value;
                    document.getElementById("id_addr_shipping_phone").value = document.getElementById("id_addr_billing_phone").value;
                }
            </script>
        {% endif %}
    {% endif %}
    {{ block.super }}
{% endblock %}
