@extends('layouts.template')

@section('title', 'Préoccupations')
@section('page-title', 'Préoccupations')

@section('content')
<div class="card">
    <div class="card-body">
        <div class="container">
            <div class="card-header bg-info text-white">
                <h4 class="text-center">Liste des Préoccupations</h4>
            </div>
            @if ($errors->any())
            <div class="alert alert-danger">
                <ul>
                    @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
            @endif
            <div class="d-flex justify-content-bcenter  pt-2">
                <!-- Bouton Créer une préoccupation -->
                <a href="{{ route('preoccupations.create') }}" class="btn btn-primary btn-sm">
                    <i class="fa fa-plus"></i> Créer une préoccupation
                </a>
            </div>




            @if (session('success'))
            <div class="alert alert-success">
                {{ session('success') }}
            </div>
            @endif

            <div class="table-responsive" style="overflow-x: auto;">
                <table class="table table-striped table-bordered display" id="zero_config">
                    <thead>
                        <tr>

                            <th>Établissement</th>
                            <th>Gestionnaire affecté</th>
                            <th>Module</th>
                            <th>Progiciel</th>
                            <th>Priorité</th>
                            <th>Description</th>
                            <th>Date soumission</th>
                            <th>Date de début</th>
                            <th>Date de fin</th>
                            <th>Durée de résolution</th>
                            <!-- <th>Média</th> -->
                            <th>Statut</th>

                            <th>Méthode de résolution</th>

                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($preoccupations as $preoccupation)
                        <tr>

                            <td>
                                @if(isset($etablissements[$preoccupation->etablissement_id]))
                                {{ $etablissements[$preoccupation->etablissement_id]->sigle ?? 'Établissement introuvable' }}

                                @php
                                $etablissement = $etablissements[$preoccupation->etablissement_id];
                                $universite = isset($universites[$etablissement->idUniversite]) ? $universites[$etablissement->idUniversite] : null;
                                @endphp

                                ({{ $universite->descruni ?? 'Université introuvable' }})
                                @else
                                <span class="text-danger">Établissement introuvable</span>
                                @endif
                            </td>

                            <td>{{ $preoccupation->gestionnaire ? $preoccupation->gestionnaire->username : 'Non affecté' }}</td>

                            <td> @if($preoccupation->module)
                                {{ $preoccupation->module->nom }}
                                @else

                                @endif
                            </td>
                            <td>{{ $preoccupation->progiciel->nom }}</td>
                            <td>{{ $preoccupation->priorite->etat }}</td>
                            <td>{{ $preoccupation->description }}</td>

                            <td>{{ \Carbon\Carbon::parse($preoccupation->date_soumission)->format('d/m/Y H:i') }}</td>
                            <td>{{ \Carbon\Carbon::parse($preoccupation->date_debut)->format('d/m/Y H:i') }}</td>
                            <td>{{ \Carbon\Carbon::parse($preoccupation->date_fin)->format('d/m/Y H:i') }}</td>

                            <!-- Calcul et affichage de la durée de résolution en heures et minutes -->
                            <td>
                                @php
                                $debut = \Carbon\Carbon::parse($preoccupation->date_debut);
                                $fin = \Carbon\Carbon::parse($preoccupation->date_fin);
                                $duree = $debut->diff($fin);
                                $dureeResolution = '';
                                if ($duree->h > 0) {
                                $dureeResolution .= $duree->h . ' heure' . ($duree->h > 1 ? 's' : '');
                                }
                                if ($duree->i > 0) {
                                if ($dureeResolution) {
                                $dureeResolution .= ' ';
                                }
                                $dureeResolution .= $duree->i . ' minute' . ($duree->i > 1 ? 's' : '');
                                }
                                if (!$dureeResolution) {
                                $dureeResolution = 'moins d\'une minute';
                                }
                                @endphp
                                {{ $dureeResolution }}
                            </td>
                            <!-- <td>
                                    @php
                                    $mediaFiles = is_array($preoccupation->media_paths) ? $preoccupation->media_paths : json_decode($preoccupation->media_paths, true) ?? [];

                                    @endphp

                                    @if(count($mediaFiles) > 0)
                                        @foreach($mediaFiles as $file)
                                            @php
                                                $extension = pathinfo($file, PATHINFO_EXTENSION);
                                            @endphp
                                            @if (in_array($extension, ['jpeg', 'jpg', 'png', 'gif']))
                                                <img src="{{ asset('storage/' . $file) }}" alt="Media" width="100" height="100" class="m-1">
                                            @elseif (in_array($extension, ['mp4', 'mov', 'avi']))
                                                <video width="100" height="100" controls class="m-1">
                                                    <source src="{{ asset('storage/' . $file) }}" type="video/{{ $extension }}">
                                                </video>
                                            @else
                                                <p>Format non pris en charge</p>
                                            @endif
                                        @endforeach
                                    @else
                                        Aucun média
                                    @endif
                             </td>  -->


                            <td>
                                @php
                                $status = $preoccupation->statut->intitule ?? 'Statut non défini'; // Valeur par défaut si null
                                $statusClass = match ($status) {
                                'En attente' => 'badge bg-warning text-dark', // Jaune
                                'En cours' => 'badge bg-primary', // Bleu
                                'Résolu' => 'badge bg-success', // Vert
                                'Non résolu' => 'badge bg-danger', // Rouge
                                default => 'badge bg-secondary', // Gris pour les autres cas
                                };
                                @endphp
                                <span class="{{ $statusClass }}">{{ $status }}</span>
                            </td>


                            <td>{{ $preoccupation->methodeResol }}</td>

                            <td class="d-flex justify-content-center align-items-center">
                                <!-- Bouton Voir -->
                                <a href="{{ route('preoccupations.show', $preoccupation->id) }}"
                                    class="btn btn-info btn-sm rounded-pill me-1 text-center"
                                    title="Voir">
                                    <i class="fa fa-eye"></i>
                                </a>

                                @if ($preoccupation->statut && $preoccupation->statut->intitule === 'En attente')
                                <a href="{{ route('preoccupations.edit', $preoccupation->id) }}"
                                    class="btn btn-primary btn-sm rounded-pill me-1"
                                    title="Modifier">
                                    <i class="fa fa-edit"></i>
                                </a>
                                @endif

                                <a href="{{ route('preoccupations.assign', $preoccupation->id) }}"
                                    class="btn btn-success btn-sm rounded-pill me-1"
                                    title="Affecter">
                                    <i class="fas fa-tasks"></i>
                                </a>

                                <!-- Bouton Supprimer -->
                                <!-- <form action="{{ route('preoccupations.destroy', $preoccupation->id) }}" method="POST" class="d-inline">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn btn-danger btn-sm rounded-pill" title="Supprimer">
                                            <i class="fa fa-trash"></i>
                                        </button>
                                </form> -->
                            </td>


                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>

        </div>
    </div>
</div>

@endsection