<template>
    <div class="container">
        <van-search
            class="search-item"
            v-model="value"
            show-action
            placeholder="请输入搜索关键词"
        >
            <template #left-icon>
                <svg-icon class="search-icon" name="search" />
            </template>
            <template #action>
                <van-button type="default" class="action-btn" @click="onSearch"
                    >搜索</van-button
                >
            </template>
        </van-search>

        <div class="content">
            <div class="list-ul">
                <div
                    class="list-li"
                    v-for="(item, index) in groupArrDic"
                    :key="item.groupId"
                    @click="goToPage(item)"
                >
                    <groupAvatar
                        class="li-img"
                        :users="item.avatars"
                    ></groupAvatar>
                    <div
                        class="li-cont"
                        :class="{
                            'no-border': index === groupArrDic.length - 1
                        }"
                    >
                        {{ item.sessionName || '群聊' }}
                    </div>
                </div>
                <div v-if="!groupArrDic.length" class="empty">暂无搜索结果</div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue';
import { useWebSocketStore } from '@/stores/modules/webSocketStore.js';
import { userList } from '@/api/path/im.api';
import { useRoute, useRouter } from 'vue-router';
import groupAvatar from '../../components/groupAvatar/index.vue';
import { useWalletStore } from "@/stores/modules/walletStore";

const router = useRouter();
const wsStore = useWebSocketStore();
const walletStore = useWalletStore();

const allGroupArr = ref([]); // 接口原始数据
const groupArrDic = ref([]); // 搜索展示数据
const value = ref(''); // 搜索关键字

// 获取群列表接口
const getuserList = async () => {
    const res = await userList({
        uuid: walletStore.account,
        category: 'group'
    });
    allGroupArr.value = res.data || [];
    groupArrDic.value = [...allGroupArr.value];
};

// 搜索监听
watch(value, newVal => {
    const keyword = newVal.trim().toLowerCase();
    if (!keyword) {
        groupArrDic.value = [...allGroupArr.value];
    } else {
        groupArrDic.value = allGroupArr.value.filter(item =>
            (item.sessionName || '群聊').toLowerCase().includes(keyword)
        );
    }
});

// 点击搜索按钮
const onSearch = () => {
    const keyword = value.value.trim().toLowerCase();
    if (!keyword) {
        groupArrDic.value = [...allGroupArr.value];
        return;
    }
    groupArrDic.value = allGroupArr.value.filter(item =>
        (item.sessionName || '群聊').toLowerCase().includes(keyword)
    );
};

// 点击进入聊天页
const goToPage = item => {
    wsStore.toUserInfo = {
        ...item,
        // avatar: item.users,
        // id: item.groupId,
        // nickname: item.sessionName || '群聊',
        type: 'group'
    };
    router.push({
        path: '/chat',
        query: { uuid: item.uuid }
    });
};

onMounted(() => {
    getuserList();
});
</script>

<style lang="less" scoped>
.container {
    height: 100%;
    display: flex;
    flex-direction: column;
    :deep(.van-search) {
        padding: 4px 16px !important;
    }
    :deep(.van-field__control) {
        font-family:
            PingFang SC,
            PingFang SC;
        font-weight: 500;
        font-size: 15px;
        color: #95a9ed;
    }
    .search-item {
        :deep(.van-search__action) {
            padding: 0 0 0 12px !important;
            box-sizing: border-box !important;
            line-height: initial !important;
        }
        :deep(.van-search__content) {
            background: #f7f8fa !important;
            border-radius: 22px !important;
            padding-left: 10px !important;
            padding-right: 10px;
        }
        :deep(.van-field__left-icon) {
            margin-right: 0 !important;
            height: 24px;
        }
        :deep(.van-search__field) {
            padding: 4px 0 !important;
            line-height: initial !important;
            box-sizing: border-box;
        }
        :deep(.van-field__value) {
            height: 24px !important;
            line-height: 26px !important;
        }
        .search-icon {
            height: 24px;
            width: 24px;
            margin-right: 10px;
            color: #95a9ed;
        }
        .action-btn {
            padding: 0 22px;
            height: 34px;
            box-sizing: border-box;
            background: #4765dd;
            border-radius: 22px;
            font-family:
                PingFang SC,
                PingFang SC;
            font-weight: 500;
            font-size: 15px;
            color: #ffffff;
            border: none !important;
        }
    }
    .search-item :deep(input::placeholder) {
        font-family:
            PingFang SC,
            PingFang SC;
        font-weight: 500;
        font-size: 15px;
        color: #95a9ed;
    }
    .content {
        margin: 16px;
        overflow: auto;
        .list-ul {
            background: #fff;
            border-radius: 12px;
            padding: 0 16px;
            .list-li {
                display: flex;
                align-items: center;
                border-bottom: 1px solid #f2f2f2;
                padding: 10px 0;
                .li-img {
                    width: 32px;
                    height: 32px;
                }
                .li-cont {
                    flex: 1;
                    display: flex;
                    align-items: center;
                    font-family:
                        PingFang SC,
                        PingFang SC;
                    font-weight: 400;
                    font-size: 15px;
                    color: #8d8d8d;
                }
                .no-border {
                    border-bottom: none;
                }
            }
            .empty {
                padding: 20px 0;
                text-align: center;
                color: #aaa;
                font-size: 14px;
            }
        }
    }
    .content::-webkit-scrollbar{
        width: 0;
    }
}
</style>
