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

<div class="container my-5">

    <div class="row justify-content-center">

        <div class="col-xl-11 col-lg-11">

            <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 tittle-shadow">
                            <i class="fas fa-database me-2"></i>
                            Copias de Seguridad
                        </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="index.php?opt=inicio_operador" class="btn btn-soft-secondary text-white">
                                <i class="fas fa-arrow-left me-1"></i> Volver
                            </a>

                        </div>

                    </div>

                </div>

                <div class="card-body p-4 d-flex flex-column">

                    <!-- RESUMEN -->

                    <div class="mb-2 mx-2 flex-shrink-0">

                        <strong>Total de Backups:</strong> <?= count($backups) ?><br>

                        <strong>Última Copia Realizada: </strong><?= $ultimoBackup ?? 'No hay backups aún' ?><br>

                        <strong>Espacio Utilizado:</strong> <?= $totalSize ?> MB

                    </div>

                    <?php if (empty($backups)): ?>

                        <div class="text-center text-muted py-5">
                            No se encontraron backups guardados.
                        </div>

                    <?php else: ?>

                        <div class="table-scroll-backup my-3 mx-2">

                        <table class="table table-fixed table-hover align-middle mb-0 table-resumen-venta">

                            <thead class="table-dark">
                                <tr>
                                    <th>Archivo</th>
                                    <th class="text-left">Fecha</th>
                                    <th class="text-left">Tamaño</th>
                                    <th class="text-center">Acciones</th>
                                </tr>
                            </thead>

                            <tbody>

                                <?php foreach ($backups as $b): ?>

                                    <tr>

                                        <td>
                                            <i class="fas fa-database text-secondary me-2"></i>
                                            <?= htmlspecialchars($b['nombre']) ?>
                                        </td>

                                        <td class="text-left">
                                            <?= $b['fecha'] ?>
                                        </td>

                                        <td class="text-left">
                                            <?= $b['size'] ?>
                                        </td>

                                        <td class="text-center">

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

                                                <!-- DESCARGAR BACKUP -->

                                                <a href="index.php?opt=descargar_backup&file=<?= urlencode($b['nombre']) ?>"
                                                title="Descargar Backup"
                                                class="btn btn-success btn-sm">
                                                    <i class="fas fa-download fa-fw"></i>
                                                </a>

                                                <!-- ELIMINAR BACKUP -->

                                                <button
                                                    class="btn btn-danger btn-sm btnConfirmar"
                                                    title="Eliminar Backup"
                                                    data-action="eliminar_backup"
                                                    data-file="<?= $b['nombre'] ?>"
                                                    data-mensaje="¿Seguro que quieres eliminar este backup?"
                                                    data-titulo="Eliminar Backup"
                                                    data-bs-toggle="modal"
                                                    data-bs-target="#modalConfirmacion">

                                                    <i class="fas fa-trash fa-fw"></i>

                                                </button>

                                                <!-- RESTAURAR BACKUP -->

                                                <button
                                                    class="btn btn-warning btn-sm btnConfirmar"
                                                    title="Restaurar Backup"
                                                    data-action="restaurar_backup"
                                                    data-file="<?= $b['nombre'] ?>"
                                                    data-mensaje="¿Seguro que quieres restaurar el sistema a partir de esta copia de seguridad?"
                                                    data-titulo="Restaurar Backup"
                                                    data-bs-toggle="modal"
                                                    data-bs-target="#modalConfirmacion">

                                                    <i class="fas fa-rotate-left fa-fw text-white"></i>

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

                    <!-- BOTÓN PARA GENERAR COPIA -->

                    <div class="d-flex justify-content-end mt-1 pt-3 mb-1 flex-shrink-0">
                        <form method="POST" action="index.php?opt=generar_backup">
                                    
                            <!-- CAMPO OCULTO (para verificación de token) -->

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

                            <button class="btn btn-primary text-white">
                                <i class="fas fa-plus me-1"></i>
                                Generar Copia
                            </button>
                        </form>
                    </div>
                </div>
            </div>

            <!-- Errores provenientes de configuracionController -->

            <!-- 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>

            <!-- MODAL -->

            <script src="js/globalModal.js"></script>

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

<!-- Validar mediante js -->

<script src="js/configuracion/configuracionValidator.js"></script>