<template>
    <view class="apply--cell">
        <view class="avatar">
            <u-avatar :src="user.portraitUrl || require('@/static/user/cover.png')" shape="square" :size="avatarSize" />
        </view>
        <view class="info">
            <view class="name"> {{ user.name }}</view>
            <view class="message"> {{ message.message!=='null' ? message.message : '听说你很优秀，想认识下你~' }} </view>
        </view>
        <view class="action">
            <u-button class="u-button" size="default" type="success" @click="resolve">通过</u-button>
            <u-button class="u-button" size="default" type="warning" @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'
    import imBase from '../mixins/imBase.js'

    export default {
        mixins: [
            imBase
        ],
        props: {
            message: {
                type: Object,
                default: {}
            }
        },
        computed: {
            user() {
                return JSON.parse(this.message.extra)
            }
        },
        methods: {
            resolve() {
                resolveFriend(this.message.sourceUserId).then(res => {
                    uni.showToast({
                        icon: 'none',
                        title: '通过好友申请'
                    })
                    uni.navigateBack({})
                }).catch(err => {
                    uni.showToast({
                        icon: 'none',
                        title: err.message
                    })
                }).finally(() => {
                    this.clearMessages()
                })
            },
            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('onNewContactConversation')
                uni.$emit('onNewContactFriends')
                uni.$emit('onNewContactPendings')
            }
        }
    }
</script>

<style lang="scss" scoped>
    .apply--cell {
        display: flex;
        padding: $padding;
        align-items: center;
        border-bottom: solid 1rpx #f9f9f9;

        .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;
            display: flex;
            flex-direction: row;
            align-items: center;
            box-sizing: border-box;

            .u-button {
                padding: 10rpx 20rpx !important;
                font-size: $title-size-m;
                height: 60rpx !important;
            }

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