{% extends 'layout.twig' %}

{% block body %}
{% if messageError == ''%}

<div class="text-center">
     <div class="contenu clearfix"  id="zedoc">
            <iframe name="I2" id="output_iframe_id"  onload="iframeLoaded();"
            src={{urlZedocLoginAccessToken}}?returnTo={{urlZedocURL}}%3F%26%26disableSendToStage%3Dfalse%26hideTaskInfo%3Dfalse%26disableChangeDefinition%3Dfalse%26loadInColor%3Dtrue%26returnTo%3DDeadEnd align="left" frameborder="0" ></iframe>
    </div> 
    <script>

    function iframeLoaded()
    {
        let iframeWindow = document.getElementById("output_iframe_id").contentWindow;
        
        fetch('{{urlZedocJWT}}', {
            method: 'POST',
            headers: {
                'Accept': 'application/x-www-form-urlencoded',
                'Content-Type': 'application/x-www-form-urlencoded',
                "Authorization" : "Bearer {{ticket}}",
                "Cookie" : "'FlexiCaptureTmpPrn'={{cookieTmp}}"
            },
            beforeSend: function(xhr){
                    xhr.setRequestHeader('Authorization', 'Bearer {{ticket}}');
                    xhr.setRequestHeader('Cookie', '{{sessionName}}={{sessionID}}');
                    },
            body: 'JwtToken={{ticket}}'
            }
        )
        .then(response => {
            let ticket = "{{ticket}}";
            iframeWindow.postMessage(
                    {
                        action: "accessTokenLogin", 
                        accessToken: ticket
                    }, "*");

        });
        
    }

    </script>
</div>
{% else %}
    <h1>{{messageError}}<h1>
{% endif %}
{% endblock %}