<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit;
$db = \Typecho\Db::get();
$uid = (int)$this->user->uid;
$avatarUrl = Mirai_getUserAvatar($uid);
$userBaseUrl = \Typecho\Common::url('/user', $this->options->index);
$currentTab = isset($tab) ? $tab : 'overview';

$groupNames = ['subscriber' => '订阅者', 'contributor' => '贡献者', 'editor' => '编辑', 'administrator' => '管理员'];
$userGroup = isset($groupNames[$this->user->group]) ? $groupNames[$this->user->group] : $this->user->group;

$actionTable = $db->getPrefix() . 'mirai_actions';
$statsData = [
    'posts'     => ['label' => '文章',  'icon' => 'ri-file-text-line',   'color' => '#3b82f6', 'bg' => '59,130,246',  'count' => (int)$db->fetchObject($db->select('COUNT(*) AS num')->from('table.contents')->where('authorId = ?', $uid)->where('type = ?', 'post'))->num, 'url' => $userBaseUrl . '/posts'],
    'comments'  => ['label' => '评论',  'icon' => 'ri-chat-3-line',       'color' => '#22c55e', 'bg' => '34,197,94',    'count' => (int)$db->fetchObject($db->select('COUNT(*) AS num')->from('table.comments')->where('authorId = ?', $uid))->num, 'url' => $userBaseUrl . '/comments'],
    'likes'     => ['label' => '点赞',  'icon' => 'ri-thumb-up-line',      'color' => '#ec4899', 'bg' => '236,72,153',   'count' => 0, 'url' => $userBaseUrl . '/likes'],
    'favorites' => ['label' => '收藏',  'icon' => 'ri-star-line',         'color' => '#06b6d4', 'bg' => '6,182,212',    'count' => 0, 'url' => $userBaseUrl . '/favorites'],
];
try { $statsData['likes']['count'] = (int)$db->fetchObject($db->select('COUNT(*) AS num')->from($actionTable)->where('uid = ?', $uid)->where('type = ?', 'like'))->num; } catch (\Exception $e) {}
try { $statsData['favorites']['count'] = (int)$db->fetchObject($db->select('COUNT(*) AS num')->from($actionTable)->where('uid = ?', $uid)->where('type = ?', 'collect'))->num; } catch (\Exception $e) {}

$wallet = function_exists('Mirai_payGetWallet') ? Mirai_payGetWallet($uid) : null;
$walletBalance = isset($wallet['balance']) ? (float)$wallet['balance'] : 0;

$pointsEnabled = false;
$pointsBalance = 0;
$pointsName = '积分';
if (function_exists('Mirai_pointsEnabled') && Mirai_pointsEnabled()) {
    $pointsEnabled = true;
    $pointsBalance = function_exists('Mirai_pointsGetBalance') ? Mirai_pointsGetBalance($uid) : 0;
    $pointsName = function_exists('Mirai_pointsName') ? Mirai_pointsName() : '积分';
}

$menuGroups = [
    'create' => [
        'title' => '创作中心',
        'items' => [
            ['key' => 'write',  'icon' => 'ri-edit-line',           'label' => '文章投稿'],
            ['key' => 'posts',  'icon' => 'ri-file-list-3-line',    'label' => '我的文章'],
            ['key' => 'income', 'icon' => 'ri-money-cny-circle-line','label' => '收益中心'],
        ],
    ],
    'site' => [
        'title' => '站点功能',
        'items' => [
            ['key' => 'orders',   'icon' => 'ri-file-list-2-line',   'label' => '订单列表'],
        ],
    ],
    'account' => [
        'title' => '个人设置',
        'items' => [
            ['key' => 'profile',  'icon' => 'ri-user-settings-line',  'label' => '基本资料'],
            ['key' => 'password', 'icon' => 'ri-lock-password-line',   'label' => '修改密码'],
        ],
    ],
];

if (function_exists('Mirai_levelEnabled') && Mirai_levelEnabled()) {
    array_splice($menuGroups['site']['items'], 1, 0, [['key' => 'level', 'icon' => 'ri-bar-chart-grouped-line', 'label' => '我的等级']]);
}
if (function_exists('Mirai_vipEnabled') && Mirai_vipEnabled()) {
    $vipInsertPos = 1 + (isset($menuGroups['site']['items'][1]) && $menuGroups['site']['items'][1]['key'] === 'level' ? 1 : 0);
    array_splice($menuGroups['site']['items'], $vipInsertPos, 0, [['key' => 'vip', 'icon' => 'ri-vip-diamond-line', 'label' => '开通会员']]);
}
if (function_exists('Mirai_referralIsEnabled') && Mirai_referralIsEnabled()) {
    $menuGroups['site']['items'][] = ['key' => 'referral', 'icon' => 'ri-share-forward-line', 'label' => '推广返佣'];
}
?>

<div class="user-sidebar">
    <div class="sidebar-card sidebar-wealth">
        <div class="sidebar-card-title">我的财富</div>
        <div class="wealth-row">
            <a href="<?php echo $userBaseUrl . '/wallet'; ?>" class="wealth-item">
                <span class="wealth-label">余额</span>
                <span class="wealth-value wealth-money">￥<?php echo number_format($walletBalance, 2); ?></span>
                <i class="ri-arrow-right-s-line"></i>
            </a>
            <?php if ($pointsEnabled): ?>
            <a href="<?php echo $userBaseUrl . '/wallet'; ?>" class="wealth-item">
                <span class="wealth-label"><?php echo htmlspecialchars($pointsName); ?></span>
                <span class="wealth-value wealth-points"><?php echo number_format($pointsBalance, 0); ?></span>
                <i class="ri-arrow-right-s-line"></i>
            </a>
            <?php endif; ?>
        </div>
    </div>

    <div class="sidebar-card sidebar-stats">
        <div class="sidebar-card-title">我的统计</div>
        <div class="stat-grid">
            <?php foreach ($statsData as $s): ?>
            <a href="<?php echo $s['url']; ?>" class="stat-item">
                <div class="stat-icon" style="color:<?php echo $s['color']; ?>;background:rgba(<?php echo $s['bg']; ?>,0.08)"><i class="<?php echo $s['icon']; ?>"></i></div>
                <span class="stat-label"><?php echo $s['label']; ?></span>
                <span class="stat-count"><?php echo $s['count']; ?></span>
            </a>
            <?php endforeach; ?>
        </div>
    </div>

    <?php foreach ($menuGroups as $group): ?>
    <div class="sidebar-card sidebar-nav">
        <div class="sidebar-card-title"><?php echo $group['title']; ?></div>
        <nav class="sidebar-menu">
            <?php foreach ($group['items'] as $item): ?>
            <a href="<?php echo $userBaseUrl . '/' . $item['key']; ?>" class="menu-item <?php echo $currentTab === $item['key'] ? 'active' : ''; ?>">
                <div class="menu-icon"><i class="<?php echo $item['icon']; ?>"></i></div>
                <span><?php echo $item['label']; ?></span>
            </a>
            <?php endforeach; ?>
        </nav>
    </div>
    <?php endforeach; ?>

    <a href="<?php $this->options->logoutUrl(); ?>" class="sidebar-logout">
        <i class="ri-logout-box-r-line"></i> 退出登录
    </a>
</div>
