<template>
    <view class="apply--cell u-border-bottom">
        <view class="avatar">
            <u-avatar :src="user.portraitUrl" shape="square" size="46" />
        </view>
        <view class="info">
            <view class="name">
                {{ user.name }}
            </view>
            <view class="message">
                {{ message.message }}
            </view>
        </view>

        <view class="action">
            <u-button type="success" size="mini" @click="resolve">通过</u-button>
            <u-button type="warning" size="mini" @click="reject">拒绝</u-button>
        </view>
    </view>
</template>

<script>
    import * as RongIMLib from '@/uni_modules/RongCloud-IMWrapper/js_sdk/index'
    import {
        resolveFriend,
        rejectFriend
    } from '@/apis/interfaces/im.js'

    export default {
        props: {
            message: {
                type: Object,
                default: {}
            }
        },
        computed: {
            user() {
                return JSON.parse(this.message.extra)
            }
        },
        methods: {
            resolve() {
                resolveFriend(this.message.sourceUserId).then(res => {
                    this.clearMessages()
                    uni.showToast({
                        icon: 'none',
                        title: '通过好友申请'
                    })
                }).catch(err => {
                    uni.showToast({
                        icon: 'none',
                        title: err.message
                    })
                })
            },
            reject() {
                uni.showModal({
                    title: '拒绝申请',
                    success: (res) => {
                        if (res.confirm) {
                            rejectFriend(this.message.sourceUserId).then(res => {
                                this.clearMessages()
                            }).catch(err => {
                                uni.showToast({
                                    icon: 'none',
                                    title: err.message
                                })
                            })
                        }
                    }
                })
            },
            // 不管是通过还是拒绝，都要把相关的信息清理
            clearMessages() {
                RongIMLib.deleteMessages(RongIMLib.ConversationType.SYSTEM, this.message.sourceUserId)
                this.$emit('success')
                uni.$emit('onContactNotification')
            }
        }
    }
</script>

<style lang="scss" scoped>
    .apply--cell {
        display: flex;
        padding: $padding;
        align-items: center;

        .info {
            flex: 1;
            margin-left: $padding;

            .name {
                font-size: $title-size + 2;
            }

            .message {
                color: $text-gray-m;
                font-size: $title-size-m;
                margin-top: 10rpx;
            }
        }

        .action {
            justify-content: space-between;

            .u-button+.u-button {
                margin-top: 10rpx;
            }
        }
    }
</style>
