<?php
/**
 * Siro Syria Redirect Gateway
 * URL Format: https://siromove.com/inviteSyria.php?code=REF_CODE&app=rider|driver
 */

$config = [
    'rider' => [
        'ios_store' => 'https://apps.apple.com/st/app/siro-rider/id6748075179',
        'android_store' => 'https://play.google.com/store/apps/details?id=com.Siro.siro',
        'direct_apk' => 'https://api-syria.siromove.com/downloads/siro_rider.apk',
        'app_name' => 'Siro Rider',
        'app_name_ar' => 'سيرو الراكب'
    ],
    'driver' => [
        'ios_store' => 'https://apps.apple.com/st/app/siro-driver/id6482995159',
        'android_store' => 'https://play.google.com/store/apps/details?id=com.siro_driver',
        'direct_apk' => 'https://api-syria.siromove.com/downloads/siro_driver.apk',
        'app_name' => 'Siro Driver',
        'app_name_ar' => 'سيرو الكابتن'
    ]
];

$code = htmlspecialchars($_GET['code'] ?? '', ENT_QUOTES, 'UTF-8');
$appType = ($_GET['app'] ?? 'rider') === 'driver' ? 'driver' : 'rider';
$appConfig = $config[$appType];
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>تحميل تطبيق <?php echo $appConfig['app_name_ar']; ?></title>
    <!-- Tailwind CSS -->
    <script src="https://cdn.tailwindcss.com"></script>
    <!-- Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;700;900&family=Outfit:wght@400;600;800;900&display=swap" rel="stylesheet">
    
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#00E5FF',
                        secondary: '#9C27B0',
                        bg: '#0F172A',
                    }
                }
            }
        }
    </script>
    
    <style>
        body {
            font-family: 'Cairo', 'Outfit', sans-serif;
            -webkit-font-smoothing: antialiased;
        }

        /* Smart Highlight Animations & Classes */
        .highlight-btn {
            border-color: rgba(0, 229, 255, 0.5);
            background-color: rgba(0, 229, 255, 0.08);
            transform: scale(1.02);
            box-shadow: 0 0 20px rgba(0, 229, 255, 0.15);
        }
        
        .dimmed-btn {
            opacity: 0.4;
            filter: grayscale(0.8);
        }
        .dimmed-btn:hover {
            opacity: 1;
            filter: none;
            transform: scale(1.01);
        }

        /* Clipboard confirmation bubble animation */
        @keyframes scaleIn {
            from { transform: scale(0.9) translateY(5px); opacity: 0; }
            to { transform: scale(1) translateY(0); opacity: 1; }
        }
        .toast-active {
            animation: scaleIn 0.2s cubic-bezier(0.16, 1, 0.3, 1) forwards;
        }
    </style>
</head>
<body class="bg-[#0F172A] text-slate-100 min-h-screen relative overflow-x-hidden selection:bg-cyan-500 selection:text-white flex items-center justify-center p-4">

    <!-- Ambient Glowing Background Elements -->
    <div class="fixed inset-0 z-0 overflow-hidden pointer-events-none">
        <div class="absolute -top-[10%] -left-[10%] w-[60%] h-[60%] bg-cyan-500/10 rounded-full filter blur-[120px] animate-pulse"></div>
        <div class="absolute bottom-[5%] -right-[10%] w-[50%] h-[50%] bg-purple-500/8 rounded-full filter blur-[120px] animate-pulse" style="animation-delay: 2s;"></div>
    </div>

    <!-- Language Toggle Button -->
    <div class="absolute top-5 left-5 z-50">
        <button id="lang-toggle" class="px-5 py-2 text-sm font-bold bg-white/5 hover:bg-white/10 hover:border-white/20 backdrop-blur-md rounded-full transition duration-300 border border-white/10 shadow-lg cursor-pointer">
            English
        </button>
    </div>

    <!-- Main Card Container -->
    <div class="relative z-10 w-full max-w-lg bg-slate-900/60 backdrop-blur-xl border border-white/10 rounded-3xl p-8 shadow-2xl flex flex-col items-center">
        
        <!-- Animated Glow Header -->
        <header class="text-center mb-8">
            <h1 class="text-4xl font-black tracking-widest mb-3 bg-gradient-to-r from-cyan-400 to-purple-500 bg-clip-text text-transparent" id="logo">
                SIRO
            </h1>
            <h2 class="text-2xl font-bold mb-2 text-slate-100" id="welcome-title">مرحباً بك في سيرو</h2>
            <p class="text-sm text-slate-400 max-w-sm mx-auto leading-relaxed" id="tagline">
                تمت دعوتك للانضمام إلى منصة التوصيل الأسرع والأوفر. يرجى اتباع التعليمات بالأسفل لتحميل التطبيق.
            </p>
            
            <!-- Floating Device Message Badge -->
            <div id="device-message" class="hidden mt-4 inline-block px-4 py-1.5 rounded-full bg-cyan-500/10 border border-cyan-500/30 text-cyan-300 text-xs font-bold animate-bounce">
                <!-- JS Populated -->
            </div>
        </header>

        <!-- Referral Code Section -->
        <?php if (!empty($code)): ?>
            <div class="w-full bg-slate-950/40 border border-dashed border-cyan-500/30 rounded-2xl p-5 mb-8 flex flex-col items-center gap-3">
                <span class="text-xs font-semibold tracking-wider text-slate-400 uppercase" id="referral-label">كود الإحالة الخاص بك</span>
                <span class="text-3xl font-black tracking-widest text-cyan-400" id="refCode"><?php echo $code; ?></span>
                <button class="px-5 py-2 text-xs font-bold bg-cyan-400/10 text-cyan-300 border border-cyan-400/20 hover:bg-cyan-400 hover:text-slate-950 rounded-full transition duration-300 cursor-pointer" onclick="copyReferralCode()" id="copy-btn">نسخ كود الإحالة</button>
                <div id="copy-toast" class="hidden text-xs text-green-400 font-bold mt-1"></div>
                <p class="text-xs text-slate-400 text-center leading-relaxed mt-2" id="referral-tip">💡 إذا لم تفتح الروابط تلقائياً، قم بنسخ هذا الكود يدوياً وأدخله في التطبيق بعد تحميله.</p>
            </div>
        <?php endif; ?>

        <!-- Dynamic Action Cards -->
        <main class="w-full space-y-4">
            
            <!-- iOS App Card -->
            <div class="ios-card bg-slate-800/30 border border-white/5 rounded-2xl p-5 text-right transition duration-300 flex flex-col">
                <div class="flex items-center gap-3 mb-2 justify-end">
                    <span class="text-xs text-slate-400" id="ios-info">المتجر الرسمي</span>
                    <h3 class="text-md font-bold text-slate-100" id="ios-title">🔒 متجر التطبيقات محظور</h3>
                </div>
                <p class="text-xs text-slate-400 leading-relaxed mb-4" id="ios-desc">
                    نظراً للقيود الجغرافية، يرجى تفعيل تطبيق الـ VPN على هاتفك أولاً، ثم الضغط على الزر بالأسفل للانتقال لصفحة التحميل الرسمية على متجر آبل.
                </p>
                <a href="<?php echo $appConfig['ios_store']; ?>" target="_blank" class="w-full py-3 bg-slate-800 border border-white/10 hover:bg-slate-700/80 text-slate-100 text-sm font-bold rounded-xl transition duration-300 flex items-center justify-center gap-2" id="ios-btn">
                    <!-- Apple SVG -->
                    <svg class="w-4 h-4 fill-current" viewBox="0 0 24 24"><path d="M18.71 19.5c-.83 1.24-1.71 2.45-3.05 2.47-1.34.03-1.77-.79-3.29-.79-1.53 0-2 .77-3.27.82-1.31.05-2.3-1.32-3.14-2.53C4.25 17 2.94 12.45 4.7 9.39c.87-1.52 2.43-2.48 4.12-2.51 1.28-.02 2.5.87 3.29.87.78 0 2.26-1.07 3.81-.91.65.03 2.47.26 3.64 1.98-.09.06-2.17 1.28-2.15 3.81.03 3.02 2.65 4.03 2.68 4.04-.03.07-4.2 14.33-1.38 18.82zM15.97 4.17c.66-.81 1.11-1.93.99-3.06-1 .04-2.2.67-2.92 1.49-.62.71-1.16 1.85-1.01 2.96 1.1.09 2.22-.57 2.94-1.39z"/></svg>
                    <span>التحميل من App Store</span>
                </a>
            </div>

            <!-- Android Direct APK Card -->
            <div class="android-card bg-slate-800/30 border border-white/5 rounded-2xl p-5 text-right transition duration-300 flex flex-col">
                <div class="flex items-center gap-3 mb-2 justify-end">
                    <span class="text-xs text-green-400 font-bold" id="apk-info">✅ لا يحتاج VPN</span>
                    <h3 class="text-md font-bold text-slate-100" id="apk-title">🚀 تحميل مباشر (APK) - بدون VPN</h3>
                </div>
                <p class="text-xs text-slate-400 leading-relaxed mb-4" id="apk-desc">
                    يمكنك تحميل وتثبيت التطبيق مباشرة كملف تثبيت أندرويد دون الحاجة لمتجر جوجل بلاي أو تفعيل VPN.
                </p>
                <a href="<?php echo $appConfig['direct_apk']; ?>" class="w-full py-3 bg-cyan-400 text-slate-950 hover:bg-cyan-300 text-slate-950 text-sm font-bold rounded-xl transition duration-300 flex items-center justify-center gap-2" id="apk-btn">
                    <!-- Download SVG -->
                    <svg class="w-4 h-4 fill-none stroke-current" stroke-width="2" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"></path></svg>
                    <span>تحميل تطبيق APK مباشرة</span>
                </a>
            </div>

            <!-- Android Play Store Card -->
            <div class="android-card bg-slate-800/30 border border-white/5 rounded-2xl p-5 text-right transition duration-300 flex flex-col">
                <div class="flex items-center gap-3 mb-2 justify-end">
                    <span class="text-xs text-yellow-500 font-bold" id="play-info">⚠️ قد يحتاج VPN</span>
                    <h3 class="text-md font-bold text-slate-100" id="play-title">🤖 التحميل من Google Play</h3>
                </div>
                <p class="text-xs text-slate-400 leading-relaxed mb-4" id="play-desc">
                    إذا كنت تستخدم شبكة VPN نشطة على جهازك، يمكنك التحميل من متجر جوجل بلاي الرسمي.
                </p>
                <a href="<?php echo $appConfig['android_store']; ?>" target="_blank" class="w-full py-3 bg-slate-800 border border-white/10 hover:bg-slate-700/80 text-slate-100 text-sm font-bold rounded-xl transition duration-300 flex items-center justify-center gap-2" id="play-btn">
                    <!-- Play Store SVG -->
                    <svg class="w-4 h-4 fill-current" viewBox="0 0 24 24"><path d="M5.223 2.013a1.5 1.5 0 00-1.31 1.782l3.29 5.637-3.29 5.637a1.5 1.5 0 001.31 1.782l10.99-6.419c.75-.447.75-1.58 0-2.027L5.223 2.013z"/></svg>
                    <span>التحميل من Google Play</span>
                </a>
            </div>

        </main>

        <footer class="mt-8 text-center text-xs text-slate-500" id="footer-text">
            حقوق النشر © <?php echo date('Y'); ?> سيرو. جميع الحقوق محفوظة.
        </footer>
    </div>

    <!-- Bilingual content JSON -->
    <script>
        const content = {
            ar: {
                appName: "سيرو",
                welcome: "مرحباً بك في سيرو",
                tagline: "تمت دعوتك للانضمام إلى منصة التوصيل الأسرع والأوفر. يرجى اتباع التعليمات بالأسفل لتحميل التطبيق.",
                referralLabel: "كود الإحالة الخاص بك",
                copyBtn: "نسخ كود الإحالة",
                copiedMsg: "تم نسخ كود الإحالة: ",
                referralTip: "💡 إذا لم تفتح الروابط تلقائياً، قم بنسخ هذا الكود يدوياً وأدخله في التطبيق بعد تحميله.",
                iosTitle: "🔒 متجر التطبيقات محظور",
                iosDesc: "نظراً للقيود الجغرافية، يرجى تفعيل تطبيق الـ VPN على هاتفك أولاً، ثم الضغط على الزر بالأسفل للانتقال لصفحة التحميل الرسمية على متجر آبل.",
                iosBtn: "التحميل من App Store",
                iosInfo: "المتجر الرسمي",
                apkTitle: "🚀 تحميل مباشر (APK) - بدون VPN",
                apkDesc: "يمكنك تحميل وتثبيت التطبيق مباشرة كملف تثبيت أندرويد دون الحاجة لمتجر جوجل بلاي أو تفعيل VPN.",
                apkBtn: "تحميل تطبيق APK مباشرة",
                apkInfo: "✅ لا يحتاج VPN",
                playTitle: "🤖 التحميل من Google Play",
                playDesc: "إذا كنت تستخدم شبكة VPN نشطة على جهازك، يمكنك التحميل من متجر جوجل بلاي الرسمي.",
                playBtn: "التحميل من Google Play",
                playInfo: "⚠️ قد يحتاج VPN",
                footer: "حقوق النشر © 2026 سيرو. جميع الحقوق محفوظة.",
                msgAndroid: "تم اكتشاف جهاز أندرويد 🤖 - تم تظليل الروابط المباشرة",
                msgIos: "تم اكتشاف جهاز آيفون 🍎 - تم تظليل متجر التطبيقات"
            },
            en: {
                appName: "SIRO",
                welcome: "Welcome to Siro",
                tagline: "You have been invited to join the fastest and most affordable delivery platform. Please follow the instructions below to download the app.",
                referralLabel: "Your Referral Code",
                copyBtn: "Copy Referral Code",
                copiedMsg: "Referral code copied: ",
                referralTip: "💡 If deep links don't open automatically, copy this code and enter it manually in the app after downloading.",
                iosTitle: "🔒 App Store is Geo-Restricted",
                iosDesc: "Due to local network restrictions, please enable a VPN on your device first, then click below to download from the Apple App Store.",
                iosBtn: "Download from App Store",
                iosInfo: "Official Store",
                apkTitle: "🚀 Direct APK Download - No VPN Required",
                apkDesc: "You can download and install the app directly as an Android package (APK) without needing a VPN or the Play Store.",
                apkBtn: "Download APK Directly",
                apkInfo: "✅ No VPN Required",
                playTitle: "🤖 Download from Google Play",
                playDesc: "If you have an active VPN connection on your device, you can download the app from the official Google Play Store.",
                playBtn: "Download from Google Play",
                playInfo: "⚠️ May Require VPN",
                footer: "Copyright © 2026 Siro. All rights reserved.",
                msgAndroid: "Android Device Detected 🤖 - Direct APK links highlighted",
                msgIos: "iOS Device Detected 🍎 - Apple Store highlighted"
            }
        };

        let currentLang = 'ar';

        function detectOS() {
            const userAgent = navigator.userAgent || navigator.vendor || window.opera;
            const androidCards = document.querySelectorAll('.android-card');
            const iosCards = document.querySelectorAll('.ios-card');
            const deviceMessage = document.getElementById('device-message');

            // Reset highlight/dimmed states
            [...androidCards, ...iosCards].forEach(card => {
                card.classList.remove('highlight-btn', 'dimmed-btn');
            });
            deviceMessage.classList.add('hidden');

            if (/android/i.test(userAgent)) {
                androidCards.forEach(c => c.classList.add('highlight-btn'));
                iosCards.forEach(c => c.classList.add('dimmed-btn'));
                
                deviceMessage.textContent = content[currentLang].msgAndroid;
                deviceMessage.classList.remove('hidden');
            } else if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
                iosCards.forEach(c => c.classList.add('highlight-btn'));
                androidCards.forEach(c => c.classList.add('dimmed-btn'));
                
                deviceMessage.textContent = content[currentLang].msgIos;
                deviceMessage.classList.remove('hidden');
            }
        }

        function updateLanguage(lang) {
            currentLang = lang;
            const t = content[lang];
            const isAr = lang === 'ar';

            document.documentElement.setAttribute('dir', isAr ? 'rtl' : 'ltr');
            document.documentElement.setAttribute('lang', lang);

            // Update DOM text contents
            document.getElementById('logo').textContent = t.appName;
            document.getElementById('welcome-title').textContent = t.welcome;
            document.getElementById('tagline').textContent = t.tagline;
            
            const refLabel = document.getElementById('referral-label');
            if (refLabel) refLabel.textContent = t.referralLabel;
            
            const copyBtn = document.getElementById('copy-btn');
            if (copyBtn) copyBtn.textContent = t.copyBtn;

            const refTip = document.getElementById('referral-tip');
            if (refTip) refTip.textContent = t.referralTip;

            document.getElementById('ios-title').textContent = t.iosTitle;
            document.getElementById('ios-desc').textContent = t.iosDesc;
            document.querySelector('#ios-btn span').textContent = t.iosBtn;
            document.getElementById('ios-info').textContent = t.iosInfo;

            document.getElementById('apk-title').textContent = t.apkTitle;
            document.getElementById('apk-desc').textContent = t.apkDesc;
            document.querySelector('#apk-btn span').textContent = t.apkBtn;
            document.getElementById('apk-info').textContent = t.apkInfo;

            document.getElementById('play-title').textContent = t.playTitle;
            document.getElementById('play-desc').textContent = t.playDesc;
            document.querySelector('#play-btn span').textContent = t.playBtn;
            document.getElementById('play-info').textContent = t.playInfo;

            document.getElementById('footer-text').textContent = t.footer;

            // Toggle language button text
            document.getElementById('lang-toggle').textContent = isAr ? 'English' : 'العربية';

            detectOS();
        }

        document.getElementById('lang-toggle').addEventListener('click', () => {
            updateLanguage(currentLang === 'ar' ? 'en' : 'ar');
        });

        function copyReferralCode() {
            const codeText = document.getElementById("refCode").innerText;
            const toast = document.getElementById("copy-toast");
            
            navigator.clipboard.writeText(codeText).then(() => {
                showToast(content[currentLang].copiedMsg + codeText);
            }, () => {
                // Fallback
                const dummy = document.createElement("textarea");
                document.body.appendChild(dummy);
                dummy.value = codeText;
                dummy.select();
                document.execCommand("copy");
                document.body.removeChild(dummy);
                showToast(content[currentLang].copiedMsg + codeText);
            });
        }

        function showToast(message) {
            const toast = document.getElementById("copy-toast");
            toast.textContent = message;
            toast.classList.remove('hidden');
            toast.classList.add('toast-active');
            setTimeout(() => {
                toast.classList.add('hidden');
                toast.classList.remove('toast-active');
            }, 3000);
        }

        document.addEventListener('DOMContentLoaded', () => {
            updateLanguage('ar');
        });
    </script>
</body>
</html>
