<template>
    <view class="chat">
        <sent-message-bar :conversationType="conversationType" :targetId="targetId" @onSuccess="getNewMessage()" />

        <view class="body">
            <view class="scroll">
                <view class="cell" v-for="(message, index) in messages" :key="index">
                    <show-message-cell :message="message" isGroup />
                </view>
            </view>
        </view>
    </view>
</template>

<script>
    import {
        getGroupBase
    } from '@/apis/interfaces/im.js'
    import * as RongIMLib from '@/uni_modules/RongCloud-IMWrapper/js_sdk/index'
    import im from '@/utils/im/index.js'
    import sentMessageBar from '../components/sentMessageBar'
    import showMessageCell from '../components/showMessageCell'
    import utils from '@/utils/index.js'

    export default {
        components: {
            sentMessageBar,
            showMessageCell
        },
        data() {
            return {
                avatarRpx: 84,
                targetId: '',
                conversationType: RongIMLib.ConversationType.GROUP,
                messages: [],
                groupInfo: {
                    name: ''
                }
            }
        },
        computed: {
            latestMessage() {
                if (this.messages.length) {
                    return this.messages[0]
                } else {
                    return {
                        sentTime: 0
                    }
                }
            }
        },
        onLoad(e) {
            this.targetId = e.targetId
            // 获取群成员数量
            getGroupBase(this.targetId).then(res => {
                uni.setNavigationBarTitle({
                    title: res.name + `(${res.members})`
                })
            })
            // 获取历史消息列表
            this.getMessageList()
            // 监听收到本群的消息，追加消息
            uni.$on('onReceiveMessage', (msg) => {
                if (msg.targetId == this.targetId) {
                    this.getNewMessage()
                }
            })
            uni.$on('onReceiptRequest', (msg) => {
                if (msg.targetId == this.targetId) {
                    RongIMLib.getMessageByUId(msg.messageUId, (result) => {
                        RongIMLib.sendReadReceiptResponse(3, this.targetId, [result.message], (res) => {
                            console.error('发送群聊已读回执成功', res);
                        })
                    })
                }
            })
            uni.$on('onReceiptResponse', (msg) => {
                console.log('远端消息已读，本地获取有几个人读了', msg);
            })
            // 清理聊天记录
            uni.$once('cleanGroupMessage', this.getMessageList)
            uni.$on('onRecallMessage', (res) => {
                if (res.targetId == this.targetId) {
                    this.messages = this.messages.map(item => {
                        if (res.messageId == item.messageId) {
                            return res
                        } else {
                            return item
                        }
                    })
                }
            })
        },
        onUnload() {
            uni.$off('onRecallMessage')
            uni.$off('onReceiptRequest')
            uni.$off('onReceiptResponse')
        },
        onNavigationBarButtonTap() {
            uni.navigateTo({
                url: '/pages/im/group/info?targetId=' + this.targetId
            })
        },
        methods: {
            onScroll(e) {
                this.$refs.messageBar.onHidePopus()
            },
            getNewMessage() {
                im.getMessageList(
                    this.conversationType,
                    this.targetId,
                    this.latestMessage.sentTime,
                    10,
                    false,
                    (messages) => {
                        this.messages.unshift(...messages)
                        this.scrollBottom()
                    })
            },
            // 获取消息列表
            getMessageList() {
                im.getMessageList(
                    this.conversationType,
                    this.targetId,
                    0,
                    20,
                    true,
                    (messages) => {
                        console.log('获取消息列表', messages);
                        RongIMLib.sendReadReceiptResponse(3, this.targetId, messages, (res) => {
                            console.error('发送群聊已读回执成功', res);
                        })
                        this.messages = messages
                        this.scrollBottom()
                    })
            },
            // 滚动到底部
            scrollBottom(type) {
                if (this.latestMessage) {
                    // 清理当前会话，未读消息数量
                    RongIMLib.clearMessagesUnreadStatus(this.conversationType, this.targetId, this.latestMessage
                        .sentTime)
                    // 更新badge提醒数量
                    im.setNotifyBadge()
                }
            }
        }
    }
</script>

<style scoped lang="scss">
    /* 窗口 */
    .chat {
        background: $window-color;
        height: 100vh;
        display: flex;
        flex-direction: column-reverse;

        .body {
            overflow: scroll;
            flex: 1;
            height: 0;
            display: flex;
            flex-direction: column-reverse;

            .scroll {
                display: flex;
                flex-direction: column-reverse;
                justify-content: flex-end;

                .cell {
                    padding: 10rpx 20rpx;


                }
            }
        }
    }
</style>
