<template>
    <view>
       <!-- <u-index-list :index-list="indexs" inactiveColor="#666" activeColor="#34CE98">
            <view class="search">
                <u--input class="search-input" placeholder="搜索" border="none" prefixIcon="search" v-model="searchTxt"
                    disabledColor="#Fff" prefixIconStyle="font-size: 22px;color: #909399" />
            </view>
            <block v-if="friends.length > 0">
                <u-checkbox-group v-model="checkboxValue" placement="column" @change="checkboxChange">
                    <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="selectFriend(friendItem.targetId)">
                            <u-checkbox :customStyle="{marginBottom: '8px'}" :name="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>
                </u-checkbox-group>
            </block>
            <view class="no-lists" v-else>
                <u-image class="cover" radius="4" width="400rpx" height="400rpx"
                    :src="searchTxt=== '' ? require('@/static/imgs/no-friend.png') :require('@/static/imgs/no-search.png')"
                    :lazy-load="true" />
                <span>{{searchTxt=== ''?'暂无好友列表~':'暂无搜索内容~'}}</span>
            </view> -->
        <button type="default" @click="onInvite">确定</button>
        <u-index-list :index-list="indexs" inactiveColor="#666" activeColor="#34CE98">
            <u-checkbox-group v-if="friends.length > 0" v-model="checkboxValue" placement="column">
                <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="addContact(friendItem.targetId)">
                        <u-checkbox :name="friendItem.targetId" shape="circle" />
                        <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>
            </u-checkbox-group>
        </u-index-list>
    </view>
</template>

<script>
    import {
        getFriendsLetter,
        inviteGroupUser

    } from '@/apis/interfaces/im';
    import * as RongIMLib from '@/uni_modules/RongCloud-IMWrapper/js_sdk/index'

    export default {
        data() {
            return {
                // indexs: [],
                // friends: [],
                // pendingCount: 0,
                // searchTxt: '',
                // checkboxValue: [],
                targetId: '',
                indexs: [],
                friends: [],
                checkboxValue: []
            };
        },
        computed: {
            contact() {
                return function(targetId) {
                    return this.$store.getters.contactInfo(targetId)
                }
            }
        },
        // onLoad() {
        //     this.getFriendList()
        //     this.checkNewFriendPending()
        //     uni.$on('onContactNotification', this.checkNewFriendPending)
        // },
        // methods: {
        //     // group事件
        //     checkboxChange(n) {
        //         console.log('change', n);
        //     },
        //     getFriendList() {
        //         getFriendsLetter().then(res => {
        //             console.log(res)
        onLoad(e) {
            this.targetId = e.targetId
            this.getFriendList()
        },
        methods: {
            getFriendList() {
                getFriendsLetter().then(res => {
                    this.indexs = res.indexList
                    this.friends = res.itemArr
                })
            },
            
            // selectFriend(targetId) {
            //     console.log(targetId,typeof targetId)
            //     let arr = this.checkboxValue
            //     let index = arr.findIndex(item => item === targetId)
            //     if(index>0){
            //         console.log('选择了再次点击清除数据le')
                    
            //     }else{
            //         console.log('没有选择过，添加数据')
                    
            //     }
                
            //     console.log(this.checkboxValue,index)
            //     console.log('奥克兰打开',targetId)
            // },
            addContact(targetId) {
                if (!this.checkboxValue.find(item => item == targetId)) {
                    this.checkboxValue.push(targetId)
                } else {
                    const index = this.checkboxValue.findIndex(item => item == targetId)
                    this.checkboxValue = this.checkboxValue.splice(index, 1)
                }
            },
            onInvite() {
                inviteGroupUser(this.targetId, this.checkboxValue).then(res => {
                    uni.navigateBack()
                }).catch(err => {
                    uni.showToast({
                        icon: 'none',
                        title: err.message
                    })
                })
            }

        }
    };
</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;
    }

    .search {
        background-color: #fff;
        padding: 30rpx;

        .search-input {
            padding: 10rpx $padding;
            width: 100%;
        }

        .searchTxt {}
    }

    .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>
