{% if erreurValid %}
	<div class="text-danger">
		{{ erreurValid }}
	</div>
{% endif %}
<form action="{{ pathSignature }}" method="post" 
	name="form_signature" id="form_signature"
	onkeydown="handleEnterKey(event);"
	onsubmit="return ajaxSubmit(event);"
	data-onsuccess="successSignature"
>
	<input type="hidden" name="action" value="validate" />
	<input type="hidden" name="DELEG" id="DELEG" value="{{ post.DELEG }}" />
	<input type="hidden" name="listeMultiSignature" id="listeMultiSignature" value="{{ post.listeMultiSignature }}" />   

	{% if commentaireSignataire %}
		<details class="block_edition" open>
			<summary class="titre_block">
				{{ 'Afficher / Masquer les commentaires des signataires précédents' | trad }}
			</summary>
			<div class="relative">

			<table class="table table-hover tableV2 m-0">
				<thead class="thead-dark" style="display: none;">
					<th scope="col" colspan="2"></th>
				</thead>
				<tbody>
					{% for commentaire in commentaireSignataire %}
						<tr>
							<td>{{ commentaire.NOM }} ({{ commentaire.CODE }})</td>
							<td>{{ inputAdvance('TEXTAREA', {
								titre : '',
								name : 'comm',
								value : commentaire.CONTMEMO,
								option : 'disabled',
							}) }}</td>
						</tr>
					{% endfor %}  
				</tbody>
			</table>

			</div>
		</details>
	{% endif %}  
	
	{% block form_error %}<div class="text-danger" data-form-error></div>{% endblock %}
	<article class="clearfix">
		{% block pre_form %}{% endblock %}
		<div class="row">
			{{ inputTextarea(
				mode == 'R' ? 'Commentaire refus' | trad : 'Commentaire signature' | trad,
				'msg_signature', 
				post.msg_signature,
				path('Ajax.validator.isStringAvecEspace'),
				{
					sizeMin: ( config.OBLIG_COMMENT ? 1 : 0),
					sizeMax: 3000
				} | json, 
				"","","","",
				"rows='10'",
				(config.OBLIG_COMMENT ? true : false)
			) }}
		</div>
		{% if config.MDP_ACTIF %}
			<div class="row">
				{{ inputPassword('Mot de passe' | trad, 'MDP', post.MDP, '', '', "{}", '', '', '', '', true ) }}
			</div>
		{% endif %}
	</article>
	<article class="modal-footer">
		<button class="principal" type="submit">
			{{- mode == 'R' ? 'Refuser' | trad : 'Signer' | trad -}}
		</button>
        {% if mode == 'R' %}
            {{ arianeAnnuler() }}
        {% else %}
            <button class="secondaire" onclick="desactivePopup();">{{ 'Annuler' | trad }}</button>
        {% endif %}
	</article>
<form/>

<script>
	{% if asynchrone %}
		window.action.successSignature = (data, status, xhr) => {
			if(data.statut == 'enregEnCours') {
				asyncBackground(wsdlEnAsync, data, '{{ path('Outil.Asynchrone.getResultAsynchrone') }}');
			}
		};

		function wsdlEnAsync(requestData, dataRetourWsdl)
		{
			$.ajax({
				dataType : 'JSON',
				url: requestData.pathNextStep,
				data: {
					dataAsync: JSON.stringify(requestData),
					dataRetourWsdl: JSON.stringify(dataRetourWsdl),
				},
				type: 'POST', 
				success: function(data) {
					desactiveLoader();
					$("#modal-content").addClass(data.class);
					$("#modal-body").html(data.bodyHtml);
					$("#modal-title").html(data.title);
					activePopup();
				}, 
				error: function (data){
					
				} 
			}); 
		}

		function handleEnterKey(event)
		{
			if (event.key === 'Enter' || event.keyCode === 13) {
				event.preventDefault(); 
				const pathSignature = document.getElementById('form_signature').getAttribute('action');
				signature(pathSignature);
			}
		}
	
	{% else %}
		window.action.successSignature = (result, status, xhr) => {
			desactiveLoader();
			$("#modal-body").html(result);

			{% if module != 'Fournisseur' %}
			// TODO : improve
			refrechDiv(window.location.pathname, 'suivi_demandes', 'filtre_suivi');

			refrechDiv(window.location.pathname, 'menu-lateral', null, 1);
			{% endif %}
		};
	 {% endif %}
</script>