@include('projets.index.layouts.onglets.commons.button-ajouter-projet')

<div>
    <div class="row mt-2 mb-2">
        <div class="col-12">
            <h5>Gains</h5><br>
        </div>
    </div>
    @if(sizeof($gains) > 0)
        <table class="table table-striped table-bordered">
            <thead>
                {{-- 1 ere ligne de header --}}
                <tr>
                    <th colspan="2"></th>
                    @for($i = $min_annee; $i < $max_annee + 1; $i++)
                        <th colspan="2">{{ $i }}</th>
                    @endfor
                </tr>
            </thead>
            <tbody>
                {{-- 2 eme ligne de header --}}
                <tr>
                    <td>Acheteur</td>
                    <td>Objet</td>
                    @for($i = $min_annee; $i < $max_annee + 1; $i++)
                        <td>Gains cibles</td>
                        <td>Gains notifiés</td>
                    @endfor
                </tr>
                {{-- Contenu du tableau --}}
                @foreach($gains as $key => $value)
                    <tr>
                        <td>{{ $value['owner'] }}</td>
                        <td>{{ $value['objet'] }}</td>
                        @for($i = $min_annee; $i < $max_annee + 1; $i++)
                                <td class="cibles_{{ $i }} autonumeric">{{ $value['gains'][$i]['cible'] ?? 0 }}</td>
                                <td class="notifies_{{ $i }} autonumeric">{{ $value['gains'][$i]['notifie'] ?? 0 }}</td>
                        @endfor
                    </tr>
                @endforeach
                <tr>
                    <td colspan="2">Totaux</td>
                    @for($i = $min_annee; $i < $max_annee + 1; $i++)
                        <td class="totaux_cibles_{{ $i }}">0</td>
                        <td class="totaux_notifies_{{ $i }}">0</td>
                    @endfor
                </tr>
            </tbody>
        </table>
    @else
        <div class="pt-2">
            <h4>Pas de données disponible.</h4>
        </div>
    @endif
</div>

{{-- Applique le script uniquement s'il y a des données --}}
@if(sizeof($gains) > 0)
    <script>
        $(document).ready(function() {
            // Permet de calculer la ligne "totaux"
            var min_annee = {{ $min_annee }};
            var annee_max = {{ $max_annee }};
            for(var i = min_annee; i < annee_max + 1; i++)
            {
                var total_cibles = 0;
                var total_notifies = 0;
                $('.cibles_' + i).each(function() {
                    total_cibles += parseFloat($(this).text().replace(/ /g, ''));
                });
                $('.notifies_' + i).each(function() {
                    total_notifies += parseFloat($(this).text().replace(/ /g, ''));
                });
                $('.totaux_cibles_' + i).html(total_cibles);
                new AutoNumeric($('.totaux_cibles_' + i)[0], autonumericConfig);
                $('.totaux_notifies_' + i).html(total_notifies);
            }
        });
    </script>
@endif
