<template>
    <div>
        <!-- 页面头部 -->
        <HomePageHead></HomePageHead>
        <HomePageNavigation1></HomePageNavigation1>

        <!-- 商圈详情 -->
        <div class="topicInfoBox">
            <div class="inner">

                <!-- 详情头部 -->
                <div class="infoHead">
                    <div class="left">
                        <div class="userInfo left">
                            <el-badge value="楼主" class="item" type="warning">
                                <img v-if='dataInfo.avatar' :src="dataInfo.avatar" alt="">
                                <img v-else src='http://img.bjzxtw.org.cn/master/bjzxtw/public/topic/Rectangle.png' alt="">
                            </el-badge>
                            <span>{{ dataInfo.author }}</span>
                        </div>
                        <div class="headContent left">
                            <h2>{{ dataInfo.title }}</h2>
                            <p v-show="dataInfo.group_name">
                                群聊：
                                <span>{{ dataInfo.group_name }}</span>
                                <button @click="joinGroup">加入群聊</button>
                            </p>
                        </div>
                    </div>
                    <div class="right">
                        <div class="one">
                            <span v-for="item in typeList" :key="index">
                                <span v-if="dataInfo.type == item.value" class="one">{{item.label}}</span> 
                            </span>
                        </div>
                        <p>{{ dataInfo.updated_at }}</p>
                    </div>
                </div>

                <!-- 详情页 文本+图片 -->
                <div class="infoContent" v-show="dataInfo.content">
                    <div v-html="dataInfo.content"></div>
                </div>

                <div class="infoContent1" v-show="!dataInfo.content">
                    <img src="http://img.bjzxtw.org.cn/master/bjzxtw/public/topic/Document_empty.png" alt="">
                    <span>可以看看大家的互动哦~</span>
                </div>

                <!-- 详情页评论 -->
                <div class="comment">
                    <h3>评论</h3>


                    <div class="commentList" v-for="item in replyList" v-show="page_total != 0">
                        <div class="left">
                            <img v-if='item.avatar' :src="item.avatar" alt="" >
                            <img v-else src='http://img.bjzxtw.org.cn/master/bjzxtw/public/topic/Rectangle.png' alt="">
                            <span class="name">{{ item.author }} : </span>
                            
                        </div>
                        <div class="center">
                            <span class="context">{{ item.content }}</span>
                        </div>
                        <div class="right">
                            <span class="time">{{ item.created_at }}</span>
                            <!-- <span>回复</span> -->
                        </div>
                    </div>



                    <div class="comment_empty" v-show="page_total == 0">
                        <img src="../../public/topic/message_empty.png" alt="">
                        <span>暂无评论</span>
                    </div>
                </div>

                <!-- 分页 -->
                <div class="paginationBox" v-show="page_total != 0">
                    <el-pagination background layout="prev, pager, next" :total="page_total" prev-text="上一页"
                        next-text="下一页" :default-page-size="pageSize" @change="changePage" />
                </div>
            </div>
        </div>
        <div class="message">
            <div class="inner">
                <input v-model="content" placeholder="快来回复吧~" type="text" class="messageInput">
                <button class="btn" @click="addReply">确定</button>
            </div>
        </div>
        <!-- 页面底部 -->
        <HomeFoot1></HomeFoot1>

    </div>
</template>

<script setup>
//1.引用模块 start ---------------------------------------->
//使用ref和reactive动态变量
import { ref, reactive, onMounted } from 'vue'
import { useRoute } from 'vue-router';
//使用官方ssr请求模块
import { useNuxtApp, useFetch } from '#app'
//使用element-plus组件
import { ElPagination, ElBadge, ElInput, ElMessage, ElMessageBox } from 'element-plus';

// axios请求
const nuxtApp = useNuxtApp();
const axios = nuxtApp.$axios;

const { $webUrl, $CwebUrl, $BwebUrl } = useNuxtApp()
//1.引用模块 end ---------------------------------------->

// 定义响应式数据
const seoData = ref({
    title: '商圈',
    description: '默认描述',
    keywords: '默认关键词',
    image: 'https://example.com/default-image.jpg'
});

// 在 onMounted 钩子中获取数据
onMounted(()=>{
    seoData.value.title = '商圈';
    seoData.value.description = '默认描述';
    seoData.value.keywords = '默认关键词';
})

//2.页面数据 start ---------------------------------------->
const dataInfo = ref({})
const groupId = useState("groupId", () => '')

const replyList = useState("replyList", () => []) //评论列表

//分页
const page = ref(1)
const pageSize = ref(5)
let page_total = ref(0)

const content = ref('')
//2.页面数据 end ---------------------------------------->


//3.获取商圈详情 start ---------------------------------------->
const route = useRoute();
const id = route.params.id; // 获取传递的 id 参数


//页码发生改变
const changePage = (val) => {
    console.log(val);
    page.value = val
    getTopicReply()
}

// 商圈信息
const getTopicInfo = () => {
    let data = new FormData()
    data.append('id', id)
    axios.post('chat/getTopicInfo', data).then(res => {
        console.log('商圈信息', res);
        dataInfo.value = res.data
        groupId.value = res.data.group_id
        console.log(groupId.value);
    })
}

// 回复商圈 列表
const getTopicReply = () => {
    let data = new FormData()
    data.append('id', id)
    data.append('page', page.value)
    data.append('page_size', pageSize.value)
    axios.post('chat/getTopicReply', data).then(res => {
        console.log('回复商圈列表 ', res);
        replyList.value = res.data.data
        // page_total.value = res.data.total

        page_total.value = res.data.total

        console.log("replyList", replyList);
    })
}


//5.获取分类和状态 start ---------------------------------------->
const listData = useState("listData", () => [])//商圈列表
const typeList = useState("typeList", () => [])//商圈分类
const topicType = () => {
    axios.post('/chat/topicType').then(response => {
        console.log(111222333)
        console.log(response.data);
        // console.log(1);
        typeList.value = response.data;
    })
}

const topicStatus = () => {
    axios.post('chat/topicStatus').then(response => {
        console.log(response);
    })
}


onMounted(() => {
    getTopicInfo();   //商圈信息
    getTopicReply();  //回复商圈列表
    topicType();
    topicStatus();
})

//加入群聊
const joinGroup = () => {
    ElMessageBox.confirm(
        '加入群聊后,页面跳转至后台群聊页面',
        '是否加入群聊?',
        {
            confirmButtonText: '是',
            cancelButtonText: '否',
            center: true,
        }
    ).then(() => {
        console.log('groupId.value', groupId.value);

        let data = new FormData()
        data.append('group_id', groupId.value)
        axios.post('chat/joinGroup', data).then(res => {
            console.log('加入群聊 ', res);
            if (res.code == 0 && res.message !== '已加入群') {
                ElMessage.error(res.message)
            } else if (res.code == 0 && res.message == '已加入群') {
                ElMessage({
                    message: res.message,
                    type: 'success',
                })
                setTimeout(() => {
                    window.open($BwebUrl + '/#/hall?userurl=' + $webUrl, '_blank');
                    //window.open('http://admindev.bjzxtw.org.cn/#/hall', '_blank');
                }, 1000)
            } else if (res.code == 200) {
                ElMessage({
                    message: '加入成功',
                    type: 'success',
                })
                setTimeout(() => {
                    window.open($BwebUrl + '/#/hall?userurl=' + $webUrl, '_blank');
                    //window.open('http://admindev.bjzxtw.org.cn/#/hall', '_blank');
                }, 1000)
            }

        })

    }).catch(() => {
        ElMessage({
            type: 'error',
            message: '已取消',
        })
    })
}

// 回复商圈
const addReply = () => {
    let data = new FormData()
    data.append('id', id)
    data.append('content', content.value)
    axios.post('chat/addReply', data).then(res => {
        console.log('回复商圈 ', res);
        if (res.code == 0) {
            ElMessage.error(res.message)
            getTopicReply();
        } else if (res.code == 200) {
            ElMessage({
                message: '回复成功',
                type: 'success',
            })
            content.value = ''
            getTopicReply();
        }
    })
}

//3.获取商圈详情 end ---------------------------------------->
//seo
useSeoMeta({
    title: "商圈" + "_" + "三农资讯网_全国政务信息一体化应用平台",
    meta: [
        { name: 'description', content: "三农资讯网以服务于党和国家各级职能部门三农政务资讯发布及政务公开信息公示为基点，为全国县级以上各级党政机关及其职能部门配置各自公开独立网络发布平台。主要频道有：三农政务资讯，农科资讯，农资购销，农产购销，农贸资讯，三农致富信息，农村文化生活，三农政策法规，三农之窗，三农调查，三农服务，三农知识，农民工，打假维权等。三农资讯网打造最具影响力的三农信息发布平台，并成为最具权威的三农资讯网。_三农资讯网-测试专用_全国政务信息一体化应用平台" , tagPriority: 10 },
        { name: 'keywords', content: "三农资讯,农业,三农,三农在线,农业新闻,三农资讯网_三农资讯网-测试专用_全国政务信息一体化应用平台" , tagPriority: 10 }
    ]
});
</script>

<style lang="less" scoped>
.topicInfoBox {

    .inner {
        width: 1200px;
        margin: 0 auto;

        //信息头部
        .infoHead {
            height: 200px;
            padding: 65px 0 60px 40px;
            border-bottom: 1px solid rgba(0, 0, 0, 0.10);
            box-sizing: border-box;

            >.left {
                .userInfo {
                    margin-right: 90px;

                    .item {
                        margin-top: -5px;
                        margin-right: -5px;

                        :deep(.el-badge__content.is-fixed) {
                            position: absolute;
                            right: calc(1px + var(--el-badge-size) / 2);
                            top: 0;
                            transform: translateY(-31%) translateX(79%);
                            z-index: var(--el-index-normal);
                        }

                        img {
                            width: 66px;
                            height: 66px;
                            vertical-align: middle;
                            border-radius: 50%;
                        }
                    }

                    span {
                        margin-left: 15px;
                        font-family: Microsoft YaHei, Microsoft YaHei;
                        font-weight: 400;
                        font-size: 18px;
                        color: #333333;

                    }
                }

                .headContent {
                    h2 {
                        width: 750px;
                        height: 56px;
                        line-height: 28px;
                        display: -webkit-box;
                        -webkit-box-orient: vertical;
                        -webkit-line-clamp: 2;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        word-break: break-all;
                        font-family: Microsoft YaHei, Microsoft YaHei;
                        font-weight: bold;
                        font-size: 20px;
                        color: #333333;
                        margin-bottom: 26px;
                    }

                    >p {
                        font-family: Microsoft YaHei, Microsoft YaHei;
                        font-weight: 400;
                        font-size: 16px;
                        color: #333333;

                        button {
                            width: 86px;
                            height: 32px;
                            background-color: #028e21;
                            color: #fff;
                            border: none;
                            margin-left: 100px;
                        }
                    }
                }
            }

            >.right {
                position: relative;

                div {
                    position: absolute;
                    right: 0;
                    top: 0;
                    width: 71px;
                    height: 32px;
                    line-height: 32px;
                    text-align: center;
                    background-color: #d9f0d6;
                    margin-bottom: 29px;
                    font-family: Microsoft YaHei, Microsoft YaHei;
                    font-weight: 400;
                    font-size: 16px;
                    color: #33B023;

                }

                .one {
                    color: #FFAA33;
                    background-color: #fbebd5;
                }

                .two {
                    color: #33B023;
                    background-color: #d5ecd2;
                }

                .three {
                    color: #666;
                    background-color: #ebebeb;
                }

                p {
                    width: 150px;
                    text-align: right;
                    position: absolute;
                    top: 62px;
                    right: 0;
                }
            }
        }

        // 有详情信息
        .infoContent {
            width:1196px;
            overflow: hidden;
            padding: 40px 46px;
            font-family: Microsoft YaHei, Microsoft YaHei;
            font-size: 20px;
            line-height: 40px;
            box-sizing: border-box;
            div{
                word-wrap: break-word;       /* 强制长单词或 URL 换行 */
                overflow-wrap: break-word;   /* 现代推荐用法，等同于 word-wrap */
                white-space: pre-wrap;       /* 保留空白字符并允许自动换行 */
                word-break: break-all;       /* 强制在任意字符处换行 */
            }
        }
        .leftBottom::v-deep p{
            width:1200px;
            white-space: pre-wrap;
        }

        //没有详情信息
        .comment_empty,
        .infoContent1 {
            height: 300px;
            line-height: 260px;
            text-align: center;

            img {
                width: 78px;
                height: 78px;
                vertical-align: -25px;
                vertical-align: middle;
                margin-right: 20px;
            }

            span {
                font-family: Microsoft YaHei, Microsoft YaHei;
                font-weight: bold;
                font-size: 26px;
                color: #CCCCCC;
            }

        }

        //评论
        .comment {

            h3 {
                height: 100px;
                padding-top: 30px;
                box-sizing: border-box;
                border-bottom: 1px solid #E1E1E1;
                font-family: Microsoft YaHei, Microsoft YaHei;
                font-weight: 400;
                font-size: 22px;
                color: #000000;
            }

            .commentList {
                // height: 112px;
                border: 1px solid #E1E1E1;
                background-color: #fafafa;
                margin-top: 20px;
                padding: 30px 30px;
                box-sizing: border-box;
                display: flex;
                align-items: flex-start;
                justify-content: space-between;

                .left {
                    width:400px;
                    margin-right: 40px;
                    // overflow: hidden;
                    display: flex;
                    justify-content: space-between;
                    img {
                        // float: left;
                        width: 52px;
                        height: 52px;
                        border-radius: 50%;
                        vertical-align: middle;
                        margin-right: 15px;
                    }
                    span{
                        // float: left;
                        height: 52px;
                        line-height: 22px;
                        padding-top: 10px;
                    }

                    .name {
                        width: 100px;
                        font-family: Microsoft YaHei, Microsoft YaHei;
                        font-weight: 400;
                        font-size: 16px;
                        color: #333333;
                        margin-right: 30px;
                        width: 100px;
                        white-space: nowrap;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        display: block;

                    }

                    
                }

                .center {
                    .context {
                        width: 700px;
                        margin-right: 40px;
                        display: -webkit-box;
                        -webkit-box-orient: vertical;
                        -webkit-line-clamp: 2;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        word-break: break-all;
                        font-family: Microsoft YaHei, Microsoft YaHei;
                        font-weight: bold;
                        font-size: 16px;
                        color: #333333;
                    }
                }

                .right {
                    .time {
                        display: block;
                        width: 200px;
                        font-family: Microsoft YaHei, Microsoft YaHei;
                        font-weight: 400;
                        font-size: 16px;
                        color: #999999;
                        margin-right: 30px;
                    }

                    span {
                        font-family: Microsoft YaHei, Microsoft YaHei;
                        font-weight: 400;
                        font-size: 16px;
                        color: #333333;
                    }
                }
            }

            // .comment_empty {
            //     height: 200px;
            //     line-height: 200px;
            //     text-align: center;

            //     img {
            //         width: 78px;
            //         height: 78px;
            //         vertical-align: -25px;
            //         margin-right: 20px;
            //     }

            //     span {
            //         font-family: Microsoft YaHei, Microsoft YaHei;
            //         font-weight: bold;
            //         font-size: 26px;
            //         color: #CCCCCC;
            //     }
            // }
        }
    }
}

//评论回复
.message {
    width: 1200px;
    margin: 0 auto;
    padding: 0px 25px;
    box-sizing: border-box;
    height: 145px;
    line-height: 145px;
    background-color: #ecf5ee;

    .inner {
        .messageInput {
            width: 1000px;
            height: 67px;
            outline: none;
            border: 1px solid #E1E1E1;
            background-color: #fafafa;
            font-family: Microsoft YaHei, Microsoft YaHei;
            font-weight: 400;
            font-size: 18px;
            color: #666;
            padding: 0 20px;
            box-sizing: border-box;
        }

        .btn {
            width: 115px;
            height: 40px;
            border: none;
            background-color: #028e21;
            color: #fff;
            border-radius: 6px;
            margin-left: 36px;
            font-family: Microsoft YaHei, Microsoft YaHei;
            font-weight: 400;
            font-size: 16px;
        }
    }
}

//分页
.paginationBox {
    display: flex;
    justify-content: center;
    margin-top: 60px;
    margin-bottom: 90px;

    // 鼠标移入后字体颜色
    :deep(.el-pagination:hover) {
        color: #139609;
    }

    :deep(.el-pagination.is-background .btn-next),
    :deep(.el-pagination.is-background .btn-prev) {
        width: 70px;
        height: 34px;
        margin: 0px 10px;
        border-radius: 4px;
    }

    :deep(.el-pagination.is-background .el-pager li) {
        margin: 0px 10px;
        width: 38px;
        height: 34px;
        border-radius: 4px;
    }

    :deep(.el-pagination.is-background .btn-next.is-active),
    :deep(.el-pagination.is-background .btn-prev.is-active),
    :deep(.el-pagination.is-background .el-pager li.is-active) {
        background-color: #028e21;
        color: #fff;
    }
}
</style>