<html>
  <head>
    <script id="data" type="application/json">{{ data_json|safe }}</script>
    <script id="error" type="application/json">{{ error_json|safe }}</script>
    <script id="error_data" type="application/json">{{ error_data_json|safe }}</script>
  </head>
  <body>
    {% if test %}<button id="continue">Continue</button>
      <p>redirect_uri: <pre>{{ redirect_uri|pprint }}</pre></p>
      <p>correlation_id: <pre>{{ franceconnect.correlation_id }}</pre></p>
      {% if error %}
        <p>{{ error }}<p>
          {% if error.data %}
            <dl>
              {% for key, value in error.data %}
                <dt>{{ key }}</td>
                <dd><pre>{{ value|pprint }}</pre></dd>
              {% endfor %}
            </dl>
          {% endif %}
      {% endif %}
      <ul>
        {% for key, value in franceconnect.items reversed %}
          <li>{{ key }}&nbsp;: <pre>{{ value|pprint }}</pre></li>
        {% endfor %}
      </ul>
    {% endif %}
    <script>
      (function () {
        const data = JSON.parse(document.getElementById('data').textContent || '{}');
        const error = JSON.parse(document.getElementById('error').textContent || '{}');
        const error_data = JSON.parse(document.getElementById('error_data').textContent || '{}');
        const post_message = function () {
          {% if error %}window.opener.postMessage({"error": error, "data": error_data}, "{{ origin }}");
          {% else %}window.opener.postMessage(data, "{{ origin }}");{% endif %}
        }{% if test %}
          const continue_button = document.getElementById('continue');
          continue_button.addEventListener('click', function () { post_message(); });
        {% else %}
          post_message(){% endif %}
      })();
    </script>
  </body>
</html>

