{% extends 'layout.twig' %}

{% block body %}

	<div class="contenu clearfix"  id="Consultation">
	
		<h1>
			{{ 'Consultations' | trad }}
		</h1>
		
		{{ ariane() }}
		
		<h2 id="divMenu" style="position:relative;">
			{{ consultation.OBJET_AO }} {{ 'créé le' | trad }} {{ consultation.DTE_AO | dateTrad }} {% if AOEventCreat.NOM %} {{ 'par' | trad }} {{ AOEventCreat.NOM }} {% endif %}
		</h2>
		
    	<div class="tab-content">
            	<div class="padding15">
            	
                {% include '@Consultation/Consultation/block_entete.twig' %}
    
                <div class="tab-content">
                    <form method="post" name="formImportDA" id="formImportDA" class="bg_form padding_form border_form clearfix">
                    
        			<div id="buttonRegroup" style="display:none; justify-content: space-between; align-items: center; margin-bottom: 1em;">
					<div>
						<span>
        					{{ 'Nombre de DA sélectionnée(s)' | trad }} : <span id="countSelected">{{ post.countSelected??0 }}</span>			
						</span>
                		<input type="hidden" id="input_countSelected" name="countSelected" value="{{ post.countSelected??0 }}"/>
					</div>
                		<div id='listInputSelected'>
                		
                			{% for key, val in listeSelectInput %}
                				<input type='hidden' name='{{ key }}' id='{{ key }}' value='{{ val }}' />
                			{% endfor %}
                		</div>
                		
                		<button type="button" class="button principal" style="margin-right: 0" onClick="addDA('{{ path('Consultation.Articles.ajaxAddArticle', {idAO:idAO}) }}');">{{ 'Importer' | trad }}</button>
        			</div>  
        			<div id="divRefreshDA" class="clearfix" >
                        <table class="table table-hover">
                          	<thead class="thead-dark">
                                <tr>
                                	<th></th>
                               		<th scope="col" width="100px;">{{ triTable('NO_DA',  post.tri_NO_DA, 'DA' | trad, '', 'divRefreshDA', 'formImportDA') }}</th>
                               		<th scope="col" width="">{{ triTable('DES_SOMM',  post.tri_DES_SOMM, 'Articles' | trad, '', 'divRefreshDA', 'formImportDA') }}</th>
                               		<th scope="col" width="">{{ triTable('DES_COMP',  post.tri_DES_SOMM, 'Désign. compl.' | trad, '', 'divRefreshDA', 'formImportDA') }}</th>
                               		<th scope="col" width="">{{ triTable('SSFAM',  post.tri_DES_SOMM, 'Sous famille' | trad, '', 'divRefreshDA', 'formImportDA') }}</th>
                               		<th scope="col" width="">{{ triTable('NO_DEM',  post.tri_NO_DEM, 'Demandeur' | trad, '', 'divRefreshDA', 'formImportDA') }}</th>
                               		<th scope="col" width="50px">{{ triTable('QTE_DEM',  post.tri_QTE_DEM, 'Quantité' | trad, '', 'divRefreshDA', 'formImportDA') }}</th>
                               		<th scope="col" width="50px">{{ triTable('UNITE',  post.tri_UNITE, 'Unité' | trad, '', 'divRefreshDA', 'formImportDA') }}</th>
                               		<th scope="col" width="70px">{{ triTable('PU',  post.tri_PU, 'PU' | trad, '', 'divRefreshDA', 'formImportDA') }}</th>
                               		<th scope="col" width="120px">{{ triTable('MNT_LIG',  post.tri_MNT_LIG, 'Mnt poste' | trad, '', 'divRefreshDA', 'formImportDA') }}</th>
                                </tr>
                            </thead>
                        	<tbody>
                            	<tr class="recherche" >
                            		<td></td>
                            		<td>{{ filterTable('NO_DA', post.filter_NO_DA, '', 'divRefreshDA', '','formImportDA', '','DA' | trad,'',true) }}</td>
                            		<td>{{ filterTable('DES_SOMM', post.filter_DES_SOMM, '', 'divRefreshDA', '','formImportDA', '','Articles' | trad,'',true) }}</td>
                            		<td>{{ filterTable('DES_COMP', post.filter_DES_COMP, '', 'divRefreshDA', '','formImportDA', '','Désign. compl.' | trad,'',true) }}</td>
                            		<td>{{ filterTable('SSFAM', post.filter_SSFAM, '', 'divRefreshDA', '','formImportDA', '','Sous famille' | trad,'',true) }}</td>
                            		<td>{{ filterTable('NO_DEM', post.filter_NO_DEM, '', 'divRefreshDA', '','formImportDA', '','Demandeur' | trad,'',true) }}</td>
                            		<td>{{ filterTable('egal_QTE_DEM', post.filter_egal_QTE_DEM, '', 'divRefreshDA', '','formImportDA', '','Quantité' | trad,'',true) }}</td>
                            		<td>{{ filterTable('UNITE', post.filter_UNITE, '', 'divRefreshDA', '','formImportDA', '','Unité' | trad,'',true) }}</td>
                            		<td>{{ filterTable('egal_PU', post.filter_egal_PU, '', 'divRefreshDA', '','formImportDA', '','PU' | trad,'',true) }}</td>
                            		<td>{{ filterTable('egal_MNT_LIG', post.filter_egal_MNT_LIG, '', 'divRefreshDA', '','formImportDA', '','Mnt poste' | trad,'',true) }}</td>
                            	</tr>
                            </tbody>
                          	{% for da in listeDA %}
                          		<tr id="da{{ loop.index }}">
                          			<td><input type="checkbox" class="form-control checkbox selected_da" data-id="{{ da.NO_DA }}_{{ da.NO_PO }}" id="input_selected_da_{{ da.NO_DA }}_{{ da.NO_PO }}" name="selected_da_{{ da.NO_DA }}_{{ da.NO_PO }}"/></td>
                          			<td>{{ da.NO_DA }} / {{ da.NO_PO }}</td>
                          			<td>{{ da.DES_SOMM }}</td>
                          			<td>{{ da.DES_COMP }}</td>
                          			<td>{{ da.DESIGNAT }} ({{ da.SSFAM }})</td>
                          			<td>{{ da.NOM_DEMANDEUR }} ({{ da.NO_DEM }})</td>
                          			<td class="montant">{{ da.QTE_DEM }}</td>
                          			<td class="devise">{{ da.UNITE }}</td>
                          			<td class="montant">{{ da.PU | prixUnitaire }}</td>
                          			<td class="montant">{{ da.MNT_LIG | prix }} {{ da.DEVISE }}</td>
                          		</tr>
                      		{% endfor %}
                        </table>
        				{{ paginations(path('Consultation.Articles.listeLigneDaImport', {idAO:idAO}),infoPagination.nombreDePage, infoPagination.page, infoPagination.post, 'divRefreshDA') }}
                        <script>
                        	function addDA(path)
                        	{	
                        		activeLoader();
                            	$.ajax({
                            		  	dataType : 'html',
                                		url: path,
                                		data: $('#formImportDA').serializeArray(),
                                		type: "POST",
                                		success: function(result){
 											redirectJSWithPost('{{ path('Consultation.Consultation.details',{idAO:idAO, onglet:'Ligne'}) }}');
                                			desactiveLoader();
                            		}
                            	});
                        	}
                        
                   			$( document ).ready(function() {
                       			$('#listInputSelected input').each(function( index ) {
                       				$('#input_'+$(this).attr('id').substring(5)).prop( "checked", true );
                                });
                            });
                            
                    		$('.selected_da').click(function(){
                    			var input = $(this)[0];
                    			
                    			var oldCount =  parseInt($('#input_countSelected').val());
                    			
                    			var id = $($(this)[0]).data('id');
                    			if($(input).is(':checked'))
                    			{
                    				var newCount = oldCount+1;
                    				$('#listInputSelected').append("<input type='hidden' name='save_selected_da_"+id+"' id='save_selected_da_"+id+"' value='1'/>");
                    			}
                    			else
                    			{
                    				var newCount = oldCount-1;
                    				$('#save_selected_da_'+id).remove();
                    			}
                    
                    			$('#countSelected').html(newCount);
                    			$('#input_countSelected').val(newCount);
                    			
                    			if(newCount >= 1)
                    			{
                    				$('#buttonRegroup').show();
                    				$('#buttonRegroup').css("display", "flex");
                    			}
                    			else
                    			{
                    				$('#buttonRegroup').hide();
                    			}
                    		});
                        </script>
                        <script type="text/javascript" src="{{ baseUrl('js/parametre.js') }}" id="parametreJS"></script>
        			</div>
                    
                    </form>
                </div>
            </div>
        </div>
    </div>
{% endblock %}