<template>
    <div class="login">
        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field
                    v-model="username"
                    name="username"
                    label="用户名"
                    placeholder="用户名"
                    :rules="[{ required: true, message: '请填写用户名' }]"
                />
                <van-field
                    v-model="password"
                    type="password"
                    name="password"
                    label="密码"
                    placeholder="密码"
                    :rules="[{ required: true, message: '请填写密码' }]"
                />
            </van-cell-group>
            <div style="margin: 40px 16px 0">
                <van-button :loading-text="$t('form.Loading')"
                :loading="loading" round block type="primary" native-type="submit">登录</van-button>
            </div>
        </van-form>
    </div>
</template>

<script setup>
import { imLoginNew } from "@/api/path/im.api";
import { useWalletStore } from "@/stores/modules/walletStore";
import { useSystemStore } from "@/stores/modules/systemStore";
const walletStore = useWalletStore();
const systemStore = useSystemStore();
const imgHost = import.meta.env.VITE_IM_PATH_FIlE
const router = useRouter();

const username = ref('');
const password = ref('');
const loading = ref(false);


const onSubmit = async (values) => {
    loading.value = true;
    console.log('submit', values);
    try {
        const results = await imLoginNew({ username: username.value,password:password.value });
        console.log(results);
        if(results.code == 200){
            walletStore.account = username.value;
            systemStore.userUuid = results.data.uuid;
            // systemStore.token = results.data.access_token;
            systemStore.USERID = results.data.uuid;

            walletStore.avatar = imgHost + results.data.avatar;
            walletStore.username = results.data.nickname;
            router.push({
                path: "/im",
            });
        }
    } catch (error) {
        
    } finally {
        loading.value = false;
    }
    
};
</script>

<style lang="less" scoped>
:deep(.van-cell){
    font-size: 16px !important;
}
:deep(.van-button--normal){
    font-size: 16px !important;
}
:deep(.van-button--primary){
    background: #4765DD !important;
}
.login{
    margin-top: 150px;
} 
</style>
