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

{% block body %}
<script>
    $("#send").click(function(e) {
		activeLoader();
    	jForm.append("validationForm", $('#validationForm').val());
    	jForm.append("NO_ORDRE", $('#NO_ORDRE').val());
    	jForm.append("typeAjout", $('#typeAjout').val());
    	jForm.append("TYPE_PJ", $('#input_TYPE_PJ').val());
    	jForm.append("TITRE", $('#input_TITRE').val());
    	if($('#LECTURE').length>0){jForm.append("LECTURE", $('#LECTURE').is(':checked'));}
    	if($('#IMP_PJ').length>0){jForm.append("IMP_PJ", $('#IMP_PJ').is(':checked'));}
    	if($('#IMP_TITRE').length>0){jForm.append("IMP_TITRE", $('#IMP_TITRE').is(':checked'));}
    	
        $.ajax({
        	url : '{{ path('Facture.popup.ajoutDocRattPJCreation') }}',
            type: 'POST',
    	  	dataType : 'JSON',
            data: jForm,
            mimeType: "multipart/form-data",
            contentType: false,
            cache: false,
            processData: false,
            success: function (data) {
				desactiveLoader();
				if(data.statut == 'O') {
					{% if post.pageParent=='creationFacture' %}
						{% if post.typeAjout=='DC' %}
							divParent = $('#rattacher_documents');
							$('#nbDocRatt').val(parseInt(data.NO_ORDRE)+parseInt(1));
						{% else %}
							divParent = $('#pieces_jointes');
							$('#nbPJ').val(parseInt(data.NO_ORDRE)+parseInt(1));
						{% endif %}
						divParent.append(
							'<div class="row " id="'+data.typeAjout+'_'+data.NO_ORDRE+'" style="display: inline-block;width: 100%;">'+
								'<span>'+data.NOM_ORI+'</span>'+
								'<input type="hidden" name="'+data.typeAjout+'-NO_ORDRE-'+data.NO_ORDRE+'" id="'+data.typeAjout+'-NO_ORDRE-'+data.NO_ORDRE+'" value="'+data.NO_ORDRE+'" />'+
								'<input type="hidden" name="'+data.typeAjout+'-TYPE_PJ-'+data.NO_ORDRE+'" id="'+data.typeAjout+'-TYPE_PJ-'+data.NO_ORDRE+'" value="'+data.TYPE_PJ+'" />'+
								'<input type="hidden" name="'+data.typeAjout+'-TITRE-'+data.NO_ORDRE+'" id="'+data.typeAjout+'-TITRE-'+data.NO_ORDRE+'" value="'+data.TITRE+'" />'+
								'<input type="hidden" name="'+data.typeAjout+'-LECTURE-'+data.NO_ORDRE+'" id="'+data.typeAjout+'-LECTURE-'+data.NO_ORDRE+'" value="'+data.LECTURE+'" />'+
								'<input type="hidden" name="'+data.typeAjout+'-IMP_PJ-'+data.NO_ORDRE+'" id="'+data.typeAjout+'-IMP_PJ-'+data.NO_ORDRE+'" value="'+data.IMP_PJ+'" />'+
								'<input type="hidden" name="'+data.typeAjout+'-IMP_TITRE-'+data.NO_ORDRE+'" id="'+data.typeAjout+'-IMP_TITRE-'+data.NO_ORDRE+'" value="'+data.IMP_TITRE+'" />'+
								'<input type="hidden" name="'+data.typeAjout+'-NOM_ORI-'+data.NO_ORDRE+'" id="'+data.typeAjout+'-NOM_ORI-'+data.NO_ORDRE+'" value="'+data.NOM_ORI+'" />'+
								'<input type="hidden" name="'+data.typeAjout+'-NOM_FIC-'+data.NO_ORDRE+'" id="'+data.typeAjout+'-NOM_FIC-'+data.NO_ORDRE+'" value="'+data.NOM_FIC+'" />'+
								'<button type="button" style="border: 0px;height: 15px;width: 15px;" class="ajouter bloquant" onclick="supprDocRattPJ(\''+data.typeAjout+'\', \''+data.NO_ORDRE+'\')"></button>'+
							'</div>');
						
					{% endif %}
					desactivePopup ();
				} else {
					$('#message_erreur_popup').html(data.message);
					$('#message_erreur_popup').show();
				}
            }
        });
    });
</script>
<div id="message_erreur_popup" class="error displayNone clearfix" ></div>
<div class="modal-body">
    <form method="post" enctype="multipart/form-data" name="form_ajoutDocRattPJ" id="form_ajoutDocRattPJ" class="box-form">
    	<input type="hidden" name="validationForm" id="validationForm" value="1" />
    	<input type="hidden" name="NO_ORDRE" id="NO_ORDRE" value="{{ post.numIndice }}" />
    	<input type="hidden" name="typeAjout" id="typeAjout" value="{{ post.typeAjout }}" />
    	<div class="row">
			{{ inputSelect(('Type :' | trad), 'TYPE_PJ', tableau_pj, 'PJ') }}
        </div>
    	<div class="row">
			{{ inputAdvance('TEXT', {
				'name' : 'TITRE',
				'titre' : 'Titre :' | trad,
				'pathValidator'     : path('Ajax.validator.isStringAvecEspace'),
				'paramsValidator'   : "{'nomChamp': '"~ 'Titre' | trad ~"', 'sizeMin' : '1', 'sizeMax' : '60'}",
				'obligatoire' 		: true
			}) }}
    	</div>
    	{% if post.typeAjout=='PJ' %}
    	<div class="row">
    		<label>{{ 'Lecture :' | trad }}</label>
    		<label class="switch">
            	<input type="checkbox" name="LECTURE" id="LECTURE" >
                <span class="slider round"></span>
                <span class="value_switch">{{ 'Non' | trad }}</span>                                               
            </label>
		</div>
    	<div class="row">
    		<label>{{ 'Ajouter la PJ à l\'impression :' | trad }}</label>
    		<label class="switch">
            	<input type="checkbox" name="IMP_PJ" id="IMP_PJ" >
                <span class="slider round"></span>
                <span class="value_switch">{{ 'Non' | trad }}</span>                                               
            </label>
		</div>
    	<div class="row">
    		<label>{{ 'Ajouter le titre du document à l\'impression :' | trad }}</label>
    		<label class="switch">
            	<input type="checkbox" name="IMP_TITRE" id="IMP_TITRE" onChange ="disableFieldByCheckbox()"/>
                <span class="slider round"></span>
				<span class="value_switch">{{ 'Non' | trad }}</span>                                               
            </label>
		</div>
		{% endif %}
	    <div class="row">
			{# <label>{{ 'Fichier :' | trad }}</label>
			<input type="file" id="file_name" name="file_name" /> #}
			{{ inputAdvance('FILE', {'name' : 'fileToUpload' }) }}
		</div>
        <div class="modal-footer">
            <button type="button" class="button secondaire" data-dismiss="modal" onClick="desactivePopup();">{{ 'Annuler' | trad}}</button>
            <button type="button" class="button principal" id="send">{{ 'Enregistrer' | trad}}</button>
        </div>
	</form>
</div>
{% endblock %}
