<?php $this->layout('default') ?>

<?php $this->start('title'); ?><?php echo htmlspecialchars($circle['name']); ?> - <?php echo Setting::getSiteName(); ?><?php $this->end(); ?>

<?php $this->start('content'); ?>
<div class="circle-detail">
    <!-- 圈子头部 -->
    <div class="circle-header-large" style="background-image: url(<?php echo !empty($circle['cover']) ? $this->uploadUrl($circle['cover']) : $this->assetUrl('images/default-banner.jpg'); ?>);">
        <div class="container">
            <div class="circle-info">
                <div class="circle-avatar-wrap">
                    <img src="<?php echo $circle['avatar'] ? $this->uploadUrl($circle['avatar']) : $this->assetUrl('images/default-circle.png'); ?>" 
                         alt="" class="circle-avatar-xl">
                    <?php if ($circle['is_verified']): ?>
                    <span class="verified-badge-large"><i class="fa fa-check-circle"></i></span>
                    <?php endif; ?>
                </div>
                <div class="circle-main-info">
                    <h1><?php echo htmlspecialchars($circle['name']); ?></h1>
                    <p class="circle-desc"><?php echo htmlspecialchars($circle['description']); ?></p>
                    <div class="circle-meta">
                        <span><?php echo $circle['member_count']; ?> 成员</span>
                        <span><?php echo $circle['post_count']; ?> 帖子</span>
                        <span>浏览</span>
                    </div>
                </div>
                <div class="circle-actions">
                    <?php if ($isMember): ?>
                    <button class="btn btn-secondary" onclick="leaveCircle(<?php echo $circle['id']; ?>)">
                        <i class="fa fa-sign-out"></i> 退出圈子
                    </button>
                    <?php else: ?>
                    <button class="btn btn-primary" onclick="joinCircle(<?php echo $circle['id']; ?>)">
                        <i class="fa fa-user-plus"></i> 加入圈子
                    </button>
                    <?php endif; ?>
                    <button class="btn btn-default" onclick="shareCircle()">
                        <i class="fa fa-share-alt"></i> 分享
                    </button>
                </div>
            </div>
        </div>
    </div>
    
    <div class="container">
        <div class="row">
            <!-- 主内容区 -->
            <div class="col-md-8">
                <!-- 发布帖子 -->
                <?php if ($isMember): ?>
                <div class="publish-box">
                    <textarea id="postContent" placeholder="分享你的想法..." rows="3" class="form-control"></textarea>
                    <div class="publish-actions">
                        <button class="btn btn-primary pull-right" onclick="publishPost(<?php echo $circle['id']; ?>)">
                            <i class="fa fa-paper-plane"></i> 发布
                        </button>
                    </div>
                </div>
                <?php endif; ?>
                
                <!-- 帖子列表 -->
                <div class="post-list">
                    <?php foreach ($posts as $post): ?>
                    <div class="post-card">
                        <div class="post-header">
                            <img src="<?php echo $post['avatar'] ? $this->uploadUrl($post['avatar']) : $this->assetUrl('images/default-avatar.png'); ?>" 
                                 alt="" class="avatar-md">
                            <div class="post-author">
                                <a href="<?php echo Helper::url('user/profile?id=' . $post['user_id']); ?>">
                                    <?php echo htmlspecialchars($post['username']); ?>
                                </a>
                                <span class="post-time"><?php echo Helper::formatTime($post['created_at']); ?></span>
                            </div>
                        </div>
                        <div class="post-content">
                            <?php echo htmlspecialchars($post['content']); ?>
                        </div>
                        <div class="post-actions">
                            <button class="action-btn" onclick="likePost(<?php echo $post['id']; ?>)">
                                <i class="fa fa-heart"></i> 点赞
                            </button>
                            <button class="action-btn" onclick="commentPost(<?php echo $post['id']; ?>)">
                                <i class="fa fa-comment"></i> 评论
                            </button>
                            <button class="action-btn" onclick="sharePost(<?php echo $post['id']; ?>)">
                                <i class="fa fa-share-alt"></i> 分享
                            </button>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
                
                <?php if (empty($posts)): ?>
                <div class="empty-state">
                    <i class="fa fa-file-text-o"></i>
                    <p>圈子里还没有帖子</p>
                    <?php if ($isMember): ?>
                    <p>成为第一个发帖的人吧！</p>
                    <?php endif; ?>
                </div>
                <?php endif; ?>
                
                <?php if ($totalPages > 1): ?>
                <div class="pagination-wrap">
                    <?php echo $this->pagination($page, $totalPages, 'circle/detail?id=' . $circle['id']); ?>
                </div>
                <?php endif; ?>
            </div>
            
            <!-- 侧边栏 -->
            <div class="col-md-4">
                <!-- 成员列表 -->
                <div class="sidebar-card">
                    <h3>圈子成员 <span class="badge"><?php echo $memberCount; ?></span></h3>
                    <div class="member-list">
                        <?php foreach ($members as $member): ?>
                        <div class="member-item">
                            <img src="<?php echo $member['avatar'] ? $this->uploadUrl($member['avatar']) : $this->assetUrl('images/default-avatar.png'); ?>" 
                                 alt="" class="avatar-sm">
                            <div class="member-info">
                                <a href="<?php echo Helper::url('user/profile?id=' . $member['user_id']); ?>">
                                    <?php echo htmlspecialchars($member['username']); ?>
                                </a>
                                <?php if ($member['role'] == 2): ?>
                                <span class="role-badge founder">创始人</span>
                                <?php elseif ($member['role'] == 1): ?>
                                <span class="role-badge admin">管理员</span>
                                <?php endif; ?>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    </div>
                    <?php if ($memberCount > count($members)): ?>
                    <a href="#" class="view-more">查看全部成员</a>
                    <?php endif; ?>
                </div>
                
                <!-- 圈子信息 -->
                <div class="sidebar-card">
                    <h3>圈子信息</h3>
                    <div class="info-list">
                        <div class="info-item">
                            <span class="info-label">类型</span>
                            <span class="info-value <?php echo empty($circle['is_private']) ? 'text-success' : 'text-warning'; ?>">
                                <?php echo empty($circle['is_private']) ? '公开圈子' : '私密圈子'; ?>
                            </span>
                        </div>
                        <div class="info-item">
                            <span class="info-label">状态</span>
                            <span class="info-value <?php echo $circle['status'] ? 'text-success' : 'text-danger'; ?>">
                                <?php echo $circle['status'] ? '正常' : '已关闭'; ?>
                            </span>
                        </div>
                        <div class="info-item">
                            <span class="info-label">创建时间</span>
                            <span class="info-value"><?php echo date('Y-m-d', $circle['created_at']); ?></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
function joinCircle(circleId) {
    $.post('<?php echo Helper::url('circle/join'); ?>', {
        circle_id: circleId,
        csrf_token: '<?php echo $_SESSION['csrf_token'] ?? ''; ?>'
    }, function(res) {
        if (res.success) {
            alert(res.message);
            window.location.reload();
        } else {
            alert(res.message);
        }
    }, 'json');
}

function leaveCircle(circleId) {
    if (confirm('确定要退出这个圈子吗？')) {
        $.post('<?php echo Helper::url('circle/leave'); ?>', {
            circle_id: circleId,
            csrf_token: '<?php echo $_SESSION['csrf_token'] ?? ''; ?>'
        }, function(res) {
            if (res.success) {
                alert(res.message);
                window.location.reload();
            } else {
                alert(res.message);
            }
        }, 'json');
    }
}

function publishPost(circleId) {
    var content = $('#postContent').val();
    if (!content.trim()) {
        alert('请输入内容');
        return;
    }
    
    $.post('<?php echo Helper::url('circle/publish'); ?>', {
        circle_id: circleId,
        content: content,
        csrf_token: '<?php echo $_SESSION['csrf_token'] ?? ''; ?>'
    }, function(res) {
        if (res.success) {
            alert(res.message);
            $('#postContent').val('');
            window.location.reload();
        } else {
            alert(res.message);
        }
    }, 'json');
}

function shareCircle() {
    var url = window.location.href;
    if (navigator.clipboard) {
        navigator.clipboard.writeText(url).then(function() {
            alert('链接已复制到剪贴板');
        });
    } else {
        prompt('复制链接:', url);
    }
}
</script>
<?php $this->end(); ?>
