<?php
// Aggiungi all'inizio del file
require 'vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable('C:/xampp/envs/invoices');
$dotenv->load();

// Configurazione FTP
$ftpServer = $_ENV['FTP_HOST'];
$ftpUser = $_ENV['FTP_USER'];
$ftpPass = $_ENV['FTP_PASSWORD'];

// Prendi l'ID dalla query string
$invoiceId = $_GET['id'];

// Validazione semplificata: deve solo iniziare con "invoice_" e finire con ".pdf"
if (!preg_match('/^invoice_.*\.pdf$/', $invoiceId)) {
    die('Invalid invoice ID');
}

// Percorso del file sul NAS
$remotePath = "invoices/" . $invoiceId;

// Connessione FTP
$conn = ftp_connect($ftpServer);
if (!$conn) {
    die('Could not connect to FTP server');
}

// Login
if (!ftp_login($conn, $ftpUser, $ftpPass)) {
    die('Could not login to FTP server');
}


ftp_pasv($conn, true);


if (ftp_size($conn, $remotePath) < 0) {
    die('Invoice not found: ' . $remotePath);
}

?>

<!DOCTYPE html>
<html class="scroll-smooth">
<head>
    <title>Thanks for your purchase!</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://cdn.jsdelivr.net/npm/alpinejs@2.8.2/dist/alpine.min.js" defer></script>
    <style>
        @keyframes float {
            0%, 100% { transform: translateY(0); }
            50% { transform: translateY(-10px); }
        }
        
        @keyframes shine {
            0% { background-position: 200% center; }
            100% { background-position: -200% center; }
        }

        .gradient-text {
            background: linear-gradient(135deg, #fff 0%, #94a3b8 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-size: 200% auto;
            animation: shine 3s linear infinite;
        }

        .bg-grid-pattern {
            background-image:
                linear-gradient(to right, rgba(0, 0, 0, 0.1) 1px, transparent 1px),
                linear-gradient(to bottom, rgba(0, 0, 0, 0.1) 1px, transparent 1px);
            background-size: 20px 20px;
        }
    </style>
</head>
<body class="bg-gradient-to-br from-zinc-900 via-zinc-800 to-zinc-900 text-zinc-100 min-h-screen">
    <div class="container mx-auto px-4 py-8">
        <div class="text-center mb-8 mt-8">
            <img src="https://i.ibb.co/w7gKVdg/ziomark-hub-comp-White.png" 
                 alt="Logo" 
                 class="w-32 mx-auto mb-8"
                 style="animation: float 3s ease-in-out infinite">
            <h1 class="text-4xl font-bold mb-4 gradient-text">
                Thanks for your purchase!
            </h1>
            <p class="text-zinc-400 max-w-2xl mx-auto text-lg">
                I hope you will enjoy your purchase, once you have downloaded the PDF, you can close this page.
            </p>
        </div>


        <div class="mb-6 flex justify-between items-center">
 
            <div class="bg-zinc-800/50 rounded-lg p-4 backdrop-blur-sm">
                <div class="flex items-center space-x-4">
                    <div class="p-3 bg-blue-500/10 rounded-lg">
                        <svg class="w-6 h-6 text-blue-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
                        </svg>
                    </div>
                    <div>
                        <h3 class="text-lg font-medium text-gray-200">Details</h3>
                        <p class="text-sm text-gray-400">
                            ID: <?php echo htmlspecialchars(str_replace(['invoice_', '.pdf'], '', $invoiceId)); ?><br>
                            Data: <?php 
                                $fileTimestamp = ftp_mdtm($conn, $remotePath);
                                if ($fileTimestamp != -1) {
                                    echo date('d/m/Y', $fileTimestamp);
                                } else {
                                    echo 'Data non disponibile';
                                }
                            ?>
                        </p>
                    </div>
                </div>
            </div>

            <!-- Download Button -->
            <a href="download.php?id=<?php echo urlencode($invoiceId); ?>" 
               onclick="setTimeout(() => window.dispatchEvent(new CustomEvent('download-success')), 1000)"
               class="group relative inline-flex items-center px-8 py-3 overflow-hidden rounded-lg bg-gradient-to-r from-[#7289da] to-[#36393e] transition-all duration-300">
                <span class="absolute right-0 w-8 h-32 -mt-12 transition-all duration-1000 transform translate-x-12 bg-white opacity-10 rotate-12 group-hover:-translate-x-40 ease"></span>
                <span class="relative flex items-center gap-2 text-white font-medium">
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 transition-transform group-hover:scale-110 group-hover:animate-bounce" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"/>
                    </svg>
                    <span class="relative group-hover:translate-x-1 transition-transform">
                        Download PDF
                        <span class="absolute bottom-0 left-0 w-full h-0.5 bg-white transform scale-x-0 group-hover:scale-x-100 transition-transform origin-left"></span>
                    </span>
                </span>
            </a>
        </div>


        <div class="fade-in" style="animation-delay: 0.2s" x-data="{ loading: true }">
            <div class="bg-zinc-800/30 backdrop-blur-sm rounded-2xl p-4 border border-zinc-700/30 shadow-2xl transition-all duration-500 hover:bg-zinc-800/50">
                <div>
          
                    <div x-show="loading" class="absolute inset-0 flex items-center justify-center z-10">
                        <div class="animate-spin rounded-full h-16 w-16 border-t-2 border-b-2 border-[#7289da]"></div>
                    </div>
                    
                    <div class="relative" style="padding-top: 141.4%">
                        <embed 
                            src="data:application/pdf;base64,<?php 
                                $tempFile = tempnam(sys_get_temp_dir(), 'invoice');
                                ftp_get($conn, $tempFile, $remotePath, FTP_BINARY);
                                echo base64_encode(file_get_contents($tempFile));
                                unlink($tempFile);
                            ?>" 
                            type="application/pdf" 
                            class="absolute top-0 left-0 w-full h-full rounded-lg"
                            style="background: white;"
                            @load="loading = false">
                    </div>
                </div>
            </div>
        </div>
    </div>


    <footer class="mt-8 text-center text-zinc-500 text-sm pb-4">
        <p>© <?php echo date('Y'); ?> ZioMark Hub. Tutti i diritti riservati.</p>
    </footer>
</body>
</html>

<?php
// Chiudi la connessione FTP
ftp_close($conn);
?>