<template>
    <div class="container">
        <van-search class="search-item"
            v-model="searchValue"
            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 groupMembers" :key="index" @click="goToPage(item.uuid)">
                    <van-image class="li-img" round :src="IM_PATH + item.avatar"/>
                    <div class="li-cont" :class="{ 'no-border': index === 9 }">
                        <span>{{item.nickname}}</span>
                        <div class="leader bg-blue" v-if="item.guserId == item.userId">群主</div>
                        <div class="leader bg-gray" v-if="item.uuid == walletStore.account">我</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { useWebSocketStore } from "@/stores/modules/webSocketStore.js";
import { groupList } from "@/api/path/im.api";
import { useWalletStore } from '@/stores/modules/walletStore';
const wsStore = useWebSocketStore();
const IM_PATH = import.meta.env.VITE_IM_PATH_FIlE;
const router = useRouter();
const walletStore = useWalletStore();

const searchValue = ref('');
const allMembers = ref([]);      // 保存所有成员
const groupMembers = ref([]);    // 展示用的成员

// 搜索方法
const onSearch = () => {
  const keyword = searchValue.value.trim();
  if (!keyword) {
    groupMembers.value = allMembers.value;
  } else {
    groupMembers.value = allMembers.value.filter(item =>
      item.nickname?.includes(keyword)
    );
  }
};
watch(searchValue, (val) => {
  const keyword = val.trim();
  if (!keyword) {
    groupMembers.value = allMembers.value;
  } else {
    groupMembers.value = allMembers.value.filter(item =>
      item.nickname?.includes(keyword)
    );
  }
});

// 获取群成员信息
const fetchGroupMembers = async () => {
  try {
    const res = await groupList(wsStore.toUserInfo.uuid);
    allMembers.value = res.data || [];
    groupMembers.value = allMembers.value;
  } catch (e) {
    console.error("获取群成员失败", e);
  }
};

const goToPage = (uuid) => {
    if(uuid == walletStore.account) return;
    router.push({
        path: 'personal',
        query:{
            uuid,
            type:2
        }
    })
}

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


<style lang="less" scoped>
.container{
    height: 100%;
    :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: 8px 16px;
        .list-ul{
            background: #fff;
            border-radius: 12px;
            padding: 12px 16px;
            .list-li{
                display: flex;
                align-items: center;
                .li-img{
                    width: 32px;
                    height: 32px;
                    flex-shrink: 0;
                    margin-right: 12px;
                }
                .li-cont{
                    flex: 1;
                    display: flex;
                    align-items: center;
                    border-bottom: 1px solid #F2F2F2;
                    padding-top:10px;
                    padding-bottom: 10px;
                    font-family: PingFang SC, PingFang SC;
                    font-weight: 500;
                    font-size: 15px;
                    color: #000000;
                    .leader{
                        border-radius: 4px;
                        padding: 1px 6px;
                        font-family: PingFang SC, PingFang SC;
                        font-weight: 400;
                        font-size: 10px;
                        margin-left: 8px;
                    }
                    .bg-blue{
                        background: rgba(71,101,221,0.2);
                        color: #4765DD;
                    }
                    .bg-gray{
                        background: rgba(141,141,141,0.2);
                        color: #8D8D8D;
                    }
                }
                .no-border {
                    border-bottom: none;
                }
            }
        }
    }
}
</style>