<template>
    <view>
        <u-index-list :index-list="indexs" inactiveColor="#666" activeColor="#34CE98">
            <view class="friend-flex" @click="toPending">
                <u-avatar class="cover" size="40" shape="square" :src="require('@/static/im/im_01.png')"></u-avatar>
                <u-badge max="99" absolute :offset="[23, 20]" :value="pendingCount" />
                <view class="info">新的朋友</view>
            </view>
            <view class="friend-flex" @click="toGroup">
                <u-avatar class="cover" size="40" shape="square" :src="require('@/static/im/im_00.png')"></u-avatar>
                <view class="info">我的群聊</view>
            </view>
            <block v-if="friends.length > 0">
                <u-index-item v-for="(item, fkey) in friends" :key="fkey">
                    <u-index-anchor :text="indexs[fkey]" bgColor="#ededed" height="20" size="12" color="#666"
                        style="padding:10rpx 30rpx" />
                    <view v-for="(friendItem, index) in item" :key="index" class="friend-flex"
                        @click="toFriend(friendItem.targetId)">
                        <u-avatar class="avatar-img" size="40" shape="square"
                            :src="contact(friendItem.targetId).portraitUrl" />
                        <view class="info">
                            <view class="name">{{ contact(friendItem.targetId).name }}</view>
                            <!-- <view class="address">Hash:{{ friendItem.address }}</view> -->
                        </view>
                    </view>
                </u-index-item>
            </block>
            <block v-else>
                <u-empty class="pages-null" mode="data" text="暂无好友" />
            </block>
        </u-index-list>
    </view>
</template>

<script>
    import {
        getFriendsLetter
    } from '@/apis/interfaces/im';
    import * as RongIMLib from '@/uni_modules/RongCloud-IMWrapper/js_sdk/index'

    export default {
        data() {
            return {
                indexs: [],
                friends: [],
                pendingCount: 0
            };
        },
        computed: {
            contact() {
                return function(targetId) {
                    return this.$store.getters.contactInfo(targetId)
                }
            }
        },
        onLoad() {
            this.getFriendList()
            this.checkNewFriendPending()
            uni.$on('onContactNotification', this.checkNewFriendPending)
        },
        methods: {
            getFriendList() {
                getFriendsLetter().then(res => {
                    this.indexs = res.indexList
                    this.friends = res.itemArr
                })
            },
            checkNewFriendPending() {
                // 获取是否有新的好友申请
                RongIMLib.getConversationList([RongIMLib.ConversationType.SYSTEM], 1000, 0, (res) => {
                    if (res.code === 0) {
                        this.pendingCount = res.conversations.filter((item) => {
                            return item.objectName == RongIMLib.ObjectName.ContactNotification
                        }).length
                    }
                })
            },
            toGroup() {
                uni.navigateTo({
                    url: '/pages/im/group/index',
                    fail(err) {
                        console.log(err);
                    }
                })
            },
            toFriend(targetId) {
                uni.navigateTo({
                    url: '/pages/im/friends/info?targetId=' + targetId
                })
            },
            // 新朋友
            toPending() {
                if (this.pendingCount > 0) {
                    uni.navigateTo({
                        url: '/pages/im/friends/pending'
                    });
                } else {
                    uni.showToast({
                        title: ` 暂无好友申请 ~ `,
                        icon: "none",
                        mask: true,
                        duration: 3000
                    })
                }
            }
        },
        onNavigationBarButtonTap(e) {
            uni.navigateTo({
                url: '/pages/im/friends/search'
            });
        }
    };
</script>

<style lang="scss" scoped>
    // 页面空
    .pages-null {
        height: 70vh;
    }

    // 好友列表
    .friend-flex {
        position: relative;
        padding: 0 $padding 0 $padding;
        display: flex;
        flex-direction: row;
        align-items: center;
        box-sizing: border-box;

        .avatar-img {
            box-shadow: 0 0 20rpx rgba($color: $main-color, $alpha: 0.2);
        }

        .info {
            flex: 1;
            margin-left: $padding;
            border-bottom: solid 1rpx #f9f9f9;
            height: 120rpx;
            line-height: 120rpx;

            .name {
                font-size: $title-size;
                color: #454545 !important;
                @extend .nowrap;
            }

            .address {
                color: $text-gray-m;
                font-size: $title-size-m - 3;
                padding-top: 10rpx;
                word-break: break-word;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
                width: 600rpx;
            }
        }
    }

    .list-cell {
        display: flex;
        box-sizing: border-box;
        width: 100%;
        padding: 10px 24rpx;
        overflow: hidden;
        color: #323233;
        font-size: 14px;
        line-height: 24px;
        background-color: #fff;
    }
</style>
