{% 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 style="margin-top:15px;">
				<form method="post" name="filtre_choixFrs" id="filtre_choixFrs">
				
        			<div id="buttonRegroup" style="margin-bottom: 10px; margin-top: 10px; font-weight: bold; width:100%;position:relative;{% if post.countSelected??0 == 0 %}display:none;{% endif %}">
        				{{ 'Nombre de fournisseur(s) sélectionné(s)' | trad }} : <span id="countSelected">{{ post.countSelected??0 }}</span>			
                		<input type="hidden" id="input_countSelected" name="countSelected" value="{{ post.countSelected??0 }}"/>
                		<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="position:absolute;top:-5px;right:0px;margin-right:0px;" onClick="addFrs('{{ path('Consultation.AjoutFournisseur.ajoutFournisseur',{idAO:idAO}) }}');">{{ 'Ajouter le(s) fournisseur(s)' | trad }}</button>
        			</div>  
				
					<div id="divRefreshFrs" class="clearfix" >
                        <table class="table table-hover">
                          	<thead class="thead-dark">
                                <tr>
                                	<th></th>
                                	<th scope="col" width="">{{ triTable('TYPE_FG',  post.tri_TYPE_FG, 'Type' | trad, '', 'divRefreshFrs', 'filtre_choixFrs') }}</th>
                                    <th scope="col" width="">{{ triTable('CODE',  post.tri_CODE, 'Code' | trad, '', 'divRefreshFrs', 'filtre_choixFrs') }}</th>
                                    <th scope="col" width="">{{ triTable('RS',  post.tri_RS, 'Raison sociale' | trad, '', 'divRefreshFrs', 'filtre_choixFrs') }}</th>
                                    <th scope="col" width="">{{ triTable('NO_SIRET',  post.tri_NO_SIRET, 'SIRET' | trad, '', 'divRefreshFrs', 'filtre_choixFrs') }}</th>
                                    <th scope="col" width="">{{ triTable('CPOSTAL',  post.tri_CPOSTAL, 'Code postal' | trad, '', 'divRefreshFrs', 'filtre_choixFrs') }}</th>
                                    <th scope="col" width="">{{ triTable('VILLE1',  post.tri_VILLE1, 'Ville' | trad, '', 'divRefreshFrs', 'filtre_choixFrs') }}</th>
                                    <th scope="col" width="">{{ 'Sous-famille' | trad }}</th>
                                    <th scope="col" width="180px">{{'Actions' | trad }}</th>
                                </tr>
                            </thead>
                        	<tbody>
                            	<tr class="recherche" >
                            		<td></td>
                            		<td>{{ filterSelect('TYPE_FG', post.filter_TYPE_FG, listeType, 'filtre_choixFrs', '','divRefreshFrs', path('Consultation.AjoutFournisseur.listeFournisseur', {idAO:idAO}), '', '') }}</td>
                                    <td>{{ filterTable('CODE', post.filter_CODE, '', 'divRefreshFrs', '','filtre_choixFrs', '','Code' | trad,'',true) }}</td>
                                    <td>{{ filterTable('RS', post.filter_RS, '', 'divRefreshFrs', '','filtre_choixFrs', '','Raison sociale' | trad,'',true) }}</td>
                                    <td>{{ filterTable('NO_SIRET', post.filter_NO_SIRET, '', 'divRefreshFrs', '','filtre_choixFrs', '','SIRET' | trad,'',true) }}</td>
                                    <td>{{ filterTable('CPOSTAL', post.filter_CPOSTAL, '', 'divRefreshFrs', '','filtre_choixFrs', '','Code postal' | trad,'',true) }}</td>
                                    <td>{{ filterTable('VILLE1', post.filter_VILLE1, '', 'divRefreshFrs', '','filtre_choixFrs', '','Ville' | trad,'',true) }}</td>
                                    <td>{{ filterTable('listeSousFamille', post.filter_listeSousFamille, '', 'divRefreshFrs', '','filtre_choixFrs', '','Sous-famille' | trad,'',true) }}</td>
                                    <td></td>
                            	</tr>
                            </tbody>
                          	{% for frs in listeFrs %}
                          		<tr id="Frs{{ loop.index }}">
                          			<td><input type="checkbox" class="form-control checkbox selected_frs" data-id="{{ frs.TYPE_FG }}{{ spliter() }}{{ frs.CODE }}" id="input_selected_frs_{{ frs.TYPE_FG }}{{ spliter() }}{{ frs.CODE }}" name="selected_frs_{{ frs.TYPE_FG }}{{ spliter() }}{{ frs.CODE }}"/></td>
                          			<td>{{ listeType[frs.TYPE_FG] }}</td>
                          			<td>{{ frs.CODE }}</td>
                          			<td>{{ frs.RS }}</td>
                          			<td>{{ frs.NO_SIRET }}</td>
                          			<td>{{ frs.CPOSTAL }}</td>
                          			<td>{{ frs.VILLE1 }}</td>
                          			<td>{{ shortLibelle( frs.listeSousFamille , 50) }}</td>
                          			<td></td>
                          		</tr>
                      		{% endfor %}
                        </table>
                        {{ paginations(path('Consultation.AjoutFournisseur.listeFournisseur', {idAO:idAO}),infoPagination.nombreDePage, infoPagination.page, infoPagination.post, 'divRefreshFrs') }}
                        
                        <script>
                        	function addFrs(path)
                        	{	
                        		activeLoader();
                            	$.ajax({
                            		  	dataType : 'html',
                                		url: path,
                                		data: $('#filtre_choixFrs').serializeArray(),
                                		type: "POST",
                                		success: function(result){
 											redirectJSWithPost('{{ path('Consultation.Consultation.details',{idAO:idAO, onglet:'Fournisseur'}) }}');
                                			desactiveLoader();
                            		}
                            	});
                        	}
                        
                   			$( document ).ready(function() {
                       			$('#listInputSelected input').each(function( index ) {
                       				$('#input_'+$(this).attr('id').substring(5)).prop( "checked", true );
                                });
                            });
                            
                    		$('.selected_frs').click(function(){
                    			var input = $(this)[0];
                    			
                    			var oldCount =  parseInt($('#input_countSelected').val());
                    			
                    			var idFrs = $($(this)[0]).data('id');
                    			if($(input).is(':checked'))
                    			{
                    				var newCount = oldCount+1;
                    				$('#listInputSelected').append("<input type='hidden' name='save_selected_frs_"+idFrs+"' id='save_selected_frs_"+idFrs+"' value='1'/>");
                    			}
                    			else
                    			{
                    				var newCount = oldCount-1;
                    				$('#save_selected_frs_'+idFrs).remove();
                    			}
                    
                    			$('#countSelected').html(newCount);
                    			$('#input_countSelected').val(newCount);
                    			
                    			if(newCount >= 1)
                    			{
                    				$('#buttonRegroup').show();
                    			}
                    			else
                    			{
                    				$('#buttonRegroup').hide();
                    			}
                    		});
                        </script>
                        <script type="text/javascript" src="{{ baseUrl('js/parametre.js') }}" id="parametreJS"></script>
                    </div>
                </form>
        	</div>

    	</div>
		
	</div>
	
{% endblock %}