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

<div class="container my-5">

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

        <div class="col-lg-8">

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

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

                    <h4 class="mb-0 title-shadow">
                        <i class="fas fa-file-alt me-2"></i>
                        Venta Confirmada
                    </h4>

                </div>

                <div class="card-body text-center p-5">

                    <i class="fas fa-check-circle text-success fa-4x mb-4"></i>

                    <h4>Venta guardada correctamente</h4>

                    <p class="text-muted">
                        Comprobante N°
                        <strong><?= $numeroVenta ?></strong>
                    </p>

                    <hr>

                    <p>
                        ¿Desea descargar el comprobante generado?
                    </p>

                    <div class="d-flex justify-content-center gap-3 mt-5">

                        <form method="POST" action="?opt=generar_venta_pdf">

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

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

                            <button class="btn btn-success">
                                <i class="fas fa-download me-1 my-2"></i>
                                Descargar Comprobante
                            </button>

                        </form>

                        <a href="?opt=continuar_sin_comprobante" class="btn btn-secondary">
                            <i class="fas fa-arrow-right me-1 my-2"></i>
                            Continuar
                        </a>

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

    <!-- Errores provenients de ventaController -->

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

<!-- Validar mediante js -->

<script src="js/ventas/ventaValidator.js"></script>

<!-- Evita que Chrome use BFCache -->

<script>
    window.onpageshow = function(event) {
        if (event.persisted) {
            window.location.reload();
        }
    };
</script>