<?php $__env->startSection('title', 'Reporte de Tickets'); ?>
<?php $__env->startSection('content'); ?>
<div class="flex items-center justify-between mb-6">
    <div class="flex items-center gap-3">
        <a href="<?php echo e(route('reports.index')); ?>" class="text-gray-400 hover:text-gray-600">
            <svg class="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"/>
            </svg>
        </a>
        <h1 class="text-2xl font-bold text-gray-900">Reporte de Tickets</h1>
    </div>
    <div class="flex gap-2">
        <a href="<?php echo e(route('reports.export.tickets.csv', ['from'=>$from,'to'=>$to,'agent'=>$agent])); ?>"
           class="inline-flex items-center px-3 py-2 bg-green-600 text-white text-sm font-medium rounded-lg hover:bg-green-700 transition">
            ↓ CSV
        </a>
        <a href="<?php echo e(route('reports.export.tickets.pdf', ['from'=>$from,'to'=>$to])); ?>"
           class="inline-flex items-center px-3 py-2 bg-red-600 text-white text-sm font-medium rounded-lg hover:bg-red-700 transition">
            ↓ PDF
        </a>
    </div>
</div>


<div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5 mb-6">
    <form method="GET" class="flex items-end gap-4 flex-wrap">
        <div>
            <label class="block text-xs font-medium text-gray-700 mb-1">Desde</label>
            <input type="date" name="from" value="<?php echo e($from); ?>"
                   class="rounded-lg border-gray-300 text-sm focus:ring-blue-500 focus:border-blue-500">
        </div>
        <div>
            <label class="block text-xs font-medium text-gray-700 mb-1">Hasta</label>
            <input type="date" name="to" value="<?php echo e($to); ?>"
                   class="rounded-lg border-gray-300 text-sm focus:ring-blue-500 focus:border-blue-500">
        </div>
        <div>
            <label class="block text-xs font-medium text-gray-700 mb-1">Agente</label>
            <select name="agent" class="rounded-lg border-gray-300 text-sm focus:ring-blue-500 focus:border-blue-500">
                <option value="">Todos los agentes</option>
                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__currentLoopData = $agents; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $a): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <option value="<?php echo e($a->id); ?>" <?php echo e($agent == $a->id ? 'selected' : ''); ?>><?php echo e($a->name); ?></option>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
            </select>
        </div>
        <button type="submit"
                class="px-4 py-2 bg-blue-600 text-white text-sm font-medium rounded-lg hover:bg-blue-700 transition">
            Aplicar filtros
        </button>
    </form>
</div>


<div class="grid grid-cols-2 md:grid-cols-6 gap-4 mb-6">
    <div class="bg-white rounded-xl border border-gray-200 p-4">
        <p class="text-xs text-gray-500 uppercase">Total</p>
        <p class="text-2xl font-bold text-gray-900 mt-1"><?php echo e($metrics['total']); ?></p>
    </div>
    <div class="bg-white rounded-xl border border-gray-200 p-4">
        <p class="text-xs text-gray-500 uppercase">Abiertos</p>
        <p class="text-2xl font-bold text-blue-600 mt-1"><?php echo e($metrics['open']); ?></p>
    </div>
    <div class="bg-white rounded-xl border border-gray-200 p-4">
        <p class="text-xs text-gray-500 uppercase">Cerrados</p>
        <p class="text-2xl font-bold text-green-600 mt-1"><?php echo e($metrics['closed']); ?></p>
    </div>
    <div class="bg-white rounded-xl border border-gray-200 p-4">
        <p class="text-xs text-gray-500 uppercase">SLA Cump.</p>
        <p class="text-2xl font-bold <?php echo e($slaCompliance >= 80 ? 'text-green-600' : 'text-red-600'); ?> mt-1"><?php echo e($slaCompliance); ?>%</p>
    </div>
    <div class="bg-white rounded-xl border border-gray-200 p-4">
        <p class="text-xs text-gray-500 uppercase">Resolución</p>
        <p class="text-2xl font-bold text-gray-900 mt-1">
            <?php echo e($metrics['avg_resolution'] ? $metrics['avg_resolution'].'h' : '—'); ?>

        </p>
    </div>
    <div class="bg-white rounded-xl border border-gray-200 p-4">
        <p class="text-xs text-gray-500 uppercase">Satisfacción</p>
        <p class="text-2xl font-bold text-yellow-500 mt-1">
            <?php echo e($metrics['satisfaction'] ? $metrics['satisfaction'].'/5' : '—'); ?>

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

<div class="grid grid-cols-1 lg:grid-cols-3 gap-6 mb-6">

    
    <div class="lg:col-span-2 bg-white rounded-xl shadow-sm border border-gray-200 p-5">
        <h3 class="text-sm font-semibold text-gray-900 mb-4">Tendencia diaria</h3>
        <?php $maxDaily = max(array_merge(array_column($daily, 'created'), array_column($daily, 'closed'), [1])) ?>
        <div class="overflow-x-auto">
            <div class="flex items-end gap-1 h-32 min-w-max">
                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__currentLoopData = $daily; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $d): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <div class="flex flex-col items-center gap-1 w-8">
                    <div class="w-full flex flex-col-reverse gap-0.5">
                        <div class="w-full bg-blue-400 rounded-t-sm"
                             style="height: <?php echo e($d['created'] > 0 ? max(4, ($d['created']/$maxDaily)*80) : 2); ?>px" title="Creados: <?php echo e($d['created']); ?>"></div>
                        <div class="w-full bg-green-400 rounded-t-sm"
                             style="height: <?php echo e($d['closed'] > 0 ? max(4, ($d['closed']/$maxDaily)*80) : 2); ?>px" title="Cerrados: <?php echo e($d['closed']); ?>"></div>
                    </div>
                    <span class="text-xs text-gray-400 text-center leading-tight" style="font-size:9px"><?php echo e($d['date']); ?></span>
                </div>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
            </div>
        </div>
        <div class="flex items-center gap-4 mt-3 text-xs text-gray-500">
            <span class="flex items-center gap-1"><span class="h-2 w-4 bg-blue-400 rounded inline-block"></span> Creados</span>
            <span class="flex items-center gap-1"><span class="h-2 w-4 bg-green-400 rounded inline-block"></span> Cerrados</span>
        </div>
    </div>

    
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
        <h3 class="text-sm font-semibold text-gray-900 mb-4">Por prioridad</h3>
        <div class="space-y-3">
            <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__currentLoopData = $byPriority; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $name => $data): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <?php $pct = $metrics['total'] > 0 ? round(($data['count']/$metrics['total'])*100) : 0 ?>
            <div>
                <div class="flex justify-between text-xs mb-1">
                    <span class="text-gray-600"><?php echo e($name); ?></span>
                    <span class="font-semibold" style="color: <?php echo e($data['color']); ?>"><?php echo e($data['count']); ?></span>
                </div>
                <div class="h-2 bg-gray-100 rounded-full overflow-hidden">
                    <div class="h-full rounded-full" style="width:<?php echo e($pct); ?>%; background-color: <?php echo e($data['color']); ?>"></div>
                </div>
            </div>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
        </div>
    </div>

</div>


<?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($byAgent->count() > 0): ?>
<div class="bg-white rounded-xl shadow-sm border border-gray-200 mb-6">
    <div class="px-6 py-4 border-b border-gray-200">
        <h3 class="text-sm font-semibold text-gray-900">Productividad por agente</h3>
    </div>
    <div class="overflow-x-auto">
        <table class="min-w-full divide-y divide-gray-200">
            <thead class="bg-gray-50">
                <tr>
                    <th class="px-6 py-3 text-left text-xs font-semibold text-gray-500 uppercase">Agente</th>
                    <th class="px-6 py-3 text-center text-xs font-semibold text-gray-500 uppercase">Total</th>
                    <th class="px-6 py-3 text-center text-xs font-semibold text-gray-500 uppercase">Resueltos</th>
                    <th class="px-6 py-3 text-center text-xs font-semibold text-gray-500 uppercase">SLA Breach</th>
                    <th class="px-6 py-3 text-center text-xs font-semibold text-gray-500 uppercase">Satisfacción</th>
                    <th class="px-6 py-3 text-center text-xs font-semibold text-gray-500 uppercase">% Resolución</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-200">
                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__currentLoopData = $byAgent; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $name => $data): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <tr class="hover:bg-gray-50">
                    <td class="px-6 py-3 text-sm font-medium text-gray-900"><?php echo e($name); ?></td>
                    <td class="px-6 py-3 text-center text-sm text-gray-700"><?php echo e($data['total']); ?></td>
                    <td class="px-6 py-3 text-center text-sm text-green-600 font-medium"><?php echo e($data['closed']); ?></td>
                    <td class="px-6 py-3 text-center text-sm <?php echo e($data['breached'] > 0 ? 'text-red-600 font-medium' : 'text-gray-400'); ?>">
                        <?php echo e($data['breached'] ?: '—'); ?>

                    </td>
                    <td class="px-6 py-3 text-center text-sm text-yellow-600">
                        <?php echo e($data['avg_sat'] ? $data['avg_sat'].'/5' : '—'); ?>

                    </td>
                    <td class="px-6 py-3 text-center text-sm">
                        <?php $pct = $data['total'] > 0 ? round(($data['closed']/$data['total'])*100) : 0 ?>
                        <div class="flex items-center gap-2">
                            <div class="flex-1 h-1.5 bg-gray-100 rounded-full">
                                <div class="h-full bg-blue-500 rounded-full" style="width:<?php echo e($pct); ?>%"></div>
                            </div>
                            <span class="text-xs text-gray-600 w-8"><?php echo e($pct); ?>%</span>
                        </div>
                    </td>
                </tr>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>


<div class="bg-white rounded-xl shadow-sm border border-gray-200">
    <div class="px-6 py-4 border-b border-gray-200 flex items-center justify-between">
        <h3 class="text-sm font-semibold text-gray-900">Detalle de tickets (<?php echo e($tickets->count()); ?>)</h3>
    </div>
    <div class="overflow-x-auto">
        <table class="min-w-full divide-y divide-gray-200">
            <thead class="bg-gray-50">
                <tr>
                    <th class="px-4 py-3 text-left text-xs font-semibold text-gray-500 uppercase">Número</th>
                    <th class="px-4 py-3 text-left text-xs font-semibold text-gray-500 uppercase">Título</th>
                    <th class="px-4 py-3 text-left text-xs font-semibold text-gray-500 uppercase">Estado</th>
                    <th class="px-4 py-3 text-left text-xs font-semibold text-gray-500 uppercase">Prioridad</th>
                    <th class="px-4 py-3 text-left text-xs font-semibold text-gray-500 uppercase">Agente</th>
                    <th class="px-4 py-3 text-center text-xs font-semibold text-gray-500 uppercase">SLA</th>
                    <th class="px-4 py-3 text-left text-xs font-semibold text-gray-500 uppercase">Creado</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-200">
                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__empty_1 = true; $__currentLoopData = $tickets->take(50); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $ticket): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                <tr class="hover:bg-gray-50">
                    <td class="px-4 py-3">
                        <a href="<?php echo e(route('tickets.show', $ticket)); ?>"
                           class="text-sm font-mono text-blue-600 hover:underline"><?php echo e($ticket->number); ?></a>
                    </td>
                    <td class="px-4 py-3 text-sm text-gray-800 max-w-xs truncate"><?php echo e($ticket->title); ?></td>
                    <td class="px-4 py-3">
                        <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium"
                              style="background-color: <?php echo e($ticket->status->color); ?>20; color: <?php echo e($ticket->status->color); ?>">
                            <?php echo e($ticket->status->name); ?>

                        </span>
                    </td>
                    <td class="px-4 py-3">
                        <span class="text-xs font-medium" style="color: <?php echo e($ticket->priority->color); ?>">
                            <?php echo e($ticket->priority->name); ?>

                        </span>
                    </td>
                    <td class="px-4 py-3 text-sm text-gray-600"><?php echo e($ticket->assignee?->name ?? '—'); ?></td>
                    <td class="px-4 py-3 text-center">
                        <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($ticket->sla_resolution_breached): ?>
                        <span class="text-xs text-red-500 font-medium">⚠ Breach</span>
                        <?php else: ?>
                        <span class="text-xs text-green-500">✓</span>
                        <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                    </td>
                    <td class="px-4 py-3 text-xs text-gray-500"><?php echo e($ticket->created_at->format('d/m/y H:i')); ?></td>
                </tr>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                <tr><td colspan="7" class="px-4 py-8 text-center text-gray-400 text-sm">Sin tickets en este período.</td></tr>
                <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
            </tbody>
        </table>
    </div>
    <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($tickets->count() > 50): ?>
    <div class="px-6 py-3 border-t border-gray-200 text-xs text-gray-400 text-center">
        Mostrando primeros 50 de <?php echo e($tickets->count()); ?>. Usa exportar CSV para ver todos.
    </div>
    <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
</div>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /home/evolucionamos/public_html/fedef/resources/views/reports/tickets.blade.php ENDPATH**/ ?>