<?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 felx-wrap gap-3">

                <h4 class="mb-0 title-shadow">
                    <i class="fas fa-truck icon-xl me-2"></i>
                    Gestión de Proveedores
                </h4>

                <div class="d-flex gap-2">
                    <a href="?opt=crear_proveedor" class="btn btn-success">
                        <i class="fas fa-plus me-1"></i>
                        Nuevo Proveedor
                    </a>

                    <a href="?opt=home_operador" 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($proveedores)): ?>
                <div class="text-center text-muted py-5">
                    No hay proveedores registrados.
                </div>
            <?php else: ?>

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

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

                <div class="d-flex align-items-center gap-2" id="filtrosIzquierda">

                    <select id="columnFilter"
                            class="form-select form-select-sm ms-2"
                            style="width: auto;">
                        <option value="">Todas las columnas</option>
                        <option value="0">ID</option>
                        <option value="1">Razón social</option>
                        <option value="2">Teléfono</option>
                        <option value="3">Email</option>   
                    </select>

                </div>

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

                        <thead class="table-dark">
                            <tr>
                                <th>ID</th>
                                <th>Razón social</th>
                                <th>Teléfono</th>
                                <th>Email</th>
                                <th class="text-center">Acciones</th>
                            </tr>
                        </thead>

                        <tbody>
                        <?php foreach ($proveedores as $p): ?>
                            <tr>
                                <td><?= $p['id'] ?? '' ?></td>
                                <td><?= htmlspecialchars($p['razon_social'] ?? '') ?></td>
                                <td><?= htmlspecialchars($p['telefono'] ?? '') ?></td>
                                <td><?= htmlspecialchars($p['email'] ?? '') ?></td>

                                <td class="text-center">
                                    <div class="d-infline-flex align-items-center gap-2">

                                        <!-- EDITAR -->

                                        <a href="?opt=editar_proveedor&id=<?= $p['id'] ?>"
                                        class="btn btn-primary btn-sm"
                                        title="Editar Proveedor">
                                            <i class="fas fa-pen fa-sm fa-fw"></i>
                                        </a>

                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>

            <!-- Errores provinientes de proveedorController -->

            <!-- 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/proveedores/proveedorValidator.js"></script>

<!-- Tabla, búsqueda y filtro por columnas -->

<script src="js/proveedores/proveedorTableLista.js"></script>