<template>
    <div class="container">
        <van-search class="search-item"
            v-model="userAddress"
            show-action
            placeholder="请输入用户账号进行搜索"
        >
            <template #left-icon>
                <svg-icon class="search-icon" name="search" />
            </template>
            <template #action>
                <van-button type="default" class="action-btn" @click="changeSearch">搜索</van-button>
            </template>
        </van-search>
        <div class="content" v-if="Object.keys(userInfo).length > 0">
            <div class="text-lf">
                <van-image class="lf-img" round :src="IM_PATH + userInfo.avatar"/>
                <div>{{userInfo.nickname || '群聊'}}</div>
            </div>
            <div class="rg-color" v-if="userInfo.type == 'user'" @click="goToPage">查看</div>
            <div class="rg-color" v-else @click="joinGroup">加入群聊</div>
        </div>
        <div class="no-more" v-else>
            <svg-icon class="no-more-img" name="no-more" />
            <div>暂无数据</div>
        </div>
    </div>
</template>

<script setup>
import { search,groupJoin } from "@/api/path/im.api";
import { useWalletStore } from "@/stores/modules/walletStore";
import { useWebSocketStore } from "@/stores/modules/webSocketStore.js";
import { useSystemStore } from "@/stores/modules/systemStore";
import { showToast } from "vant";
import * as MsgType from "@/common/constant/msgType";
const router = useRouter();
const route = useRoute();
let walletAddress = route.query?.walletAddress || '';
const userAddress = ref(walletAddress);
const userInfo = ref({});
const IM_PATH = import.meta.env.VITE_IM_PATH_FIlE;
const walletStore = useWalletStore();
const wsStore = useWebSocketStore();
const systemStore = useSystemStore();


const goToPage = () => {
    router.push({
        path: 'personal',
        query:{
            uuid:userAddress.value,
            type:1
        }
    })
}
// 搜索
const changeSearch = async () => {
    if(!userAddress.value) return;
    const res = await search(userAddress.value);
    userInfo.value = res.data[0] || null;
}

const joinGroup = async () => {
    try {
        const res = await groupJoin(walletStore.account,userAddress.value);
        if(res.code == 200){
            showToast('加入成功');
            const message = {
                content: `${walletStore.username}加入了群聊`,
                contentType: MsgType.MSG_TYPE.TEXT,
                messageType: MsgType.JION_GROUP,
            };
            wsStore.sendMessage(message);
            systemStore.needRefreshIm = true;
            router.push('im')
        }else{
            showToast('加入失败')
        }
    } catch (error) {
        showToast('加入失败')
    }
}
</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{
        background: #fff;
        border-radius: 12px;
        padding: 12px 16px;
        display: flex;
        align-items: center;
        font-family: PingFang SC, PingFang SC;
        font-weight: 500;
        font-size: 15px;
        color: #000000;
        justify-content: space-between;
        margin: 12px 16px;
        .text-lf{
            display: flex;
            align-items: center;
            .lf-img{
                width: 32px;
                height: 32px;
                margin-right: 12px;
            }
        }
        .rg-color{
            color: #4765DD;
        }
    }
    .join-btn{
        height: 40px !important;
        line-height: 40px !important;
        background: linear-gradient( 90deg, @theme-color1 0%, #40A4FB 100%) !important;
        border-radius:51px;
        font-family: PingFang SC, PingFang SC;
        font-weight: 500;
        font-size: 15px;
        padding: 9px 0;
        box-sizing: border-box;
        color: #fff;
    }
    .no-more{
        margin-top: 178px;
        text-align: center;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        font-size: 15px;
        color: #8D8D8D;
        .no-more-img{
            width: 302px;
            height: 222px;
        }
    }
}
</style>