<template>
  <div class="apply-permission">
    <div class="apply-mask"></div>
    <div class="apply-content">
      <div class="slide-line"></div>
      <div class="apply-text pf600 fs18 fc121212">申请授权</div>
      <img src="../../assets/img/index/Bank_transfer_2_.png" class="apply-img" alt="" />
      <div class="apply-desc pf400 fs14 fc333333">
        <div>正在申请访问你的钱包地址，</div>
        <div>你确认将钱包地址公开给此网站吗?</div>
      </div>
      <div class="sure-btn pf600 fs14 fcFFFFFF" @click="sure">确认</div>
      <div class="cancel-btn pf400 fs14 fcDF384C" @click="cancel">取消</div>
    </div>
  </div>
</template>
<script setup>
  import { l_setItem, l_getItem } from "@/utils/storage";
  import { login } from "@/api/index";
  import { useRouter } from "vue-router";
  import { onMounted } from "vue";

  const router = useRouter();

  const sure = async () => {
    const accounts = await ethereum.request({ method: "eth_requestAccounts" });
    const address = accounts[0];

    if (address) {
      const params = {
        address: address,
      };
      const data = await login(params);

      l_setItem("token", data.accessToken);
      l_setItem("address", address);
      router.push("/index");
    }
  };

  const cancel = () => {
    // 强制退出当前 dApp WebView
    window.location.replace("about:blank");

    // 备用关闭（部分钱包生效）
    setTimeout(() => {
      window.close();
    }, 80);
  };

  onMounted(() => {
    if (l_getItem("address")) {
      router.push("/index");
    }
  });
</script>
<style lang="less" scoped>
  .apply-permission {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 1;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    width: 100%;
    min-height: 100vh;

    .apply-mask {
      position: fixed;
      left: 0;
      top: 0;
      z-index: -1;
      width: 100%;
      min-height: 100vh;
      background: rgba(0, 0, 0, 0.5);
    }

    .apply-content {
      display: flex;
      flex-direction: column;
      justify-content: flex-start;
      align-items: center;
      width: 375px;
      height: 337px;
      background: #ffffff;
      border-radius: 24px 24px 0px 0px;

      .slide-line {
        margin-top: 12px;
        width: 32px;
        height: 4px;
        border-radius: 2px;
        opacity: 0.4;
        background: rgba(0, 0, 0, 0.5);
      }

      .apply-text {
        margin-top: 20px;
        height: 24px;
        line-height: 24px;
        letter-spacing: 0.2px;
      }

      .apply-img {
        margin-top: 17px;
        width: 120px;
        height: 80px;
      }

      .apply-desc {
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        align-items: center;
        margin-top: 11px;
        width: 221px;
        height: 50px;
        line-height: 25px;
        letter-spacing: 0.2px;
      }

      .sure-btn {
        margin-top: 13px;
        width: 311px;
        height: 40px;
        line-height: 40px;
        text-align: center;
        border-radius: 100px;
        background: #df384c;
      }

      .cancel-btn {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        margin-top: 10px;
        width: 29px;
        height: 25px;
      }
    }
  }
</style>
