﻿﻿﻿﻿﻿{include file="app/index/view/public/header"}

<article class="log-con">
    <nav class="crumb">
        <a href="{url($route='index/index/index', $params=array())}">首页</a> / 社区
    </nav>
    <h3 class="log-title">微社区</h3>

    <!-- 板块 Tab -->
    <div class="forum-tabs">
        <a href="{url($route='index/forum/index', $params=array())}" class="forum-tab <?php echo (int)($currentBid ?? 0) === 0 ? 'active' : ''; ?>">综合</a>
        <?php if (!empty($boards)): ?>
        <?php foreach ($boards as $b): ?>
        <a href="{url($route='index/forum/index/bid=<bid>', $params=array('bid' => (int)$b['id']))}" class="forum-tab <?php echo (int)($currentBid ?? 0) === (int)$b['id'] ? 'active' : ''; ?>">
            <?php echo htmlspecialchars($b['name'], ENT_QUOTES, 'UTF-8'); ?>
        </a>
        <?php endforeach; ?>
        <?php endif; ?>
    </div>

    <!-- 发帖组件 -->
    {include file="app/index/view/forum/_post_form"}

    <!-- 帖子列表（微博式） -->
    <div class="forum-list">
        <?php if (!empty($page['list'])): ?>
        <?php foreach ($page['list'] as $forum): ?>
        <?php
        $title = $forum['title'] ?? '';
        $poster = $forum['poster'] ?? '访客';
        if (empty($poster) && !empty($forum['uid'])) $poster = 'UID:'.$forum['uid'];
        $initial = mb_substr($poster ?: '访', 0, 1, 'UTF-8');
        $imagesArr = $forum['imagesArr'] ?? array();
        $goodsCards = $forum['goodsCards'] ?? array();
        $forumUrl = url($route='index/forum/view/id=<id>', $params=array('id' => (int)$forum['id']));
        $pfNames = array('tb'=>'淘宝/天猫','jd'=>'京东','pdd'=>'拼多多','vip'=>'唯品会');
        ?>
        <div class="forum-card">
            <div class="fc-header">
                <span class="fc-avatar"><?php echo htmlspecialchars($initial, ENT_QUOTES, 'UTF-8'); ?></span>
                <div class="fc-user">
                    <strong><?php echo htmlspecialchars($poster ?: '访客', ENT_QUOTES, 'UTF-8'); ?></strong>
                    <small class="text-muted"><?php echo htmlspecialchars($forum['date'] ?? '', ENT_QUOTES, 'UTF-8'); ?></small>
                </div>
                <span class="fc-stats">
                    <span title="浏览"><i class="far fa-eye"></i> <?php echo (int)$forum['view']; ?></span>
                    <span title="回复"><i class="far fa-comment"></i> <?php echo (int)$forum['reply_count']; ?></span>
                    <span title="点赞"><i class="far fa-heart"></i> <?php echo (int)$forum['like']; ?></span>
                </span>
            </div>

            <div class="fc-body">
                <a href="<?php echo $forumUrl; ?>" class="fc-title-link">
                    <?php if (!empty($title)): ?>
                    <h4 class="fc-title"><?php echo htmlspecialchars($title, ENT_QUOTES, 'UTF-8'); ?></h4>
                    <?php endif; ?>
                </a>
                <div class="fc-content"><?php echo $forum['content']; ?></div>

                <!-- 图片九宫格 -->
                <?php if (!empty($imagesArr)): ?>
                <div class="fc-images <?php echo count($imagesArr) == 1 ? 'fc-images-single' : 'fc-images-grid'; ?>">
                    <?php foreach ($imagesArr as $imgUrl): ?>
                    <div class="fc-img-item">
                        <a href="<?php echo htmlspecialchars($imgUrl, ENT_QUOTES, 'UTF-8'); ?>" target="_blank">
                            <img src="<?php echo htmlspecialchars($imgUrl, ENT_QUOTES, 'UTF-8'); ?>" alt="" loading="lazy">
                        </a>
                    </div>
                    <?php endforeach; ?>
                </div>
                <?php endif; ?>

                <!-- 商品卡片（多个） -->
                <?php if (!empty($goodsCards)): ?>
                <?php foreach ($goodsCards as $goodsCard): ?>
                <?php
                $pfName = $goodsCard['platformName'] ?? '';
                if (empty($pfName)) {
                    $pfName = $pfNames[$goodsCard['platform']] ?? $goodsCard['platform'];
                }
                $buyUrl = url('index/redirect/jump', array('platform' => $goodsCard['platform'], 'id' => $goodsCard['goodsId']));
                ?>
                <a href="<?php echo $buyUrl; ?>" target="_blank" class="fc-goods-card">
                    <?php if (!empty($goodsCard['pic'])): ?>
                    <img class="fc-goods-pic" src="<?php echo htmlspecialchars($goodsCard['pic'], ENT_QUOTES, 'UTF-8'); ?>" alt="">
                    <?php endif; ?>
                    <div class="fc-goods-info">
                        <div class="fc-goods-title"><?php echo htmlspecialchars($goodsCard['title'] ?? '', ENT_QUOTES, 'UTF-8'); ?></div>
                        <div class="fc-goods-price">
                            <span class="fc-goods-platform"><?php echo htmlspecialchars($pfName, ENT_QUOTES, 'UTF-8'); ?></span>
                            <?php if (!empty($goodsCard['actPrice'])): ?>
                            <span class="fc-goods-act">¥<?php echo number_format($goodsCard['actPrice'], 2); ?></span>
                            <?php endif; ?>
                            <?php if (!empty($goodsCard['origPrice']) && $goodsCard['origPrice'] > $goodsCard['actPrice']): ?>
                            <span class="fc-goods-orig">¥<?php echo number_format($goodsCard['origPrice'], 2); ?></span>
                            <?php endif; ?>
                            <?php if (!empty($goodsCard['coupon'])): ?>
                            <span class="fc-goods-coupon">券 ¥<?php echo number_format($goodsCard['coupon'], 2); ?></span>
                            <?php endif; ?>
                        </div>
                        <?php if (!empty($goodsCard['sales'])): ?>
                        <small class="text-muted">月销 <?php echo (int)$goodsCard['sales']; ?></small>
                        <?php endif; ?>
                    </div>
                </a>
                <?php endforeach; ?>
                <?php endif; ?>
            </div>

            <div class="fc-footer">
                <a href="<?php echo $forumUrl; ?>" class="fc-action">
                    <i class="far fa-comment"></i> <?php echo (int)$forum['reply_count']; ?>
                </a>
                <a href="javascript:;" class="fc-action fc-like-btn" data-id="<?php echo (int)$forum['id']; ?>">
                    <i class="far fa-heart"></i> <span class="like-count"><?php echo (int)$forum['like']; ?></span>
                </a>
                <a href="<?php echo $forumUrl; ?>" class="fc-action">
                    <i class="far fa-share-square"></i> 详情
                </a>
            </div>
        </div>
        <?php endforeach; ?>

        <!-- 分页 -->
        <?php if (!empty($page['page'])): ?>
        <div class="pagination-box">
            <?php echo $page['page']; ?>
        </div>
        <?php endif; ?>

        <?php else: ?>
        <div class="empty-state">
            <i class="far fa-comments fa-3x text-muted mb-3"></i>
            <h5 class="text-muted">暂无帖子</h5>
            <p class="text-muted">点击上方"发帖"按钮发布第一条</p>
        </div>
        <?php endif; ?>
    </div>
</article>

<!-- 论坛样式已整合到公共 forum.css -->
<link rel="stylesheet" href="{__PUBLIC__}web/css/forum.css?v=5.0.2">

{include file="app/index/view/public/footer"}

<script>
// 帖子点赞（列表页）
document.querySelectorAll('.fc-like-btn').forEach(function(btn) {
    btn.addEventListener('click', function() {
        var id = parseInt(this.dataset.id);
        if (!id) return;
        var self = this;
        var xhr = new XMLHttpRequest();
        xhr.open('POST', 'index.php?r=index/forum/likeForum&id=' + id, true);
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4 && xhr.status === 200) {
                try {
                    var r = JSON.parse(xhr.responseText);
                    alert(r.info);
                    if (r.status === 'y') {
                        self.classList.add('liked');
                        var counter = self.querySelector('.like-count');
                        if (counter) counter.textContent = r.count;
                    }
                } catch (e) {
                    alert('请求异常');
                }
            }
        };
        xhr.send();
    });
});
</script>
