<template>
  <div class="mine" @scroll="handleScroll">
    <div class="banner">
      <div class="flex-row">
        <div class="block_1">
          <img :src="userinfo.avatar" class="avatar" />
          <input type="file" @change="onFileChange" class="upload" accept="image/*" />
          <!-- <van-uploader v-model="fileList" multiple :max-count="1" :after-read="afterRead" class="avatar"> </van-uploader> -->
        </div>
        <div class="flex-col">
          <span class="text_1">{{ userinfo.heading }}</span>
          <div class="flex-row">
            <span class="text_2">Uid：{{ userinfo.nickname }}</span>
            <img src="@/assets/images/trade/copy.png" alt="" class="copys" @click="copy" :data-clipboard-text="this.userinfo.nickname" />
          </div>
          <div class="flex-row">
            <span
              :class="[
                userinfo.is_super == 1 && userinfo.direct_super.direct_super <  userinfo.direct_super.one
                  ? 'text_3 level1'
                  : userinfo.direct_super.direct_super >= userinfo.direct_super.one && userinfo.direct_super.direct_super < userinfo.direct_super.two
                  ? 'text_3 level2'
                  : userinfo.direct_super.direct_super >= userinfo.direct_super.two
                  ? 'text_3 level3'
                  : 'text_3',
              ]"
              >{{ $t('lang35') }}{{ userinfo.addressEncrypt }}</span
            >
            <img src="@/assets/images/trade/copy.png" alt="" class="copys" @click="copy" :data-clipboard-text="this.userinfo.address" />
          </div>
          <div class="lang flex-col">
            <img src="@/assets/images/lang1.png" alt="" class="lang_img" @click="reckoningClick('language')" />
            <span>{{ $t('lang79') }}</span>
          </div>
        </div>
      </div>
    </div>
    <div class="content">
      <div class="navbox flex-row jsb">
        <div class="navbox_item flex-row" v-for="(item, index) in navlist" :key="index" @click="navigatorTo(item.url)">
          <img :src="item.icon" alt="" />
          <span>{{ item.text }}</span>
        </div>
      </div>
      <!-- <div class="balance_box">
        <div class="balance_li" @click="reckoningClick('assets')">
          <div class="left">
            <span class="amount">{{ $t('lang36') }}</span>
          </div>
          <div class="right">
            <span>{{ $t('lang37') }}</span>
          </div>
        </div>
        <div class="balance_li" @click="reckoningClick('teaRights')">
          <div class="left">
            <span class="amount">{{ $t('lang218') }}</span>
          </div>
          <div class="right">
            <span>{{ $t('lang39') }}</span>
          </div>
        </div>
      </div> -->
      <!-- <div class="list">
        <div @click="switchovers('myTeaFriends')" class="li">
          <div class="li_left">
            <div class="imgbox">
              <img src="@/assets/images/my.png" class="li_img" />
            </div>
            <span>{{ $t('lang40') }}</span>
          </div>
          <van-icon name="arrow" color="#aaaaaa" size="16" />
        </div>
        <div @click="switchovers('invite')" class="li">
          <div class="li_left">
            <div class="imgbox">
              <img src="@/assets/images/invite.png" class="li_img" />
            </div>
            <span>{{ $t('lang41') }}</span>
          </div>
          <van-icon name="arrow" color="#aaaaaa" size="16" />
        </div>
        <div @click="switchovers('1')" class="li">
          <div class="li_left">
            <div class="imgbox">
              <img src="@/assets/images/trade/custom.png" class="li_img" />
            </div>
            <span>{{ $t('lang43') }}</span>
          </div>
          <van-icon name="arrow" color="#aaaaaa" size="16" />
        </div>
        <div @click="switchovers('setting')" class="li">
          <div class="li_left">
            <div class="imgbox">
              <img src="@/assets/images/set.png" class="li_img" />
            </div>
            <span>{{ $t('lang44') }}</span>
          </div>
          <van-icon name="arrow" color="#aaaaaa" size="16" />
        </div>
      </div> -->
    </div>
    <div>
      <van-tabs @click="onClick" color="#29B286" title-active-color="#29B286">
        <van-tab v-for="(item, index) in statusList" :key="index" :title="item.key"> </van-tab>
      </van-tabs>
    </div>
    <div class="list_box">
      <div class="flex-row jsb">
        <div class="small_nav flex-row">
          <span :class="[index == smallNavIndex ? 'small_nav_item nav_active' : 'small_nav_item']" v-for="(item, index) in smallNav" :key="index">{{ item.key }}</span>
        </div>
        <div class="flex-row">
          <van-radio-group v-model="radio">
            <van-radio name="1" checked-color="#29b286" icon-size="16px">{{ $t('lang256') }}</van-radio>
          </van-radio-group>
          <van-icon name="search" size="22" color="#000000" />
        </div>
      </div>
      <div class="record flex-row jsb" v-if="nft_list.length > 0">
        <div class="record_item flex-col" v-for="(item, index) in nft_list" :key="index" @click="toTeaRight('teaRights', item)">
          <img :src="item.img_url" alt="" />
          <span class="record_item_name">{{ item.name }}</span>
          <span class="record_item_count">{{ $t('lang257') }} {{ item.hold_num }}</span>
        </div>
      </div>
      <div class="record bare" v-else>
        <span>{{ $t('lang82') }}</span>
      </div>
    </div>
    <!-- 上级弹窗 -->
    <van-popup v-model="superiorShow" round style="width: 80%">
      <Superior :address="userinfo.parent_address" :level="userinfo.team_level_name" :parantId="userinfo.parent_id"></Superior>
    </van-popup>
    <BottomNavigation></BottomNavigation>
  </div>
</template>

<script>
import Superior from './superiorPopup.vue';
import { Notify } from 'vant';
import { usApi, newsLetterApi, walletApi, homeApi } from '@/api/index';
import { mapState } from 'vuex';
import union from '../../api/union';
import BottomNavigation from '@/components/BottomNavigation.vue';
import Clipboard from 'clipboard';
import Web3 from 'web3';
export default {
  data() {
    return {
      show: false, //上级弹窗状态
      withdrawShow: false, //提现弹窗状态
      total: 0,
      team_level_name: '',
      userinfo: {
        address: undefined,
        nickname: undefined,
        avatar: undefined,
        team_level_id: undefined,
        seso_url: undefined,
        submit_limit: 0,
        direct_super: {
          one: 0,
          two: 0,
          direct_super: 0,
        },
      },
      order_count: {
        pay: 0,
        sell: 0,
        success: 0,
        cancel: 0,
      },
      withdrawAmount: 0, // 提现金额
      feeObj: {
        usdt: 0,
        withdraw_fee_rate: 0,
        withdraw_min_amount: 0,
      },
      receiptAmount: 0, // 实际到账金额
      superiorShow: false, //上级弹窗
      navlist: [
        {
          text: this.$t('lang250'),
          icon: require('@/assets/images/index/qb.png'),
          url: 'assets',
        },
        {
          text: this.$t('lang251'),
          icon: require('@/assets/images/index/dd.png'),
          url: 'operationLog',
        },
        {
          text: this.$t('lang252'),
          icon: require('@/assets/images/index/sq.png'),
          url: 'myTeaFriends',
        },
        {
          text: this.$t('lang253'),
          icon: require('@/assets/images/index/sz.png'),
          url: 'setting',
        },
      ],
      fileList: [
        {
          url: '',
        },
      ],
      nft_list: [],
      statusList: [
        //0总览 1寄售中 2已售出 3存储中 4已赠送 5已提货
        {
          key: this.$t('lang248'),
          value: 0,
        },
        {
          key: this.$t('lang141'),
          value: 1,
        },
        {
          key: this.$t('lang221'),
          value: 2,
        },
        {
          key: this.$t('lang222'),
          value: 3,
        },
        {
          key: this.$t('lang193'),
          value: 4,
        },
        {
          key: this.$t('lang223'),
          value: 5,
        },
      ],
      chooseStatus: 0,
      page: 1,
      smallNav: [
        {
          key: this.$t('lang254'),
          value: 0,
        },
        {
          key: this.$t('lang255'),
          value: 1,
        },
      ],
      smallNavIndex: 0,
      radio: '',
      selectedFile: null, // 存储选中的文件
      imageUrl: null, // 存储图片预览的URL
    };
  },
  created() {
    this.init();
  },
  mounted() {
    // this.init();
  },
  methods: {
    init() {
      if (this.account) {
        this.userinfo = JSON.parse(localStorage.getItem('userinfo'));
        this.userinfo.addressEncrypt = this.userinfo.address.substring(0, 5) + '****' + this.userinfo.address.substring(this.userinfo.address.length - 5, this.userinfo.address.length);
        this.getUserInfo();
        this.getNftList();
      }
    },
    afterRead(file) {
      // 此时可以自行将文件上传至服务器
      file.status = 'uploading';
      file.message = '上传中...';

      setTimeout(() => {
        file.status = 'failed';
        file.message = '上传失败';
      }, 1000);
    },
    submit() {},
    onClick(status) {
      // console.log(status);
      this.chooseStatus = status;
      this.page = 1;
      this.nft_list = [];
      this.getNftList();
    },
    getNftList() {
      homeApi.getMyNftList({ page: this.page, type_id: this.chooseStatus }).then(res => {
        if (res.code == 200) {
          this.total = res.data.total;
          if (this.nft_list.length >= res.data.total) {
            this.page = -1; // 重置为 -1，表示没有更多数据
          } else {
            this.nft_list = [...this.nft_list, ...res.data.data];
            // console.log(this.nft_list.length);
            if (this.nft_list.length >= res.data.total) {
              this.page = -1; // 重置为 -1，表示没有更多数据
            }
          }
        } else {
        }
      });
    },
    switchovers(router) {
      console.log(router);
      if (router == '') {
        // window.location.href = this.userinfo.seso_url;
        this.superiorShow = true;
      } else if (router == '1') {
        this.$toast(this.$t('lang2'));
      } else {
        this.$router.push({ name: router });
      }
    },
    reckoningClick(name) {
      this.$router.push({ name: name });
    },
    toTeaRight(name, item) {
      this.$router.push({ name: name, query: { product_id: item.product_id, type_id: this.chooseStatus } });
    },
    onFileChange(event) {
      console.log(event.target.files[0]);

      this.selectedFile = event.target.files[0]; // 获取选中的文件
      const formData = new FormData();
      formData.append('file', this.selectedFile);
      homeApi.upload(formData).then(res => {
        if (res.code == 200) {
          this.getUserInfo();
          homeApi.setUserInfo({ avatar: res.data.fullurl }).then(res1 => {
            if (res1.code == 200) {
              this.getUserInfo();
            }
          });
        } else {
        }
      });
    },
    previewImage() {
      const reader = new FileReader();
      reader.onload = e => {
        this.imageUrl = e.target.result; // 设置图片预览的URL
      };
      reader.readAsDataURL(this.selectedFile); // 读取文件内容，并显示为DataURL格式的base64编码字符串
    },
    getUserInfo() {
      homeApi.getUserInfo({}).then(res => {
        if (res.code == 200) {
          this.userinfo = res.data;
          this.order_count = res.data.order_count;
          this.userinfo.addressEncrypt = this.userinfo.address.substring(0, 5) + '****' + this.userinfo.address.substring(this.userinfo.address.length - 5, this.userinfo.address.length);
        }
      });
    },
    handleScroll(event) {
      const container = event.target;
      const scrollTop = container.scrollTop; // 滚动距离
      const scrollHeight = container.scrollHeight; // 内容总高度
      const clientHeight = container.clientHeight; // 可视区域高度
      // 判断是否滑动到底部
      if (scrollTop + clientHeight >= scrollHeight - 10) {
        // console.log(this.page);
        // console.log('到底部');

        if (this.page != -1) {
          this.page++; // 页数加 1
          this.getNftList(); // 触发加载更多
        }
      }
    },
    copy() {
      if (this.userinfo.address) {
        var clipboard = new Clipboard('.copys');
        clipboard.on('success', e => {
          this.$toast(this.$t('lang45'));
          clipboard.destroy(); // 释放内存
        });
        clipboard.on('error', e => {
          this.$toast(this.$t('lang46'));
          clipboard.destroy(); // 释放内存
        });
      }
    },
    navigatorTo(router) {
      if (this.$route.name == router) return;
      if (router != '') {
        this.$router.push({ name: router });
      } else {
        this.$toast(this.$t('lang2'));
      }
    },
  },
  computed: {
    ...mapState(['account']),
  },
  watch: {
    account() {
      this.init();
    },
    withdrawAmount(e) {
      // this.receiptAmount = 0;
      this.receiptAmount = Number(e) - Number(e) * Number(this.feeObj.withdraw_fee_rate);
    },
  },
  components: {
    Superior,
    BottomNavigation,
  },
};
</script>

<style lang="less" scoped>
.mine {
  height: 100vh;
  overflow: scroll;
  .banner {
    // display: flex;
    // justify-content: space-between;
    background: linear-gradient(to bottom right, #29b286, #dae9fd);
    padding: 30px 16px 30px;
    .block_1 {
      position: relative;
      box-shadow: 0px 0px 29px 0px rgba(97, 142, 184, 0.19);
      // background-color: rgba(169, 191, 245, 1);
      border-radius: 50%;
      width: 68px;
      height: 68px;
      // border: 3px solid rgba(#b6b3b3, 1);
      margin-right: 14px;
      overflow: hidden;
      .avatar {
        width: 100%;
        height: 100%;
        background-color: #fff;
        // /deep/ .van-uploader__preview,
        // /deep/.van-image,
        // /deep/.van-uploader__wrapper,
        // /deep/.van-uploader__upload {
        //   width: 68px !important;
        //   height: 68px !important;
        //   margin: 0;
        // }
        // /deep/ .van-uploader__upload,
        // /deep/ .van-image__error,
        // /deep/ .van-image__loading {
        //   background-color: rgba(#000, 0) !important;
        // }
      }
      .upload {
        width: 68px;
        height: 68px;
        left: 0;
        pointer-events: auto;
        position: absolute;
        opacity: 0;
      }
    }
    .copys {
      width: 16px;
      height: 16px;
      margin-left: 4px;
    }
    .lang {
      position: absolute;
      right: 14px;
      top: 14px;
      color: #373737;
      font-size: 10px;
      align-items: center;
      &_img {
        width: 20px;
        margin-bottom: 2px;
      }
    }
    .grade {
      display: flex;
      align-items: center;
      font-size: 14px;
      color: #fff;
      .grade_img {
        display: block;
        width: 7px;
        height: 12px;
      }
    }
    .text_1 {
      color: rgb(0, 0, 0);
      font-size: 16px;
      font-weight: normal;
      padding: 8px 0 0;
    }
    .text_2 {
      color: rgba(#000, 0.6);
      font-size: 13px;
      padding-top: 3px;
    }
    .text_3 {
      color: rgba(#000, 0.6);
      font-size: 13px;
      margin-top: 3px;
    }
    .level1 {
      color: #8b4513 !important;
      border-radius: 6px;
      padding: 2px 6px;
      background-color: #000;
    }
    .level2 {
      color: silver !important;
      border-radius: 10px;
      padding: 2px 6px;
      background-color: #000;
    }
    .level3 {
      color: gold !important;
      border-radius: 10px;
      padding: 2px 6px;
      background-color: #000;
    }
    .text-wrapper_1 {
      font-size: 11px;
      line-height: 100%;
      padding: 5px 10px;
      background-color: rgba(246, 162, 15, 1);
      border-radius: 6px;
      margin: 6px;
    }
    .text-wrapper_2 {
      font-size: 11px;
      line-height: 100%;
      padding: 5px 10px;
      background-color: #7938cf;
      border-radius: 6px;
      margin: 6px;
    }
  }
  .flex-col {
    display: flex;
    flex-direction: column;
  }
  .flex-ac {
    align-items: center;
    justify-content: center;
  }
  .flex-row {
    display: flex;
    align-items: center;
  }
  .jsb {
    justify-content: space-between;
  }
  .content {
    padding: 16px;
    color: #000;
    margin: 8px 0;
    background-color: #fff;

    .navbox {
      // border-radius: 12px;
      align-items: flex-start;
      &_item {
        width: 25%;
        color: #000;
        font-size: 13px;
        font-weight: 550;
        text-align: center;
        flex-direction: column;
        img {
          width: 30px;
          height: 30px;
          margin-bottom: 6px;
        }
      }
    }
    .balance_box {
      display: flex;
      align-items: center;
      justify-content: space-between;

      .balance_li {
        color: #fff;
        display: flex;
        flex-direction: column;
        width: 48%;
        font-size: 14px;
        padding: 14px;
        box-shadow: 4px 4px 15px 0px rgba(180, 212, 212, 0.29);
        border-radius: 6px;
        background: linear-gradient(to bottom right, #29b286, #30d5a0);

        .left {
          display: flex;
          flex-direction: column;
        }
        .icon1 {
          width: 16px;
          height: 16px;
          margin-right: 4px;
        }
        .icon2 {
          width: 16px;
          height: 14px;
          margin-right: 4px;
        }
        .right {
          display: flex;
          align-items: center;
          color: rgba(#fff, 0.6);
          font-size: 13px;
          margin-top: 6px;
        }
        .btn {
          color: #7938cf;
          font-size: 12px;
          padding: 4px 8px;
          border-radius: 3px;
          margin-left: 8px;
          background-color: rgba(48, 70, 212, 0.1);
        }
        .amount {
          font-size: 16px;
        }
      }
      .balance_li:last-child {
        background: linear-gradient(to bottom right, #14bcc6, #28d3de);
      }
    }
    .order {
      font-size: 13px;
      color: rgba(153, 153, 153, 1);
      border-radius: 16px;
      padding: 16px;
      box-shadow: 4px 4px 15px 0px rgba(180, 212, 212, 0.59);
      margin-top: 16px;
      background-color: rgba(255, 255, 255, 1);
      .top {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding-bottom: 16px;
        border-bottom: 1px solid rgba(232, 240, 247, 1);
        .order_text1 {
          font-size: 16px;
          color: rgba(0, 0, 0, 1);
          font-weight: 600;
        }
        .order_text2 {
          font-size: 12px;
        }
      }
      .bottom {
        padding-top: 16px;
        justify-content: space-between;
        .item {
          align-items: center;
          text-align: center;
          .num {
            color: rgba(#000, 0.75);
            font-size: 16px;
            font-weight: 650;
            padding-bottom: 4px;
          }
        }
      }
    }
    .list {
      margin-top: 16px;
      padding: 0 20px;
      border-radius: 10px;
      background-color: rgba(255, 255, 255, 1);

      .li {
        display: flex;
        align-items: center;
        justify-content: space-between;
        font-size: 14px;
        color: rgb(36, 36, 36);
        padding: 24px 0;
        // margin-bottom: 10px;
        border-top: 1px solid rgba(232, 240, 247, 1);
        // box-shadow: 4px 4px 15px 0px rgba(180, 212, 212, 0.29);

        .li_left {
          display: flex;
          align-items: center;
          .imgbox {
            width: 20px;
            height: 20px;
            margin-right: 12px;
          }
          .li_img {
            width: 20px;
            height: 20px;
          }
        }
        .right {
          width: 7px;
          height: 12px;
        }
      }
    }
  }

  .list_box {
    padding: 14px;
    background-color: #fff;

    .small_nav {
      font-size: 13px;
      padding: 2px;
      border-radius: 20px;

      background-color: #f5f5f5;
      &_item {
        color: #8b8b8b;
        display: block;
        text-align: center;
        min-width: 70px;
        padding: 2px 6px;
        margin: 1px;
        border-radius: 20px;
        overflow: hidden; //超出的文本隐藏
        text-overflow: ellipsis; //溢出用省略号显示
        white-space: nowrap; //溢出不换行
      }
    }
    .nav_active {
      color: #29b286;
      background-color: #fff !important;
    }

    .record {
      color: #000;
      padding: 0 16px 100px;
      flex-wrap: wrap;

      &_item {
        width: 46%;
        padding: 16px 0 0;

        img {
          width: 140px;
          height: 140px;
          border-radius: 10px;
        }
        &_name {
          font-size: 15px;
          font-weight: 600;
          padding: 8px 0 4px;
          height: 32px;
          overflow: hidden; //超出的文本隐藏
          text-overflow: ellipsis; //溢出用省略号显示
          white-space: nowrap; //溢出不换行
        }
        &_count {
          color: #999999;
          font-size: 12px;
        }
      }
    }
    .bare {
      text-align: center;
      font-size: 14px;
      padding: 60px 14px 60px;
      color: #aaa;
      background-color: #fff;
    }
  }

  .withdrawPopup {
    padding: 20px 30px;
    color: #666666;
    font-size: 14px;
    .title {
      display: flex;
      justify-content: center;
      padding-bottom: 20px;
      font-weight: bold;
      font-size: 16px;
      color: #000000;
    }

    .tip {
      font-size: 12px;
      display: flex;
      flex-direction: column;
    }
    .btn {
      color: #fff;
      padding: 16px 0;
      text-align: center;
      line-height: 50%;
      border-radius: 20px;
      margin-top: 18px;
      background-color: #7938cf;
    }
    .close—icon {
      position: absolute;
      bottom: -40px;
      left: 45%;
    }
  }
}

/deep/ .van-popup {
  overflow-y: visible;
}

/deep/ .van-field {
  background-color: #f2f9f9;
  border-radius: 8px;
  margin: 6px 0;
}
/deep/ .van-field__body {
  padding: 10px 10px;
}

/deep/ .van-radio__label {
  font-size: 13px;
  padding-right: 10px;
}
</style>