<template>
    <view class="chat">
        <sent-message-bar :conversationType="conversationType" :targetId="targetId" @onSuccess="getNewMessage()" />
        <!-- chat -->
        <view class="body">
            <view class="scroll">
                <view class="cell" v-for="(message, index) in messages" :key="index">
                    <show-message-cell :message="message" />
                </view>
            </view>
        </view>
    </view>
</template>

<script>
    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'
    import imBase from '../mixins/imBase.js'

    export default {
        mixins: [
            imBase
        ],
        components: {
            sentMessageBar,
            showMessageCell
        },
        data() {
            return {
                targetId: '',
                messages: [],
                conversationType: 1,
                userInfo: {
                    name: '',
                    userId: '',
                    portraitUrl: ''
                }
            }
        },
        computed: {
            latestMessage() {
                if (this.messages.length) {
                    return this.messages[0]
                } else {
                    return {
                        sentTime: 0
                    }
                }
            }
        },
        onLoad(e) {
            this.targetId = e.targetId
            this.userInfo = this.$store.getters.contactInfo(this.targetId)
            uni.setNavigationBarTitle({
                title: this.contact(this.targetId).name
            })
            // 获取消息列表
            this.getMessageList()
            // 监听新消息
            uni.$on('onReceiveMessage_' + this.targetId, (message) => {
                this.getNewMessage()
            })
            // 监听消息已读状态
            uni.$on('onReadReceiptReceived', (data) => {
                if (data.targetId == this.targetId) {
                    this.messages = this.messages.map((item) => {
                        if (item.messageDirection == 1 && item.sentStatus == 30 && item.receivedTime <
                            data.messageTime + 1000) {
                            item.sentStatus = 50
                        }
                        return item
                    })
                }
            })
            uni.$on('onRecallMessage_' + this.targetId, (message) => {
                this.messages = this.messages.map(item => {
                    if (message.messageId == item.messageId) {
                        return message
                    } else {
                        return item
                    }
                })
            })
            uni.$once('onUserDelete_' + this.targetId, () => {
                uni.showToast({
                    icon: 'none',
                    title: '好友关系已解除'
                })
                uni.switchTab({
                    url: '/pages/im/index'
                })
            })
        },
        onUnload() {
            uni.$off('onUserDelete_' + this.targetId)
            uni.$off('onReceiveMessage_' + this.targetId)
            uni.$off('onRecallMessage_' + this.targetId)
            uni.$off('onRecallMessage')
            uni.$off('onReadReceiptReceived')
        },
        methods: {
            getNewMessage() {
                im.getMessageList(
                    this.conversationType,
                    this.targetId,
                    this.latestMessage.sentTime || 0,
                    1,
                    false,
                    (messages) => {
                        this.messages.unshift(...messages)
                        this.scrollBottom()
                    })
            },
            // 获取消息列表
            getMessageList() {
                im.getMessageList(
                    this.conversationType,
                    this.targetId,
                    0,
                    20,
                    true,
                    (messages) => {
                        this.messages = messages
                        this.scrollBottom()
                    })
            },
            // 滚动到底部
            scrollBottom(type) {
                if (this.latestMessage) {
                    // 清理当前会话，未读消息数量
                    RongIMLib.clearMessagesUnreadStatus(this.conversationType, this.targetId, this.latestMessage
                        .sentTime)
                    // // 发送消息已读状态给对方
                    RongIMLib.sendReadReceiptMessage(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;

                    .time {
                        text-align: center;
                        font-size: 24rpx;
                        color: #666;
                        margin-bottom: 10rpx;
                    }

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

                        &.left {
                            flex-direction: row;
                        }

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

                            .state {
                                text-align: right;
                            }
                        }

                        .msg {
                            margin: 0 20rpx;
                        }
                    }

                    .cell-footer {
                        height: 20rpx;
                    }
                }
            }
        }
    }
</style>
