<script setup>
import { ref, computed } from 'vue';
import { Link, usePage } from '@inertiajs/vue3';
import AppLayout from '@/Layouts/AppLayout.vue';
import CarTimeline from '@/Components/CarTimeline.vue';

const props = defineProps({
  title: String,
  stats: Object,
  casos_recientes: Array,
  aseguradoras: Array
});

const page = usePage();
const searchQuery = ref('');
const selectedEstado = ref('');

const estadoColores = {
  pendiente_coordinacion: { bg: 'bg-slate-100', text: 'text-slate-700', label: 'Pendiente' },
  coordinador: { bg: 'bg-blue-100', text: 'text-blue-700', label: 'Coordinado' },
  mesa_compras: { bg: 'bg-amber-100', text: 'text-amber-700', label: 'Mesa Compras' },
  en_reparacion: { bg: 'bg-rose-100', text: 'text-rose-700', label: 'En Reparación' },
  analisis: { bg: 'bg-purple-100', text: 'text-purple-700', label: 'Análisis' },
  facturacion: { bg: 'bg-cyan-100', text: 'text-cyan-700', label: 'Facturación' },
  finalizado: { bg: 'bg-emerald-100', text: 'text-emerald-700', label: 'Finalizado' },
};

const colorClasses = {
  blue: 'bg-blue-50 text-blue-600',
  amber: 'bg-amber-50 text-amber-600',
  rose: 'bg-rose-50 text-rose-600',
  emerald: 'bg-emerald-50 text-emerald-600',
};

const statIcons = {
  casos_activos: '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',
  en_reparacion: 'M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z',
  pendientes: 'M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z',
  finalizados: 'M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z',
};

const statColors = {
  casos_activos: 'blue',
  en_reparacion: 'amber',
  pendientes: 'rose',
  finalizados: 'emerald',
};

</script>

<template>
  <AppLayout :title="title">
    <div class="space-y-8">

      <!-- Aseguradoras -->
      <div v-if="aseguradoras.length > 0" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
        <div 
          v-for="aseguradora in aseguradoras" 
          :key="aseguradora.id"
          class="bg-gradient-to-br from-white to-slate-50 rounded-2xl p-6 border border-slate-100 hover:shadow-lg transition-all duration-300 cursor-pointer"
        >
          <div class="flex items-center gap-4">
            <div class="w-3 h-3 rounded-full bg-blue-600"></div>
            <span class="font-semibold text-slate-800">{{ aseguradora.nombre }}</span>
          </div>
          <p class="mt-4 text-3xl font-bold text-slate-800">{{ aseguradora.casos_count }}</p>
          <p class="text-sm text-slate-500">casos activos</p>
        </div>
      </div>

      <!-- Table -->
      <div class="bg-white rounded-2xl shadow-sm overflow-hidden">
        <div class="p-6 border-b border-slate-100 flex justify-between items-center">
          <h2 class="text-lg font-bold text-slate-800">Casos Recientes</h2>
          <Link :href="route('casos.index')" class="text-primary-600 hover:underline text-sm">Ver todos →</Link>
        </div>
        <div class="overflow-x-auto hide-scrollbar">
          <table class="w-full block md:table">
            <thead class="hidden md:table-header-group bg-slate-50">
              <tr>
                <th class="px-6 py-4 text-left text-sm font-semibold text-slate-600">Número</th>
                <th class="px-6 py-4 text-left text-sm font-semibold text-slate-600">Placa</th>
                <th class="px-6 py-4 text-left text-sm font-semibold text-slate-600">Cliente</th>
                <th class="px-6 py-4 text-left text-sm font-semibold text-slate-600">Aseguradora</th>
                <th class="px-6 py-4 text-left text-sm font-semibold text-slate-600">Estado</th>
                <th class="px-6 py-4 text-right text-sm font-semibold text-slate-600"></th>
              </tr>
            </thead>
            <tbody class="block md:table-row-group divide-y md:divide-slate-100 p-4 md:p-0 space-y-4 md:space-y-0 bg-slate-50/50 md:bg-white">
              <tr 
                v-for="caso in casos_recientes" 
                :key="caso.id"
                class="block md:table-row bg-white rounded-2xl md:rounded-none shadow-sm md:shadow-none border border-slate-100 md:border-0 hover:bg-slate-50/50 transition-colors"
              >
                <!-- Mobile Card View -->
                <td class="block md:hidden p-5">
                   <div class="flex justify-between items-center mb-3">
                     <span class="font-black text-slate-800 tracking-tight">{{ caso.numero_reclamo }}</span>
                     <span 
                        :class="[
                        'px-2.5 py-1 text-[9px] font-bold rounded-lg uppercase tracking-widest shadow-sm',
                        estadoColores[caso.estado_actual]?.bg || 'bg-slate-100',
                        estadoColores[caso.estado_actual]?.text || 'text-slate-700'
                        ]"
                     >
                       {{ caso.estado_actual?.replace('_', ' ') || 'N/A' }}
                     </span>
                   </div>
                   <div class="flex justify-between items-center bg-slate-50 p-3 rounded-xl border border-slate-100 mb-3">
                     <div class="flex flex-col">
                       <span class="text-xs font-bold text-slate-700 truncate max-w-[150px]">{{ caso.cliente?.nombre || 'N/A' }}</span>
                       <span class="text-[10px] text-slate-500 font-semibold mt-0.5 truncate max-w-[150px]">{{ caso.aseguradora?.nombre || 'N/A' }}</span>
                     </div>
                     <span class="bg-slate-800 text-white text-[10px] px-2.5 py-1.5 rounded-lg font-bold shadow-sm whitespace-nowrap">{{ caso.vehiculo?.placa || 'N/A' }}</span>
                   </div>
                   <CarTimeline :caso="caso" class="mt-2 mb-4" />
                   <div class="border-t border-slate-100 pt-4 mt-2">
                     <Link :href="route('casos.show', caso.id)" class="flex justify-center items-center gap-2 w-full py-2.5 bg-blue-50 text-blue-600 font-bold text-[10px] uppercase tracking-widest rounded-xl hover:bg-blue-100">
                        Detalles
                     </Link>
                   </div>
                </td>

                <td class="hidden md:table-cell px-6 py-4">
                  <span class="font-medium text-slate-800">{{ caso.numero_reclamo }}</span>
                </td>
                <td class="hidden md:table-cell px-6 py-4">
                  <span class="px-3 py-1.5 rounded-lg bg-slate-100 text-slate-700 font-bold text-xs whitespace-nowrap">
                    {{ caso.vehiculo?.placa || 'N/A' }}
                  </span>
                </td>
                <td class="hidden md:table-cell px-6 py-4 text-slate-600 max-w-[150px] truncate">{{ caso.cliente?.nombre || 'N/A' }}</td>
                <td class="hidden md:table-cell px-6 py-4 text-slate-600">{{ caso.aseguradora?.nombre || 'N/A' }}</td>
                <td class="hidden md:table-cell px-6 py-4">
                  <div class="flex flex-col gap-2">
                    <span 
                        :class="[
                        'px-3 py-1 rounded-full text-xs font-medium w-max',
                        estadoColores[caso.estado_actual]?.bg || 'bg-slate-100',
                        estadoColores[caso.estado_actual]?.text || 'text-slate-700'
                        ]"
                    >
                        {{ caso.estado_actual?.replace('_', ' ') || 'N/A' }}
                    </span>
                    <CarTimeline :caso="caso" class="mt-2" />
                  </div>
                </td>
                <td class="hidden md:table-cell px-6 py-4 text-right">
                  <Link :href="route('casos.show', caso.id)" class="p-2 rounded-lg hover:bg-slate-100 transition-colors inline-block">
                    <svg class="w-5 h-5 text-slate-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
                    </svg>
                  </Link>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </AppLayout>
</template>
