<?php
/**
 * 芋头CMS - 会员中心框架
 * ============================================================================
 */

session_start();

if (!isset($_SESSION['member_id']) || empty($_SESSION['member_id'])) {
    header('Location: /member/login.php');
    exit;
}

require_once dirname(__FILE__) . '/../core/db/db.php';
require_once dirname(__FILE__) . '/../core/lib/common.php';
$db = \Core\Db\DB::getInstance();

$member = $db->fetch("SELECT m.*, g.group_name FROM {$db->table('member')} m LEFT JOIN {$db->table('member_group')} g ON m.group_id = g.id WHERE m.id = ?", [$_SESSION['member_id']]);

// 当前页面
$page = $_GET['page'] ?? 'dashboard';
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>会员中心 - 芋头CMS</title>
    <link rel="stylesheet" href="/public/css/site.css">
    <link rel="stylesheet" href="/public/css/member.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/lucide-static@latest/font/lucide.min.css">
</head>
<body>
    <!-- 顶部导航 -->
    <header class="member-topbar">
        <div class="topbar-inner">
            <div class="topbar-logo">
                <span class="logo-icon">🌿</span>
                <span class="logo-text">会员中心</span>
            </div>
            <nav class="topbar-nav">
                <a href="/" class="nav-link">网站首页</a>
                <a href="/member/logout.php" class="nav-link logout">退出登录</a>
            </nav>
        </div>
    </header>

    <div class="member-wrapper">
        <!-- 左侧菜单 -->
        <aside class="member-sidebar" id="sidebar">
            <div class="sidebar-header">
                <div class="user-card">
                    <div class="user-avatar">
                        <?php if (!empty($member['avatar'])): ?>
                        <img src="<?php echo htmlspecialchars($member['avatar']); ?>" alt="avatar">
                        <?php else: ?>
                        <span>👤</span>
                        <?php endif; ?>
                    </div>
                    <div class="user-info">
                        <div class="user-name"><?php echo htmlspecialchars($member['nickname'] ?: $member['username']); ?></div>
                        <div class="user-group"><?php echo htmlspecialchars($member['group_name'] ?? '普通会员'); ?></div>
                    </div>
                </div>
                <div class="user-stats">
                    <div class="stat-item">
                        <span class="stat-val"><?php echo $member['points'] ?? 0; ?></span>
                        <span class="stat-lbl">积分</span>
                    </div>
                    <a href="?page=recharge" class="stat-item" style="text-decoration:none;color:inherit;">
                        <span class="stat-val" style="color:#10b981;">充值</span>
                        <span class="stat-lbl">账户充值</span>
                    </a>
                    <div class="stat-item">
                        <span class="stat-val">¥<?php echo number_format($member['balance'] ?? 0, 2); ?></span>
                        <span class="stat-lbl">余额</span>
                    </div>
                </div>
            </div>

            <nav class="sidebar-nav">
                <div class="nav-section">
                    <div class="nav-section-title">账户管理</div>
                    <a href="?page=profile" class="nav-item <?php if($page=='profile') echo 'active'; ?>">
                        <i class="icon-user"></i>
                        <span>个人资料</span>
                    </a>
                    <a href="?page=password" class="nav-item <?php if($page=='password') echo 'active'; ?>">
                        <i class="icon-lock"></i>
                        <span>修改密码</span>
                    </a>
                </div>

                <div class="nav-section">
                    <div class="nav-section-title">我的内容</div>
                    <a href="?page=guestbook" class="nav-item <?php if($page=='guestbook') echo 'active'; ?>">
                        <i class="icon-message-square"></i>
                        <span>我的留言</span>
                    </a>
                    <a href="?page=mydownload" class="nav-item <?php if($page=='mydownload') echo 'active'; ?>">
                        <i class="icon-download"></i>
                        <span>我的下载</span>
                    </a>
                </div>

                <div class="nav-section">
                    <div class="nav-section-title">订单中心</div>
                    <a href="?page=myorder" class="nav-item <?php if($page=='myorder') echo 'active'; ?>">
                        <i class="icon-shopping-bag"></i>
                        <span>我的订单</span>
                    </a>
                    <a href="?page=cart" class="nav-item <?php if($page=='cart') echo 'active'; ?>">
                        <i class="icon-shopping-cart"></i>
                        <span>购物车</span>
                    </a>
                    <a href="?page=finance" class="nav-item <?php if($page=='finance') echo 'active'; ?>">
                        <i class="icon-dollar-sign"></i>
                        <span>财务明细</span>
                    </a>
                </div>

                <div class="nav-section">
                    <div class="nav-section-title">投稿中心</div>
                    <a href="?page=contribute" class="nav-item <?php if($page=='contribute') echo 'active'; ?>">
                        <i class="icon-file-text"></i>
                        <span>投稿列表</span>
                    </a>
                    <a href="?page=contribute_add" class="nav-item <?php if($page=='contribute_add') echo 'active'; ?>">
                        <i class="icon-edit-3"></i>
                        <span>我要投稿</span>
                    </a>
                </div>

                <div class="nav-section">
                    <div class="nav-section-title">收货管理</div>
                    <a href="?page=address" class="nav-item <?php if($page=='address') echo 'active'; ?>">
                        <i class="icon-map-pin"></i>
                        <span>收货地址</span>
                    </a>
                </div>
            </nav>
        </aside>

        <!-- 右侧内容 -->
        <main class="member-main">
            <?php
            $page_file = dirname(__FILE__) . '/pages/' . $page . '.php';
            if (file_exists($page_file)) {
                include $page_file;
            } else {
                echo '<div class="page-empty">页面不存在</div>';
            }
            ?>
        </main>
    </div>

    <script>
    // 侧边栏折叠
    document.getElementById('sidebar').addEventListener('click', function(e) {
        var target = e.target;
        if (target.nodeName === 'SPAN' || target.nodeName === 'I') {
            target = target.parentNode;
        }
        if (target.classList && target.classList.contains('nav-section-title')) {
            target.parentNode.classList.toggle('collapsed');
        }
    });
    </script>
</body>
</html>
