<div class="row">
    <div class="col-12">

        @include('layouts.commons.success')
        @include('layouts.commons.errors')

        @if(sizeof($thematiques) > 0)
            <form method="post" action="{{ route('projets.update-resp-soc-env', $projet->id) }}">
            @csrf
            {{ method_field('PUT') }}

            <table class="table table-responsive mt-4">
                <thead>
                    <tr>
                        <th>Thématique</th>
                        <th colspan="2">Actions</th>
                        <th>Unité d'oeuvre</th>
                        <th>Cible</th>
                        <th>Notifié</th>
                        <th>Réalisé</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($thematiques as $t)
                        @if ($t->actions_visibles()->count() > 0)
                            <tr>
                                <td rowspan="{{ $t->actions_visibles()->count() + 1 }}">{{ $t->nom }}</td>
                            </tr>
                                @foreach($t->actions_visibles() as $a)
                                    @php
                                        $is_disabled = !in_array($a->id, array_column($actions_checked, 'action_id')) || Auth::user()->cannot('edit_projet');
                                    @endphp
                                    <tr>
                                        <td>{{ $a->nom }}</td>
                                        <td>
                                            {{-- <input name="action_{{ $a->id }}" type="checkbox" value="{{ $a->id }}"
                                                @if(in_array($a->id, $actions_checked)) checked @endif 
                                                @cannot('edit_projet') disabled @endcannot> --}}
                                            <input name="action_rse[]" type="checkbox" value="{{ $a->id }}"
                                                @if(in_array($a->id, array_column($actions_checked, 'action_id'))) checked @endif 
                                                @cannot('edit_projet') disabled @endcannot>
                                        </td>
                                        <td>
                                            <select name="unite_oeuvre_rse[{{ $a->id }}]" class="form-control" data-action-id="{{ $a->id }}" @if ($is_disabled) disabled @endif>
                                                @foreach ($actions_unite_oeuvre as $ue)
                                                    <option value="{{ $ue->id }}" 
                                                        @if (!$is_disabled && old('unite_oeuvre_rse[$a->id]', $actions_checked[$a->id]['action_unite_oeuvre_id']) == $ue->id)
                                                            selected
                                                        @endif
                                                        >{{ $ue->libelle }}</option>
                                                @endforeach
                                            </select>
                                        </td>
                                        <td>
                                            <input type="text" class="form-control" data-action-id="{{ $a->id }}" @if ($is_disabled) disabled @endif
                                                name="cible-rse[{{ $a->id }}]" value="{{ !$is_disabled ? old('cible-rse[$a->id]', $actions_checked[$a->id]['cible']) : '' }}">
                                        </td>
                                        <td>
                                            <input type="text" class="form-control" data-action-id="{{ $a->id }}" @if ($is_disabled) disabled @endif
                                                name="notifie-rse[{{ $a->id }}]" value="{{ !$is_disabled ? old('notifie-rse[$a->id]', $actions_checked[$a->id]['notifie']) : '' }}">
                                        </td>
                                        <td>
                                            <input type="text" class="form-control" data-action-id="{{ $a->id }}" @if ($is_disabled) disabled @endif
                                                name="realise-rse[{{ $a->id }}]" value="{{ !$is_disabled ? old('realise-rse[$a->id]', $actions_checked[$a->id]['realise']) : '' }}">
                                        </td>
                                    </tr>
                                @endforeach
                        @else
                            <tr>
                                <td>-</td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                            </tr>
                        @endif
                    @endforeach
                </tbody>
            </table>
            <div class="row">
                <div class="col-12">
                    <div class="form-group row text-right">
                        <div class="col-sm-12">
                            <a href="{{ route('projets.index') }}" class="btn btn-outline-warning">Annuler</a>
                            @if($editable)
                                <button id="sauvegarder" type="submit" class="btn btn-outline-success">
                                    Sauvegarder
                                </button>
                            @endif
                        </div>
                    </div>
                </div>
            </div>
        </form>
        @else
            <div class="pt-2">
                <h4>Pas de données disponible.</h4>
            </div>
        @endif
    </div>
</div>


@can('edit_projet')
    <script>
        $(document).ready(function() {
            $("input[type='checkbox'][name='action_rse[]']").on('change', function() {
                // Si on décoche la case
                if (!this.checked) {
                    // On efface les valeurs entrées
                    $("input[data-action-id='" + this.value + "']").val('');
                }
                // Dans tous les cas, on inverse la propriété "disabled"
                $("[data-action-id='" + this.value + "']").prop('disabled', function(i, v) { return !v; });
                // alert(this.checked);
            });
        });
    </script>
@endcan