<template>

    <div class="common">
        <div class="title">
            =====☆我的区服☆=====
        </div>
        <div class="content">
            <div class="item" v-for="(item, index) in userData" :key="index">
                ✧<Abutton @click="loginGame(item.userId)" >【{{ item.areaId }}区】{{ item.nick }}({{ (item.sex == null || item.sex == '') ?
            "未知" : item.sex }})</Abutton>      
            </div>
             <span v-if="userData.length == 0">暂无角色.</span>
        </div>
    </div>

    <Abar href="/">返回游戏首页</Abar>
</template>
<script setup lang="ts">

definePageMeta({
    layout: layout.empty,
    middleware: 'page-loading'
})
let userData = ref<Array<any>>([]);

const BindData = async (): Promise<void> => {    
    var result = await PubService.GetMyGame(StateHelper.Sid);    
    if (result.code == 0) {
        userData.value = result.data;
    }
    else {
        MessageExtend.ShowDialogEvent("提示", result.msg, () => {
            PageExtend.Redirect("/");
        }, "我知道了");
    }
};

const loginGame = async (gameId: string): Promise<void> => {
  let result = await LoginService.LoginGame(StateHelper.Sid, gameId);
  if (result.code == 0) {
    StateHelper.SetToken(result.data.userId, result.data.token, result.data.refToken);
    if (result.data.regOk == 0) {
      PageExtend.Redirect("/login/register");
    }
    else {
      PageExtend.Redirect("/map");
    }
  }
  else {
    MessageExtend.ShowDialog("登录游戏", result.msg);
  }
};

onMounted(async () => {
    try {
        await BindData();
    }
    finally {
        PageLoading.Close();
    }
})


</script>