<?php
session_start();
if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit;
}
require_once 'layout_header.php';
require_once 'db/config.php';
$pdo = db();
?>

<div class="container-fluid mt-4">
    <h1>Panel de Inventario</h1>

    <ul class="nav nav-tabs" id="inventoryTabs" role="tablist">
        <li class="nav-item" role="presentation">
            <a class="nav-link active" id="general-tab" data-bs-toggle="tab" href="#general" role="tab" aria-controls="general" aria-selected="true">Inventario General</a>
        </li>
        <li class="nav-item" role="presentation">
            <a class="nav-link" id="cobertura-tab" data-bs-toggle="tab" href="#cobertura" role="tab" aria-controls="cobertura" aria-selected="false">Cobertura</a>
        </li>
        <li class="nav-item" role="presentation">
            <a class="nav-link" id="cobertura-xpress-tab" data-bs-toggle="tab" href="#cobertura-xpress" role="tab" aria-controls="cobertura-xpress" aria-selected="false">Cobertura Xpress</a>
        </li>
    </ul>

    <div class="tab-content" id="inventoryTabsContent">
        <div class="tab-pane fade show active" id="inventario" role="tabpanel" aria-labelledby="inventario-tab">
            <?php
            // Obtener productos
            $stmt = $pdo->query("SELECT * FROM products ORDER BY order_position ASC");
            $products = $stmt->fetchAll(PDO::FETCH_ASSOC);
            ?>
            <div class="d-flex justify-content-between align-items-center mb-3">
                <h2 class="m-0">Inventario General</h2>
                <a href="edit_product.php" class="btn btn-primary">Añadir Producto</a>
            </div>
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Nombre</th>
                        <th>SKU</th>
                        <th>Precio</th>
                        <th>Acciones</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($products as $product): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($product['id']); ?></td>
                            <td><?php echo htmlspecialchars($product['nombre']); ?></td>
                            <td><?php echo htmlspecialchars($product['sku']); ?></td>
                            <td>S/ <?php echo htmlspecialchars(number_format($product['precio'], 2)); ?></td>
                            <td>
                                <a href="edit_product.php?id=<?php echo $product['id']; ?>" class="btn btn-sm btn-warning">Editar</a>
                                <a href="delete_product.php?id=<?php echo $product['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('¿Estás seguro de que quieres eliminar este producto?');">Eliminar</a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <div id="cobertura" class="tab-pane fade">
                <?php
                // Restaurando la pestaña Cobertura
                $cobertura_banner = 'assets/uploads/cobertura_banner.jpg';
                if (file_exists($cobertura_banner)) {
                    echo "<img src='{$cobertura_banner}?v=" . time() . "' alt='Banner Cobertura' class='img-fluid mb-3'>";
                }
                ?>
                <form action="save_cobertura_banner.php" method="post" enctype="multipart/form-data" class="mb-3">
                    <div class="form-group">
                        <label for="cobertura_banner_input">Cambiar Banner de Cobertura (JPG)</label>
                        <input type="file" name="cobertura_banner" id="cobertura_banner_input" class="form-control" accept=".jpg">
                    </div>
                    <button type="submit" class="btn btn-primary">Subir Banner</button>
                </form>

                <a href="add_cobertura.php" class="btn btn-success mb-3">Agregar Nueva Cobertura</a>

                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th>Imagen</th>
                            <th>Título</th>
                            <th>Descripción</th>
                            <th>Acciones</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $stmt = $pdo->query("SELECT * FROM cobertura ORDER BY id DESC");
                        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                            echo "<tr>";
                            $image_path = 'assets/uploads/cobertura_images/' . $row['imagen'];
                            if (file_exists($image_path)) {
                                echo "<td><img src='{$image_path}?v=".time()."' alt='Imagen de cobertura' style='width: 100px;'></td>";
                            } else {
                                echo "<td><span class='text-danger'>Imagen no encontrada</span></td>";
                            }
                            echo "<td>" . htmlspecialchars($row['titulo']) . "</td>";
                            echo "<td>" . htmlspecialchars($row['descripcion']) . "</td>";
                            echo "<td>";
                            echo "<a href='delete_cobertura.php?id=" . $row['id'] . "' class='btn btn-danger btn-sm' onclick='return confirm("¿Estás seguro de que quieres eliminar esta cobertura?");'>Eliminar</a>";
                            echo "</td>";
                            echo "</tr>";
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        <div class="tab-pane fade" id="cobertura-xpress" role="tabpanel" aria-labelledby="cobertura-xpress-tab">
            <p class="mt-3">Aquí irá la configuración de cobertura xpress.</p>
        </div>
    </div>
</div>

<?php
require_once 'layout_footer.php';
?>
