<?php
session_start();
include 'config.php';
$p_id = $_SESSION['last_purchase_id'] ?? 0;
$data = $conn->query("SELECT * FROM purchases WHERE id=$p_id")->fetch_assoc();

if(!$data) { header("Location: dashboard.php"); exit(); }

$product_name = $data['product_name'];
$is_iphone = (stripos($product_name, 'iPhone') !== false || stripos($product_name, 'iOS') !== false);

function scaleTo200($value, $type) {
    $val = (int)trim($value);
    if (stripos($type, 'General') !== false) return rand(196, 200); 
    if (stripos($type, 'Red Dot') !== false) return rand(190, 198);
    return ($val <= 100) ? $val * 2 : $val;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sensi AI Ultra 🚀</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@700;900&family=Inter:wght@400;600;700;800&display=swap');
        
        /* ব্যাকগ্রাউন্ড অ্যানিমেশন */
        body { 
            background: #020617; 
            color: #fff; 
            font-family: 'Inter', sans-serif; 
            min-height: 100vh;
            overflow-x: hidden;
            position: relative;
        }

        .bg-animate {
            position: fixed; top: 0; left: 0; width: 100%; height: 100%;
            background: radial-gradient(circle at 50% 50%, #0f172a 0%, #020617 100%);
            z-index: -1;
        }

        .orb {
            position: absolute; width: 300px; height: 300px;
            background: linear-gradient(45deg, #06b6d4, #3b82f6);
            filter: blur(100px); opacity: 0.15; border-radius: 50%;
            animation: move 20s infinite alternate;
        }
        @keyframes move { from { transform: translate(-10%, -10%); } to { transform: translate(100%, 50%); } }

        .orbitron { font-family: 'Orbitron', sans-serif; }
        .glass-card { 
            background: rgba(15, 23, 42, 0.7); 
            backdrop-filter: blur(12px); 
            border: 1px solid rgba(255, 255, 255, 0.1); 
            border-radius: 32px; 
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
        }

        /* বাটন স্টাইল */
        .cyber-btn {
            background: linear-gradient(90deg, #06b6d4, #22d3ee);
            color: #000; font-weight: 900; text-transform: uppercase;
            padding: 12px 24px; border-radius: 16px; transition: all 0.3s;
            box-shadow: 0 0 20px rgba(6, 182, 212, 0.4);
        }
        .cyber-btn:hover { transform: scale(1.05); box-shadow: 0 0 30px rgba(6, 182, 212, 0.6); }

        /* টাস্ক আইটেম অ্যানিমেশন */
        .step-item { transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1); border: 1px solid rgba(255,255,255,0.05); }
        .step-done { opacity: 0.4; transform: scale(0.96); background: rgba(0, 255, 255, 0.05); border-color: #06b6d4; }
        
        .checkbox-custom { width: 26px; height: 26px; border: 2px solid #06b6d4; border-radius: 8px; cursor: pointer; display: flex; align-items: center; justify-content: center; transition: all 0.3s; }
        .checkbox-custom.checked { background: #06b6d4; transform: rotate(360deg); }
        .checkbox-custom i { display: none; color: #000; font-size: 14px; }
        .checkbox-custom.checked i { display: block; }

        /* গ্রিড লেআউট */
        .main-grid { display: grid; gap: 1.5rem; grid-template-columns: 1fr; }
        @media (min-width: 1024px) { .main-grid { grid-template-columns: repeat(3, 1fr); } .full-col { grid-column: span 3; } }
    </style>
</head>
<body class="p-4 md:p-8">
    <div class="bg-animate"><div class="orb"></div></div>

    <div class="max-w-7xl mx-auto main-grid">
        
        <!-- Header Section -->
        <div class="full-col glass-card p-8 text-center relative overflow-hidden">
            <div class="inline-flex items-center gap-2 px-4 py-1 bg-cyan-500/20 text-cyan-400 rounded-full text-[10px] font-bold tracking-[3px] uppercase mb-4">
                <span class="relative flex h-2 w-2">
                    <span class="animate-ping absolute inline-flex h-full w-full rounded-full bg-cyan-400 opacity-75"></span>
                    <span class="relative inline-flex rounded-full h-2 w-2 bg-cyan-500"></span>
                </span>
                AI Core Connected ⚡
            </div>
            <h1 class="text-4xl md:text-6xl font-black orbitron italic text-white uppercase mb-2">Config <span class="text-cyan-400">Unlocked</span></h1>
            <p class="text-gray-400 text-xs tracking-[4px] font-bold uppercase">Target: <?php echo $product_name; ?> 🎯</p>
            
            <!-- Progress Bar -->
            <div class="max-w-md mx-auto mt-8">
                <div class="flex justify-between text-[10px] font-bold mb-3 uppercase tracking-widest text-cyan-500">
                    <span>Calibration Progress</span>
                    <span id="progress-text">0%</span>
                </div>
                <div class="h-3 bg-white/5 rounded-full overflow-hidden p-[2px] border border-white/10">
                    <div id="progress-bar" class="h-full bg-gradient-to-r from-cyan-600 to-blue-400 rounded-full transition-all duration-700 shadow-[0_0_15px_#06b6d4]" style="width: 0%"></div>
                </div>
            </div>
        </div>

        <!-- Column 1: Core Controls -->
        <div class="space-y-6">
            <div class="glass-card p-8 text-center relative overflow-hidden group">
                <div class="absolute -right-4 -top-4 opacity-10 text-6xl group-hover:rotate-12 transition-all">🔥</div>
                <p class="text-gray-500 font-bold uppercase text-[10px] mb-4 tracking-widest">Perfect Fire Button</p>
                <div class="inline-block px-8 py-4 bg-gradient-to-b from-orange-500/20 to-transparent border border-orange-500/30 rounded-3xl mb-4">
                    <span class="orbitron text-7xl font-black text-white"><?php echo $data['fire_button']; ?></span>
                    <span class="text-2xl font-bold text-orange-500">%</span>
                </div>
                <button class="w-full cyber-btn text-sm">SET SIZE NOW 🚀</button>
            </div>
            
            <div class="glass-card p-8 text-center relative overflow-hidden group">
                <div class="absolute -right-4 -top-4 opacity-10 text-6xl group-hover:scale-110 transition-all">🎯</div>
                <p class="text-gray-500 font-bold uppercase text-[10px] mb-4 tracking-widest">Aim Lock Status</p>
                <div class="orbitron text-6xl font-black text-cyan-400 italic mb-4">99.9<span class="text-xl text-white">%</span></div>
                <div class="bg-cyan-500/10 py-2 rounded-xl text-cyan-500 text-[10px] font-bold uppercase tracking-widest">Auto-Correction Active ✅</div>
            </div>
        </div>

        <!-- Column 2: Advanced Sensi -->
        <div class="glass-card p-8 relative">
            <h3 class="orbitron text-sm font-bold border-b border-white/5 pb-4 mb-8 flex justify-between items-center text-gray-300">
                <span>IN-GAME SENSI ⚙️</span>
                <span class="text-[9px] bg-cyan-500 text-black px-2 py-0.5 rounded font-black">200 MAX</span>
            </h3>
            <div class="space-y-6">
                <?php 
                $sensi_parts = explode(',', $data['sensitivity']);
                $icons = ['🔭', '🔴', '🔬', '🛡️', '⚡', '🛸'];
                foreach($sensi_parts as $index => $s): 
                    $v = explode(':', $s);
                    $name = trim($v[0]);
                    $val = scaleTo200($v[1], $name);
                    $ico = $icons[$index] ?? '🔥';
                ?>
                <div class="flex justify-between items-center bg-white/5 hover:bg-white/10 p-5 rounded-2xl transition-all border border-white/5">
                    <div class="flex items-center gap-3">
                        <span class="text-xl"><?php echo $ico; ?></span>
                        <span class="text-[11px] font-bold text-gray-300 uppercase tracking-tighter"><?php echo $name; ?></span>
                    </div>
                    <span class="text-3xl font-black orbitron text-white"><?php echo $val; ?></span>
                </div>
                <?php endforeach; ?>
            </div>
        </div>

        <!-- Column 3: Task Master & Guide -->
        <div class="space-y-6">
            <div class="glass-card p-6">
                <h3 class="orbitron text-xs font-bold mb-6 tracking-widest text-cyan-400 flex items-center gap-2">
                    <i class="fas fa-tasks"></i> OPTIMIZATION CHECKLIST ✅
                </h3>
                
                <div class="space-y-3" id="task-list">
                    <?php 
                    $steps = $is_iphone ? [
                        "🍎 Accessibility > <b>Tracking Sensitivity: MAX</b>",
                        "📱 Display Zoom: Set to <b>'More Space'</b>",
                        "👆 Touch Accommodations: <b>0.10 Seconds</b>",
                        "💨 Motion Settings: <b>Reduce Motion ON</b>",
                        "🔋 Battery: <b>Disable Low Power Mode</b>"
                    ] : [
                        "🤖 Developer Options > <b>DPI: 520</b>",
                        "⚡ Animation Scale: <b>Set to 0.5x</b>",
                        "💎 Force 4x MSAA: <b>Enable (High FPS)</b>",
                        "🖱️ Pointer Speed: <b>Set to Maximum</b>",
                        "🚀 Game Space: <b>Ultra Performance Mode</b>"
                    ];

                    foreach($steps as $step):
                    ?>
                    <div class="step-item bg-white/5 p-4 rounded-2xl flex items-center gap-4 cursor-pointer hover:bg-white/10" onclick="toggleStep(this)">
                        <div class="checkbox-custom">
                            <i class="fas fa-check"></i>
                        </div>
                        <p class="text-[11px] text-gray-300 font-semibold"><?php echo $step; ?></p>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>

            <!-- AI Guide Box -->
            <div class="glass-card p-6 border-l-4 border-l-cyan-500">
                <div class="flex items-center gap-2 mb-3">
                    <span class="text-xl">💡</span>
                    <p class="text-[10px] font-bold text-cyan-400 uppercase tracking-widest italic">System Insight:</p>
                </div>
                <p class="text-[12px] text-gray-300 leading-relaxed font-medium">
                    <?php echo nl2br($data['guide']); ?>
                </p>
            </div>
        </div>

        <!-- Footer Actions -->
        <div class="full-col grid grid-cols-1 md:grid-cols-2 gap-4 pt-4">
            <div class="glass-card p-6 flex items-center justify-between border-red-500/20 bg-red-500/5 group">
                <div>
                    <p class="text-red-500 text-[9px] font-bold uppercase mb-1 flex items-center gap-1">
                        <i class="fas fa-clock"></i> License Security
                    </p>
                    <p class="text-2xl font-black orbitron italic text-white uppercase"><?php echo date('d M, Y', strtotime($data['expiry_date'])); ?></p>
                </div>
                <div class="text-3xl group-hover:scale-125 transition-all">🛡️</div>
            </div>
            
            <a href="dashboard.php" class="cyber-btn flex items-center justify-center h-20 text-lg tracking-[5px] shadow-cyan-500/20">
                SAVE & INJECT CONFIG 🚀
            </a>
        </div>

    </div>

    <script>
        function toggleStep(element) {
            const checkbox = element.querySelector('.checkbox-custom');
            element.classList.toggle('step-done');
            checkbox.classList.toggle('checked');
            
            // সাউন্ড বা হ্যাপটিক ফিল দেয়ার জন্য হালকা অ্যানিমেশন
            if(checkbox.classList.contains('checked')){
                element.style.transform = "scale(0.95)";
                setTimeout(() => element.style.transform = "scale(0.96)", 100);
            } else {
                element.style.transform = "scale(1.02)";
                setTimeout(() => element.style.transform = "scale(1)", 100);
            }
            
            updateProgress();
        }

        function updateProgress() {
            const total = document.querySelectorAll('.step-item').length;
            const checked = document.querySelectorAll('.step-done').length;
            const percentage = Math.round((checked / total) * 100);
            
            const bar = document.getElementById('progress-bar');
            const text = document.getElementById('progress-text');
            
            bar.style.width = percentage + '%';
            text.innerText = percentage + '%';
            
            if(percentage === 100) {
                text.innerHTML = "SYSTEM READY! 🔥";
                bar.classList.add('bg-green-500');
            } else {
                bar.classList.remove('bg-green-500');
            }
        }
    </script>
</body>
</html>