<?php
/**
 * HuSNS - 一款免费开源的社交平台
 *
 * @author  HYR
 * @QQ      281900864
 * @website https://huyourui.com
 * @license MIT
 * @声明    严禁用于违法违规用途
 */
?>
<!-- 左侧栏 -->
<aside class="sidebar-left">
    <?php if (isset($_SESSION['user_id'])): ?>
    <div class="user-card">
            <img src="/templates/defaultpro/images/avatar_default.png" alt="" style="width:70px;height:70px;border-radius:50%;object-fit:cover;">
            <div class="user-card-name"><?php echo $this->escape($_SESSION['username'] ?? '用户'); ?></div>
            <div class="user-card-bio">这个人很懒，什么都没写</div>
            <div class="user-card-stats">
                <div class="stat-item"><div class="stat-value">-</div><div class="stat-label">动态</div></div>
                <div class="stat-item"><div class="stat-value">-</div><div class="stat-label">关注</div></div>
                <div class="stat-item"><div class="stat-value">-</div><div class="stat-label">粉丝</div></div>
            </div>
        </div>
        <?php else: ?>
        <div class="user-card">
            <img src="/templates/defaultpro/images/avatar_default.png" alt="" style="width:70px;height:70px;border-radius:50%;object-fit:cover;">
            <div class="user-card-name">游客</div>
            <div class="user-card-bio">登录后体验更多功能</div>
            <div class="user-card-stats">
                <div class="stat-item"><div class="stat-value">-</div><div class="stat-label">动态</div></div>
                <div class="stat-item"><div class="stat-value">-</div><div class="stat-label">关注</div></div>
                <div class="stat-item"><div class="stat-value">-</div><div class="stat-label">粉丝</div></div>
            </div>
        </div>
    <?php endif; ?>
    <?php if (!isset($_SESSION['user_id'])): ?>
    <div class="guest-tip">
        <p><a href="<?php echo $this->url('user/login'); ?>">登录</a> | <a href="<?php echo $this->url('user/register'); ?>">注册</a></p>
    </div>
    <?php endif; ?>

    <ul class="menu">
        <li><a href="<?php echo $this->url(); ?>">🏠 首页</a></li>
        <li><a href="<?php echo $this->url('?tab=following'); ?>">❤️ 关注</a></li>
        <li><a href="<?php echo $this->url('post/hot'); ?>">🔥 热门</a></li>
        <li><a href="<?php echo $this->url('post/featured'); ?>">✨ 推荐</a></li>
        <li><a href="<?php echo $this->url('notification'); ?>">💬 消息</a></li>
        <?php if (isset($_SESSION['user_id'])): ?>
        <li><a href="<?php echo $this->url('user/profile'); ?>">👤 我的主页</a></li>
        <li><a href="<?php echo $this->url('user/settings'); ?>">⚙️ 设置</a></li>
        <?php else: ?>
        <li><a href="<?php echo $this->url('user/login'); ?>">🔑 登录</a></li>
        <li><a href="<?php echo $this->url('user/register'); ?>">📝 注册</a></li>
        <?php endif; ?>
    </ul>

    <div class="side-card">
        <div class="side-title">快捷操作</div>
        <ul class="menu">
            <li><a href="#" onclick="showPublishPopup();return false;">✍️ 发布动态</a></li>
            <li><a href="<?php echo $this->url('user/search'); ?>">🔍 查找用户</a></li>
            <li><a href="<?php echo $this->url('post/topic'); ?>">🏷️ 话题广场</a></li>
        </ul>
    </div>
</aside>

<!-- 中间内容 -->
<section class="home-main">
    <div class="page-privacy">
    <h2>隐私设置</h2>
    
    <?php if ($this->hasFlash('success')): ?>
    <div class="alert alert-success"><?php echo $this->flash('success'); ?></div>
    <?php endif; ?>
    
    <?php if ($this->hasFlash('error')): ?>
    <div class="alert alert-error"><?php echo $this->flash('error'); ?></div>
    <?php endif; ?>
    
    <form method="post" id="privacyForm">
        <?php echo $this->csrf(); ?>
        
        <!-- 动态可见性 -->
        <div class="privacy-section">
            <h3>动态可见性</h3>
            <div class="privacy-item">
                <label class="privacy-label">
                    <input type="radio" name="post_visibility" value="public" <?php echo ($privacy['post_visibility'] ?? 'public') === 'public' ? 'checked' : ''; ?>>
                    <span>公开</span>
                </label>
                <span class="privacy-desc">所有人都可以看到你的动态</span>
            </div>
            <div class="privacy-item">
                <label class="privacy-label">
                    <input type="radio" name="post_visibility" value="followers" <?php echo ($privacy['post_visibility'] ?? 'public') === 'followers' ? 'checked' : ''; ?>>
                    <span>仅关注者可见</span>
                </label>
                <span class="privacy-desc">只有关注你的人可以看到你的动态</span>
            </div>
            <div class="privacy-item">
                <label class="privacy-label">
                    <input type="radio" name="post_visibility" value="private" <?php echo ($privacy['post_visibility'] ?? 'public') === 'private' ? 'checked' : ''; ?>>
                    <span>仅自己可见</span>
                </label>
                <span class="privacy-desc">只有你自己可以看到你的动态</span>
            </div>
        </div>
        
        <!-- 个人资料可见性 -->
        <div class="privacy-section">
            <h3>个人资料</h3>
            <div class="privacy-item">
                <label class="privacy-label">
                    <input type="checkbox" name="show_bio" value="1" <?php echo isset($privacy['show_bio']) && $privacy['show_bio'] ? 'checked' : ''; ?>>
                    <span>显示个人简介</span>
                </label>
            </div>
            <div class="privacy-item">
                <label class="privacy-label">
                    <input type="checkbox" name="show_stats" value="1" <?php echo isset($privacy['show_stats']) && $privacy['show_stats'] ? 'checked' : ''; ?>>
                    <span>显示动态、关注、粉丝数量</span>
                </label>
            </div>
            <div class="privacy-item">
                <label class="privacy-label">
                    <input type="checkbox" name="show_points" value="1" <?php echo isset($privacy['show_points']) && $privacy['show_points'] ? 'checked' : ''; ?>>
                    <span>显示积分等级</span>
                </label>
            </div>
        </div>
        
        <!-- 消息设置 -->
        <div class="privacy-section">
            <h3>消息设置</h3>
            <div class="privacy-item">
                <label class="privacy-label">
                    <input type="checkbox" name="allow_messages" value="1" <?php echo isset($privacy['allow_messages']) && $privacy['allow_messages'] ? 'checked' : ''; ?>>
                    <span>允许所有人发私信</span>
                </label>
                <span class="privacy-desc">关闭后仅关注的人可以给你发私信</span>
            </div>
            <div class="privacy-item">
                <label class="privacy-label">
                    <input type="checkbox" name="allow_mentions" value="1" <?php echo isset($privacy['allow_mentions']) && $privacy['allow_mentions'] ? 'checked' : ''; ?>>
                    <span>允许被@提及</span>
                </label>
            </div>
        </div>
        
        <!-- 搜索设置 -->
        <div class="privacy-section">
            <h3>搜索设置</h3>
            <div class="privacy-item">
                <label class="privacy-label">
                    <input type="checkbox" name="searchable" value="1" <?php echo isset($privacy['searchable']) && $privacy['searchable'] ? 'checked' : ''; ?>>
                    <span>允许被搜索到</span>
                </label>
                <span class="privacy-desc">关闭后其他人无法通过搜索找到你</span>
            </div>
        </div>
        
        <!-- 黑名单 -->
        <div class="privacy-section">
            <h3>黑名单</h3>
            <div class="block-list">
                <?php if (!empty($blocked_users)): ?>
                    <?php foreach ($blocked_users as $user): ?>
                    <div class="block-item">
                        <img src="<?php echo $this->escape($user['avatar'] ?? '/templates/defaultpro/images/avatar_default.png'); ?>" alt="头像" class="block-avatar">
                        <span class="block-name"><?php echo $this->escape($user['username']); ?></span>
                        <button class="unblock-btn" data-user-id="<?php echo $user['id']; ?>">解除屏蔽</button>
                    </div>
                    <?php endforeach; ?>
                <?php else: ?>
                    <div class="empty-state">
                        <div class="empty-icon">🚫</div>
                        <p>暂无屏蔽用户</p>
                    </div>
                <?php endif; ?>
            </div>
        </div>
        
        <button type="submit" class="btn btn-primary">保存设置</button>
    </form>
    </div>
</section>

<!-- 右侧栏 -->
<aside class="sidebar-right">
    <div class="side-card">
        <div class="side-title">推荐关注</div>
        <div id="recommend-users">
            <div class="empty-tip">登录后查看推荐</div>
        </div>
    </div>

    <div class="side-card">
        <div class="side-title">热门话题</div>
        <div id="hot-topics">
            <div class="empty-tip">登录后查看热门话题</div>
        </div>
    </div>
</aside>

<script>
(function() {
    // 解除屏蔽
    var unblockBtns = document.querySelectorAll('.unblock-btn');
    unblockBtns.forEach(function(btn) {
        btn.addEventListener('click', function() {
            var userId = this.dataset.userId;
            var btn = this;
            
            if (!confirm('确定要解除屏蔽该用户吗？')) return;
            
            fetch('<?php echo $this->url('user/unblock'); ?>', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: 'user_id=' + userId + '&csrf_token=' + '<?php echo $_SESSION['csrf_token'] ?? ''; ?>'
            }).then(function(response) {
                return response.json();
            }).then(function(data) {
                if (data.code === 0) {
                    btn.parentElement.remove();
                    alert('已解除屏蔽');
                } else {
                    alert(data.message || '操作失败');
                }
            }).catch(function() {
                alert('网络错误');
            });
        });
    });
})();
</script>