<?php require __DIR__ . '/../../includes/navBar.php'; ?>

<div class="container my-5">

    <div class="card card-rounded card-shadow border-0">

        <!-- HEADER -->

        <div class="card-header header-dark px-4 py-3">

            <div class="d-flex justify-content-between align-items-center flex-wrap gap-3">

                <h4 class="mb-0 title-shadow">
                    <i class="fas fa-box-open icon-xl me-2"></i>
                    Selección de Insumos
                </h4>

                <div class="d-flex gap-2">

                    <!-- BOTÓN FANTASMA (mantiene el layout) -->

                    <a class="btn btn-success invisible">
                        <i class="fas fa-plus me-1"></i>
                    </a>

                    <a href="?opt=actualizar_servicio&id=<?= $idServicio ?>" class="btn btn-soft-secondary text-white">
                        <i class="fas fa-arrow-left me-1"></i>
                        Volver
                    </a>

                </div>
            </div>
        </div>

        <!-- BODY -->

        <div class="card-body px-4 py-4">

            <?php if (empty($insumos)): ?>
                <div class="text-center text-muted py-5">
                    No hay insumos registrados.
                </div>
            <?php else: ?>

                <!-- FILTRO POR COLUMNA + BUSCADOR -->

                <!-- Input de búsqueda añadido automáticamente por DataTables -->

                <div class="d-flex justify-content-between align-items-center mb-1">

                    <div id="filtrosIzquierda" class="d-flex align-items-center gap-2">
                        
                        <select id="columnFilter"
                                class="form-select form-select-sm"
                                style="width:auto;">
                            <option value="">Todas las columnas</option>
                            <option value="0">ID</option>
                            <option value="1">Marca</option>
                            <option value="2">Modelo</option>
                        </select>
                    </div>

                </div>

                <form method="POST" action="index.php?opt=agregar_insumo_caja&id=<?= $idServicio ?>" novalidate>

                    <!-- CAMPO OCULTO (para verificación de token) -->

                    <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">

                    <div class="table-responsive">
                        <table id="tablaInsumos" class="table table-hover align-middle mb-0">

                            <thead class="table-dark">
                                <tr>
                                    <th>ID</th>
                                    <th>Marca</th>
                                    <th>Modelo</th>
                                    <th>Precio Unit</th>
                                    <th>Stock</th>
                                    <th>Seleccionar</th>
                                    <th>Cantidad</th>
                                </tr>
                            </thead>

                            <tbody>
                                <?php foreach ($insumos as $i): ?>
                                    <tr>
                                        <td><?= $i['id'] ?? '' ?></td>
                                        <td><?= htmlspecialchars($i['marca'] ?? '') ?></td>
                                        <td><?= htmlspecialchars($i['modelo'] ?? '') ?></td>
                                        <td>$ <?= number_format($i['precio'], 2, '.', ',') ?></td>
                                        <td><?= htmlspecialchars($i['stock'] ?? '') ?></td>

                                        <!-- CHECKBOX PARA SELECCIONAR PRODUCTO -->

                                        <td class="text-left">
                                            <input type="checkbox"
                                                name="ids[]"
                                                value="<?= $i['id'] ?>"
                                                class="form-check-input">
                                        </td>

                                        <!-- INPUT DE CANTIDAD -->

                                        <td class="text-center">
                                            <input type="number"
                                                name="cantidades[<?= $i['id'] ?>]"
                                                value="1"
                                                min="1"
                                                max="<?= $i['stock'] ?>"
                                                class="form-control form-control-sm"
                                                style="width: 60px;">
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>

                    <!-- BOTÓN FINALIZAR SELECCIÓN -->

                    <div class="mt-3 text-end">
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-check me-1"></i> Finalizar Selección
                        </button>
                    </div>
                </form>
            <?php endif; ?>

            <!-- Errores provenientes de servicioController -->

            <!-- ERROR -->

            <script>
                const serverErrors = <?php 
                    if (!empty($_SESSION['flash_error'])) {
                        $errorsArray = explode('<br>', $_SESSION['flash_error']);
                        unset($_SESSION['flash_error']);
                        echo json_encode($errorsArray);
                    } else {
                        echo '[]';
                    }
                ?>;
            </script>

            <!-- SUCCES -->

            <script>
                const serverSuccess = <?php
                    if (!empty($_SESSION['flash_success'])) {
                        $successArray = explode('<br>', $_SESSION['flash_success']);
                        unset($_SESSION['flash_success']);
                        echo json_encode($successArray);
                    } else {
                        echo '[]';
                    }
                ?>;
            </script>
        </div>
    </div>
</div>

<!-- Validar mediante js -->

<script src="js/servicios/servicioValidator.js"></script>

<!-- Búsqueda y filtro por columnas -->

<script src="js/servicios/servicioTableInsumo.js"></script>