<?php
/**
 * HuSNS - 一款免费开源的社交平台
 * 
 * @author  HYR
 * @QQ      281900864
 * @website https://huyourui.com
 * @license MIT
 * @声明    严禁用于违法违规用途
 */
$currentPage = $currentPage ?? 'home';
$bodyClass = $bodyClass ?? '';
$pageType = $pageType ?? '';
$pageData = $pageData ?? [];
$siteName = Setting::getSiteName();

// 获取返回URL参数
$returnUrl = isset($_GET['return_url']) ? urldecode($_GET['return_url']) : '';
if (empty($returnUrl)) {
    $returnUrl = Helper::getBaseUrl() . '/';
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="default">
    <title><?= $this->escape($headerTitle ?? $siteName) ?></title>
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="<?= $this->themeUrl() ?>/static/mobile/app.css?v=1.3">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script>
        (function() {
            var theme = localStorage.getItem('theme');
            if (theme === 'dark' || (!theme && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
                document.body.classList.add('dark-mode');
            }
        })();
    </script>
</head>
<body class="mobile-body <?= $bodyClass ?>" 
      data-base-url="<?= Helper::getBaseUrl() ?>"
      data-page-type="<?= $pageType ?>"
      data-return-url="<?= $this->escape($returnUrl) ?>"
      <?php foreach ($pageData as $key => $value): ?>
      data-<?= $key ?>="<?= $this->escape($value) ?>"
      <?php endforeach; ?>>
    
    <header class="m-header">
        <div class="m-header-content">
            <?php if (isset($showBack) && $showBack): ?>
            <a href="javascript:history.back()" class="m-header-back">
                <i class="fa-solid fa-chevron-left"></i>
            </a>
            <?php else: ?>
            <a href="<?= $returnUrl ?>" class="m-header-back">
                <i class="fa-solid fa-desktop"></i>
            </a>
            <?php endif; ?>
            <h1 class="m-header-title"><?= $this->escape($headerTitle ?? $siteName) ?></h1>
            <div class="m-header-right">
                <?php if (isset($headerRight)): ?>
                <?= $headerRight ?>
                <?php endif; ?>
            </div>
        </div>
    </header>

    <main class="m-main">
        <?= $content ?>
    </main>

    <?php if (!isset($hideTabBar) || !$hideTabBar): ?>
    <nav class="m-tabbar">
        <a href="<?= $this->url('mobile') ?>" class="m-tabbar-item <?= $currentPage === 'home' ? 'active' : '' ?>">
            <i class="fa-solid fa-house m-tabbar-icon"></i>
            <span class="m-tabbar-label">首页</span>
        </a>
        <a href="<?= $this->url('mobile/hot') ?>" class="m-tabbar-item <?= $currentPage === 'hot' ? 'active' : '' ?>">
            <i class="fa-solid fa-fire m-tabbar-icon"></i>
            <span class="m-tabbar-label">热门</span>
        </a>
        <a href="javascript:void(0);" class="m-tabbar-item m-tabbar-publish" id="publishBtn">
            <i class="fa-solid fa-plus m-tabbar-icon-publish"></i>
        </a>
        <a href="<?= $this->url('mobile/notification') ?>" class="m-tabbar-item <?= $currentPage === 'notification' ? 'active' : '' ?>">
            <i class="fa-regular fa-bell m-tabbar-icon"></i>
            <span class="m-tabbar-label">消息</span>
            <span class="unread-badge" style="display:none;">0</span>
        </a>
        <a href="<?= $this->url('mobile/profile') ?>" class="m-tabbar-item <?= $currentPage === 'profile' ? 'active' : '' ?>">
            <i class="fa-regular fa-user m-tabbar-icon"></i>
            <span class="m-tabbar-label">我的</span>
        </a>
    </nav>
    
    <!-- 底部固定：返回PC版入口 -->
    <div class="m-desktop-link">
        <a href="<?= $returnUrl ?>">
            <i class="fa-solid fa-desktop"></i> 电脑版
        </a>
    </div>
    <?php endif; ?>

    <div class="loading-indicator" style="display:none;">
        <div class="loading-spinner"></div>
    </div>

    <!-- 登录弹窗 -->
    <div class="popup-overlay" id="loginPopup">
        <div class="popup-box">
            <div class="popup-header">
                <span class="popup-title">登录</span>
                <button class="popup-close" onclick="hidePopup('loginPopup')">
                    <i class="fa-solid fa-xmark"></i>
                </button>
            </div>
            <div class="popup-content">
                <div class="login-logo"><?= $siteName ?></div>
                <div class="form-group">
                    <input type="text" class="form-input" id="loginUsername" placeholder="用户名/邮箱">
                </div>
                <div class="form-group">
                    <input type="password" class="form-input" id="loginPassword" placeholder="密码">
                </div>
                <button class="btn-primary" id="loginBtn">登录</button>
                <div class="form-link">
                    <a href="javascript:void(0)" onclick="switchPopup('loginPopup','registerPopup')">还没有账号？立即注册</a>
                </div>
            </div>
        </div>
    </div>

    <!-- 注册弹窗 -->
    <div class="popup-overlay" id="registerPopup">
        <div class="popup-box">
            <div class="popup-header">
                <span class="popup-title">注册</span>
                <button class="popup-close" onclick="hidePopup('registerPopup')">
                    <i class="fa-solid fa-xmark"></i>
                </button>
            </div>
            <div class="popup-content">
                <div class="form-group">
                    <input type="text" class="form-input" id="regUsername" placeholder="用户名">
                </div>
                <div class="form-group">
                    <input type="email" class="form-input" id="regEmail" placeholder="邮箱">
                </div>
                <div class="form-group">
                    <input type="password" class="form-input" id="regPassword" placeholder="密码">
                </div>
                <button class="btn-primary" id="regBtn">注册</button>
                <div class="form-link">
                    <a href="javascript:void(0)" onclick="switchPopup('registerPopup','loginPopup')">已有账号？立即登录</a>
                </div>
            </div>
        </div>
    </div>

    <!-- 发布弹窗 -->
    <div class="popup-overlay" id="publishPopup">
        <div class="popup-box publish-box">
            <div class="popup-header">
                <span class="popup-title">发布动态</span>
                <button class="popup-close" onclick="hidePopup('publishPopup')">
                    <i class="fa-solid fa-xmark"></i>
                </button>
            </div>
            <div class="popup-content">
                <textarea class="pub-textarea" id="pubContent" placeholder="分享你的新鲜事..."></textarea>
                <div class="pub-images-preview" id="pubImagesPreview"></div>
                <div class="pub-toolbar">
                    <div class="pub-tools">
                        <button class="pub-tool" onclick="triggerImageUpload()">
                            <i class="fa-regular fa-image"></i>
                        </button>
                        <button class="pub-tool" onclick="insertTopic()">
                            <i class="fa-solid fa-hashtag"></i>
                        </button>
                    </div>
                    <button class="pub-submit" id="pubSubmitBtn">发布</button>
                </div>
            </div>
        </div>
        <input type="file" id="pubImageInput" accept="image/*" multiple style="display:none;">
    </div>

    <!-- 评论弹窗 -->
    <div class="popup-overlay" id="commentPopup">
        <div class="popup-box comment-box">
            <div class="popup-header">
                <span class="popup-title">评论</span>
                <button class="popup-close" onclick="hidePopup('commentPopup')">
                    <i class="fa-solid fa-xmark"></i>
                </button>
            </div>
            <div class="comment-popup-list" id="commentPopupList"></div>
            <div class="comment-input-box">
                <input type="text" id="commentInput" placeholder="发表评论...">
                <button onclick="submitComment()">
                    <i class="fa-solid fa-paper-plane"></i>
                </button>
            </div>
        </div>
    </div>

    <!-- 转发弹窗 -->
    <div class="popup-overlay" id="repostPopup">
        <div class="popup-box">
            <div class="popup-header">
                <span class="popup-title">转发</span>
                <button class="popup-close" onclick="hidePopup('repostPopup')">
                    <i class="fa-solid fa-xmark"></i>
                </button>
            </div>
            <div class="popup-content">
                <textarea class="pub-textarea" id="repostContent" placeholder="说点什么..."></textarea>
                <div class="repost-original" id="repostOriginal"></div>
                <div class="pub-toolbar">
                    <button class="pub-submit" id="repostSubmitBtn">转发</button>
                </div>
            </div>
        </div>
    </div>

    <script src="<?= $this->themeUrl() ?>/static/mobile/app.js?v=1.2"></script>
</body>
</html>
