<?php
$pageTitle = '我的';
$pageCss = '/css/profile.css';
require_once '../includes/header.php';

// 获取公众号配置
$wechatEnabled = false;
$stmt = $db->query("SELECT value FROM settings WHERE group_name = 'wechat' AND key_name = 'wechat_enabled'");
$wechatRow = $stmt->fetch();
$wechatEnabled = $wechatRow && $wechatRow['value'] === '1';

// 检查用户是否已登录
$isLoggedIn = isset($_SESSION['user_id']) && $_SESSION['user_id'] > 0;

// 未登录时直接跳转登录页
if (!$isLoggedIn) {
    header('Location: ' . SITE_URL . '/pages/login.php');
    exit;
}

// 获取用户信息
$userId = $_SESSION['user_id'];
$user = null;
$stmt = $db->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$userId]);
$user = $stmt->fetch();

// 数据同步选项：根据后台菜单配置动态显示
// 映射：菜单page_url => 同步类型
$syncMenuMap = [
    'pages/schedule.php'   => ['type' => 'course',   'icon' => 'fa-calendar-alt', 'label' => '同步课表'],
    'pages/grades.php'     => ['type' => 'score',    'icon' => 'fa-graduation-cap', 'label' => '同步成绩'],
    'pages/academic.php'   => ['type' => 'progress', 'icon' => 'fa-chart-line', 'label' => '同步学业进度'],
    'pages/classroom.php'  => ['type' => 'classroom', 'icon' => 'fa-chalkboard', 'label' => '同步空教室'],
    'pages/exam.php'       => ['type' => 'exam',     'icon' => 'fa-clipboard-list', 'label' => '同步考试安排'],
];

// 查询后台已启用的菜单中包含教务相关page_url的项
$enabledSyncItems = [];
$stmt = $db->query("SELECT page_url FROM functions WHERE is_external = 0 AND page_url IS NOT NULL AND page_url != ''");
$enabledPages = [];
while ($row = $stmt->fetch()) {
    $enabledPages[] = $row['page_url'];
}
foreach ($syncMenuMap as $pageUrl => $syncInfo) {
    if (in_array($pageUrl, $enabledPages)) {
        $enabledSyncItems[] = $syncInfo;
    }
}
?>

<!-- 个人中心头部 -->
<div class="profile-header">
    <div class="profile-top">
        <div class="avatar">
            <?php if ($user && ($user['avatar'] || $user['wechat_avatar'])): ?>
            <img src="<?php echo htmlspecialchars($user['wechat_avatar'] ?: $user['avatar']); ?>" style="width: 100%; height: 100%; border-radius: 50%; object-fit: cover;">
            <?php else: ?>
            <i class="fas fa-user"></i>
            <?php endif; ?>
        </div>
        <div class="profile-info">
            <div class="user-name"><?php echo $user ? htmlspecialchars($user['wechat_nickname'] ?: $user['real_name'] ?: '未设置') : '未设置'; ?></div>
        </div>
    </div>
</div>

<?php if (!empty($enabledSyncItems)): ?>
<!-- 数据同步（跟随后台菜单配置） -->
<div class="menu-section">
    <div class="menu-section-title">数据同步</div>
    <div class="menu-group">
        <?php foreach ($enabledSyncItems as $item): ?>
        <div class="menu-item" onclick="syncData('<?php echo $item['type']; ?>')">
            <div class="menu-item-icon"><i class="fas <?php echo $item['icon']; ?>"></i></div>
            <div class="menu-item-title"><?php echo $item['label']; ?></div>
            <div class="menu-item-arrow"><i class="fas fa-chevron-right"></i></div>
        </div>
        <?php endforeach; ?>
    </div>
</div>
<?php endif; ?>

<!-- 其他 -->
<div class="menu-section">
    <div class="menu-section-title">其他</div>
    <div class="menu-group">
        <div class="menu-item" onclick="location.href='about.php'">
            <div class="menu-item-icon"><i class="fas fa-info-circle"></i></div>
            <div class="menu-item-title">关于我们</div>
            <div class="menu-item-arrow"><i class="fas fa-chevron-right"></i></div>
        </div>
        <?php if ($wechatEnabled && $isLoggedIn): ?>
        <div class="menu-item" onclick="location.href='<?php echo SITE_URL; ?>/pages/login.php'">
            <div class="menu-item-icon"><i class="fab fa-weixin"></i></div>
            <div class="menu-item-title">公众号绑定</div>
            <div class="menu-item-arrow"><i class="fas fa-chevron-right"></i></div>
        </div>
        <?php endif; ?>
    </div>
</div>

<script>
function syncData(type) {
    location.href = '<?php echo SITE_URL; ?>/pages/zsos-import.php?type=' + (type || 'course');
}
</script>

<!-- 底部导航 -->
<div class="bottom-nav">
    <div class="nav-item" onclick="location.href='../index.php'">
        <div class="nav-icon">
            <i class="fas fa-home"></i>
        </div>
        <div class="nav-name">大厅</div>
    </div>
    <div class="nav-item" onclick="location.href='schedule.php'">
        <div class="nav-icon">
            <i class="fas fa-calendar-alt"></i>
        </div>
        <div class="nav-name">课表</div>
    </div>
    <div class="nav-item active">
        <div class="nav-icon">
            <i class="fas fa-user"></i>
        </div>
        <div class="nav-name">我的</div>
    </div>
</div>

<?php require_once '../includes/footer.php'; ?>
