<script setup>
import { ref,reactive, onMounted} from 'vue'
import { ElMessage,ElMessageBox } from 'element-plus'
import { 
    apiReportLike, 
    apiReportComment, 
    apiReportMyCommnet,
    apiReportHotCommentList,
    apiDelMyComment 
} from '@/api/report'
import { useStore } from 'vuex'
import { useRoute, useRouter } from 'vue-router'

const store=useStore()
const router=useRouter()
const route=useRoute()

const props=defineProps({
    data:{
        type:Object
    }
})

const msgState = reactive({
    show:false,//是否显示评论输入框
    msg:'',
    hasNickName:store.state.userInfo&&store.state.userInfo.nick_name?true:false,//false 匿名
    like_num:props.data.like_num,
    like_enabled:props.data.like_enabled
})

//点赞
const handleSetLike=async ()=>{
    const res=await apiReportLike({
        report_id:Number(props.data.report_id),
        report_chapter_id:Number(props.data.report_chapter_id),
        old_report_id:Number(props.data.old_report_id),
        old_report_chapter_id:Number(props.data.old_report_chapter_id)
    })
    if(res.code===200){
        msgState.like_num=res.data.like_num
        msgState.like_enabled=res.data.like_enabled
    }
}

// 是否匿名
const handleIsAnonymous=()=>{
    if(!store.state.userInfo.nick_name){
        msgState.hasNickName=false
        return
    }
    msgState.hasNickName=!msgState.hasNickName
}

// 点击写留言
const handleClickShowMsg=()=>{
    // 判断是否设置过昵称
    if(!store.state.userInfo.nick_name){
        const htmlStr=`检测到您还未设置头像和昵称，您的留言将发布为匿名，是否立即去设置？`
        ElMessageBox({
            title:`温馨提醒`,
            message:htmlStr,
            center: true,
            dangerouslyUseHTMLString: true,
            confirmButtonText:'去设置',
            confirmButtonClass:'self-elmessage-confirm-btn',
            showCancelButton:true,
            cancelButtonText:'暂时不用',
            cancelButtonClass:'self-elmessage-cancel-btn'
        }).then(()=>{
            router.push('/user/setinfo')
        }).catch(()=>{
            msgState.show=true
        })
        return
    }

    msgState.show=true
}

//发布留言
const handlePublishMsg=async ()=>{
    if(!msgState.msg){
        ElMessage.warning('请填写评论内容')
        return
    }
    const res=await apiReportComment({
        report_id:Number(props.data.report_id),
        report_chapter_id:Number(props.data.report_chapter_id),
        old_report_id:Number(props.data.old_report_id),
        old_report_chapter_id:Number(props.data.old_report_chapter_id),
        content:msgState.msg,
        is_show_name:msgState.hasNickName?1:0
    })
    if(res.code===200){
        msgState.msg=''
        getMyCommnet()
        getHotCommentList()
    }
}

// 获取我的留言
let myCommentList=ref([])
const getMyCommnet=async ()=>{
    const res=await apiReportMyCommnet({
        report_id:Number(props.data.report_id),
        report_chapter_id:Number(props.data.report_chapter_id),
        old_report_id:Number(props.data.old_report_id),
        old_report_chapter_id:Number(props.data.old_report_chapter_id),
    })
    if(res.code===200){
        myCommentList.value=res.data.list||[]
        if(res.data.is_show_name==0){
            msgState.hasNickName=false
        }else{
            msgState.hasNickName=true
        }
    }
}

//获取精选留言
let hotCommentList=reactive({
    list:[],
    page:1,
    pageSize:10000,
    finished:false,
    loading:false
})
const getHotCommentList=async ()=>{
    hotCommentList.loading=true
    const res=await apiReportHotCommentList({
        report_id:Number(props.data.report_id),
        report_chapter_id:Number(props.data.report_chapter_id),
        old_report_id:Number(props.data.old_report_id),
        old_report_chapter_id:Number(props.data.old_report_chapter_id),
        current_index:hotCommentList.page,
        page_size:hotCommentList.pageSize
    })
    hotCommentList.loading=false
    if(res.code===200){
        if(res.data.paging.is_end){
            hotCommentList.finished=true
        }
        const arr=res.data.list||[]
        hotCommentList.list=arr
    }
}

// 删除我的留言
const handleDelMyComment=(item)=>{
    const htmlStr=`确定要删除留言吗？`
    ElMessageBox({
        title:`操作提醒`,
        message:htmlStr,
        center: true,
        dangerouslyUseHTMLString: true,
        confirmButtonText:'确定',
        confirmButtonClass:'self-elmessage-confirm-btn',
        showCancelButton:true,
        cancelButtonText:'取消',
        cancelButtonClass:'self-elmessage-cancel-btn'
    }).then(()=>{
        apiDelMyComment({
            comment_id:Number(item.comment_id)
        }).then(res=>{
            if(res.code===200){
                ElMessage.success('删除成功')
                formatCommentList(item.comment_id)
            }
        })
    }).catch(()=>{
        console.log('取消删除');
    })
}

//删除留言后 更新我的留言和精选留言数据
const formatCommentList=(id)=>{
    // 我的留言列表
    if(myCommentList.value.length>0){
        myCommentList.value=myCommentList.value.filter(item=>{
            return item.comment_id!=id
        })
    }
    
    // 精选留言
    if(hotCommentList.list.length>0){
        hotCommentList.list=hotCommentList.list.filter(item=>{
            return item.comment_id!=id
        })
    }
}

const linkToQuestionHandle=()=>{
    router.push({path:'/question/list',query:{isAutoQuestion:true}})
}
// 当前选中哪种留言
let msgType=ref('全部留言')
// 留言类型切换
const msgTypeChange=(e)=>{
	msgType.value=e
	if(e==='全部留言'){
		getHotCommentList();
	}else{
		getMyCommnet();
	}
}


onMounted(() => {
    getMyCommnet()
    getHotCommentList()
})

</script>


<template>
    <div class="comment-list-wrap">
        <div class="input-box-wrap">
            <div class="flex top" style="justify-content:space-between;align-items: center;">
                <div class="like-num-box" @click="handleSetLike">
                    <img src="@/assets/icon-good-s.png" alt="" v-if="msgState.like_enabled">
                    <img src="@/assets/icon-good.png" alt="" v-else>
                    <span v-if="msgState.like_num>0">{{msgState.like_num}}</span>
                </div>
                <div class="right-box" style="cursor: pointer;">
                    <span  v-show="!msgState.show" @click="handleClickShowMsg">写留言</span>
                    <span class="closemsg-btn" v-show="msgState.show" @click="msgState.show=false">收起</span>
                </div>
            </div>
            <el-input
                class="msg-text-box"
                v-show="msgState.show"
                v-model="msgState.msg"
                :rows="7"
                type="textarea"
                placeholder="发布一条留言"
            />
            <div class="flex bot" style="justify-content:space-between;align-items: center;" v-show="msgState.show">
                <div class="left">
                    <span :class="['span',!msgState.hasNickName&&'span-active']" @click="handleIsAnonymous">匿名</span>
                    <el-tooltip placement="top">
                        <template #content>匿名发布的留言将使用默认头像和默认昵称，<br />用户需设置头像和昵称后才能取消匿名</template>
                        <img style="width:16px;position: relative;top:3px;left:4px" src="@/assets/icon-msg.png" alt="">
                    </el-tooltip>
                </div>
                <div :class="['publish-btn',msgState.msg&&'publish-btn-active']" style="cursor: pointer;" @click="handlePublishMsg">发布</div>
            </div>
        </div>
        <div class="tabs-box">
            <span 
				:class="msgType==='全部留言'?'active':''" 
				@click="msgTypeChange('全部留言')"
			>全部留言</span>
			<span 
				:class="msgType==='我的留言'?'active':''"
				@click="msgTypeChange('我的留言')"
			>我的留言</span>
        </div>
        <!-- 我的留言模块  -->
        <template v-if="msgType==='我的留言'">
            <ul class="comment-list-box my-comment-box">
                <li class="flex comment-item" v-for="item in myCommentList" :key="item.comment_id">
                    <el-image 
                        class="avatar" 
                        style="width: 46px; height: 46px" 
                        :src="item.user_img_url" 
                        fit="cover" 
                    />
                    <div class="comment-con">
                        <div class="right-box">
                            <span class="del-btn" @click="handleDelMyComment(item)">删除</span>
                            <!-- <span>{{item.IsHot===0?'未精选':'已精选'}}</span> -->
                        </div>
                        <div :class="['user-name',item.is_top==1&&'is-top']">{{item.user_name}}</div>
                        <div class="commment-msg">{{item.content}}</div>
                        <ul>
                            <li class="flex comment-item" style="padding:10px 0 0 0" v-for="ritem in item.ReplyList" :key="ritem.comment_id">
                                <el-image 
                                    class="avatar" 
                                    style="width: 46px; height: 46px" 
                                    :src="ritem.admin_img_url" 
                                    fit="cover" 
                                />
                                <div class="comment-con">
                                    <div class="user-name">{{ritem.admin_name}}</div>
                                    <div class="commment-msg">{{ritem.content}}</div>
                                </div>
                            </li>
                        </ul>
                    </div>
                </li>
            </ul>
            <p style="text-align:center;font-size:16px;margin:30px 0;color:#999" v-if="myCommentList.length==0">暂无留言</p>
        </template>
        <!-- 精选留言模块 -->
        <template v-if="msgType==='全部留言'">
            <ul class="comment-list-box hot-comment-box">
                <li class="flex comment-item" v-for="(item,index) in hotCommentList.list" :key="item.comment_id">
                    <el-image 
                        class="avatar" 
                        style="width: 46px; height: 46px" 
                        :src="item.user_img_url" 
                        fit="cover" 
                    />
                    <div class="comment-con">
                        <div class="right-box" v-if="item.user_id==$store.state.userInfo.user_id">
                            <span class="del-btn2" @click="handleDelMyComment(item,index)">删除</span>
                        </div>
                        <div class="user-name">{{item.user_name}}</div>
                        <div class="commment-msg">{{item.content}}</div>
                        <ul>
                            <li class="flex comment-item" style="padding:10px 0 0 0" v-for="ritem in item.ReplyList" :key="ritem.comment_id">
                                <el-image 
                                    class="avatar" 
                                    style="width: 46px; height: 46px" 
                                    :src="ritem.admin_img_url" 
                                    fit="cover" 
                                />
                                <div class="comment-con">
                                    <div class="user-name">{{ritem.admin_name}}</div>
                                    <div class="commment-msg">{{ritem.content}}</div>
                                </div>
                            </li>
                        </ul>
                    </div>
                </li>
            </ul>
            <p style="text-align:center;font-size:16px;margin:30px 0;color:#999" v-if="hotCommentList.list.length==0">暂无留言</p>
        </template>

        <!-- 跳转问答区域 -->
		<div style="text-align:center;font-size:18px;color:#F3A52F;margin-top:20px;cursor: pointer;" @click="linkToQuestionHandle">
			想要一对一回复吗？去问答社区提问
		</div>
    </div>
</template>

<style lang="scss" scoped>
.comment-list-wrap{
    margin-top: 24px;
    .input-box-wrap{
        .like-num-box{
            border:1px solid #F3A52F;
            padding: 6px 25px;
            border-radius:20px;
            cursor: pointer;
            img{
                width: 17px;
                vertical-align: middle;
            }
            span{
                font-size:18px;
                color:#F3A52F;
                margin-left: 10px;
                vertical-align: middle;
            }
        }
        .right-box{
            color: #F3A52F;
            .closemsg-btn::after{
                content: '';
                display: inline-block;
                width: 10px;
                height: 10px;
                border-right: 1px solid #F3A52F;
                border-bottom: 1px solid #F3A52F;
                transform: rotate(45deg) translate(5px,-9px);
            }
        }
        .msg-text-box{
            margin-top: 20px;
            margin-bottom: 25px;
            :deep(.el-textarea__inner:focus){
                border-color: #F3A52F;
            }
        }
        .bot{
            .left{
                .span{
                    cursor: pointer;
                    color: #999999;
                    &::before{
                        content: '';
                        display: inline-block;
                        width: 16px;
                        height: 16px;
                        border-radius: 50%;
                        border: 1px solid #999999;
                        box-sizing: border-box;
                        position: relative;
                        top: 3px;
                        margin-right: 4px;
                    }
                }
                .span-active{
                    cursor: pointer;
                    color: #F3A52F;
                    &::before{
                        content: '';
                        display: inline-block;
                        box-sizing: border-box;
                        width: 16px;
                        height: 16px;
                        border-radius: 50%;
                        border: 5px solid #F3A52F;
                        background-color: #fff;
                        position: relative;
                        top: 3px;
                        margin-right: 4px;
                    }
                }
            }
            .publish-btn{
                width: 112px;
                height: 36px;
                background: rgba(243, 165, 47, 0.5);
                border-radius: 20px 20px 20px 20px;
                color: #fff;
                text-align: center;
                line-height: 36px;
            }
            .publish-btn-active{
                background: rgba(243, 165, 47, 1);
            }
        }
    }
    .tips{
        text-align: center;
        color: #999;
        &::after,&::before{
            content: '';
            display: inline-block;
            width: 43px;
            height: 1px;
            background-color: #999;
            vertical-align: middle;
        }
        &::before{
            margin-right: 10px;
        }
        &::after{
            margin-left: 10px;
        }
    }
    .comment-list-box{
        .label{
            color: #999;
            font-size: 18px;
            margin-bottom: 0;
        }
        .comment-item{
            padding: 17px 20px;
            &:hover{
                background-color: #FFFBF5;
            }
            .avatar{
                margin-right: 10px;
                flex-shrink: 0;
                border-radius: 4px;
                overflow: hidden;
            }
            .comment-con{
                flex: 1;
                position: relative;
                .right-box{
                    position: absolute;
                    right: 0;
                    top: 0;
                    font-size: 16px;
                    color: #999;
                }
            }
            .del-btn,.del-btn2{
                display: none;
                cursor: pointer;
                color: #F3A52F;
            }
            &:hover .del-btn{
                display: inline-block;
                // &::after{
                //     content: ' | ';
                //     color: #999;
                //     margin-right: 5px;
                // }
            }
            &:hover .del-btn2{
                display: inline-block;
            }
            .user-name{
                font-size: 18px;
                color: #999;
                margin-bottom: 5px;
            }
            .is-top::after{
                content: '置顶';
                display: inline-block;
                color: #F3A52F;
                font-size: 16px;
                border: 1px solid #F3A52F;
                border-radius: 4px;
                line-height: 23px;
                width: 65px;
                text-align: center;
                margin-left: 10px;
            }
            .commment-msg{
                font-size: 16px;
            }
        }
    }
    .my-comment-box{
        margin-top: 50px;
    }
    .hot-comment-box{
        margin-top: 30px;
    }
    .tabs-box{
        margin-top: 30px;
		span{
			font-size: 18px;
			display: inline-block;
			margin-right: 60px;
			color: #999;
            cursor: pointer;
		}
		.active{
			font-size: 18px;
			color: #333;
			position: relative;
			&::after{
				content: '';
                position: absolute;
				top: 105%;
				left: 50%;
                display: block;
				transform: translateX(-50%);
				width: 73px;
				height: 5px;
				background: #E3B377;
				border-radius: 2px;
			}
		}
	}
}
</style>