<template>
    <div class="bg-grey integral-details-con my-points-con">
    <m-nav-bar title="我的积分" background="transparent" light />
    <div class="integral-details-top">
      <div class="amt-info flex-default">
        <div class="info-item">
          <div class="label">可用积分</div>
          <div class="amt">{{ printsData.amount }}</div>
        </div>
        <div class="info-item">
          <div class="label">冻结积分</div>
          <div class="amt">{{ printsData.frozen }}</div>
        </div>
        <div class="qrcode" @click="previewImage" />
      </div>
      <div class="address-info flex-between">
        <span class="address"
          >地址&nbsp;&nbsp;&nbsp;{{ printsData.address | hashFilter }}</span
        >
        <span class="copy" @click="handleCopy(printsData.address)"> 复制 </span>
      </div>
    </div>

    <div class="integral-details-tabs">
      <div class="tabs-title">交易记录</div>
      <div class="tabs-content">
        <m-tabs
          v-model="tabIndex"
          :tabs="tabList"
          class="tab-list"
        />
        <van-swipe
          ref="integralSwipe"
          @change="swipeChange"
          class="integral-swiper"
          :loop="false"
          :show-indicators="false"
        >
          <van-swipe-item
            v-for="(item, i) of tabList"
            :key="`swipe-item-${i}-${item.type}`"
          >
            <scroll-details :i="i" coin="BASE" :tab="item" :index="tabIndex" />
          </van-swipe-item>
        </van-swipe>
      </div>
    </div>

    <van-popup
      v-model="showPopup"
      round
      closeable
      @click-overlay="overlay"
      @click-close-icon="overlay"
    >
      <div class="qrcode-address-popup">
        <div class="title">积分地址</div>
        <div ref="qrcodeLarge" class="qrcodeLarge" id="qrcodeLarge"></div>
        <div class="address-value" @click="handleCopy(printsData.address)">
          {{ printsData.address }}
          <!-- <i class="copy"></i> -->
        </div>
      </div>
    </van-popup>
  </div>
</template>

<script>
import ScrollDetails from "./scroll-details.vue";
import QRCode from "qrcodejs2";
import html2canvas from "html2canvas";
import { ImagePreview } from "vant";
export default {
  components: {
    ScrollDetails,
    [ImagePreview.Component.name]: ImagePreview.Component,
    //   tkiQrcode
  },
  data() {
    return {
      tabIndex: 0,
      tabList: [
        { name: "全部", payment: "" },
        { name: "支出", payment: "pay" },
        { name: "收入", payment: "income" },
      ],
      downOptions: {
        autoShowLoading: false,
        auto: true,
      },
      upOptions: {
        auto: false,
        use: false,
        // textNoMore:'没有更多啦'
      },
      printsData: {},
      urlValue: "",
      qrImg: "",
      showPopup: false,
    };
  },
  watch: {
    tabIndex(val) {
      this.$refs.integralSwipe.swipeTo(val);
    },
  },
  created() {
    this.requestData();
  },
  methods: {
    previewImage() {
      this.showPopup = true;
      this.$nextTick(() => {
        new QRCode(this.$refs.qrcodeLarge, {
          width: 190,
          height: 190, // 高度
          text: this.printsData.address,
          correctLevel: QRCode.CorrectLevel.H,
        });
      });
    },
    overlay() {
      this.$refs.qrcodeLarge.innerHTML = "";
    },

    requestData() {
      this.$showLoading();
      this.$api({
        apiName: "getOwnAsset",
        postData: { coin: "BASE" },
        success: (res) => {
          this.printsData = res;
          this.urlValue = `slgAddress:${this.printsData.address}`;
        },
        complete: () => {
          this.$hideLoading();
        },
      });
    },
    swipeChange(index) {
      this.tabIndex = index;
    },
    handleExchange() {},
    handleTransfer() {
      this.$router.push({ name: "integral-transfer" });
    },
    handleCollection() {
      this.$router.push({ name: "integral-collection" });
    },
    handleTabClick() {
      this.$refs.integralSwipe.swipeTo(this.tabIndex);
    },
  },
};
</script>
<style lang="less">
.integral-details-con {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: url(~@/assets/img/integral-detail-bg.png);
  background-size: 100%;
  background-repeat: no-repeat;
  background-position-y: -14px;

  > .m-nav-bar {
    width: 100%;
    flex-shrink: 0;
    margin-bottom: 18px;
  }

  > .integral-details-top {
    width: 100%;
    padding: 36px 28px;
    .amt-info {
      align-items: flex-start;
      margin-bottom: 20px;
    }
    .amt-info .info-item {
      color: #ffffff;
      flex: 1;
      margin-right: 5px;
      .label {
        font-size: 14px;
      }
      .amt {
        font-size: 32px;
        font-family: DINAlternate-Bold;
        // font-weight: bold;
      }
    }
    .amt-info .qrcode {
      width: 20px;
      height: 20px;
      background: url(~@/assets/img/qrcode-light.png);
      background-size: contain;
    }
    .address-info {
      height: 42px;
      padding: 0 14px;
      background: rgba(255, 255, 255, 0.5);
      border-radius: 4px;
      .address {
        color: #ffffff;
        font-size: 12px;
      }
      .copy {
        color: @mall-primary-color;
        font-size: 12px;
      }
    }
  }

  > .integral-details-tabs {
    background-color: #ffffff;
    border-radius: 8px;
    width: 343px;
    flex-shrink: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
    .tabs-title {
      font-size: 17px;
      color: #140603;
      font-weight: bold;
      line-height: 52px;
      border-bottom: 1px solid #f1f1f1;
      text-indent: 26px;
      flex: 0;
    }
    .tabs-content {
      position: relative;
      flex-shrink: 1;
      display: flex;
      flex-direction: column;
      height: calc(~"100% - 52px");
      overflow: hidden;
      padding-top: 44px;
      .m-tabs {
        border-bottom: none;
        position: absolute;
        top: 0;
        left: -22px;
        width: 390px;
        .van-tab__text {
          font-weight: bold;
        }
        .van-tab--active .van-tab__text {
          color: #140603;
        }
      }
      .integral-swiper {
        height: 100%;
      }
      .m-tabs-con {
        flex-shrink: 0;
        border: 0;
      }
    }
  }

  .qrcode-address-popup {
    width: 320px;
    padding: 18px;
    margin: auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    > .title {
      font-size: 20px;
      font-weight: bold;
    }
    .qrcodeLarge {
      width: 200px;
      height: 200px;
      padding: 5px;
      display: flex;
      justify-content: center;
      margin-top: 32px;
      margin-bottom: 20px;
      border: 1px solid #e5e5e5;
      img {
        width: 100%;
        height: 100%;
      }
    }

    .address-value {
      font-size: 12px;
      width: 259px;
      padding: 10px 15px;
      background: #f6f6f6;
      border-radius: 6px;
      word-wrap: break-word;
      word-break: break-all;
      color: #666666;
    }
    i.copy {
      flex-shrink: 0;
      margin-left: 4px;
      display: inline-block;
      background: url(https://xls-data-test.oss-cn-shanghai.aliyuncs.com/manage/img/20210914/20210914172331304_170133.png)
        center;
      background-size: cover;
      width: 16px;
      height: 16px;
      transform: translateY(3px);
    }
  }
}
</style>