{% extends '@ajax-layout/layout.twig' %}

{% block body %}
<form
	method="POST"
	action="{{ path('Consultation.ImportExcel.import',{idAO: idAO}) }}"  
	onsubmit="return ajaxSubmit(event);"
	data-onsuccess="consultImportExcelSuccess"
	data-onerror="consultImportExcelError"
>
	<div class="modal-body clearfix display_table" style="width:100%;">

		<div id="message_info" class="zoneError" style="margin-bottom:15px;"></div>
		<div class="single row">
			{{ inputAdvance('FILE',{name: 'import'}) }}
		</div>
	</div>
	<div class="modal-footer">
		<a href="javascript:void(0)" onclick="desactivePopup();" class="button secondaire">
			{{ 'Annuler' | trad }}
		</a>
		<button type="submit" class="button principal">
			{{ 'Importer' | trad }}
		</button>
	</div>
</form>
	
<script>
window.action.consultImportExcelSuccess = function (data) {
    
	activePopupInfo(
		{{ 'Informations' | trad | json }},
		{{ 'Les réponses du fournisseur ont bien été importées.' | trad | json }},
		'modal_small'
	);
    if (document.body.getAttribute('data-route') == 'Consultation.Consultation.details') {
        window.action.arianeReload();
    }
    desactiveLoader();
};

window.action.consultImportExcelError = function (xhr, form) {
    
    desactiveLoader();
	
    if (xhr.getResponseHeader('Content-Type') == 'application/json') {
		
        let response = JSON.parse(xhr.responseText);


        if (response.type == 'confirm_bddart99') {
            activePopup(
                {{ 'Erreur validation' | trad | json }},
                response.popupHtml,
                'modal_small'
            );
            document.querySelector('[data-popupwarning-ignore]')
                .addEventListener('click', function () {
                    // Resend form with confirm & ignore
                    var formData = new FormData();
                    formData.append('confirm', '1');
                    formData.append('bddart99_ignore', '1');
                    ajaxSubmit(form, formData);
                    desactivePopup();
                });
            document.querySelector('[data-popupwarning-update]')
                .addEventListener('click', function () {
                    // Resend form with confirm & ignore
                    var formData = new FormData();
                    formData.append('confirm', '1');
                    formData.append('bddart99_ignore', '0');
                    ajaxSubmit(form, formData);
                    desactivePopup();
                });
            return;
        }

        // Failsafe : only handle response 'confirm' for json content type
        if (response.type != 'confirm') {
            throw '!!!';
        }
        activePopup(
            {{ 'Erreur validation' | trad | json }},
            response.popupHtml,
            'modal_small'
        );
        document.querySelector('[data-popupwarning-continue]')
            .addEventListener('click', function () {
                var formData = new FormData();
		        formData.append('confirm', '1');

                // Resend form with confirm
                ajaxSubmit(form, formData);
                desactivePopup();
            });
        return;
    }

    
    // Assume http 400 -> show constraint validation error
    activePopupError(
        {{ 'Erreur validation' | trad | json  }},
        xhr.responseText,
        'modal_small'
    );
};
</script>
{% endblock %}