Current File : //var/www/pharmacius/view/productos_view.php
<?php
require_once("menu.php");

// Si el usuario no ha iniciado sesión, mostrar mensaje de aviso y detener la ejecución
if (!isset($_SESSION['nombre'])): ?>
    <div class="container d-flex justify-content-center align-items-center" style="min-height: 70vh;">
        <div class="card shadow p-4" style="max-width: 500px; width: 100%;">
            <h2 class="text-center mb-3" style="color: #00bcd4;">Bienvenido a PFarma</h2>
            <p class="text-center mb-4">Para acceder al listado de productos, es necesario iniciar sesión con tu cuenta.</p>
            <div class="d-flex justify-content-center">
                <a href="index.php?controlador=usuarios&action=login" class="btn btn-info text-white">
                    Iniciar sesión
                </a>
            </div>
        </div>
    </div>
<?php return; endif;

// ---------------------- PAGINACIÓN ----------------------
$productosPorPagina = 40;
$paginaActual = isset($_GET['pagina']) ? (int)$_GET['pagina'] : 1;
$inicio = ($paginaActual - 1) * $productosPorPagina;
$totalProductos = count($productos);
$totalPaginas = ceil($totalProductos / $productosPorPagina);
$productosPagina = array_slice($productos, $inicio, $productosPorPagina);
?>

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Lista de Productos</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">
</head>
<body class="m-0 p-0">

<!-- Barra de búsqueda -->
<div class="container my-4">
    <form class="d-flex" action="index.php" method="GET">
        <input type="hidden" name="controlador" value="productos">
        <input type="hidden" name="action" value="home">

        <select class="form-control me-2" name="search_type">
            <option value="producto" <?= isset($_GET['search_type']) && $_GET['search_type'] == 'producto' ? 'selected' : '' ?>>Buscar por Producto</option>
            <option value="marca" <?= isset($_GET['search_type']) && $_GET['search_type'] == 'marca' ? 'selected' : '' ?>>Buscar por Marca</option>
            <option value="categoria" <?= isset($_GET['search_type']) && $_GET['search_type'] == 'categoria' ? 'selected' : '' ?>>Buscar por Categoría</option>
            <option value="referencia" <?= isset($_GET['search_type']) && $_GET['search_type'] == 'referencia' ? 'selected' : '' ?>>Buscar por Referencia</option>
        </select>

        <input class="form-control me-2" type="search" placeholder="Buscar" aria-label="Buscar" name="search" value="<?= htmlspecialchars($_GET['search'] ?? '') ?>">

        <button class="btn btn-outline-success me-2" type="submit">
            <i class="bi bi-search"></i> Buscar
        </button>

        <a href="index.php?controlador=productos&action=home" class="btn btn-outline-secondary">
            <i class="bi bi-arrow-counterclockwise"></i> Reset
        </a>
    </form>
</div>

<!-- Tabla de productos -->
<div class="table-responsive">
    <table class="table table-bordered w-100">
        <thead class="table-light">
            <tr>
                <?php if (isset($_SESSION['nombre'])): ?>
                    <th>Acciones</th>
                <?php endif; ?>
                <th>Imagen</th>
                <th>IDProducto</th>
                <th>Referencia</th>
                <th>Nombre</th>
                <th>Enlace</th>
                <th>EAN13</th>
                <th>Precio Sin Impuestos</th>
                <th>Precio Con Impuestos</th>
                <th>Precio Mayorista</th>
                <th>IDFiscal</th>
                <th>Nombre Fiscal</th>
                <th>Categoría</th>
                <th>IDMarca</th>
                <th>Peso</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($productosPagina as $item): ?>
                <tr>
                    <?php if (isset($_SESSION['nombre'])): ?>
                        <td>
                            <a href="editar.php?IDProducto=<?= htmlspecialchars($item['IDProducto']) ?>" class="btn btn-sm btn-warning" title="Editar">
                                <i class="bi bi-pencil-square"></i>
                            </a>
                           <form action="index.php" method="POST" style="display: inline;">
    <input type="hidden" name="controlador" value="productos"> <!-- ¡NECESARIO! -->
    <input type="hidden" name="action" value="eliminar">       <!-- ¡NECESARIO! -->
    <input type="hidden" name="IDProducto" value="<?= htmlspecialchars($item['IDProducto']) ?>">
    <button class="btn btn-sm btn-danger" type="submit" title="Eliminar">
        <i class="bi bi-trash"></i>
    </button>
</form>

                        </td>
                    <?php endif; ?>

                    <td><img src="<?= htmlspecialchars($item['URLImagen']) ?>" alt="Imagen del producto" width="50"></td>
                    <td><?= htmlspecialchars($item['IDProducto']) ?></td>
                    <td><?= htmlspecialchars($item['Referencia']) ?></td>
                    <td><?= htmlspecialchars($item['Nombre']) ?></td>
                    <td><a href="<?= htmlspecialchars($item['Enlace']) ?>" target="_blank">Ver Producto</a></td>
                    <td><?= htmlspecialchars($item['EAN13']) ?></td>
                    <td><?= number_format($item['PrecioSinImpuestos'], 2) ?> €</td>
                    <td><?= number_format($item['PrecioConImpuestos'], 2) ?> €</td>
                    <td><?= number_format($item['PrecioMayorista'], 2) ?> €</td>
                    <td><?= htmlspecialchars($item['IDFiscal']) ?></td>
                    <td><?= htmlspecialchars($item['NombreFiscal']) ?></td>

                    <td>
                        <ul class="mb-0 ps-3">
                            <?php foreach (explode(',', $item['categorias']) as $categoria): ?>
                                <li><?= htmlspecialchars(trim($categoria)) ?></li>
                            <?php endforeach; ?>
                        </ul>
                    </td>
                    <td><?= htmlspecialchars($item['IDMarca']) ?></td>
                    <td><?= htmlspecialchars($item['Peso']) ?> kg</td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

<!-- Navegación de páginas -->
<div class="container my-4 d-flex justify-content-center">
    <nav>
        <ul class="pagination">
    <?php
    $query = $_GET;
    unset($query['pagina']);
    $urlBase = 'index.php?' . http_build_query($query) . '&pagina=';

    if ($paginaActual > 1): ?>
        <li class="page-item">
            <a class="page-link" href="<?= $urlBase . ($paginaActual - 1) ?>">Anterior</a>
        </li>
    <?php endif;

    $mostrarPaginas = 5;
    $inicio = max(1, $paginaActual - floor($mostrarPaginas / 2));
    $fin = min($totalPaginas, $inicio + $mostrarPaginas - 1);

    if ($inicio > 1): ?>
        <li class="page-item">
            <a class="page-link" href="<?= $urlBase ?>1">1</a>
        </li>
        <?php if ($inicio > 2): ?>
            <li class="page-item disabled"><span class="page-link">...</span></li>
        <?php endif;
    endif;

    for ($i = $inicio; $i <= $fin; $i++): ?>
        <li class="page-item <?= $i == $paginaActual ? 'active' : '' ?>">
            <a class="page-link" href="<?= $urlBase . $i ?>"><?= $i ?></a>
        </li>
    <?php endfor;

    if ($fin < $totalPaginas): 
        if ($fin < $totalPaginas - 1): ?>
            <li class="page-item disabled"><span class="page-link">...</span></li>
        <?php endif; ?>
        <li class="page-item">
            <a class="page-link" href="<?= $urlBase . $totalPaginas ?>"><?= $totalPaginas ?></a>
        </li>
    <?php endif;

    if ($paginaActual < $totalPaginas): ?>
        <li class="page-item">
            <a class="page-link" href="<?= $urlBase . ($paginaActual + 1) ?>">Siguiente</a>
        </li>
    <?php endif; ?>
</ul>

    </nav>
</div>

<!-- Mensaje informativo tras acciones (insertar/eliminar) -->
<?php if (isset($_SESSION['message'])): ?>
    <div class="alert alert-info">
        <?= $_SESSION['message']; ?>
    </div>
    <?php unset($_SESSION['message']); ?>
<?php endif; ?>

</body>
</html>