'use client';

import { useState, useEffect, useRef } from 'react';
import { useRouter } from 'next/navigation';
import TrajetForm from './TrajetForm';
import AdherentForm from './AdherentForm';
import TrajetDetailModal from './TrajetDetailModal';
import { getParticipationRef } from '@/lib/participation-ref';

type PeriodType = 'day' | 'yesterday' | 'week' | 'month' | 'quarter' | 'year' | 'custom';

interface Stats {
  periodLabel?: string;
  participationsMois: {
    montant: number;
    nombreFactures: number;
  };
  trajetsAujourdhui: {
    nombre: number;
    difference: number;
  };
  trajetsRealisesMois: {
    nombre: number;
    pourcentageEvolution: number;
  };
  chauffeursActifs: {
    nombre: number;
    total: number;
  };
}

interface Trajet {
  id: string;
  date: string;
  adresseDepart: string;
  adresseArrivee: string;
  commentaire?: string | null;
  statut: string;
  participations?: { id: string }[];
  adherent: {
    id: string;
    nom: string;
    prenom: string;
    telephone: string;
    email: string;
  };
  chauffeur?: {
    id: string;
    nom: string;
    prenom: string;
    telephone: string;
  } | null;
}

interface DashboardContentProps {
  userName?: string | null;
}

const PERIOD_OPTIONS: { value: PeriodType; label: string }[] = [
  { value: 'day', label: "Aujourd'hui" },
  { value: 'yesterday', label: 'Hier' },
  { value: 'week', label: 'Cette semaine' },
  { value: 'month', label: 'Ce mois-ci' },
  { value: 'quarter', label: 'Ce trimestre' },
  { value: 'year', label: 'Cette année' },
  { value: 'custom', label: 'Personnalisé' },
];

export default function DashboardContent({ userName }: DashboardContentProps) {
  const router = useRouter();
  const [stats, setStats] = useState<Stats | null>(null);
  const [trajetsRecents, setTrajetsRecents] = useState<Trajet[]>([]);
  const [loading, setLoading] = useState(true);
  const [period, setPeriod] = useState<PeriodType>('month');
  const [dropdownOpen, setDropdownOpen] = useState(false);
  const dropdownRef = useRef<HTMLDivElement>(null);
  const [customFrom, setCustomFrom] = useState('');
  const [customTo, setCustomTo] = useState('');
  const [showTrajetForm, setShowTrajetForm] = useState(false);
  const [showAdherentForm, setShowAdherentForm] = useState(false);
  const [selectedTrajet, setSelectedTrajet] = useState<Trajet | null>(null);

  useEffect(() => {
    if (period === 'custom' && customFrom && customTo) {
      fetchStatsCustom(customFrom, customTo);
    } else if (period !== 'custom') {
      fetchStats(period);
    }
    fetchTrajetsRecents();
  }, [period, customFrom, customTo]);

  useEffect(() => {
    const handleClickOutside = (e: MouseEvent) => {
      if (dropdownRef.current && !dropdownRef.current.contains(e.target as Node)) {
        setDropdownOpen(false);
      }
    };
    document.addEventListener('mousedown', handleClickOutside);
    return () => document.removeEventListener('mousedown', handleClickOutside);
  }, []);

  const fetchStats = async (p: PeriodType = period) => {
    if (p === 'custom') return;
    try {
      const params = new URLSearchParams({ period: p });
      const response = await fetch(`/api/dashboard/stats?${params}`);
      if (response.ok) {
        const data = await response.json();
        setStats(data);
      }
    } catch (error) {
      console.error('Erreur lors du chargement des statistiques:', error);
    }
  };

  const fetchStatsCustom = async (from: string, to: string) => {
    try {
      const params = new URLSearchParams({ from, to });
      const response = await fetch(`/api/dashboard/stats?${params}`);
      if (response.ok) {
        const data = await response.json();
        setStats(data);
      }
    } catch (error) {
      console.error('Erreur lors du chargement des statistiques:', error);
    }
  };

  const refreshStats = () => {
    if (period === 'custom' && customFrom && customTo) {
      fetchStatsCustom(customFrom, customTo);
    } else if (period !== 'custom') {
      fetchStats(period);
    }
  };

  const fetchTrajetsRecents = async () => {
    setLoading(true);
    try {
      const response = await fetch('/api/trajets?limit=3');
      if (response.ok) {
        const data = await response.json();
        setTrajetsRecents(data);
      }
    } catch (error) {
      console.error('Erreur lors du chargement des trajets récents:', error);
    } finally {
      setLoading(false);
    }
  };

  const formatDate = (dateString: string) => {
    const date = new Date(dateString);
    return date.toLocaleDateString('fr-FR', {
      day: '2-digit',
      month: '2-digit',
      year: 'numeric',
    });
  };

  const formatTime = (dateString: string) => {
    const date = new Date(dateString);
    return date.toLocaleTimeString('fr-FR', { hour: '2-digit', minute: '2-digit' });
  };

  const getInitials = (nom: string, prenom: string) => {
    return `${prenom.charAt(0)}${nom.charAt(0)}`.toUpperCase();
  };

  // Montant estimé par trajet (basé sur l'image)
  const getMontantTrajet = () => {
    return 6.80; // Montant moyen
  };

  return (
    <div className="p-4 sm:p-6 space-y-6 sm:space-y-8">
      {/* En-tête avec menu déroulant de période */}
      <div className="mb-6 sm:mb-8">
        <div className="flex flex-col sm:flex-row sm:items-start sm:justify-between gap-4 mb-2">
          <div>
            <h1 className="text-2xl sm:text-3xl font-bold text-gray-900">
              Content de vous revoir <span className="text-dyellow">{userName || 'Utilisateur'}</span>
            </h1>
            <p className="text-xs sm:text-sm text-gray-600 mt-1">
              Bienvenue sur votre tableau de bord.
            </p>
          </div>
          <div className="relative flex-shrink-0" ref={dropdownRef}>
            <button
              type="button"
              onClick={() => setDropdownOpen(!dropdownOpen)}
              className="inline-flex items-center gap-2 px-4 py-2.5 text-sm font-medium text-gray-700 bg-white border border-gray-200/80 rounded-xl shadow-[0_1px_3px_rgba(0,0,0,0.06)] hover:shadow-[0_2px_8px_rgba(0,0,0,0.08)] hover:border-gray-200 transition-all duration-200 focus:outline-none focus:ring-2 focus:ring-dyellow/20 focus:border-dyellow"
            >
              <svg className="w-4 h-4 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z" />
              </svg>
              <span>
                {period === 'custom' && customFrom && customTo
                  ? `${customFrom.split('-').reverse().slice(0, 2).join('/')} → ${customTo.split('-').reverse().slice(0, 2).join('/')}`
                  : PERIOD_OPTIONS.find((o) => o.value === period)?.label || 'Période'}
              </span>
              <svg className={`w-4 h-4 text-gray-400 transition-transform duration-200 ${dropdownOpen ? 'rotate-180' : ''}`} fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 9l-7 7-7-7" />
              </svg>
            </button>
            {dropdownOpen && (
              <div className="absolute right-0 top-full mt-2 w-64 bg-white rounded-xl shadow-[0_4px_20px_rgba(0,0,0,0.08)] border border-gray-100 py-2 z-50 overflow-hidden">
                <div className="px-3 py-2 border-b border-gray-100">
                  <p className="text-xs font-semibold text-gray-400 uppercase tracking-wider">Période des statistiques</p>
                </div>
                <div className="max-h-72 overflow-y-auto">
                  {PERIOD_OPTIONS.filter((o) => o.value !== 'custom').map((opt) => (
                    <button
                      key={opt.value}
                      type="button"
                      onClick={() => {
                        setPeriod(opt.value);
                        setDropdownOpen(false);
                      }}
                      className={`w-full flex items-center gap-3 px-4 py-2.5 text-sm transition-colors ${
                        period === opt.value
                          ? 'bg-dyellow/10 text-dyellow font-medium'
                          : 'text-gray-700 hover:bg-gray-50'
                      }`}
                    >
                      <span className="w-6 h-6 rounded-lg bg-gray-100 flex items-center justify-center flex-shrink-0">
                        <svg className="w-3.5 h-3.5 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                          <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z" />
                        </svg>
                      </span>
                      {opt.label}
                      {period === opt.value && (
                        <svg className="w-4 h-4 ml-auto text-dyellow" fill="currentColor" viewBox="0 0 20 20">
                          <path fillRule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clipRule="evenodd" />
                        </svg>
                      )}
                    </button>
                  ))}
                  <div className="border-t border-gray-100 my-2" />
                  <button
                    type="button"
                    onClick={() => setPeriod('custom')}
                    className={`w-full flex items-center gap-3 px-4 py-2.5 text-sm transition-colors ${
                      period === 'custom'
                        ? 'bg-dyellow/10 text-dyellow font-medium'
                        : 'text-gray-700 hover:bg-gray-50'
                    }`}
                  >
                    <span className="w-6 h-6 rounded-lg bg-gray-100 flex items-center justify-center flex-shrink-0">
                      <svg className="w-3.5 h-3.5 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z" />
                      </svg>
                    </span>
                    Personnalisé
                    {period === 'custom' && (
                      <svg className="w-4 h-4 ml-auto text-dyellow" fill="currentColor" viewBox="0 0 20 20">
                        <path fillRule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clipRule="evenodd" />
                      </svg>
                    )}
                  </button>
                  {period === 'custom' && (
                    <div className="px-4 py-3 bg-gray-50/80 border-t border-gray-100 space-y-3">
                      <p className="text-xs font-medium text-gray-600">Sélectionner une plage</p>
                      <div className="flex items-center gap-2">
                        <input
                          type="date"
                          value={customFrom}
                          onChange={(e) => setCustomFrom(e.target.value)}
                          className="flex-1 px-3 py-2 text-sm border border-gray-200 rounded-lg bg-white focus:ring-2 focus:ring-dyellow/30 focus:border-dyellow transition-shadow"
                        />
                        <span className="text-gray-400 text-sm">→</span>
                        <input
                          type="date"
                          value={customTo}
                          onChange={(e) => setCustomTo(e.target.value)}
                          className="flex-1 px-3 py-2 text-sm border border-gray-200 rounded-lg bg-white focus:ring-2 focus:ring-dyellow/30 focus:border-dyellow transition-shadow"
                        />
                      </div>
                      <button
                        type="button"
                        onClick={() => setDropdownOpen(false)}
                        className="w-full py-2.5 text-sm font-medium text-white bg-dyellow hover:bg-dyellow/90 rounded-lg transition-colors"
                      >
                        Appliquer
                      </button>
                    </div>
                  )}
                </div>
              </div>
            )}
          </div>
        </div>
      </div>

      {/* Statistiques */}
      <div className="grid grid-cols-2 lg:grid-cols-4 gap-4 sm:gap-5">
        {/* Participations du mois */}
        <div className="bg-white rounded-xl shadow-sm p-4 sm:p-6 border border-gray-100 hover:shadow-lg hover:border-dyellow/30 transition-all duration-300 group">
          <div className="flex items-start justify-between mb-3 sm:mb-4">
            <div className="w-10 h-10 sm:w-12 sm:h-12 rounded-xl bg-gradient-to-br from-dyellow/20 to-dyellow/10 flex items-center justify-center flex-shrink-0 group-hover:scale-110 transition-transform duration-300">
              <svg className="w-5 h-5 sm:w-6 sm:h-6 text-dyellow" fill="currentColor" viewBox="0 0 24 24">
                <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1.41 16.09V20h-2.67v-1.93c-1.71-.36-3.16-1.46-3.27-3.4h1.96c.1 1.05.82 1.87 2.65 1.87 1.96 0 2.4-.98 2.4-1.59 0-.83-.44-1.61-2.67-2.14-2.48-.6-4.18-1.62-4.18-3.67 0-1.72 1.39-2.84 3.11-3.21V4h2.67v1.95c1.86.45 2.79 1.86 2.85 3.39H14.3c-.05-1.11-.64-1.87-2.22-1.87-1.5 0-2.4.68-2.4 1.64 0 .84.65 1.39 2.67 1.91s4.18 1.39 4.18 3.91c-.01 1.83-1.38 2.83-3.12 3.16z"/>
              </svg>
            </div>
          </div>
          <div>
            <p className="text-[10px] sm:text-xs text-gray-600 mb-1.5 sm:mb-2 font-medium uppercase tracking-wide">
              Participations {period === 'month' ? 'ce mois-ci' : 'sur la période'}
            </p>
            <p className="text-2xl sm:text-3xl font-bold text-gray-900 mb-1.5 sm:mb-2">
              {stats ? `${stats.participationsMois.montant.toFixed(2).replace('.', ',')}€` : '0,00€'}
            </p>
            <p className="text-[10px] sm:text-xs text-gray-500 font-medium">
              {stats ? `${stats.participationsMois.nombreFactures} participation${stats.participationsMois.nombreFactures > 1 ? 's' : ''}` : '0 participation'}
            </p>
          </div>
        </div>

        {/* Trajets Aujourd'hui */}
        <div className="bg-white rounded-xl shadow-sm p-4 sm:p-6 border border-gray-100 hover:shadow-lg hover:border-dyellow/30 transition-all duration-300 group">
          <div className="flex items-start justify-between mb-3 sm:mb-4">
            <div className="w-10 h-10 sm:w-12 sm:h-12 rounded-xl bg-gradient-to-br from-dyellow/20 to-dyellow/10 flex items-center justify-center flex-shrink-0 group-hover:scale-110 transition-transform duration-300">
              <svg className="w-5 h-5 sm:w-6 sm:h-6 text-dyellow" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13 7h8m0 0v8m0-8l-8 8-4-4-6 6" />
              </svg>
            </div>
          </div>
          <div>
            <p className="text-[10px] sm:text-xs text-gray-600 mb-1.5 sm:mb-2 font-medium uppercase tracking-wide">
              Trajets {period === 'day' ? "aujourd'hui" : period === 'yesterday' ? 'hier' : 'sur la période'}
            </p>
            <p className="text-2xl sm:text-3xl font-bold text-gray-900 mb-1.5 sm:mb-2">
              {stats ? stats.trajetsAujourdhui.nombre : 0}
            </p>
            <p className="text-[10px] sm:text-xs text-gray-500 font-medium">
              {stats && stats.trajetsAujourdhui.difference !== 0
                ? `${stats.trajetsAujourdhui.difference > 0 ? '+' : ''}${stats.trajetsAujourdhui.difference} vs période précédente`
                : 'Aucun changement'}
            </p>
          </div>
        </div>

        {/* Trajets réalisés ce mois */}
        <div className="bg-white rounded-xl shadow-sm p-4 sm:p-6 border border-gray-100 hover:shadow-lg hover:border-dyellow/30 transition-all duration-300 group">
          <div className="flex items-start justify-between mb-3 sm:mb-4">
            <div className="w-10 h-10 sm:w-12 sm:h-12 rounded-xl bg-gradient-to-br from-dyellow/20 to-dyellow/10 flex items-center justify-center flex-shrink-0 group-hover:scale-110 transition-transform duration-300">
              <svg className="w-5 h-5 sm:w-6 sm:h-6 text-dyellow" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
              </svg>
            </div>
          </div>
          <div>
            <p className="text-[10px] sm:text-xs text-gray-600 mb-1.5 sm:mb-2 font-medium uppercase tracking-wide">
              Trajets réalisés {period === 'month' ? 'ce mois-ci' : 'sur la période'}
            </p>
            <p className="text-2xl sm:text-3xl font-bold text-gray-900 mb-1.5 sm:mb-2">
              {stats ? stats.trajetsRealisesMois.nombre : 0}
            </p>
            <p className="text-[10px] sm:text-xs text-gray-500 font-medium">
              {stats && stats.trajetsRealisesMois.pourcentageEvolution !== 0
                ? `${stats.trajetsRealisesMois.pourcentageEvolution > 0 ? '+' : ''}${stats.trajetsRealisesMois.pourcentageEvolution}% vs période précédente`
                : 'Aucun changement'}
            </p>
          </div>
        </div>

        {/* Chauffeurs actifs */}
        <div className="bg-white rounded-xl shadow-sm p-4 sm:p-6 border border-gray-100 hover:shadow-lg hover:border-dyellow/30 transition-all duration-300 group">
          <div className="flex items-start justify-between mb-3 sm:mb-4">
            <div className="w-10 h-10 sm:w-12 sm:h-12 rounded-xl bg-gradient-to-br from-dyellow/20 to-dyellow/10 flex items-center justify-center flex-shrink-0 group-hover:scale-110 transition-transform duration-300">
              <svg className="w-5 h-5 sm:w-6 sm:h-6 text-dyellow" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z" />
              </svg>
            </div>
          </div>
          <div>
            <p className="text-[10px] sm:text-xs text-gray-600 mb-1.5 sm:mb-2 font-medium uppercase tracking-wide">Chauffeurs actifs</p>
            <p className="text-2xl sm:text-3xl font-bold text-gray-900 mb-1.5 sm:mb-2">
              {stats ? stats.chauffeursActifs.nombre : 0}
            </p>
            <p className="text-[10px] sm:text-xs text-gray-500 font-medium">
              {stats ? `Sur ${stats.chauffeursActifs.total} total` : 'Sur 0 total'}
            </p>
          </div>
        </div>
      </div>

      {/* Actions Rapides et Trajets Récents côte à côte */}
      <div className="grid grid-cols-1 lg:grid-cols-2 gap-5 sm:gap-6">
        {/* Actions Rapides */}
        <div>
          <div className="flex items-center justify-between mb-3 sm:mb-4">
            <h2 className="text-lg sm:text-xl font-bold text-gray-900 flex items-center gap-2">
              <span className="w-8 h-8 rounded-lg bg-lblue/10 flex items-center justify-center">
                <svg className="w-4 h-4 text-lblue" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13 10V3L4 14h7v7l9-11h-7z" />
                </svg>
              </span>
              Actions Rapides
            </h2>
          </div>
          <div className="grid grid-cols-2 gap-3 sm:gap-4">
            <button
              onClick={() => setShowTrajetForm(true)}
              className="bg-white rounded-xl shadow-sm p-4 sm:p-6 border border-gray-100 hover:border-lblue hover:shadow-lg transition-all duration-300 text-left group relative overflow-hidden"
            >
              <div className="absolute inset-0 bg-gradient-to-br from-lblue/5 to-transparent opacity-0 group-hover:opacity-100 transition-opacity duration-300"></div>
              <div className="relative">
                <div className="w-10 h-10 sm:w-12 sm:h-12 rounded-xl bg-gradient-to-br from-lblue/15 to-lblue/5 flex items-center justify-center mb-3 sm:mb-4 group-hover:scale-110 group-hover:from-lblue/25 group-hover:to-lblue/15 transition-all duration-300">
                  <svg className="w-5 h-5 sm:w-6 sm:h-6 text-lblue" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 20l-5.447-2.724A1 1 0 013 16.382V5.618a1 1 0 011.447-.894L9 7m0 13l6-3m-6 3V7m6 10l4.553 2.276A1 1 0 0021 18.382V7.618a1 1 0 00-.553-.894L15 4m0 13V4m0 0L9 7" />
                  </svg>
                </div>
                <h3 className="text-xs sm:text-sm font-bold text-gray-900 group-hover:text-lblue transition-colors">Nouveau trajet</h3>
              </div>
            </button>

            <button
              onClick={() => setShowAdherentForm(true)}
              className="bg-white rounded-xl shadow-sm p-4 sm:p-6 border border-gray-100 hover:border-lblue hover:shadow-lg transition-all duration-300 text-left group relative overflow-hidden"
            >
              <div className="absolute inset-0 bg-gradient-to-br from-lblue/5 to-transparent opacity-0 group-hover:opacity-100 transition-opacity duration-300"></div>
              <div className="relative">
                <div className="w-10 h-10 sm:w-12 sm:h-12 rounded-xl bg-gradient-to-br from-lblue/15 to-lblue/5 flex items-center justify-center mb-3 sm:mb-4 group-hover:scale-110 group-hover:from-lblue/25 group-hover:to-lblue/15 transition-all duration-300">
                  <svg className="w-5 h-5 sm:w-6 sm:h-6 text-lblue" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M13 7a4 4 0 11-8 0 4 4 0 018 0z" />
                  </svg>
                </div>
                <h3 className="text-xs sm:text-sm font-bold text-gray-900 group-hover:text-lblue transition-colors">Nouvel adhérent</h3>
              </div>
            </button>

            <button
              onClick={() => router.push('/dashboard/factures')}
              className="bg-white rounded-xl shadow-sm p-4 sm:p-6 border border-gray-100 hover:border-lblue hover:shadow-lg transition-all duration-300 text-left group relative overflow-hidden"
            >
              <div className="absolute inset-0 bg-gradient-to-br from-lblue/5 to-transparent opacity-0 group-hover:opacity-100 transition-opacity duration-300"></div>
              <div className="relative">
                <div className="w-10 h-10 sm:w-12 sm:h-12 rounded-xl bg-gradient-to-br from-lblue/15 to-lblue/5 flex items-center justify-center mb-3 sm:mb-4 group-hover:scale-110 group-hover:from-lblue/25 group-hover:to-lblue/15 transition-all duration-300">
                  <svg className="w-5 h-5 sm:w-6 sm:h-6 text-lblue" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
                  </svg>
                </div>
                <h3 className="text-xs sm:text-sm font-bold text-gray-900 group-hover:text-lblue transition-colors">Participation financière</h3>
              </div>
            </button>

            <div className="bg-white rounded-xl shadow-sm p-4 sm:p-6 border border-gray-100 opacity-60 cursor-not-allowed">
              <div className="w-10 h-10 sm:w-12 sm:h-12 rounded-xl bg-gray-100 flex items-center justify-center mb-3 sm:mb-4">
                <svg className="w-5 h-5 sm:w-6 sm:h-6 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z" />
                </svg>
              </div>
              <h3 className="text-xs sm:text-sm font-bold text-gray-400">Bientôt ?</h3>
            </div>
          </div>
        </div>

        {/* Trajets Récents */}
        <div>
          <div className="flex items-center justify-between mb-3 sm:mb-4">
            <h2 className="text-lg sm:text-xl font-bold text-gray-900 tracking-tight flex items-center gap-2">
              <span className="w-8 h-8 rounded-lg bg-lblue/10 flex items-center justify-center">
                <svg className="w-4 h-4 text-lblue" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 20l-5.447-2.724A1 1 0 013 16.382V5.618a1 1 0 011.447-.894L9 7m0 13l6-3m-6 3V7m6 10l4.553 2.276A1 1 0 0021 18.382V7.618a1 1 0 00-.553-.894L15 4m0 13V4m0 0L9 7" />
                </svg>
              </span>
              Trajets Récents
            </h2>
            <button
              onClick={fetchTrajetsRecents}
              className="text-xs sm:text-sm text-gray-500 hover:text-lblue font-medium flex items-center gap-1.5 transition-colors"
            >
              <svg className="w-3.5 h-3.5 sm:w-4 sm:h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
              </svg>
              <span className="hidden sm:inline">Actualiser</span>
            </button>
          </div>
          <div className="bg-white rounded-xl shadow-[0_1px_3px_rgba(0,0,0,0.05)] border border-gray-100/80 overflow-hidden">
            {loading ? (
              <div className="text-center py-6 sm:py-8 text-sm text-gray-500">Chargement...</div>
            ) : trajetsRecents.length === 0 ? (
              <div className="text-center py-6 sm:py-8 text-sm text-gray-500">
                Aucun trajet créé récemment
              </div>
            ) : (
              <div
                className="overflow-x-auto"
                style={{ WebkitOverflowScrolling: 'touch' } as React.CSSProperties}
              >
                <table className="w-full text-sm min-w-[420px]">
                  <thead>
                    <tr className="border-b border-gray-200 bg-gray-50/80">
                      <th className="text-left py-3 px-4 font-semibold text-gray-600">Adhérent</th>
                      <th className="text-left py-3 px-4 font-semibold text-gray-600">Statut</th>
                      <th className="text-left py-3 px-4 font-semibold text-gray-600">Fichier</th>
                      <th className="w-10 px-4"></th>
                    </tr>
                  </thead>
                  <tbody>
                    {trajetsRecents.map((trajet) => (
                      <tr
                        key={trajet.id}
                        onClick={() => setSelectedTrajet(trajet)}
                        className="border-b border-gray-100 last:border-b-0 hover:bg-gray-50/80 transition-colors cursor-pointer"
                      >
                        <td className="py-3 px-4">
                          <div className="flex items-center gap-3">
                            <div className="w-9 h-9 rounded-full bg-lgreen flex items-center justify-center text-white text-xs font-semibold flex-shrink-0 shadow-sm">
                              {getInitials(trajet.adherent.nom, trajet.adherent.prenom)}
                            </div>
                            <div>
                              <span className="font-semibold text-gray-900">
                                {trajet.adherent.prenom} {trajet.adherent.nom}
                              </span>
                              <p className="text-xs text-gray-400 mt-0.5">
                                {formatDate(trajet.date)} · {formatTime(trajet.date)}
                              </p>
                            </div>
                          </div>
                        </td>
                        <td className="py-3 px-4">
                          <span
                            className={`inline-flex px-2 py-0.5 text-[11px] font-medium rounded-md ${
                              trajet.statut === 'Terminé' || trajet.statut === 'Validé'
                                ? 'bg-emerald-50 text-emerald-700'
                                : trajet.statut === 'En cours'
                                ? 'bg-sky-50 text-sky-700'
                                : trajet.statut === 'Annulé'
                                ? 'bg-red-50 text-red-700'
                                : 'bg-gray-100 text-gray-600'
                            }`}
                          >
                            {trajet.statut}
                          </span>
                        </td>
                        <td className="py-3 px-4">
                          {trajet.participations?.[0] ? (
                            <span className="px-2 py-0.5 text-[11px] font-mono font-semibold rounded-md bg-lblue/15 text-lblue border border-lblue/20">
                              {getParticipationRef(trajet.participations[0].id)}
                            </span>
                          ) : (
                            <span className="text-gray-300">—</span>
                          )}
                        </td>
                        <td className="py-3 px-4">
                          <svg className="w-4 h-4 text-gray-300" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5l7 7-7 7" />
                          </svg>
                        </td>
                      </tr>
                    ))}
                  </tbody>
                </table>
              </div>
            )}
          </div>
        </div>
      </div>

      {/* Modals */}
      {showTrajetForm && (
        <TrajetForm
          onClose={() => setShowTrajetForm(false)}
          onSuccess={() => {
            setShowTrajetForm(false);
            fetchTrajetsRecents();
            refreshStats();
          }}
        />
      )}

      {showAdherentForm && (
        <AdherentForm
          adherent={null}
          onClose={() => {
            setShowAdherentForm(false);
            refreshStats();
          }}
        />
      )}

      {selectedTrajet && (
        <TrajetDetailModal
          trajet={selectedTrajet}
          onClose={() => setSelectedTrajet(null)}
          onUpdate={() => {
            fetchTrajetsRecents();
            refreshStats();
          }}
        />
      )}
    </div>
  );
}
