<template>
  <div class="naber">
    <div class="content">
      <div class="logo">
        <router-link to="/">
          <a><img src="@/assets/img/task/logo.png" alt="" /></a>
        </router-link>
      </div>
      <div class="header-right">
        <div class="twitter">
          <a
            href="https://twitter.com/CEBG_GAME"
            target="_blank"
            rel="noopener noreferrer"
          >
            <img src="@/assets/img/task/TW.png" alt=""
          /></a>
        </div>
        <div class="discode">
          <a
            href="https://discord.com/invite/fNSn2NHUvf"
            target="_blank"
            rel="noopener noreferrer"
            ><img src="@/assets/img/task/DC.png" alt=""
          /></a>
        </div>
        <div v-if="!chain.logined" class="login-btn" @click="login">
          Connect Wallet
        </div>
        <div v-else class="login-btn-active" @click="showMenu = !showMenu">
          {{ formatAddress }}
          <div class="menu" v-show="showMenu">
            <div class="menu-item" @click="logout">Disconnect</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, computed, watchEffect } from "vue";
import { useChainStore } from "@/store/chain";
import { useAppStore } from "@/store/app";
import { useRouter, useRoute } from "vue-router";
import { UserStatus } from "@/api/User";
import ChainModel from "@/components/home/ChainModel.vue";
import { useCopyToClipboard } from "./../../hooks/useCopyToClipboard";
import { hasMetamask } from "@/utils/chain.util";

const AppModule = useAppStore();
const router = useRouter();
const route = useRoute();
const showMenu = ref(false);
const chain = useChainStore();
const app = useAppStore();
const { copied, error, reset, copyToClipboard } = useCopyToClipboard();
const emit = defineEmits(["login-success"]);
const formatAddress = computed(() => {
  const accountId = AppModule.accountId;
  if (!accountId) return "-";
  if (accountId.length >= 10) {
    return `${accountId.substring(0, 6)}......${accountId.slice(-4)}`;
  }
  return accountId;
});
async function login(event) {
  
  var next = window.location.href;
  if (!hasMetamask()) {
    var url = `https://metamask.app.link/dapp/${next}`;
    location.href = url;
  } else {
    if (!chain.logined) {
      //没有登录情况下执行登录

      await chain.chainManager.login();
      // console.log("logined:", chain.chainManager.isLogined);
      chain.logined = chain.chainManager.isLogined;
      const resq = await UserStatus({
      account: AppModule.accountId,invite_code: route.query.code
    });
      emit("login-success");
    }
  }
}
const logout = async () => {
  await chain.chainManager.logout();
  window.location.reload();
  // console.log("logined:", chain.chainManager.isLogined);
  chain.logined = chain.chainManager.isLogined;
  window.location.reload();
};

const handleCopy = () => {
  const accountId = AppModule.accountId;
  reset();
  copyToClipboard(accountId).then(() => {
    // if (copied.value) {
    //   message.success("Copy successful!");
    // }
  });
};
</script>

<style lang="scss" scoped>
.naber {
  //   position: fixed;
  //   z-index: 20;
  //   top: 0;
  //   left: 0;
  //   background-color: rgb(15, 15, 15);
  width: 100%;
  height: 75px;
  padding-top: 10px;
  display: flex;
  //padding-right: 360px;
  justify-content: center;
  align-items: center;
  .content {
    width: 1200px;
    display: flex;
    justify-content: space-between;
  }
  .login-btn {
    width: 180px;
    height: 50px;
    color: #fff;
    cursor: pointer;
    text-align: center;
    line-height: 50px;
    //border: 1px solid #ffffff;
    background: url("@/assets/img/task/login-btn.png") no-repeat;
    background-size: 100% 100%;
  }
  .login-btn:hover {
    background: url("@/assets/img/task/login-btn-active.png") no-repeat;
    background-size: 100% 100%;
    color: #000000;
  }
  .login-btn-active {
    width: 180px;
    height: 50px;
    cursor: pointer;
    color: #fff;
    text-align: center;
    line-height: 50px;
    // border: 1px solid #ffffff;
    background: url("@/assets/img/task/login-btn.png") no-repeat;
    background-size: 100% 100%;
    position: relative;
    .menu {
      position: absolute;
      top: 100%;
      right: 0; //update this line to right: 0;
      width: 180px;
      z-index: 1;
      // padding: 10px 0;
      // background-color: #bdb8b8;
      border-radius: 5px;
      box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
      list-style: none;
      transition: all 0.3s ease-out;
    }
    li {
      margin: 5px 0;
    }
    .menu-item-top {
      padding: 10px 15px;
      color: #f5f5f5;
      display: flex;
      align-items: center;
      position: relative;
      padding-bottom: 15px;
      .title {
        margin-right: 20px;
      }
    }
    .menu-item-top:before {
      position: absolute;
      top: 100%;
      left: 15px;
      content: "";
      width: 140px;
      height: 1px;
      background: #676767;
    }
    .menu-item {
      // padding: 5px 15px;
      color: #f5f5f5;
      display: flex;
      //border: 1px solid #ccc;
      background-color: #161515;
      justify-content: center;
      text-align: center;
      //  padding-top: 10px;
      align-items: center;
      .title {
        margin-right: 10px;
      }
    }
  }
  .login-btn-active:hover {
    background: url("@/assets/img/task/login-btn-active.png") no-repeat;
    background-size: 100% 100%;
    color: #000000;
  }
  .header-right {
    // width: 228px;

    display: flex;
    align-items: center;
    .discode {
      padding-left: 28px;
      padding-right: 39px;
    }
    .twitter {
      // padding-left: 28px;
      //padding-right: 39px;
    }
  }
}
</style>
