<?php
/**
 * ICYLAB OS - Módulo de Producción
 * Vista de entrada normalizada para SPA.
 */
require_once '../../core/config.php';
require_once '../../core/session.php';
require_once '../../core/permissions.php';
require_once '../../core/database.php';
require_once '../../core/format.php';

require_login();
require_permission('module.operations');

$page_title = 'Producción';

$pdo = db();
$activeProducts = 0;
$trackedStock = 0.0;
$lowStock = 0;

try {
    $stmt = $pdo->query("SELECT COUNT(*) AS total, COALESCE(SUM(stock), 0) AS stock_total, SUM(CASE WHEN stock <= 0 THEN 1 ELSE 0 END) AS low_stock FROM products WHERE is_active = 1");
    $summary = $stmt->fetch(PDO::FETCH_ASSOC) ?: [];
    $activeProducts = (int)($summary['total'] ?? 0);
    $trackedStock = (float)($summary['stock_total'] ?? 0);
    $lowStock = (int)($summary['low_stock'] ?? 0);
} catch (Throwable $e) {
    $activeProducts = 0;
    $trackedStock = 0.0;
    $lowStock = 0;
}

require_once '../../layouts/header.php';
require_once '../../layouts/sidebar.php';
?>

<main class="app-main no-scroll-main" id="operations-main">
    <?php require_once '../../layouts/topbar.php'; ?>

    <section class="content">
        <div class="page-header">
            <div class="page-header-content">
                <span class="label">Operación</span>
                <h1>Producción</h1>
                <p class="text-secondary">Panel base para planificar lotes, controlar avance y conectar producción con inventario.</p>
            </div>

            <div class="cluster actions-cluster page-header-actions">
                <a class="btn btn-outline" href="/modules/stock/">
                    <i data-lucide="boxes"></i>
                    <span>Ver stock</span>
                </a>
                <a class="btn btn-primary" href="/modules/products/">
                    <i data-lucide="package-plus"></i>
                    <span>Productos</span>
                </a>
            </div>
        </div>

        <section class="grid grid-3 mb-4">
            <article class="kpi-card">
                <div class="kpi-label">Productos activos</div>
                <div class="kpi-value"><?php echo number_format($activeProducts, 0, ',', '.'); ?></div>
                <div class="kpi-subtitle">Catálogo disponible para producción.</div>
            </article>
            <article class="kpi-card">
                <div class="kpi-label">Stock registrado</div>
                <div class="kpi-value"><?php echo number_format($trackedStock, 0, ',', '.'); ?></div>
                <div class="kpi-subtitle">Unidades acumuladas en productos activos.</div>
            </article>
            <article class="kpi-card">
                <div class="kpi-label">Sin stock</div>
                <div class="kpi-value"><?php echo number_format($lowStock, 0, ',', '.'); ?></div>
                <div class="kpi-subtitle">SKU que requieren revisión operacional.</div>
            </article>
        </section>

        <section class="card card-table-container">
            <div class="card-header">
                <div>
                    <h2>Tablero operativo</h2>
                    <p class="text-secondary">Estructura preparada para órdenes, lotes y estados de producción.</p>
                </div>
            </div>

            <div class="table-wrap table-container-scroll operations-list-scroll">
                <table class="table">
                    <thead>
                        <tr>
                            <th>Flujo</th>
                            <th>Estado</th>
                            <th>Responsable</th>
                            <th>Próxima acción</th>
                            <th>Acciones</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>
                                <strong>Planificación de producción</strong><br>
                                <span class="text-muted">Definición de lotes y recursos.</span>
                            </td>
                            <td><span class="badge badge-soft">Preparado</span></td>
                            <td>Operaciones</td>
                            <td>Configurar órdenes productivas.</td>
                            <td>
                                <div class="row-actions">
                                    <a class="btn btn-sm btn-outline" href="/modules/products/">
                                        <i data-lucide="package"></i>
                                        <span>Catálogo</span>
                                    </a>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <strong>Control de stock</strong><br>
                                <span class="text-muted">Validación de disponibilidad por SKU.</span>
                            </td>
                            <td><span class="badge badge-soft">Activo</span></td>
                            <td>Operaciones</td>
                            <td>Revisar productos sin stock.</td>
                            <td>
                                <div class="row-actions">
                                    <a class="btn btn-sm btn-outline" href="/modules/stock/">
                                        <i data-lucide="boxes"></i>
                                        <span>Stock</span>
                                    </a>
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </section>
    </section>
</main>

<?php require_once '../../layouts/footer.php'; ?>
