<template>
  <div>
    <div class="head">
      <img src="/images/logo.png" class="logo" />
      <br />
      【驰骋四海】
    </div>
    <div>
      ✧当前在线<strong> {{ OnCount }} </strong>位玩家在驰骋四海✧
    </div>
    <div class="content" style="font-size:17px" v-if="isOnline">
      <div>
        亲爱的&nbsp;<strong style="color:red">{{ AccountInfo.nick }}</strong>，欢迎来到驰骋四海!
      </div>
      <div style="margin-top:5px;">
        <div>
          ➢<a :href='"http://m.twbar.cn/Regain/ToMain?openid=" + AccountInfo.openId'>返回探玩驿站</a>
        </div>
        <div>
          ➸<a :href='"http://m.twbar.cn/Regain/ToBbs?openid=" + AccountInfo.openId + "&bbs=1146"'>游戏论坛</a>&nbsp;&nbsp;
          ➸<a @click.stop="offOnline">退出游戏</a>
        </div>
      </div>
    </div>
    <div class="content" v-else>
      <a href="https://3g.fan/Login">立即进入游戏?</a>
    </div>
    <div class="common" v-if="isOnline">
      <div class="title">
        =====☆<Abar href="/area/my">我的区服</Abar>☆=====
      </div>
      <div class="content">
        <div class="item">
          <Abar href="/">&#x2727;&#x3010;1区&#x3011;新手村&#x2730;村长(男)</Abar>
        </div>
      </div>
    </div>
    <div class="common">
      <div class="title">
        =====☆{{ isOnline ? "其他区服" : "全部区服" }}☆=====
      </div>
      <div class="content">
        <div class="item" v-for="(item, index) in areaData" :key="index">
          ✧<Abar :href='"/login/register?id=" + item.areaId'>({{ item.areaId }}区){{ item.name }}</Abar>
          {{ item.status == 1 ? "(推荐)" : "(繁忙)" }}
        </div>
        <span v-if="areaData.length == 0">暂无区服.</span>
      </div>
    </div>

    <div class="title">
      =====☆<Abar  href="/news/">官方公告</Abar>☆=====
    </div>
    <div>
      <div class="item" v-for="(item, index) in noticeData" :key="index">
        {{ index + 1 }}.<Abar :href='"/news/info?id=" + item.noticeId'>{{ item.title }}</Abar>
      </div>
      <span v-if="noticeData.length == 0">暂无公告.</span>
    </div>
  </div>
  <div class="title">
    =====☆服务导航☆=====
  </div>
  <div class="content">
    <Abar href="/customer">客服</Abar>.
    <Abar href="/customer/about">关于</Abar>.
    <Abar href="/customer/cooperation">合作</Abar>    
  </div>
  <div class="foot">
    <div class="timeService">
      小G报时(18:33)
    </div>
  </div>
</template>

<script setup lang="ts">

definePageMeta({
  layout: layout.empty,
  middleware: middleware.loading
})

const areaData = ref<Array<any>>([]);
const noticeData = ref<Array<any>>([]);
const isOnline = ref(false);
const OnCount = ref(0);
const AccountInfo = ref<any>();

const Initialize = async (): Promise<void> => {
  var result = await PubService.GetMain(StateHelper.Sid);  
  if (result.code == 0) {
    isOnline.value = result.data.isOnline;
    if (isOnline.value == false) {
      StateHelper.OffOnline();
    }
    areaData.value = result.data?.area;
    noticeData.value = result.data.notice;
    OnCount.value = result.data.onCount;
    AccountInfo.value = result.data.account;
  }
  else {
    MessageExtend.ShowToast(result.msg, "fail");
  }
};

//退出登录
const offOnline = async () => {
  MessageExtend.ShowConfirmDialog("退出游戏", "您确定要退出游戏吗？", async () => {
    var result = await PubService.GetMain(StateHelper.Sid);
    if (result.code == 0) {
      StateHelper.OffOnline();
      isOnline.value = false;
      MessageExtend.ShowToast("退出成功!", "success");
    }
    else {
      MessageExtend.ShowToast(result.msg, "fail");
    }
  });
}

const login = async (code: string): Promise<void> => {
  var result = await LoginService.TwLogin(code);
  console.log(result);
  if (result.code == 0) {
    StateHelper.SetSid(result.data.token);
    await Initialize();
  }
  else {
    MessageExtend.ShowToast(result.msg, "default");
  }
}

onMounted(async () => {
  try {
    const code = PageExtend.QueryString("code");
    if (code != '' && StateHelper.IsAccLogin == false) {

      //执行登录
      await login(code);
    }
    else {
      await Initialize();
    }
  }
  finally {
    PageLoading.Close();
  }
})
</script>
