<template>
  <view class="container">
    <!-- <image class="gpu-image" src="/static/gpu.png" mode="aspectFit"></image> -->
    <view class="info-container">
      <view class="info-row">
        <text class="info-label">我的地址：</text>
        <text class="info-value">{{ adressOptions(myAddress) }}</text>
        <!-- <button class="copy-button" @tap="copyText(myAddress)">復製</button> -->
        <image @click="copyText(myAddress)" src="/static/fuzhi.png"
          style="width: 40rpx;height: 40rpx;margin-left: 10px;"></image>
      </view>
      <view class="info-row" v-if="userInfos.level > 0">
        <text class="info-label">邀請鏈接：</text>
        <text class="info-value" style="line-height: 20rpx;height: 52rpx;">{{ inviteLink }}</text>
        <button class="copy-button" @tap="copyText(inviteLink)">復製</button>
      </view>
      <view class="info-row" v-if="userInfos.level == 0">
        <text class="info-label">邀請鏈接：</text>
        <text class="info-value">遊客不能推薦,請先升級為KOC及以上</text>
      </view>
	  <view class="info-row">
	    <text class="info-label">邀請人：</text>
	    <input :disabled="isDisabled" v-model="initAdress" style="color: #fff;flex: 1;" />
	  </view>
    </view>
    <!-- 邀請好友 -->
    <button v-if="!isDisabled" @tap="confirmInviter" class="invite-button" style="background-color: #4F5AD7;">綁定好友</button>
  </view>
</template>

<script>
  import {
    _userInfos,
    _bindpid,
    _myparent
  } from "@/request/api.js"
  export default {
    data() {
      return {
        initAdress: "",
        isDisabled: false,
        parentUser: {},
        myAddress: '',
        inviteLink: '',
        userInfos: {}
      }
    },
    onShow() {
      this.getUserInfos();
      this.getMyparent();
    },
    methods: {
      adressOptions(data) {
        let start = data.substring(0, 12);
        let end = data.substring(data.length - 4);
        let middle = "…";
        let finalString = start + middle + end;
        return finalString
      },
      async getMyparent() {
        let res = await _myparent();
        if (res.code === 1) {
          this.parentUser = res.data;
          this.parentUser.wallet = JSON.parse(res.data.wallet.replace(/&quot;/g, "\""));
          this.initAdress = this.parentUser.wallet.address;
          if (this.parentUser.wallet) {
            this.isDisabled = true;
          }
        }
      },
      async confirmInviter() {
        if (this.initAdress == '') {
          return uni.showToast({
            title: '請輸入邀請人的錢包地址。',
            icon: "none"
          })
        }
        if (this.initAdress.length > 1 && this.initAdress.length < 20) {
          return uni.showToast({
            title: '請輸入大於20位的錢包地址',
            icon: "none"
          })
        }
        let account = {
          pwallet: this.initAdress,
          pcode: "",
        }
        let res = await _bindpid(account);
        if (res.code === 1) {
          uni.showToast({
            title: "綁定成功！",
            icon: "success",
            duration: 1000
          })
          this.getUserInfos();
          this.getMyparent();
        } else {
          uni.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000
          })
        }
      },
      async getUserInfos() {
        let res = await _userInfos();
        if (res.code === 1) {
          this.userInfos = res.data.userinfo;
          this.myAddress = res.data.userinfo.wallet.address;
          this.inviteLink = `
						https://onlif.klinygm.com/h5/#/pages/login/forget?pwallet=${this.myAddress}
					`
        }
      },
      inviteFriend() {
        // 實現邀請好友的邏輯
        console.log('邀請好友')
      },
      copyText(text) {
        uni.setClipboardData({
          data: text,
          success: () => {
            uni.showToast({
              title: '複製成功',
              icon: 'success'
            })
          }
        })
      }
    }
  }
</script>

<style>
  .container {
    min-height: 100vh;
    padding: 20px;
    box-sizing: border-box;
    background: linear-gradient(to bottom, #000033, #51599b);
  }

  .gpu-image {
    width: 200px;
    height: 200px;
    margin-bottom: 30px;
  }

  .invite-button,
  .confirm-button {
    width: 80%;
    height: 50px;
    border-radius: 25px;
    background-color: grey;
    color: #fff;
    font-size: 18px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 20px;
    position: fixed;
    top: 80%;
    left: 10%;
  }

  .info-container {
    width: 100%;
    margin-bottom: 20px;
  }

  .info-row {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
    /* background-color: rgba(255, 255, 255, 0.1); */
    background-color: #393C84;
    border-radius: 10px;
    padding: 10px;
    height: 60rpx;
  }

  .info-label {
    color: #e0e0e0;
    margin-right: 10px;
  }

  .info-value {
    color: white;
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
	white-space: nowrap;
  }

  .copy-button {
    background-color: #4CAF50;
    color: white;
    padding: 5px 10px;
    border-radius: 5px;
    font-size: 14px;
  }

  .inviterbot {
    margin-top: 120rpx;
    display: flex;

    .bindd {
      width: 220rpx;
      height: 100rpx;
      background-color: #4F5AD7;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 30rpx;
      color: #fff;
      margin-left: 20rpx;
    }
  }

  .inviter-info {
    width: 100%;
    /* background-color: rgba(255, 255, 255, 0.1); */
    background-color: #393C84;
    border-radius: 10px;
    padding: 10px;
    display: flex;
    align-items: center;
    height: 100rpx;
    box-sizing: border-box;
  }

  .modal {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    top: 0;
    background-color: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: flex-end;
    z-index: 999;
  }

  .modal-content {
    background-color: #b6b5c8;
    border-radius: 20px 20px 0 0;
    padding: 20px;
    width: 100%;
    animation: slideUp 0.3s ease-out;
    box-shadow: 0 -4px 10px rgba(0, 0, 0, 0.1);
  }

  @keyframes slideUp {
    from {
      transform: translateY(100%);
    }

    to {
      transform: translateY(0);
    }
  }

  .modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
  }

  .modal-title {
    font-size: 20px;
    font-weight: bold;
    color: #333;
  }

  .modal-close {
    font-size: 24px;
    color: #999;
    cursor: pointer;
  }

  .modal-body {
    margin-bottom: 20px;
  }

  .modal-item {
    display: flex;
    margin-bottom: 15px;
    padding-bottom: 10px;
    border-bottom: 1px solid #f0f0f0;
  }

  .modal-label {
    color: #666;
    font-size: 14px;
    margin-right: 20rpx;
  }

  .modal-value {
    font-weight: bold;
    color: #333;
    font-size: 14px;
    word-break: break-all;
  }

  .modal-footer {
    display: flex;
    justify-content: space-between;
  }

  .modal-button {
    width: 48%;
    height: 44px;
    border-radius: 22px;
    font-size: 16px;
    font-weight: bold;
    border: none;
    cursor: pointer;
    transition: all 0.3s ease;
  }

  .modal-button.cancel {
    background-color: #f0f0f0;
    color: #333;
  }

  .modal-button.cancel:hover {
    background-color: #e0e0e0;
  }

  .modal-button.confirm {
    background-color: #4CAF50;
    color: white;
  }

  .modal-button.confirm:hover {
    background-color: #45a049;
  }
</style>