@extends('layouts.admin')
@section('title', 'Demandes d\'audience au rectorat UAC')

<style>
  #appointmentsTable {
    border-collapse: separate;
    border-spacing: 0;
  }

  #appointmentsTable thead th {
    background-color: transparent;
    border-bottom: 2px solid var(--border-light);
    padding-top: 1rem;
    padding-bottom: 1rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    font-size: 0.875rem;
    letter-spacing: 0.05em;
  }

  #appointmentsTable tbody td {
    border-bottom: 1px solid var(--border-light);
    padding-top: 1rem;
    padding-bottom: 1rem;
    vertical-align: middle;
  }

  #appointmentsTable tbody tr:last-child td {
    border-bottom: none;
  }

  #appointmentsTable tbody tr:hover {
    background-color: rgba(14, 165, 233, 0.04) !important;
  }

  .avatar-xs {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .avatar-xs img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .badge {
    padding: 0.35rem 0.75rem;
    font-weight: 500;
    border-radius: 50px;
    font-size: 0.75rem;
    letter-spacing: 0.02em;
  }

  .dropdown-menu {
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border-light);
    padding: 0.5rem;
  }

  .dropdown-item {
    border-radius: var(--radius-sm);
    padding: 0.5rem 0.75rem;
    margin: 0.125rem 0;
    transition: all var(--transition-fast);
  }

  .dropdown-item:hover {
    background-color: var(--primary-50);
    color: var(--primary-700);
  }

  @media (max-width: 768px) {
    #appointmentsTable {
      font-size: 0.875rem;
    }

    .stats-header h1 {
      font-size: 1.5rem;
    }
  }
</style>
<style>
  /* Styles spécifiques pour le modal */
  .modal-content {
    border: none;
    box-shadow: var(--shadow-xl);
  }

  .modal-header {
    background: linear-gradient(135deg, rgba(14, 165, 233, 0.05), rgba(14, 165, 233, 0.02));
    border-radius: var(--radius-xl) var(--radius-xl) 0 0 !important;
  }

  .action-icon-container {
    width: 48px;
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(14, 165, 233, 0.1);
    border-radius: var(--radius-md);
  }

  .form-label {
    color: var(--text-secondary);
    margin-bottom: 0.5rem;
  }

  .input-group-text {
    background-color: var(--bg-secondary);
    border-color: var(--border-light);
    color: var(--text-muted);
  }

  .form-control,
  .form-select {
    border-color: var(--border-light);
    background-color: var(--bg-surface);
    color: var(--text-primary);
    transition: all var(--transition-fast);
  }

  .form-control:focus,
  .form-select:focus {
    border-color: var(--primary-400);
    box-shadow: 0 0 0 3px rgba(14, 165, 233, 0.1);
    background-color: var(--bg-surface);
  }

  .alert {
    border-radius: var(--radius-md);
    border: 1px solid var(--border-light);
    background-color: var(--bg-secondary);
  }

  /* Responsive */
  @media (max-width: 576px) {
    .modal-dialog {
      margin: 0.5rem;
    }

    .modal-header .d-flex {
      flex-direction: column;
      align-items: flex-start;
      gap: 1rem;
    }

    .action-icon-container {
      align-self: flex-start;
    }
  }
</style>
<style>
  /* Styles spécifiques pour le modal de vue */
  .avatar-large {
    position: relative;
  }

  .avatar-large img {
    width: 56px;
    height: 56px;
    object-fit: cover;
  }

  .icon-container {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-md);
  }

  .card-header-modern {
    padding: 0;
  }

  .card-body-modern {
    padding: 0;
  }

  .info-group {
    margin-bottom: 1rem;
  }

  .info-label {
    margin-bottom: 0.25rem;
  }

  .info-label i {
    width: 16px;
  }

  .info-value {
    color: var(--text-primary);
    line-height: 1.4;
  }

  .message-content {
    font-size: 0.95rem;
    line-height: 1.6;
    border-left: 3px solid var(--primary-400);
    background: var(--bg-secondary) !important;
  }

  .admin-note {
    border-left: 3px solid var(--warning);
    background: var(--warning-bg, rgba(245, 158, 11, 0.1)) !important;
  }

  .file-icon {
    width: 48px;
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .hover-lift:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
    background: var(--bg-secondary) !important;
  }

  /* Scrollbar personnalisée pour le message */
  .message-content::-webkit-scrollbar {
    width: 6px;
  }

  .message-content::-webkit-scrollbar-track {
    background: transparent;
  }

  .message-content::-webkit-scrollbar-thumb {
    background: var(--primary-300);
    border-radius: 3px;
  }

  .message-content::-webkit-scrollbar-thumb:hover {
    background: var(--primary-400);
  }

  /* Responsive */
  @media (max-width: 768px) {
    .modal-dialog {
      margin: 0.5rem;
    }

    .avatar-large img {
      width: 48px;
      height: 48px;
    }

    .icon-container {
      width: 36px;
      height: 36px;
    }

    .modal-footer .d-flex {
      flex-direction: column;
    }

    .modal-footer .btn {
      width: 100%;
    }
  }

  /* Animation d'entrée pour les éléments */
  @keyframes fadeIn {
    from {
      opacity: 0;
      transform: translateY(10px);
    }

    to {
      opacity: 1;
      transform: translateY(0);
    }
  }

  .card-modern {
    animation: fadeIn 0.3s ease-out;
  }

  .info-group {
    animation: fadeIn 0.4s ease-out;
  }
</style>

@section('content')
  <div class="container-fluid py-4">

    <div class="row justify-content-center">
      <div class="col-lg-10">
        <div class="stats-header text-white mb-4">
          <h3 class="mb-1 fw-bold">
            Dossier électronique
          </h3>
          <div class="d-flex gap-3 flex-wrap">
            <span class="badge bg-light text-dark">
              Réf : {{ $audienceFile->reference }}
            </span>
            <span class="small">
              Ouvert le {{ $audienceFile->opened_at->format('d/m/Y H:i') }}
            </span>
          </div>
        </div>
        <div class="row g-4">
          <div class="col-md-6">
            <div class="card-modern h-100">
              <div class="card-header-modern bg-transparent border-0 d-flex align-items-center gap-2 mb-3">
                <div class="icon-container bg-primary bg-opacity-10">
                  <i class="bi bi-person-circle text-primary"></i>
                </div>
                <h6 class="mb-0 fw-semibold text-primary">Informations du requérant</h6>
              </div>

              <div class="card-body-modern p-0">
                <div class="info-group">
                  <span class="small text-muted">Nom</span>
                  <div class="fw-semibold">{{ $appointment->last_name }}</div>
                </div>

                <div class="info-group">
                  <span class="small text-muted">Prénom</span>
                  <div class="fw-semibold">{{ $appointment->first_name }}</div>
                </div>

                <div class="info-group">
                  <span class="small text-muted">Email</span>
                  <div>
                    <a href="mailto:{{ $appointment->email }}" class="text-primary">
                      {{ $appointment->email }}
                    </a>
                  </div>
                </div>

                <div class="info-group">
                  <span class="small text-muted">Contact</span>
                  <div class="fw-semibold">{{ $appointment->mobile ?? '—' }}</div>
                </div>
              </div>
            </div>
          </div>
          <div class="col-md-6">
            <div class="card-modern h-100">
              <div class="card-header-modern bg-transparent border-0 d-flex align-items-center gap-2 mb-3">
                <div class="icon-container bg-success bg-opacity-10">
                  <i class="bi bi-calendar-check text-success"></i>
                </div>
                <h6 class="mb-0 fw-semibold text-success">Audience</h6>
              </div>

              <div class="card-body-modern p-0">
                <div class="info-group">
                  <span class="small text-muted">Audience tenue le</span>
                  <div class="fw-semibold">
                    {{ $report->start_date->format('d/m/Y H:i') }}
                  </div>
                </div>
                <div class="info-group">
                  <span class="small text-muted">Objet</span>
                  <div class="fw-semibold">
                    {{ $appointment->subject }}
                  </div>
                </div>
                <div class="info-group">
                  <span class="small text-muted">Autorité rencontrée</span>
                  <div class="fw-semibold">
                    {{ $appointment->service ? $appointment->service->name : "_"  }}
                  </div>
                </div>

                <div class="info-group">
                  <span class="small text-muted">Greffier</span>
                  <div class="fw-semibold">{{ $report->author->name }}</div>
                </div>

                  <div class="info-group">
                <a href="{{ route('admin.reports.show', $report) }}"
                   class="btn btn-sm btn-primary-modern mt-2">
                    Consulter le compte-rendu
                </a>
            </div>
              </div>
            </div>
          </div>
          <div class="col-md-6">
            <div class="card-modern h-100">
              <div class="card-header-modern bg-transparent border-0 d-flex align-items-center gap-2 mb-3">
                <div class="icon-container bg-warning bg-opacity-10">
                  <i class="bi bi-chat-square-text text-warning"></i>
                </div>
                <h6 class="mb-0 fw-semibold text-warning">Avis techniques</h6>
              </div>

              <div class="card-body-modern p-0">
                @forelse($technical as $avis)
                  <div class="mb-3">
                    <div class="fw-semibold">
                      {{ $avis->assignedTo->name }}
                    </div>
                    <div class="small text-muted">
                      {{ $avis->assignedTo->institution ? $avis->assignedTo->institution->name : '-' }}
                    </div>
                    <div class="mt-1">
                      {{ $avis->answer_message ? $avis->answer_message : '_'  }}
                    </div>
                  </div>
                @empty
                  <p class="text-muted mb-0">Aucun avis technique</p>
                @endforelse
              </div>
            </div>
          </div>
          <div class="col-md-6">
            @php
              $uploads = $appointment->uploads ?? [];
            @endphp

            @if(count($uploads))
              <div class="card-modern h-100">
                <div
                  class="card-header-modern bg-transparent border-0 d-flex align-items-center justify-content-between mb-3">
                  <div class="d-flex align-items-center gap-2">
                    <div class="icon-container bg-secondary bg-opacity-10">
                      <i class="bi bi-paperclip text-secondary"></i>
                    </div>
                    <h6 class="mb-0 fw-semibold text-secondary">
                      Documents fournis
                    </h6>
                  </div>

                  <span class="badge bg-light text-dark">
                    {{ count($uploads) }} fichier(s)
                  </span>
                </div>

                <div class="card-body-modern">
                  @foreach($uploads as $file)
                    @php
                      // Cas 1 : tableau détaillé
                      $path = is_array($file) ? $file['path'] ?? null : $file;
                      $name = is_array($file)
                        ? ($file['original_name'] ?? basename($path))
                        : basename($path);

                      $mime = is_array($file) ? ($file['mime'] ?? '') : '';
                      $sizeKb = is_array($file) && isset($file['size'])
                        ? number_format($file['size'] / 1024, 2)
                        : '—';

                      $url = asset('storage/' . $path);
                    @endphp

                    <a href="{{ $url }}" target="_blank"
                      class="list-group-item list-group-item-action d-flex align-items-center gap-3 p-3 border-0 hover-lift mb-2"
                      style="border-radius: var(--radius-sm); transition: all var(--transition-fast);">

                      <div class="flex-shrink-0">
                        <div class="file-icon {{ fileColorClass($mime) }} p-2 rounded">
                          <i class="{{ fileIcon($mime) }} fs-5"></i>
                        </div>
                      </div>

                      <div class="flex-grow-1">
                        <div class="d-flex justify-content-between align-items-center">
                          <h6 class="mb-0 small fw-semibold text-truncate" style="max-width: 240px;">
                            {{ $name }}
                          </h6>
                          <span class="badge bg-light text-dark small">
                            {{ $sizeKb }} KB
                          </span>
                        </div>

                        <div class="small text-muted mt-1 d-flex align-items-center gap-1">
                          <i class="bi bi-download"></i>
                          Télécharger
                        </div>
                      </div>
                    </a>
                  @endforeach
                </div>
              </div>
            @endif

          </div>
        </div>
      </div>
    </div>
  </div>
@endsection