<?= $this->extend('layouts/main_layout') ?>
<?= $this->section('content') ?>

<div class="flex items-center justify-between mb-6">
    <div>
        <h1 class="text-2xl font-bold text-gray-800">
            <i class="bi bi-search"></i> Resultados de búsqueda
        </h1>
        <p class="text-gray-500 text-sm mt-1">
            <?= count($products) + count($sales) + count($users) ?> resultado(s) para "<strong><?= esc($query) ?></strong>"
        </p>
    </div>
    <a href="<?= site_url('dashboard') ?>" class="bg-gray-200 hover:bg-gray-300 text-gray-700 px-4 py-2 rounded-lg text-sm font-medium no-underline transition inline-flex items-center gap-2">
        <i class="bi bi-arrow-left"></i> Volver
    </a>
</div>

<?php if (count($products) > 0): ?>
<div class="bg-slate-100 rounded-xl shadow-sm border border-gray-100 p-5 mb-6">
    <h5 class="font-semibold text-gray-800 flex items-center gap-2 mb-4">
        <i class="bi bi-box-seam text-[#EBDF00]"></i> Productos (<?= count($products) ?>)
    </h5>
    <div class="overflow-x-auto">
        <table class="w-full text-sm">
            <thead>
                <tr class="text-left text-gray-500 border-b">
                    <th class="pb-2 font-medium">Código</th>
                    <th class="pb-2 font-medium">Nombre</th>
                    <th class="pb-2 font-medium">Categoría</th>
                    <th class="pb-2 font-medium">Stock</th>
                    <th class="pb-2 font-medium">Precio Venta</th>
                    <th class="pb-2 font-medium"></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($products as $p): ?>
                <tr class="border-b border-gray-50 hover:bg-gray-50">
                    <td class="py-2.5 text-gray-600"><?= esc($p['codigo_unico']) ?></td>
                    <td class="py-2.5 font-medium"><?= esc($p['nombre']) ?></td>
                    <td class="py-2.5"><span class="bg-teal-100 text-teal-700 text-xs px-2 py-0.5 rounded-full"><?= esc($p['categoria_nombre']) ?></span></td>
                    <td class="py-2.5">
                        <span class="<?= $p['stock_actual'] <= 5 ? 'text-red-600 font-semibold' : 'text-gray-600' ?>"><?= $p['stock_actual'] ?></span>
                    </td>
                    <td class="py-2.5 font-semibold">S/ <?= number_format($p['precio_venta'], 2) ?></td>
                    <td class="py-2.5">
                        <a href="<?= site_url('products/' . $p['id_producto'] . '/edit') ?>" class="text-[#EBDF00] hover:text-[#D4C800]"><i class="bi bi-pencil"></i></a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<?php if (count($sales) > 0): ?>
<div class="bg-slate-100 rounded-xl shadow-sm border border-gray-100 p-5 mb-6">
    <h5 class="font-semibold text-gray-800 flex items-center gap-2 mb-4">
        <i class="bi bi-cart text-emerald-500"></i> Ventas (<?= count($sales) ?>)
    </h5>
    <div class="overflow-x-auto">
        <table class="w-full text-sm">
            <thead>
                <tr class="text-left text-gray-500 border-b">
                    <th class="pb-2 font-medium">#</th>
                    <th class="pb-2 font-medium">Fecha</th>
                    <th class="pb-2 font-medium">Vendedor</th>
                    <th class="pb-2 font-medium">Total</th>
                    <th class="pb-2 font-medium"></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($sales as $s): ?>
                <tr class="border-b border-gray-50 hover:bg-gray-50">
                    <td class="py-2.5 font-medium">#<?= $s['id_venta'] ?></td>
                    <td class="py-2.5 text-gray-600"><?= date('d/m/Y H:i', strtotime($s['fecha'])) ?></td>
                    <td class="py-2.5"><?= esc($s['username']) ?></td>
                    <td class="py-2.5 font-semibold text-emerald-600">S/ <?= number_format($s['total'], 2) ?></td>
                    <td class="py-2.5">
                        <a href="<?= site_url('sales/' . $s['id_venta']) ?>" class="text-[#EBDF00] hover:text-[#D4C800]"><i class="bi bi-eye"></i></a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<?php if (count($users) > 0): ?>
<div class="bg-slate-100 rounded-xl shadow-sm border border-gray-100 p-5 mb-6">
    <h5 class="font-semibold text-gray-800 flex items-center gap-2 mb-4">
        <i class="bi bi-people text-violet-500"></i> Usuarios (<?= count($users) ?>)
    </h5>
    <div class="overflow-x-auto">
        <table class="w-full text-sm">
            <thead>
                <tr class="text-left text-gray-500 border-b">
                    <th class="pb-2 font-medium">Usuario</th>
                    <th class="pb-2 font-medium">Email</th>
                    <th class="pb-2 font-medium">Rol</th>
                    <th class="pb-2 font-medium"></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($users as $u): ?>
                <tr class="border-b border-gray-50 hover:bg-gray-50">
                    <td class="py-2.5 font-medium"><?= esc($u['username']) ?></td>
                    <td class="py-2.5 text-gray-600"><?= esc($u['email']) ?></td>
                    <td class="py-2.5"><span class="bg-<?= $u['id_rol'] == 1 ? 'teal' : 'teal' ?>-100 text-<?= $u['id_rol'] == 1 ? 'teal' : 'teal' ?>-700 text-xs px-2 py-0.5 rounded-full"><?= esc($u['nombre_rol']) ?></span></td>
                    <td class="py-2.5">
                        <a href="<?= site_url('users/' . $u['id_usuario'] . '/edit') ?>" class="text-[#EBDF00] hover:text-[#D4C800]"><i class="bi bi-pencil"></i></a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<?php if (count($products) + count($sales) + count($users) == 0): ?>
<div class="bg-slate-100 rounded-xl shadow-sm border border-gray-100 p-12 text-center">
    <i class="bi bi-search text-5xl text-gray-300"></i>
    <h3 class="text-xl font-semibold text-gray-600 mt-4">Sin resultados</h3>
    <p class="text-gray-400 mt-1">No se encontraron productos, ventas o usuarios para "<strong><?= esc($query) ?></strong>"</p>
</div>
<?php endif; ?>

<?= $this->endSection() ?>
