<?php
require_once __DIR__ . '/../includes/init.php';
require_once __DIR__ . '/../includes/auth.php';
checkUserAuth();
$user_id = $_SESSION['user_id'];
$stmt = $pdo->prepare("SELECT username, nickname, referral_code FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);

// 获取系统设置
$settings = [];
$stmt = $pdo->query("SELECT key_name, value FROM system_settings");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $settings[$row['key_name']] = $row['value'];
}
$min_deposit = floatval($settings['referral_min_deposit'] ?? 98);

// 判断是否满足推广功能生效条件
$can_promote = false;
$stmt = $pdo->prepare("SELECT COUNT(*) FROM recharge_orders WHERE user_id=? AND amount>=? AND status='confirmed'");
$stmt->execute([$user_id, $min_deposit]);
if ($stmt->fetchColumn() > 0) {
    $can_promote = true;
}

// 查询用户所有返佣相关字段
$stat = get_user_commission_stat($user_id);
$commission_can_withdraw = $stat['can_withdraw'];
$commission_withdrawn = $stat['withdrawn'];
$commission_frozen = $stat['frozen'];
$commission_total = $stat['total'];

// 反代环境下 HTTP_HOST 常为上游 web.ggai365.com，需解析用户真实访问域名
$host = function_exists('ggai_resolve_client_host') ? ggai_resolve_client_host() : ($_SERVER['HTTP_HOST'] ?? '');
$host = preg_replace('/^www\./i', '', trim((string) $host));
if ($host === '' || (function_exists('ggai_is_upstream_host') && ggai_is_upstream_host($host))) {
    global $partnerInfo;
    $partnerDomain = preg_replace('/^www\./i', '', trim((string) ($partnerInfo['domain'] ?? '')));
    $host = ($partnerDomain !== '' && (!function_exists('ggai_is_upstream_host') || !ggai_is_upstream_host($partnerDomain)))
        ? $partnerDomain
        : 'ggai365.com';
}
// 生成专属注册链接（注册页）
$invite_url = $user['referral_code'] ? "https://{$host}/register.php?invite=" . $user['referral_code'] : '';
// 生成二维码（回退到原API）
$qr_url = $invite_url ? 'https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=' . urlencode($invite_url) : '';

$pageTitle = __t('invite_page_title', '邀请有奖 - 我的推广中心');
$layoutExtraHead = '<link rel="stylesheet" href="/public/css/style.css">';

ob_start();
?>
<style>
    /* 正文在 layout 的 .content-wrapper 内：勿再 include sidebar / 勿再包一层 content-wrapper，否则重复 #userSidebar 导致侧栏不可见 */
    .invite-page-wrap { background: #f5f7fa; padding: 12px 4px; max-width: 100%; width: 100%; box-sizing: border-box; min-height: 100%; border-radius: 8px; }
    body { background: #f5f7fa; }
    .invite-card { background: #fff; border-radius: 14px; box-shadow: 0 2px 12px rgba(0,0,0,0.08); padding: 20px 12px; margin-bottom: 24px; max-width:980px; margin-left:auto; margin-right:auto; }
    .invite-title { font-size: 1.5rem; font-weight: bold; color: #232946; margin-bottom: 18px; }
    .invite-code { font-size: 1.3em; font-weight: bold; color: #4facfe; letter-spacing: 2px; }
    .invite-link { font-size: 1.05em; background: #f5f7fa; border-radius: 8px; padding: 10px 14px; margin-bottom: 12px; word-break: break-all; }
    .mindset-intro-lead { text-align: center !important; }
    .qr-img { display: flex; justify-content: center; align-items: center; margin: 0 auto 12px auto; border-radius: 8px; border: 1px solid #eee; width: 200px; height: 200px; background: #fff; }
    .copy-btn { margin-left: 8px; }
    @media (min-width: 992px) {
      .invite-card .row { align-items: flex-start; }
      .invite-card .col-md-7 { padding-right: 32px; border-right: 1px solid #f0f0f0; }
      .invite-card .col-md-5 { display: flex; flex-direction: column; align-items: center; justify-content: center; min-width: 220px; margin-left: 32px; }
      /* 桌面端文案卡片对齐优化 */
      .mindset-card p:not(.mindset-intro-lead) { text-align: left !important; }
      .mindset-card li { text-align: left !important; }
      .mindset-intro-lead { text-align: center !important; }
    }
    @media (max-width: 991px) {
      .invite-card .col-md-5 { margin-top: 24px; text-align: center; border-right: none; margin-left:0; }
      .invite-card .col-md-7 { padding-right: 0; border-right: none; }
    }
    /* 移动端优化样式 */
    @media (max-width: 768px) {
      .content-wrapper { padding: 8px 8px !important; }
      .invite-card { padding: 16px 12px !important; margin-bottom: 16px !important; border-radius: 12px !important; }
      .invite-title { font-size: 1.2rem !important; }
      .invite-code { font-size: 1.5em !important; }
      .invite-link { font-size: 0.9em !important; padding: 8px 10px !important; }
      /* 文案卡片移动端优化 */
      .mindset-card { padding: 16px 12px !important; margin-bottom: 16px !important; }
      .mindset-card h4 { font-size: 1.15rem !important; padding-bottom: 10px !important; word-break: break-word; }
      .mindset-card h5 { font-size: 1rem !important; margin-top: 20px !important; margin-bottom: 12px !important; word-break: break-word; }
      .mindset-card p { font-size: 0.95em !important; line-height: 1.7 !important; margin-bottom: 12px !important; word-break: break-word; word-wrap: break-word; text-align: justify; }
      .mindset-card ul, .mindset-card ol { padding-left: 20px !important; font-size: 0.95em !important; line-height: 1.7 !important; }
      .mindset-card li { margin-bottom: 8px !important; word-break: break-word; text-align: justify; }
      .mindset-card strong { word-break: break-word; }
      .quote-box { font-size: 1em !important; padding: 12px !important; margin-bottom: 12px !important; word-break: break-word; line-height: 1.6 !important; }
      .conclusion-box { font-size: 0.95em !important; padding: 16px 12px !important; line-height: 1.6 !important; word-break: break-word; }
      .conclusion-box strong { font-size: 0.95em !important; word-break: break-word; }
    }
</style>
<div class="invite-page-wrap">
    <div class="invite-card member-light-panel">
        <div class="invite-title">
            <i class="fas fa-gift"></i>
            <?= __t('invite_page_title', '邀请有奖 - 我的推广中心'); ?>
            <a href="<?= htmlspecialchars(ggai_mu('/user/index.php'), ENT_QUOTES, 'UTF-8') ?>" class="btn btn-outline-secondary btn-sm ml-2">
                <i class="fas fa-home me-1"></i> <?= __t('common_back_home', '返回会员首页'); ?>
            </a>
        </div>
        <?php if ($user['referral_code']): ?>
            <div class="row align-items-center">
                <div class="col-md-7 col-12 mb-3 mb-md-0">
                    <div class="mb-2" style="font-size:1.1em;color:#232946;font-weight:500;"><?= __t('invite_label_code', '我的专属推广码(不要带空格)：'); ?></div>
                    <div class="invite-code mb-3" style="font-size:2em;">
                        <i class="fas fa-qrcode me-2"></i>
                        <span id="referralCode"><?php echo htmlspecialchars($user['referral_code']); ?></span>
                        <button class="btn btn-outline-primary btn-sm ms-2" id="copyCodeBtn" onclick="copyReferralCode()" style="font-size:0.6em;padding:4px 8px;">
                            <i class="fas fa-copy"></i> <?= __t('invite_btn_copy_code', '复制推广码'); ?>
                        </button>
                    </div>
                    <div class="mb-2" style="font-size:1.1em;color:#232946;font-weight:500;"><?= __t('invite_label_link', '专属注册链接：'); ?></div>
                    <div class="invite-link mb-2" id="inviteLink"><?php echo htmlspecialchars($invite_url); ?></div>
                    <button class="btn btn-primary btn-sm copy-btn mb-3" onclick="copyInvite()"><i class="fas fa-copy"></i> <?= __t('invite_btn_copy_link', '复制推广链接'); ?></button>
                    <div class="mb-3" style="text-align:center;">
                        <div class="mb-2" style="font-size:1.1em;color:#232946;font-weight:500;"><?= __t('invite_label_qr', '扫码注册'); ?></div>
                        <div id="qrcode" class="qr-img mb-2" style="margin:0 auto;"></div>
                        <div class="text-muted qr-desc" style="font-size:0.95em;text-align:center;"><?= __t('invite_hint_qr', '长按或截图保存二维码，分享给好友'); ?></div>
                    </div>
                    <div class="text-muted mt-3" style="font-size:0.98em;">
                        <?= __t('invite_hint_intro', '邀请好友注册并充值，您可获得丰厚返佣奖励！'); ?>
                    </div>
                    <?php if (!$can_promote): ?>
                    <div class="alert alert-warning mt-3">
                        <?= __t('invite_warning_not_reach', '当前未达推广门槛，奖励将被冻结，满足条件后自动解冻。'); ?><br>
                    <?php else: ?>
                    <div class="alert alert-success mt-3">
                    <?php endif; ?>
                        <b><?= __t('invite_stat_can', '当前可领取返佣：'); ?></b> <span style="color:#27ae60;font-weight:600;"> <?php echo number_format($commission_can_withdraw,2); ?> USDT</span><br>
                        <b><?= __t('invite_stat_withdrawn', '当前已提取返佣：'); ?></b> <span style="color:#27ae60;font-weight:600;"> <?php echo number_format($commission_withdrawn,2); ?> USDT</span><br>
                        <b><?= __t('invite_stat_frozen', '当前冻结返佣：'); ?></b> <span style="color:#e67e22;font-weight:600;"> <?php echo number_format($commission_frozen,2); ?> USDT</span><br>
                        <b><?= __t('invite_stat_total', '当前返佣总额：'); ?></b> <span style="color:#e67e22;font-weight:600;"> <?php echo number_format($commission_total,2); ?> USDT</span>
                    </div>
                    <a href="<?= htmlspecialchars(ggai_mu('/user/withdraw.php'), ENT_QUOTES, 'UTF-8') ?>" class="btn btn-success mt-3"><i class="fas fa-arrow-up"></i> <?= __t('invite_btn_withdraw', '返佣提现'); ?></a>
                </div>
            </div>
        <?php endif; ?>
    </div>
    <div class="card p-4 mb-4 mindset-card member-light-panel" style="background:#fff;border-radius:12px;width:100%;max-width:980px;margin:0 auto;box-shadow: 0 2px 12px rgba(0,0,0,0.08);">
        <h4 class="mb-4" style="color:#232946;font-weight:700;border-bottom:2px solid #4facfe;padding-bottom:12px;font-size:1.3rem;">
            <i class="fas fa-lightbulb me-2" style="color:#ffc107;"></i><?= __t('invite_mindset_title', '从\"赌徒思维\"到\"平台思维\"的转变'); ?>
        </h4>
        <?php
        // 统一使用语言包和实时翻译，支持所有语言
        $mindsetIntro = __t('invite_mindset_intro', '');
        if (empty($mindsetIntro) && function_exists('translate_content') && defined('CURRENT_LANG') && CURRENT_LANG !== 'zh_cn' && CURRENT_LANG !== 'zh_tw') {
            global $pdo;
            $mindsetIntro = translate_content(__t('invite_mindset_intro', '很多合作伙伴在推广市场时，存在一个严重的认知误区：<strong style="color:#e74c3c;word-break:break-word;">非要看到自己的账号盈利才会去做市场</strong>。这种思维模式，本质上是一种"赌徒思维"。'), CURRENT_LANG, $pdo);
        }
        if (empty($mindsetIntro)) {
            $mindsetIntro = __t('invite_mindset_intro', '很多合作伙伴在推广市场时，存在一个严重的认知误区：<strong style="color:#e74c3c;word-break:break-word;">非要看到自己的账号盈利才会去做市场</strong>。这种思维模式，本质上是一种"赌徒思维"。');
        }
        ?>
        <div style="color:#555;font-size:1.05em;line-height:1.8;word-break:break-word;">
            <p class="mb-3 mindset-intro-lead" style="font-size:1.1em;color:#232946;font-weight:500;word-break:break-word;text-align:center;">
                <?php echo $mindsetIntro; ?>
            </p>
            
            <h5 class="mt-4 mb-3" style="color:#232946;font-weight:600;font-size:1.15rem;"><?php echo __t('invite_mindset_why_title', '为什么这是赌徒思维？'); ?></h5>
            <p class="mb-3" style="word-break:break-word;text-align:left;">
                <?php echo __t('invite_mindset_why_p1', '让我们用数据说话：如果一个平台上<strong style="color:#27ae60;">90%的用户是赚钱的</strong>，而只有<strong style="color:#e74c3c;">10%的用户是亏损的</strong>，这个平台能不能做？答案显而易见——<strong>当然能做！</strong>'); ?>
            </p>
            <p class="mb-3" style="word-break:break-word;text-align:left;">
                <?php echo __t('invite_mindset_why_p2', '但问题来了：如果你非要等到自己账号盈利了才去推广市场，而你又恰好是那10%亏损的人群，那你还能做出来市场吗？答案很残酷——<strong style="color:#e74c3c;">不能！</strong>'); ?>
            </p>
            
            <h5 class="mt-4 mb-3" style="color:#232946;font-weight:600;font-size:1.15rem;"><?php echo __t('invite_mindset_cost_title', '时间成本与机会成本'); ?></h5>
            <p class="mb-3" style="word-break:break-word;text-align:left;">
                <?php echo __t('invite_mindset_cost_p', '即使你几个月后终于盈利了，再开始做市场，也已经错过了最佳时机。更重要的是，<strong>个人账号的短期表现，在统计学上没有任何参考意义</strong>。一个账号的盈亏，受多种因素影响：市场行情、操作策略、风险控制、运气成分等等。用个人短期表现来判断一个平台的优劣，就像用一次抛硬币的结果来判断硬币是否公平一样荒谬。'); ?>
            </p>
            
            <h5 class="mt-4 mb-3" style="color:#232946;font-weight:600;font-size:1.15rem;"><?php echo __t('invite_mindset_logic_title', '商业逻辑的本质'); ?></h5>
            <p class="mb-3 quote-box" style="font-size:1.15em;color:#e74c3c;font-weight:600;text-align:center;padding:15px;background:#fff3cd;border-radius:8px;border-left:4px solid #ffc107;">
                <?php echo __t('invite_mindset_logic_quote', '<i class="fas fa-quote-left me-2"></i>当赌徒的，永远没有开赌场的赚钱多。<i class="fas fa-quote-right ms-2"></i>'); ?>
            </p>
            <p class="mb-3" style="word-break:break-word;text-align:left;"><?php echo __t('invite_mindset_logic_intro', '这个道理放在任何行业都适用：'); ?></p>
            <ul class="mb-3" style="padding-left:25px;">
                <li style="margin-bottom:6px;word-break:break-word;text-align:left;"><?php echo __t('invite_mindset_logic_li1', '期货公司、证券公司、交易所，它们需要所有客户都盈利才能开吗？<strong>显然不是！</strong>'); ?></li>
                <li style="margin-bottom:6px;word-break:break-word;text-align:left;"><?php echo __t('invite_mindset_logic_li2', '银行需要所有储户都赚钱才能运营吗？<strong>显然不是！</strong>'); ?></li>
                <li style="margin-bottom:6px;word-break:break-word;text-align:left;"><?php echo __t('invite_mindset_logic_li3', '保险公司需要所有投保人都理赔才能盈利吗？<strong>显然不是！</strong>'); ?></li>
            </ul>
            <p class="mb-3" style="word-break:break-word;text-align:left;">
                <?php echo __t('invite_mindset_logic_conclusion', '<strong>平台的价值在于提供服务和工具，而不是保证每个参与者都盈利。</strong> 如果一个平台能够帮助90%的用户实现盈利，这已经是一个极其优秀的平台了。'); ?>
            </p>
            
            <h5 class="mt-4 mb-3" style="color:#232946;font-weight:600;font-size:1.15rem;"><?php echo __t('invite_mindset_right_title', '正确的思维模式'); ?></h5>
            <p class="mb-3 quote-box" style="font-size:1.15em;color:#27ae60;font-weight:600;text-align:center;padding:15px;background:#d4edda;border-radius:8px;border-left:4px solid #27ae60;word-break:break-word;line-height:1.6;">
                <?php echo __t('invite_mindset_right_quote', '<i class="fas fa-arrow-right me-2"></i>从"我要先赚钱"转变为"我要先帮助别人赚钱"<i class="fas fa-arrow-left ms-2"></i>'); ?>
            </p>
            <p class="mb-2" style="word-break:break-word;text-align:left;"><?php echo __t('invite_mindset_right_intro', '真正的市场推广者，应该关注的是：'); ?></p>
            <ol class="mb-3" style="padding-left:25px;">
                <li style="margin-bottom:8px;word-break:break-word;text-align:left;"><?php echo __t('invite_mindset_right_li1', '<strong>平台的整体数据表现</strong>：90%的用户盈利率，已经证明了平台的价值'); ?></li>
                <li style="margin-bottom:8px;word-break:break-word;text-align:left;"><?php echo __t('invite_mindset_right_li2', '<strong>市场机会的时效性</strong>：机会窗口不会等人，错过就是错过'); ?></li>
                <li style="margin-bottom:8px;word-break:break-word;text-align:left;"><?php echo __t('invite_mindset_right_li3', '<strong>规模效应</strong>：帮助更多人使用平台，才能实现更大的价值'); ?></li>
                <li style="margin-bottom:8px;word-break:break-word;text-align:left;"><?php echo __t('invite_mindset_right_li4', '<strong>长期价值</strong>：建立市场网络，比短期个人盈利更有意义'); ?></li>
            </ol>
            
            <h5 class="mt-4 mb-3" style="color:#232946;font-weight:600;font-size:1.15rem;"><?php echo __t('invite_mindset_conclusion_title', '结语'); ?></h5>
            <p class="mb-3" style="word-break:break-word;text-align:left;">
                <?php echo __t('invite_mindset_conclusion_p', '如果你还在等待自己账号盈利才开始做市场，请思考一个问题：<strong style="color:#e74c3c;">如果平台90%的用户都在赚钱，而你却还在亏损，问题可能不在平台，而在你的策略和方法。</strong> 与其等待，不如立即行动，在帮助他人成功的过程中，你也会找到属于自己的成功路径。'); ?>
            </p>
            <p class="mb-0 conclusion-box" style="font-size:1.1em;color:#fff;font-weight:600;text-align:center;padding:20px;background:linear-gradient(135deg, #667eea 0%, #764ba2 100%);border-radius:10px;margin-top:20px;line-height:1.7;">
                <?php echo __t('invite_mindset_final_quote', '<i class="fas fa-gem me-2"></i>记住：<strong>开赌场的，永远比赌徒赚得多。做平台的，永远比参与者赚得多。做市场的，永远比等待者赚得多。</strong>'); ?>
            </p>
            <p class="text-center mt-3 mb-0" style="color:#999;font-style:italic;font-size:0.95em;">
                <?php echo __t('invite_mindset_final_note', '<i class="fas fa-heart me-1" style="color:#e74c3c;"></i>转变思维，把握机会，成就他人，成就自己。'); ?>
            </p>
        </div>
    </div>
    <div class="card p-4 mb-4" style="background:#f8fafc;border-radius:12px;width:100%;max-width:980px;margin:0 auto;box-shadow: 0 2px 12px rgba(0,0,0,0.08);">
        <h5 class="mb-3" style="color:#232946;font-weight:600;"><i class="fas fa-info-circle me-2"></i><?= __t('invite_rules_title', '推广奖励规则'); ?></h5>
        <ul style="color:#555;font-size:1.05em;line-height:2;">
            <li><?php echo __t('invite_rules_li1', '每个用户注册即自动生成唯一推广码，可在本页查看和分享。'); ?></li>
            <li><?php echo __t('invite_rules_li2', '好友通过您的专属链接或扫码注册并充值算力，您将获得直推、间推奖励以及分红。'); ?></li>
            <li><?php echo __t('invite_rules_li3', '未达门槛或超期7天再充的返佣将被冻结，进入公共补偿池。'); ?></li>
            <li><?php echo __t('invite_rules_li4', '亏损的用户可按亏损金额得到一定比例的补偿，补偿比例按加权平均计算。'); ?></li>
            <li><?php echo __t('invite_rules_li5', '可在"返佣明细"页面随时查看奖励到账与冻结情况。'); ?></li>
        </ul>
    </div>
</div>
<script src="/assets/js/qrcode.min.js"></script>
<script>
function copyInvite() {
    var link = document.getElementById('inviteLink').innerText;
    navigator.clipboard.writeText(link).then(function() {
        alert('<?= __t('invite_alert_link_copied', '推广链接已复制！'); ?>');
    });
}

function copyReferralCode() {
    var code = document.getElementById('referralCode').innerText;
    var btn = document.getElementById('copyCodeBtn');
    navigator.clipboard.writeText(code).then(function() {
        // 保存原始文字
        var originalText = btn.innerHTML;
        // 显示已复制状态
        btn.innerHTML = '<i class="fas fa-check"></i> <?= __t('invite_btn_copied', '已复制'); ?>';
        btn.classList.remove('btn-outline-primary');
        btn.classList.add('btn-success');
        // 2秒后恢复原状
        setTimeout(function() {
            btn.innerHTML = originalText;
            btn.classList.remove('btn-success');
            btn.classList.add('btn-outline-primary');
        }, 2000);
    });
}
// 本地生成二维码
window.onload = function() {
    var inviteUrl = document.getElementById('inviteLink').innerText;
    if(inviteUrl && document.getElementById('qrcode')) {
        new QRCode(document.getElementById('qrcode'), {
            text: inviteUrl,
            width: 200,
            height: 200
        });
    }
}
</script>
<?php
$pageContent = ob_get_clean();
require_once __DIR__ . '/includes/layout.php'; 