<template>
    <dl data-type="4" :class='[(chatMen ? "chat-right" : "chat-left")]'>
        <dt>
            <a>
                <img :src='chatImg' class="c-images-cycle">
            </a>
        </dt>
        <div class="c-content">
            <span class="c-f12 name">{{chatObj.sender_name}}</span>
            <div class="c-msg">
                <dd class="word-bread word-article">
                    <span class="yidu yidu2" v-show="chatMen && chatJudge && chatJudge > chatObj.timestamp">已读</span>
                    <h4 class="text-ellipsis c-f15">
                        {{handleContent(chatObj.content).title ? handleContent(chatObj.content).title : ""}}
                    </h4>
                    <div class="article-content">
                        <img :src='handleImgSplit(handleContent(chatObj.content).img)' v-show="isShowPhoto">
                        <div v-html='handleContent(chatObj.content).content ? handleContent(chatObj.content).content : ""'></div>
                    </div>
                </dd>
            </div>
        </div>
    </dl>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
    @import "../../assets/sass/message-chat";
</style>
<script>
import common from '../../assets/js/common'
import linkImg from '@/assets/images/default_share_blue.png'

export default {
    props: ['chatMen', 'chatJudge', 'chatObj', 'chatImg'],
    components: {
    },
    mounted() {
    },
    data() {
        return {
            isShowPhoto: true
        }
    },
    methods: {
        photoSrc(str) {
            if (str === 'null') {
                this.isShowPhoto = false
                return
            }
            return common.setImgUrl(str)
        },
        handleContent(content) {
            let json = {
                title: false,
                img: false,
                content: false
            }
            if (!content) return json
            try {
                let str1 = content.replace(/\n|\r\n/ig, '<br>')
                    // str2 = str1.replace(/\\/ig, '\\')
                json = JSON.parse(str1)
                if (!json.img) {
                    json.img = false
                }
                return json
            } catch (error) {
                console.error(error)
                return json
            }
        },
        handleImgSplit(str) {
            let imgUrl = null
            if (!str) return linkImg
            // photoSrc(handleContent(chatObj.content).img.split(",")[0])
            try {
                imgUrl = this.photoSrc(str.split(',')[0])
            } catch (error) {
                imgUrl = this.photoSrc(str)
            }
            return imgUrl
        }
    }
}
</script>
