{% extends 'oscar/checkout/thank_you.html' %}
{% load l10n ocyan_main %}

{% block cookiejar_ecommerce %}
    {% block google_merchant %}
    {% if cookie_jar.marketing.is_allowed and cookie_jar.settings.google_merchant_id %}
        <script src="https://apis.google.com/js/platform.js?onload=renderOptIn" async defer></script>
        
        <script>
            window.renderOptIn = function() {
                window.gapi.load('surveyoptin', function() {
                window.gapi.surveyoptin.render(
                    {
                        // REQUIRED FIELDS
                        "merchant_id": {{cookie_jar.settings.google_merchant_id}},
                        "order_id": "{{order.number}}",
                        "email": "{{order.email}}",
                        "delivery_country": "{{order.shipping_address.country.pk}}",
                        "estimated_delivery_date": "{{ order.date_placed|date:'Y-m-d' }}",
                    
                        // OPTIONAL FIELDS
                        "products": [{% for l in order.all_lines %}{"gtin":"{{l.product.upc}}"},{% endfor %}]
                    });
                });
            }
        </script>
    {% endif %}
    {% endblock google_merchant %}

    {% block google_analytics %}
    {% if cookie_jar.settings.google_analytics_4 or cookie_jar.settings.google_analytics %}
      {% if send_analytics_event and cookie_jar.analytical.is_allowed %}
          <script nonce="{% nonce %}">
            $( document ).ready(function() {
              gtag('event', 'purchase', {
                  "transaction_id": '{{ order.number|escapejs }}',
                  "value": '{{ order.total_incl_tax|unlocalize|escapejs }}',
                  "currency": '{{ order.currency|escapejs }}',
                  "tax": '{{ order.total_tax|unlocalize|escapejs }}',
                  "shipping": '{{ order.shipping_incl_tax|unlocalize|escapejs }}',
                  items: [
                      {% for line in order.lines.all %}
                          {
                              'item_id': '{{ line.partner_sku|escapejs }}',
                              'item_name': '{{ line.title|escapejs }}',
                              "affiliation": '{{ shop_name|escapejs }}',
                              'item_category': '{{ line.category|default:"Uncategorised"|escapejs }}',
                              'price': '{{ line.unit_price_incl_tax|unlocalize|escapejs }}',
                              'quantity': '{{ line.quantity|escapejs }}'
                          }{% if not forloop.last %},{% endif %}
                      {% endfor %}
                  ]
              });
            }
            );
          </script>
      {% endif %}
    {% endif %}
    {% endblock %}
    
    {% block google_tag_manager %}

      {% if send_analytics_event and cookie_jar.settings.google_ads and cookie_jar.settings.conversielabel and cookie_jar.tagmanager.is_allowed %}
          <script nonce="{% nonce %}">
              window.dataLayer = window.dataLayer || [];
              function gtag(){dataLayer.push(arguments);}
              gtag('event', 'conversion', {
                  'send_to': '{{cookie_jar.settings.google_ads}}/{{cookie_jar.settings.conversielabel}}',
                  'value': {{ order.total_incl_tax|unlocalize|escapejs }},
                  'currency': '{{ order.currency|escapejs }}',
                  'transaction_id': '{{ order.number|escapejs }}'
              });
          </script>
      {% endif %}

      {% if send_analytics_event and cookie_jar.settings.google_tag_manager and cookie_jar.tagmanager.is_allowed %}
          <script nonce="{% nonce %}">
              window.dataLayer = window.dataLayer || [];
              dataLayer.push({
                 'event': 'purchase',
                 'transactionId': '{{order.number}}',
                 'transactionAffiliation': '{{order.site.name}}',
                 'transactionTotal': {{order.total_incl_tax|unlocalize}},
                 'transactionTax': {{order.total_tax|unlocalize}},
                 'transactionShipping': {{order.shipping_incl_tax|unlocalize}},
                 'transactionCurrency': '{{ order.currency|escapejs }}',
                 'transactionProducts': [
                     {% for line in order.all_lines %}
                     {
                         'sku': '{{line.product.upc}}',
                         'name': '{{line.product.get_title}}',
                         'category': '{{line.product.category.first}}',
                         'price': {{line.unit_price_incl_tax|unlocalize}},
                         'quantity': {{line.quantity|unlocalize}}
                 },{% endfor %}]
              });
          </script>
      {% endif %}
    {% endblock %}
    
    
    {% block bing %}
      {% if send_analytics_event and cookie_jar.settings.bing_uet and cookie_jar.analytical.is_allowed %}
       <script nonce="{% nonce %}">
          window.uetq = window.uetq || [];
          window.uetq.push('event', 'purchase', {
             "revenue_value": {{order.total_incl_tax|unlocalize|escapejs}},
             "currency": "{{order.currency|escapejs}}"
          });
       </script>
      {% endif %}
    {% endblock %}

    {% block meta_pixel %}
        {% if send_analytics_event and cookie_jar.settings.facebook_pixel and cookie_jar.facebook.is_allowed %}
            <script>
                fbq('track', 'AddPaymentInfo', {
                        value: {{order.total_incl_tax|unlocalize|escapejs}},
                        currency: "{{ order.currency }}",
                        payment_method: "{{order.payment_source_type.name|escapejs}}",
                        content_ids: [{% for line in order.lines.all %}
                            "{{line.product.upc}}",
                        {% endfor %}],
                        content: [{% for line in order.lines.all %}
                            "{{line.product.title}}",
                        {% endfor %}],
                    }
                );
                fbq('track', 'Purchase', {
                        value: {{order.total_incl_tax|unlocalize|escapejs}},
                        currency: "{{ order.currency }}",
                        content_ids: [{% for line in order.lines.all %}
                            "{{line.product.upc}}",
                        {% endfor %}],
                        content: [{% for line in order.lines.all %}
                            "{{line.product.title}}",
                        {% endfor %}],
                    }
                );
            </script>
        {% endif %}
    {% endblock meta_pixel %}
{% endblock %}

{% block extra_footer_content %}
{{ block.super }}
    {% if cookie_jar.settings.trusted %}
    <div id="trustedShopsCheckout" style="display: none;">
        <span id="tsCheckoutOrderNr">{{ order.number }}</span>
        <span id="tsCheckoutBuyerEmail">{{ order.email }}</span>
        <span id="tsCheckoutOrderAmount">{{ order.total_incl_tax }}</span>
        <span id="tsCheckoutOrderCurrency">{{ order.currency }}</span>
        <span id="tsCheckoutOrderPaymentType">{{ order.payment_source_type.name }}</span>
        <span id="tsCheckoutOrderEstDeliveryDate">{{ order.delivery_date|date:'Y-m-d' }}</span>
        {% for line in order.lines.all %}<span class="tsCheckoutProductItem">
          {% with product_url=line.product.get_absolute_url %}<span class="tsCheckoutProductUrl">{% absolute_url product_url %}</span>{% endwith %}
          <span class="tsCheckoutProductName">{{ line.product.title }}</span>
          <span class="tsCheckoutProductSKU">{{ line.product.upc }}</span>
        </span>
        {% endfor %}
    </div>
    {% endif %}
{% endblock %}