<?php
/**
 * HuSNS - 移动端主题主布局文件
 * 已拆分为多个模块文件，便于维护
 */

// 引入配置文件
include $this->themePath() . '/layouts/partials/config.php';

// 检查护眼模式设置（支持 cookie 和 localStorage 同步）
$isDarkMode = isset($_COOKIE['theme']) && $_COOKIE['theme'] === 'dark';
$darkModeClass = $isDarkMode ? 'dark-mode' : '';
?>

<!DOCTYPE html>
<html lang="zh-CN">
<?php include $this->themePath() . '/layouts/partials/head.php'; ?>

<!-- 额外的样式文件 -->
<link rel="stylesheet" href="<?php echo $this->themeUrl(); ?>/css/dark-mode.css">

<!-- 页面特定CSS文件 -->
<?php if (isset($pageCssFiles) && is_array($pageCssFiles)): ?>
<?php foreach ($pageCssFiles as $cssFile): ?>
<link rel="stylesheet" href="<?php echo $cssFile; ?>">
<?php endforeach; ?>
<?php endif; ?>

<!-- 额外的 JavaScript 文件 -->
<script src="<?php echo $this->themeUrl(); ?>/js/app-layout.js"></script>
<script src="<?php echo $this->themeUrl(); ?>/js/dialog.js"></script>
<script src="<?php echo $this->themeUrl(); ?>/js/modal-controller.js"></script>
<script src="<?php echo $this->themeUrl(); ?>/js/auth-modal.js"></script>
<script src="<?php echo $this->themeUrl(); ?>/js/publish-modal.js"></script>
<script src="<?php echo $this->themeUrl(); ?>/js/publish-emoji.js"></script>
<script src="<?php echo $this->themeUrl(); ?>/js/publish-topic.js"></script>
<script src="<?php echo $this->themeUrl(); ?>/js/publish-location.js"></script>
<script src="<?php echo $this->themeUrl(); ?>/js/publish-at.js"></script>
<script src="<?php echo $this->themeUrl(); ?>/js/comment-modal.js"></script>
<script src="<?php echo $this->themeUrl(); ?>/js/share-modal.js"></script>
<script src="<?php echo $this->themeUrl(); ?>/app/js/video-player.js"></script>
<script src="<?php echo $this->themeUrl(); ?>/app/js/location-modal.js"></script>

<!-- 页面特定JS文件 -->
<?php if (isset($pageJsFiles) && is_array($pageJsFiles)): ?>
<?php foreach ($pageJsFiles as $jsFile): ?>
<script src="<?php echo $jsFile; ?>"></script>
<?php endforeach; ?>
<?php endif; ?>

<!-- 页面特定样式 -->
<?php if (isset($pageStyle)): ?>
<style><?php echo $pageStyle; ?></style>
<?php endif; ?>


<body class="mobile-body <?php echo $bodyClass ?? ''; ?> <?php echo $darkModeClass; ?>" 
      data-user-id="<?php echo isset($userId) ? $userId : ''; ?>"
      data-page-type="<?php echo $pageType ?? ''; ?>">
    
    <!-- 主内容区域 -->
    <div class="app-main">
        <!-- 页面内容 -->
        <?php echo $content; ?>
    </div>
    
    <!-- 弹窗组件 -->
    <?php include $this->themePath() . '/layouts/partials/modals.php'; ?>
    
    <!-- 底部导航栏 -->
    <?php if (!isset($hideTabBar) || !$hideTabBar): ?>
    <?php include $this->themePath() . '/layouts/partials/footer.php'; ?>
    <?php endif; ?>
    
    <!-- 页面特定脚本 -->
    <?php if (isset($pageScript)): ?>
    <script><?php echo $pageScript; ?></script>
    <?php endif; ?>

    <!-- 定位功能（与修改版一致） -->
    <script>
        // 跟踪用户是否主动设置了位置
        let locationSetByUser = false;

        // 页面加载时清除之前可能缓存的位置信息
        document.addEventListener('DOMContentLoaded', function() {
            const locationLatLng = document.getElementById('locationLatLng');
            if (locationLatLng) {
                locationLatLng.value = '';
            }
        });

        /**
         * 插入位置 - 使用浏览器GPS定位
         */
        function insertLocation() {
            // 检查浏览器是否支持地理位置
            if (!navigator.geolocation) {
                showToast('浏览器不支持定位功能');
                return;
            }
            
            showToast('正在获取位置...');
            
            navigator.geolocation.getCurrentPosition(
                function(position) {
                    const textarea = document.getElementById('publishContent');
                    if (!textarea) return;
                    
                    const lat = position.coords.latitude;
                    const lng = position.coords.longitude;
                    
                    // 保存经纬度到隐藏字段
                    let latLngInput = document.getElementById('locationLatLng');
                    if (!latLngInput) {
                        latLngInput = document.createElement('input');
                        latLngInput.type = 'hidden';
                        latLngInput.id = 'locationLatLng';
                        latLngInput.name = 'location_latlng';
                        latLngInput.autocomplete = 'off';
                        document.getElementById('publishForm').appendChild(latLngInput);
                    }
                    latLngInput.value = lat + ',' + lng;
                    locationSetByUser = true; // 只有成功获取位置后才设置为true
                    
                    const start = textarea.selectionStart;
                    const text = textarea.value;
                    const locationText = '📍[位置]';
                    
                    textarea.value = text.substring(0, start) + locationText + text.substring(start);
                    textarea.focus();
                    if (typeof updatePublishCharCount === 'function') {
                        updatePublishCharCount();
                    }
                },
                function(error) {
                    showToast('获取位置失败');
                }
            );
        }
    </script>
</body>
</html>