{% extends 'menu_left.html.twig' %}

{% block title %} Participations financières {% endblock %}

{% block main %}


    <div class="container-fluid">

        <div id="particip-financ-intro" class="row plr-16 ptb-24">
            <!-- INTRO-->
            <div class="col-xs-12 col-sm-8 p-0">
                <div class="bloc-base p-16">
                    {% if not entete is null and not entete.texte==''%}
                        <div class="col-xs-12 p-0">
                            <div class="bloc-base p-16">
                                {{ entete.texte|raw }}
                            </div>
                        </div>
                    {% else %}
                        <p class="mb-16">Ci-après l’ensemble des participations financières actives attribuées en votre faveur.</p>
                        {#<p class="mb-16">Pour la bonne exécution de vos dossiers, veillez à utiliser les documents type suivants :</p>#}
                    {% endif %}

                </div>
            </div>
            <div class="col-xs-12 col-sm-4 p-0">
                <div class="bloc-base p-16 text-align-r">
                    <p class="mb-16">Consulter les écheanciers<a href="{{ path('global_echeances') }}" class="button-rond-xs align-middle ml-8"><i class="fas fa-arrow-right"></i></a></p>

                   {% if interlocuteurs|length != 0 %}
                    {% set existPCE = false %}
                    <div class="dropdown">
                        <a href="#" class="button-square" id="dropdown-action-02" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">télécharger pce <i class="fas fa-angle-down font-size-16 align-middle ml-8"></i></a>
                        <ul id="button-dropdown-menu-02" class="dropdown-menu m-0 p-0" aria-labelledby="dropdown-action-02">
                            {% for inter in interlocuteurs %}
                                {% if inter.ppcCodePpc.codePpc != 0 %}
                                    <li>
                                        <a class="p-16" href="{{ path('download_ppc', {interSiret: inter.siret}) }}" >
                                            {#Interlocuteur: {{ inter.siret }} Commune : {{ inter.commune }}#}
                                            Nom du document: {{ inter.ppcCodePpc.documentNom }}
                                            {% set existPCE = true %}
                                        </a>
                                    </li>
                              {% endif %}
                            {% endfor %}
                            {% if not existPCE and interlocuteurs is not empty %}
                                <li>Aucun de vos interlocuteurs n'a de PCE disponible.</li>
                            {% endif %}
                            {% if interlocuteurs is empty %}
                            <li>Aucun de vos interlocuteurs n'a de PCE disponible.</li>
                            {% endif %}

                        </ul>
                        {% if errors is defined %}
                        {{ errors }}
                        {% endif %}
                    </div>
                    {% endif %}

                </div>
            </div>
        </div>
        <div class="row plr-16 ptb-24">
            <div align="left">
            {% if app.session.has('interlocuteurSelected') and  app.session.get('interlocuteurSelected') != "tous" %}
                {% set interlocuteurSelected = app.session.get('interlocuteurSelected') %}
            {% elseif  app.session.has('interlocuteurSelected') and app.session.get('interlocuteurSelected') == "tous" %}
                {% set interlocuteurSelected = 'tous' %}
            {% else %}
                {% set interlocuteurSelected = null %}
            {% endif %}
            <select id="inter" name="inter" class="selectpicker">
                <option value="tous" {% if interlocuteurSelected is null or interlocuteurSelected == 'tous' %}selected{% endif %}>Tous les interlocuteurs</option>
                {% for inter in interlocuteurs %}
                    <option value="{{ inter.siret }}" {% if inter.siret == interlocuteurSelected %} selected {% endif %}>{{ inter.siret }} - {{ inter.raisonSociale }}</option>
                {% endfor %}
            </select>
        </div>
        </div>

        <div id="particip-financ-tableau" class="row plr-16 ptb-24">
            <!-- TABLEAU-->
            <div class="col-xs-12 p-0">
                <div class="bloc-base p-16">
                    <div class="row m-0">
                        <div class="col-xs-12 p-16 background-white">
                            <div class="table-responsive">
                                <table id="myTable" class="display">
                                    <thead class="background-blue">
                                    <tr>
                                        <th class="pl-8 pr-16 ptb-16"><a class="popover-aide"  data-placement="bottom">Ligne de programme</a></th>
                                        <th class="pl-8 pr-16 ptb-16"><a class="popover-aide"  data-placement="bottom">N° de convention ou acte d'attrib.</a></th>
                                        <th class="pl-8 pr-16 ptb-16"><a class="popover-aide"  data-placement="bottom">Interlocuteur</a></th>
                                        <th class="pl-8 pr-16 ptb-16"><a class="popover-aide"  data-placement="bottom">Objet</a></th>
                                        <th class="pl-8 pr-16 ptb-16"><a class="popover-aide"  data-placement="bottom">Localisation</a></th>
                                        <th class="pl-8 pr-16 ptb-16"><a class="popover-aide"  data-placement="bottom">Notifié le</a></th>
                                        <th class="pl-8 pr-16 ptb-16"><a class="popover-aide"  data-placement="bottom">Etat du dossier</a></th>
                                        <th class="pl-8 pr-16 ptb-16"><a class="popover-aide"  data-placement="bottom">Montant PF</a></th>
                                        <th class="pl-8 pr-16 ptb-16"><a class="popover-aide"  data-placement="bottom">Echéancier</a></th>
                                        <th class="pl-8 pr-16 ptb-16"><a class="popover-aide"  data-placement="bottom">Consulter le dossier</a></th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    {% for d in dossiers %}
                                    <tr>
                                        <td class="plr-8 ptb-24">{{ d.ligneProgramme }}</td>
                                        <td class="plr-8 ptb-24">{{ d.numero }}</td>
                                        <td class="plr-8 ptb-24">{{ d.interlocuteurSiret.raisonSociale }} - {{ d.interlocuteurSiret.commune}}</td>
                                        <td class="plr-8 ptb-24">{{ d.objet }}</td>
                                        <td class="plr-8 ptb-24">{{ d.localisation }}</td>
                                        {% if '30/11/-0001' not in d.dateNotification |date('d/m/Y') and d.dateNotification is not null %}
                                            <td class="plr-8 ptb-24">{{d.dateNotification ? d.dateNotification|date('d/m/Y') }}</td>
                                        {% else %}
                                            <td class="plr-8 ptb-24"></td>
                                        {% endif %}

                                        <td class="plr-8 ptb-24"><span class="tag ptb-8 plr-16 background-purple">{{ d.etat }}</span></td>
                                        <td class="plr-8 ptb-24 forceOneLine">{{ d.montantParticipation|number_format(2,',',' ') }} €</td>
                                        <td class="plr-8 ptb-24"><a id="consultEcheance{{d.numero}}" href="{{ path('echeance_dossier', {dossierID : d.numero}) }}" class="button-rond-xs align-middle"><i class="fas fa-calendar-alt"></i></a></td>
                                        <td class="plr-8 ptb-24"><a id="consultFolder{{d.numero}}" href="{{ path('dossier_aide_informations', {dossierID : d.numero}) }}" class="button-rond-xs align-middle"><i class="fas fa-book"></i></a></td>
                                    </tr>
                                    {% endfor %}

                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{% block javascript %}
    {{ parent() }}

    <script type="text/javascript">
        $(document).ready(function() {
            $('#inter').change(function(){
                let siret = $( this ).val();
                if(siret == -1)
                    document.location.href= Routing.generate('participations_list');
                else
                    document.location.href= Routing.generate('participations_list', {'inter' : siret });
            });    
        });
    </script>

{% endblock %}
{% block javascript_inline %}
    <script>
        let tableApi = $('#myTable').dataTable({
                "order": [[ 5, "desc" ]],
                "sPaginationType": "full_numbers",
                "aoColumns": [
                    null,
                    null,
                    null,
                    null,
                    null,
                    { "sType": "date-uk" },
                    null,
                    { "sType": "currency" },
                    null,
                    null,
                ]
            }).api();

            {% for d in dossiers %}
                $(document).on('click', "#consultFolder{{d.numero}}",function(){
                    var value = $('.dataTables_filter input').val();
                    var page = tableApi.page() + 1;
                    value = "?searchedValue=" + value + "&pageNumber=" + page;
                    $( this ).attr('href',function(i, a){ return a + value });
                });
                $(document).on('click', "#consultEcheance{{d.numero}}",function(){
                    var value = $('.dataTables_filter input').val();
                    var page = tableApi.page() + 1;
                    value = "?searchedValue=" + value + "&pageNumber=" + page;
                    $( this ).attr('href',function(i, a){ return a + value });
                });
            {% endfor %}
            let inputFilter =  $('.dataTables_filter input');
            {% if app.request.query.has('searchedValue') %}
                {% set searchedValue =  app.request.query.all['searchedValue'] %}
            {% endif %}
            {% if app.request.query.has('pageNumber') %}
                {% set pageNumber =  app.request.query.all['pageNumber'] %}
            {% endif %}
            {% if searchedValue is defined %}
                inputFilter.val("{{ searchedValue }}");
                inputFilter.trigger('keyup');
            {% endif %}
            {% if pageNumber is defined %}
                tableApi.page({{ pageNumber }} - 1 ).draw('page');
            {% endif %}

    </script>
{% endblock %}