<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 class="notify" v-else-if="message.objectName === 'RC:InfoNtf'">
            {{ message.sentTime|timeCustomCN }}
        </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="messageAction($event, message)">
                <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-file v-else-if="message.objectName === 'RC:FileMsg'" :message="message" :isGroup="isGroup" />
                <show-normal v-else-if="message.objectName === 'RC:IWNormalMsg'" :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 showFile from './show/showFile'
    import showNormal from './show/showNormal'
    import utils from '@/utils/index.js'
    import imBase from '../mixins/imBase.js'

    export default {
        mixins: [
            imBase
        ],
        components: {
            showVoice,
            showImage,
            showText,
            showFile,
            showNormal
        },
        props: {
            message: {
                type: Object,
                default: function() {
                    return {}
                }
            },
            isGroup: {
                type: Boolean,
                default: false
            }
        },
        methods: {
            messageAction(event, message) {
                this.$emit('messageAction', {
                    event,
                    message
                })
            },
            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: '旧版本，无链接'
                    })
                }
            }
        }
    }
</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>
