<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-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')" />
                <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="#f9f9f9" height="20" size="12" color="#666"
                        class="anchor" />
                    <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>
                    </view>
                </u-index-item>
            </block>
            <view class="no-lists" v-else>
                <u-image class="cover" radius="4" width="400rpx" height="400rpx"
                    :src=" require('@/static/imgs/no-friend.png')" :lazy-load="true" />
                <span>暂无好友列表~</span>
            </view>
        </u-index-list>
    </view>
</template>

<script>
    import {
        getFriendsLetter
    } from '@/apis/interfaces/im';
    import * as RongIMLib from '@/uni_modules/RongCloud-IMWrapper/js_sdk/index'
    import im from '@/utils/im/index.js'
    import imBase from '@/utils/im/imBase.js'
    
    export default {
        mixins: [
            imBase
        ],
        data() {
            return {
                indexs: [],
                friends: [],
                pendingCount: 0
            };
        },
        computed: {
            contact() {
                return function(targetId) {
                    return this.$store.getters.contactInfo(targetId)
                }
            }
        },
        onLoad() {
            uni.$on('onNewContactFriends', this.checkNewFriendPending)
        },
        onShow() {
            this.getFriendList()
            this.checkNewFriendPending()
        },
        onUnload() {
            uni.$off("onNewContactFriends")
        },
        methods: {
            getFriendList() {
                getFriendsLetter().then(res => {
                    this.indexs = res.indexList
                    this.friends = res.itemArr
                })
            },
            checkNewFriendPending() {
                im.getPendingList((pendings) => {
                    this.pendingCount = pendings.length
                })
            },
            toGroup() {
                uni.navigateTo({
                    url: '/pages/im/group/index'
                })
            },
            toFriend(targetId) {
                uni.navigateTo({
                    url: '/pages/im/friends/info?targetId=' + targetId
                })
            },
            // 新朋友
            toPending() {
                uni.navigateTo({
                    url: '/pages/im/friends/pending'
                })
            }
        },
        onNavigationBarButtonTap(e) {
            uni.navigateTo({
                url: '/pages/im/friends/search'
            })
        }
    };
</script>

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

    .anchor {
        background-color: #f9f9f9 !important;
        padding: 10rpx 30rpx;
        border-bottom: none !important;
    }

    // 好友列表
    .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;
    }

    .no-lists {
        padding-top: $padding * 3;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        box-sizing: border-box;
        font-size: $title-size-m;
        color: $text-gray-m;

        span {
            padding-top: $padding;
        }
    }
</style>
