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

// Fetch all products
$stmt = db()->query("SELECT * FROM products ORDER BY nombre ASC");
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<div class="content-wrapper">
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1>Lista de Productos</h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item"><a href="dashboard.php">Home</a></li>
                        <li class="breadcrumb-item active">Productos</li>
                    </ol>
                </div>
            </div>
        </div>
    </section>

    <section class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-12">
                    <div class="card">
                        <div class="card-header">
                            <h3 class="card-title">Todos los productos</h3>
                            <div class="card-tools">
                                <a href="edit_product.php" class="btn btn-primary">Nuevo Producto</a>
                            </div>
                        </div>
                        <div class="card-body">
                            <table id="products-table" class="table table-bordered table-striped">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>Nombre</th>
                                        <th>SKU</th>
                                        <th>Costo</th>
                                        <th>Precio Venta</th>
                                        <th>Unidades Vendidas</th>
                                        <th>Ganancia/Unidad</th>
                                        <th>Ingreso Total</th>
                                        <th>Ganancia Total</th>
                                        <th>Acciones</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($products as $product): ?>
                                    <tr>
                                        <td><?php echo $product['id']; ?></td>
                                        <td><?php echo htmlspecialchars($product['nombre']); ?></td>
                                        <td><?php echo htmlspecialchars($product['sku']); ?></td>
                                        <td><?php echo htmlspecialchars($product['costo']); ?></td>
                                        <td><?php echo htmlspecialchars($product['precio_venta']); ?></td>
                                        <td><?php echo htmlspecialchars($product['unidades_vendidas']); ?></td>
                                        <td><?php echo htmlspecialchars($product['ganancia_unidad']); ?></td>
                                        <td><?php echo htmlspecialchars($product['ingreso_total']); ?></td>
                                        <td><?php echo htmlspecialchars($product['ganancia_total']); ?></td>
                                        <td>
                                            <div class="btn-group">
                                                <button type="button" class="btn btn-info btn-sm dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                                    Acciones
                                                </button>
                                                <div class="dropdown-menu">
                                                    <a class="dropdown-item" href="edit_product.php?id=<?php echo $product['id']; ?>">Editar</a>
                                                    <a class="dropdown-item" href="delete_product.php?id=<?php echo $product['id']; ?>" onclick="return confirm('¿Estás seguro de que quieres eliminar este producto?');">Eliminar</a>
                                                </div>
                                            </div>
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>

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

<script src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.11.3/js/dataTables.bootstrap4.min.js"></script>

<script>
$(function () {
    $("#products-table").DataTable({
        "responsive": true,
        "lengthChange": false,
        "autoWidth": false,
    });
});
</script>