<template>
    <view class="">
        <view class="notify" v-if="message.objectName === 'RC:GrpNtf'">
            {{ message.content.message }}
            <text class="link" @click="toAnnounce(message)" v-if="message.content.operation == 'ANNOUNCEMENT'">点击查看
            </text>
        </view>
        <view class="notify" v-else-if="message.objectName === 'RC:RcNtf'">
            {{ contact(message.senderUserId).name }} 撤回了一条消息
        </view>
        <view v-else :class="['cell-item', message.messageDirection == 1 ? 'right' : 'left']">
            <u-avatar class="avatar" @click="toUser(message)" :size="avatarSize" shape="square"
                :src="contact(message.senderUserId).portraitUrl" />
            <view class="msg" @longpress="backMessage">
                <show-text v-if="message.objectName === 'RC:TxtMsg'" :message="message" :isGroup="isGroup" />
                <show-voice v-else-if="message.objectName === 'RC:HQVCMsg'" :message="message" :isGroup="isGroup" />
                <show-image v-else-if="message.objectName === 'RC:ImgMsg'" :message="message" :isGroup="isGroup" />
                <show-call v-else-if="message.objectName === 'RC:InfoNtf'" :message="message" :isGroup="isGroup" />
                <view v-else class="">
                    [未处理的消息类型 {{ message.objectName }}]
                </view>
            </view>
            <!-- <view class="time">{{ item.sentTime|timeCustomCN }}</view> 时间判断，最好是隔一段时间没有消息才展示一个 -->
        </view>
    </view>
</template>

<script>
    import {
        getGroupAnnouncement
    } from '@/apis/interfaces/im.js'
    import * as IMLib from '@/uni_modules/RongCloud-IMWrapper/js_sdk/index'
    import showVoice from './show/showVoice'
    import showImage from './show/showImage'
    import showText from './show/showText'
    import showCall from './show/showCall'
    import utils from '@/utils/index.js'
    import imBase from '../mixins/imBase.js'

    export default {
        mixins: [
            imBase
        ],
        components: {
            showCall,
            showVoice,
            showImage,
            showText
        },
        props: {
            message: {
                type: Object,
                default: function() {
                    return {}
                }
            },
            isGroup: {
                type: Boolean,
                default: false
            }
        },
        methods: {
            toUser(item) {
                if (item.messageDirection == 1) {
                    uni.navigateTo({
                        url: '/pages/im/friends/mine?targetId=' + item.senderUserId
                    })
                } else {
                    uni.navigateTo({
                        url: '/pages/im/friends/info?targetId=' + item.senderUserId
                    })
                }
            },
            // 跳转群公告
            toAnnounce(message) {
                if (message.content.extra) {
                    getGroupAnnouncement(message.targetId, message.content.extra).then(res => {
                        uni.navigateTo({
                            url: '/pages/im/group/announceCreate?type=check&targetId=' + message
                                .targetId + '&aId=' + message.content.extra
                        })
                    }).catch(err => {
                        IMLib.deleteMessagesByIds([message.messageId], ({
                            code
                        }) => {
                            if (code === 0) {
                                uni.$emit('remove_message_' + message.targetId, message.messageId)
                            }
                        })
                        uni.showToast({
                            title: '公告不存在或已删除',
                            icon: 'none',
                        })
                    })
                } else {
                    uni.showToast({
                        icon: 'none',
                        title: '旧版本，无链接'
                    })
                }
            },
            // 撤回消息测试
            backMessage() {
                if (this.$store.getters.sender.userId != this.message.senderUserId) {
                    uni.showToast({
                        icon: 'none',
                        title: '不能撤回别人的消息'
                    })
                    return
                }
                // 判断时间超过了多久 就不能撤回 TODO
                // this.message.sentTime
                const pushContent = this.$store.getters.sender.name + '撤回了一条消息'
                IMLib.recallMessage(this.message.messageId, pushContent,
                    ({
                        code,
                        message
                    }) => {
                        if (code === 0) {
                            uni.showToast({
                                icon: 'none',
                                title: '消息撤回成功'
                            })
                            IMLib.getMessage(this.message.messageId, res => {
                                uni.$emit('onRecallMessage', res.message)
                            })
                        } else {
                            uni.showToast({
                                icon: 'none',
                                title: '撤回失败' + code
                            })
                        }
                    }
                )
            }
        }
    }
</script>

<style lang="scss" scoped>
    .notify {
        text-align: center;
        font-size: 24rpx;
        color: #666;

        .link {
            color: $main-color;
            margin-left: 10rpx;
        }
    }

    .cell-item {
        display: flex;
        width: 710rpx;
        justify-content: flex-start;

        &.left {
            flex-direction: row;
        }

        &.right {
            flex-direction: row-reverse;

            .state {
                flex-direction: row;
                justify-content: flex-end;
            }
        }

        .msg {
            margin: 0 20rpx;
        }
    }
</style>
