<form method="post" action="{{ route('home.filtres') }}">
    @csrf

    <div class="row">
        {{-- PAAT --}}
        @if(!Auth::user()->hasRole(ROLE::RESPONSABLE_PERFORMANCE_ARS))
        <div class="col-md-3">
            <div class="form-group row">
                <label for="paat" class="col-sm-4 col-form-label">PAAT</label>
                <div class="col-sm-8">
                    <select id="filtre_paat" name="paat_id" class="form-control">
                        @foreach($paats as $id => $description)
                        <option value="{{ $id }}" @if($paat_id==$id) selected @endif>{{ $description }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
        </div>
        @endif
        {{-- Date début --}}
        <div class="col-md-3">
            <div class="form-group row">
                <label for="date_debut" class="col-md-4 col-form-label text-md-right">Du</label>
                <div class="col-md-8">
                    <div class="input-group">
                        <input id="date_debut" type="text" class="form-control" name="date_debut"
                            value="{{ date('d/m/Y', strtotime($date_debut)) }}">
                        <div class="input-group-append">
                            <span class="input-group-text">
                                <svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-calendar3"
                                    fill="currentColor" xmlns="http://www.w3.org/2000/svg">
                                    <path fill-rule="evenodd"
                                        d="M14 0H2a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2zM1 3.857C1 3.384 1.448 3 2 3h12c.552 0 1 .384 1 .857v10.286c0 .473-.448.857-1 .857H2c-.552 0-1-.384-1-.857V3.857z" />
                                    <path fill-rule="evenodd"
                                        d="M6.5 7a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm-9 3a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm-9 3a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2z" />
                                </svg>
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        {{-- Date fin --}}
        <div class="col-md-3">
            <div class="form-group row">
                <label for="date_fin" class="col-md-4 col-form-label text-md-right">au</label>
                <div class="col-md-8">
                    <div class="input-group">
                        <input id="date_fin" type="text" class="form-control" name="date_fin"
                            value="{{ date('d/m/Y', strtotime($date_fin)) }}">
                        <div class="input-group-append">
                            <span class="input-group-text">
                                <svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-calendar3"
                                    fill="currentColor" xmlns="http://www.w3.org/2000/svg">
                                    <path fill-rule="evenodd"
                                        d="M14 0H2a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2zM1 3.857C1 3.384 1.448 3 2 3h12c.552 0 1 .384 1 .857v10.286c0 .473-.448.857-1 .857H2c-.552 0-1-.384-1-.857V3.857z" />
                                    <path fill-rule="evenodd"
                                        d="M6.5 7a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm-9 3a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm-9 3a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2z" />
                                </svg>
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        {{-- Utilisateurs --}}
        @if($utilisateurs != null)
        <div class="col-md-3">
            <div class="form-group row">
                <label for="utilisateur_id" class="col-sm-4 col-form-label">Utilisateur</label>
                <div class="col-sm-8">
                    <select id="utilisateur_id" name="utilisateur_id" class="form-control">
                        <option value="">Tous</option>
                        @foreach($utilisateurs as $u)
                        <option value="{{ $u->id }}" @if($utilisateur_id==$u->id) selected
                            @endif>{{ $u->nom_complet() }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
        </div>
        @endif
    </div>
    <div class="row">
        {{-- Famille --}}
        <div class="col-md-3">
            <div class="form-group row">
                <label for="famille_id" class="col-sm-4 col-form-label">Famille</label>
                <div class="col-sm-8">
                    <select id="famille_id" name="famille_id" class="form-control">
                        <option value="">Tous</option>
                        @foreach($familles as $f)
                        <option value="{{ $f->id }}" @if($famille_id==$f->id) selected @endif>{{ $f->nom }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
        </div>
        {{-- Domaine --}}
        <div class="col-md-3">
            <div class="form-group row">
                <label for="domaine_id" class="col-sm-4 col-form-label">Domaine</label>
                <div class="col-sm-8">
                    <select id="domaine_id" name="domaine_id" class="form-control">
                        <option value="">Tous</option>
                        @foreach($domaines as $d)
                        <option value="{{ $d->famille_id . '_' . $d->id }}" @if($d->famille_id . '_' . $d->id ===
                            $d->famille_id . '_' . $domaine_id) selected @endif
                            @if($d->famille_id != $famille_id) style="display: none" @endif>
                            {{ $d->nom }}
                        </option>
                        @endforeach
                    </select>
                </div>
            </div>
        </div>
        {{-- Catégorie --}}
        <div class="col-md-3">
            <div class="form-group row">
                <label for="categorie_id" class="col-sm-4 col-form-label">Catégorie</label>
                <div class="col-sm-8">
                    <select id="categorie_id" name="categorie_id" class="form-control">
                        <option value="">Tous</option>
                        @foreach($categories as $c)
                        <option value="{{ $c->domaine_id . '_' . $c->id }}" @if($c->domaine_id . '_' . $c->id ===
                            $c->domaine_id . '_' . $categorie_id) selected @endif
                            @if($c->domaine_id != $domaine_id) style="display: none" @endif>
                            {{ $c->nom }}
                        </option>
                        @endforeach
                    </select>
                </div>
            </div>
        </div>
        {{-- Segment --}}
        <div class="col-md-3">
            <div class="form-group row">
                <label for="segment_id" class="col-sm-4 col-form-label">Segment</label>
                <div class="col-sm-8">
                    <select id="segment_id" name="segment_id" class="form-control">
                        <option value="">Tous</option>
                        @foreach($segments as $s)
                        <option value="{{ $s->categorie_id . '_' . $s->id }}" @if($s->categorie_id . '_' . $s->id ===
                            $s->categorie_id . '_' . $segment_id) selected @endif
                            @if($s->categorie_id != $categorie_id) style="display: none" @endif>
                            {{ $s->nom }}
                        </option>
                        @endforeach
                    </select>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="form-group">
            <button type="submit" class="btn btn-outline-primary">
                <i class="fas fa-filter"></i>
                Filtrer
            </button>
            {{-- @if($show_export_button && (Auth::user()->can('export_indicateurs') || Auth::user()->hasRole(ROLE::RESPONSABLE_PERFORMANCE_ARS)))
                <a id="exporter_pdf" class="btn btn-outline-primary ml-auto">Format PDF</a>
            @endif --}}

            @if (Auth::user()->can('export_indicateurs') || Auth::user()->hasRole(ROLE::RESPONSABLE_PERFORMANCE_ARS))
            @if($show_export_button)
            <a class="btn btn-outline-primary" data-toggle="modal" data-target="#modal-export-pdf">
                <i class="far fa-file-pdf" style="color: red;"></i>
                Exporter</button>
            </a>
            @else
            <span class="d-inline-block" tabindex="0" data-toggle="tooltip" title="Pas de donnée à exporter">
                <button type="button" class="btn btn-outline-primary" style="pointer-events: none;" disabled>
                    <i class="far fa-file-pdf" style="color: red;"></i>
                    Exporter</button>
            </span>
            @endif
            <button type="button" class="btn btn-outline-primary" data-toggle="modal"
                data-target="#modal-historique-pdf">
                <i class="far fa-file-pdf" style="color: red;"></i>
                Historique
            </button>
            @endif
            @role(ROLE::RESPONSABLE_PERFORMANCE_ARS)
            <button type="button" class="btn btn-outline-primary ml-auto" data-toggle="modal"
                data-target="#modal-export-paats-resp-ars">
                <i class="far fa-file-excel" style="color: green;"></i>
                Exporter
            </button>
            @endrole
        </div>
    </div>
</form>

{{-- Modal Export PDF --}}
@if (Auth::user()->can('export_indicateurs') || Auth::user()->hasRole(ROLE::RESPONSABLE_PERFORMANCE_ARS))
<div class="modal fade" id="modal-export-pdf" tabindex="-1" aria-labelledby="modal-export-pdf" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Commentaires du rapport d'activité</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form id="form-export-pdf" action="{{ route('home.save_export_pdf') }}" method="POST">
                    @csrf
                    @method('PUT')

                    <div class="form-group">
                        <label for="modal-export-pdf-titre">Titre du rapport</label>
                        <input type="text" class="form-control" id="modal-export-pdf-titre"
                            name="modal-export-pdf-titre" required
                            value="{{ old('modal-export-pdf-titre', $user_rapport_activite ? $user_rapport_activite->titre : '') }}">
                    </div>

                    <div class="form-group">
                        <label for="modal-export-pdf-date-paat">Date du PAAT</label>
                        <input type="text" class="form-control" id="modal-export-pdf-date-paat"
                            name="modal-export-pdf-date-paat" required
                            value="{{ old('modal-export-pdf-date-paat', $user_rapport_activite ? $user_rapport_activite->date_paat : '') }}">
                    </div>

                    <div class="form-group">
                        <label for="modal-export-pdf-periode-reference">Période de référence</label>
                        <input type="text" class="form-control" id="modal-export-pdf-periode-reference"
                            name="modal-export-pdf-periode-reference" required
                            value="{{ old('modal-export-pdf-periode-reference', $user_rapport_activite ? $user_rapport_activite->periode_reference : '') }}">
                    </div>

                    <div class="form-group">
                        <label for="modal-export-pdf-commentaires">Commentaires</label>
                        <textarea class="form-control" id="modal-export-pdf-commentaires"
                            name="modal-export-pdf-commentaires" required
                            rows="3">{{ $user_rapport_activite ? $user_rapport_activite->commentaires : '' }}</textarea>
                    </div>

                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Fermer</button>
                <button type="submit" class="btn btn-primary" form="form-export-pdf">
                    <i class="far fa-save"></i>
                    Sauvegarder
                </button>
                <button type="button" class="btn btn-success" id="exporter_pdf">
                    <i class="fas fa-download"></i>
                    Télécharger
                </button>
            </div>
        </div>
    </div>
</div>
@endif

{{-- Modal Export Excel --}}
<div class="modal fade" id="modal-export-paats-resp-ars" tabindex="-1" aria-labelledby="modal-export-paats-resp-ars"
    aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Exporter les données des PAAT</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form id="form-export-paats-resp-ars" action="{{ route('home.export_paats_resp_ars') }}" method="GET">
                    @method('GET')

                    <div class="form-group">
                        <label for="annee-export-paats-resp-ars">Année référence PAAT</label>
                        <select class="form-control" id="annee-export-paats-resp-ars"
                            name="annee-export-paats-resp-ars">
                            <option value="-1">Toutes</option>
                            @foreach($liste_paat_annees as $annee)
                            <option value="{{ $annee->id }}">{{ $annee->annee_reference }}</option>
                            @endforeach
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="structure-export-paats-resp-ars">Structure</label>
                        <select class="form-control" id="structure-export-paats-resp-ars"
                            name="structure-export-paats-resp-ars">
                            <option value="-1">Toutes</option>
                            @foreach($liste_structures as $structure)
                            <option value="{{ $structure->id }}">{{ $structure->nom }}</option>
                            @endforeach
                        </select>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Fermer</button>
                <button type="submit" class="btn btn-success ml-auto" form="form-export-paats-resp-ars">
                    <i class="far fa-file-excel"></i>
                    Télécharger
                </button>
            </div>
        </div>
    </div>
</div>


{{-- Modal Historique Pdf --}}
@if (Auth::user()->can('export_indicateurs') || Auth::user()->hasRole(ROLE::RESPONSABLE_PERFORMANCE_ARS))
<div class="modal fade" id="modal-historique-pdf" tabindex="-1" aria-labelledby="modal-historique-pdf" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Historique des exports</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                @if(sizeof($user_liste_rapports_activite) === 0)
                    <div class="h6">Pas de données disponible.</div>
                @else
                    <table class="table table-striped table-bordered">
                        <thead>
                            <tr>
                                <th>Titre</th>
                                <th>Date</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                        @foreach($user_liste_rapports_activite as $export)
                            <tr>
                                <td>{{ $export->titre }}</td>
                                <td style="width: 10%">{{ Carbon\Carbon::createFromFormat('Y-m-d', $export->date_rapport)->format('d/m/Y') }}</td>
                                <td style="width: 20%">
                                    @if(isset($export->fichier_id))
                                        <a class="btn btn-primary" href="{{ route('fichiers.download_export_pdf', $export->fichier_id ?? -1) }}"
                                            data-toggle="tooltip" data-placement="top" title="Télécharger">
                                            <i class="fas fa-file-pdf" style="color: red;"></i>
                                        </a>
                                        <form class="inline-form-table" action="{{ route('fichiers.notify_export_pdf', [ $export->fichier_id, ROLE::DIRECTEUR_ACHAT ]) }}" method="POST">
                                            @csrf
                                            {{ method_field('PUT') }}
                                            <button type="submit" class="btn btn-primary"
                                                data-toggle="tooltip" data-placement="top" title="Notifier directeur achat">
                                                <i class="fas fa-bell" style="color: yellow;"></i>
                                            </button>
                                        </form>
                                        <form class="inline-form-table" action="{{ route('fichiers.notify_export_pdf', [ $export->fichier_id, ROLE::RESPONSABLE_PERFORMANCE_ARS ]) }}" method="POST">
                                            @csrf
                                            {{ method_field('PUT') }}
                                            <button type="submit" class="btn btn-primary"
                                                data-toggle="tooltip" data-placement="top" title="Notifier responsable performance ARS">
                                                <i class="fas fa-bell" style="color: yellow;"></i>
                                            </button>
                                        </form>
                                    @else
                                        <span class="d-inline-block" tabindex="0" data-toggle="tooltip" title="Pas de rapport d'enregistré">
                                            <button type="button" class="btn btn-primary" style="pointer-events: none;" disabled>
                                                <i class="fas fa-file-pdf" style="color: red;"></i>
                                            </button>
                                        </span>
                                        <span class="d-inline-block" tabindex="0" data-toggle="tooltip" title="Impossible de notifier">
                                            <button type="button" class="btn btn-primary" style="pointer-events: none;" disabled>
                                                <i class="fas fa-bell" style="color: yellow;"></i>
                                            </button>
                                        </span>
                                    @endif
                                </td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                @endif
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Fermer</button>
            </div>
        </div>
    </div>
</div>

<meta name="_token" content="{{ csrf_token() }}">
@endif