<template>
  <div v-loading.body.fullscreen="fullscreenLoading" element-loading-background="rgba(255, 255, 255, 0)">
    <div class="header" v-if="!appFlag">
      <div class="back">
        <backIcon/>
      </div>
      <!-- 邀请分享 -->
      {{ $t('share.l38') }}
    </div>
    <div class="container">
      <div class="rule-box" @click="rulePop = true">
        <!-- 规<br/>则 -->
        <template v-if="language == 'zh-CN' || language == 'zh-TW'">
          {{ $t('share.l39') }}<br/>{{ $t('share.l40') }}
        </template>
        <template v-else>
          {{ $t('share.l16') }}
        </template>
      </div>
      <div class="page-title" :class="{'en': language != 'zh-CN' && language != 'zh-TW'}">
        <!-- 邀请好友 赢取高达 -->
        {{ $t('share.l41') }}
      </div>
      <div class="page-tip" :class="{'en': language != 'zh-CN' && language != 'zh-TW'}">
        <!-- 用户xxx获得21USD 现金券奖励 -->
        {{ $t('share.l61') }}
      </div>
      <div class="box">
        <div class="line"></div>
          <div class="flex item">
            <div class="radius"></div>
            <img class="icon" src="/img/share/icon1.png?1" alt="">
            <div class="right">
              <div class="title">
                <!-- 邀请好友注册 -->
                {{ $t('share.l42') }}
              </div>
              <div>
                <!-- 好友可得 1000 USD 新人大礼包 -->
                {{ $t('share.l57') }}
              </div>
            </div>
          </div>
          <div class="flex item">
            <div class="radius"></div>
            <img class="icon" src="/img/share/icon2.png?1" alt="">
            <div class="right">
              <div class="title">
                <!-- 好友存款 -->
                {{ $t('share.l58') }}
              </div>
              <div>
                <!-- 您可得 500 USD 抵扣券 -->
                {{ $t('share.l43') }}
              </div>
            </div>
          </div>
          <div class="flex item">
            <div class="radius"></div>
            <img class="icon" src="/img/share/icon3.png?1" alt="">
            <div class="right">
              <div class="title">
                <!-- 好友交易 -->
                {{ $t('share.l44') }}
              </div>
              <div>
                <!-- 您最高可得 500 USD 现金券 -->
                {{ $t('share.l45') }}
              </div>
            </div>
          </div>
      </div>
    </div>
    <div class="container1">
      <div class="box1">
        <div class="title">
          <!-- 抵扣券： -->
          {{ $t('share.l46') }}<span class="number">{{ discountList.length }} {{ $t('share.l47') }}
            <!-- 张 -->
          </span>
        </div>
        <div class="tip">
          <!-- 每位好友累计存款 ≥ 100 USD或等值货币后 -->
          {{ $t('share.l48') }}<br/>{{ $t('share.l49') }}
          <!-- 您将获得 500 USD 抵扣券 -->
        </div>
        <div class="flex">
          <div class="btn" @click="enableCardOrder">
            <!-- 立即领取 -->
            {{ $t('share.l50') }}
          </div>
        </div>
        <img class="icon" src="/img/share/icon5.png" alt="">
      </div>
      <div class="box1">
        <div class="title">
          <!-- 现金宝箱： -->
          {{ $t('share.l51') }}<span class="number">{{ cashList.length }} {{ $t('share.l52') }}
            <!-- 个 -->
          </span>
        </div>
        <div class="tip">
          <!-- 每位好友累计交易额 ≥ 1000 USD 或等值货币后 -->
          {{ $t('share.l53') }}<br/>{{ $t('share.l54') }}
          <!-- 您将获得一个现金宝箱 -->
        </div>
        <div class="flex">
          <div class="btn" @click="openCardOrder">
            <!-- 立即开启 -->
            {{ $t('share.l55') }}
          </div>
        </div>
        <div class="tip1">
          <!-- 随机奖励：5-500 USD 现金券 -->
          {{ $t('share.l56') }}
        </div>
        <img class="icon1" src="/img/share/icon6.png" alt="">
      </div>
      <div class="box2">
        <div class="tabs flex">
          <div class="tab" :class="{active:type == 1}" @click="type = 1">
            <!-- 邀请记录 -->
            {{ $t('share.l59') }}
          </div>
          <div class="tab" :class="{active:type == 2}" @click="type = 2">
            <!-- 奖励记录 -->
            {{ $t('share.l60') }}
          </div>
        </div>
        <userList :list="userList" v-if="type == 1" />
        <rewardList v-if="type == 2"/>
      </div>
    </div>
    <div class="fixed" v-if="!appFlag && userInfo.code">
      <div class="box3">
        {{ link }}
        <img class="copy" @click="copyText(link)" src="/img/icon/copy.png" alt="">
      </div>
      <div class="box4" @click="sharePop = true">
        <!-- 立即邀请 -->
        {{ $t('my.l10') }}
      </div>
    </div>
    <van-popup get-container="body" v-model="voucherPop" :style="{ width: '292px' }">
      <voucherPop :total="discountListLength" @cancel="voucherPop = false" />
    </van-popup>
    <van-popup get-container="body" v-model="cashPop" :style="{ width: '292px' }">
      <cashPop :amount="amount" @cancel="cashPop = false" />
    </van-popup>
    <van-popup class="rule-pop" get-container="body" v-model="rulePop" :style="{ width: '324px' }">
      <rulePop @cancel="rulePop = false" />
    </van-popup>
    <van-popup class="share-popup" v-model="sharePop" position="bottom">
      <shareBox v-if="sharePop" @cancel="sharePop = false" />
    </van-popup>
  </div>
</template>
<script>
import { copyText } from '@/utils/index'
import { mapGetters } from 'vuex'
import { getParams } from "@/api/app";
import { getChildrenList, getCardOrderList, enableCardOrder, openCardOrder } from '@/api/share'
import userList from "./components/userList.vue";
import rewardList from "./components/rewardList.vue";
import voucherPop from "./components/voucherPop.vue";
import cashPop from "./components/cashPop.vue";
import rulePop from "./components/rulePop.vue";
import shareBox from "./components/shareBox.vue";
import NativeShare from 'nativeshare'
export default {
  components: {
    userList,
    rewardList,
    voucherPop,
    cashPop,
    rulePop,
    shareBox
  },
  data () {
    return {
      userList: [],
      couponList: [],
      type: 1,
      voucherPop: false,
      cashPop: false,
      rulePop: false,
      sharePop: false,
      discountListLength: 0,
      amount: 0
    }
  },
  computed: {
    ...mapGetters(['userInfo']),
    link() {
      return location.origin + '/rf/' + this.userInfo.code
    },
    discountList() {
      return this.couponList.filter(item => (item.type == 1 || item.type == 2))
    },
    cashList() {
      return this.couponList.filter(item => (item.type == 10))
    }
  },
  created () {
    this.getChildrenList()
    this.getCardOrderList()
    // this.getParams()
  },
  methods: {
    openCardOrder() {
      if (this.cashList.length) {
        this.fullscreenLoading = true
        openCardOrder({
          ordernum: this.cashList[0].ordernum
        }).then(res => {
          this.amount = res
          this.cashPop = true
          this.couponList = []
          this.getCardOrderList()
        }).catch(() => {}).finally(() => {
          this.fullscreenLoading = false
        })
      }
    },
    enableCardOrder() {
      if (this.discountList.length) {
        this.discountListLength = this.discountList.length
        this.fullscreenLoading = true
        let ordernum = this.discountList.map(item => item.ordernum)
        enableCardOrder({
          ordernum: JSON.stringify(ordernum)
        }).then(res => {
          this.voucherPop = true
          this.couponList = []
          this.getCardOrderList()
        }).catch(() => {}).finally(() => {
          this.fullscreenLoading = false
        })
      }
    },
    getChildrenList() {
      getChildrenList({
        rank: 1
      }).then(res => {
        let list = res.list || []
        list.forEach(item => {
          item.info = item.info ? JSON.parse(item.info) : {}
        })
        this.userList = list
      }).catch(() => {})
    },
    getCardOrderList() {
      getCardOrderList({
        pageNum: 1,
        status: 0,
      }).then(res => {
        this.couponList = res.list || []
      }).catch(() => {})
    },
    getParams() {
      getParams({
        type: 'Card'
      }).then(res => {
        console.log(res)
      }).catch(() => {})
    },
    shareLink() {
      try {
        this.nativeShare = new NativeShare()
        var shareData = {
          link: this.link,
          icon: window.location.origin + '/logo.png',
        }
        this.nativeShare.setShareData(shareData)
        this.nativeShare.call()
      } catch (err) {
        copyText(this.link)
      }
    },
    copyText(text) {
      copyText(text)
    },
  }
}
</script>
<style lang="less" scoped>
@r: 3.75vw;
.header {
  position: sticky;
  top: 0;
  z-index: 10;
  text-align: center;
  padding-top: 15px;
  padding-bottom: 17px;
  font-size: 18px;
  font-weight: 600;
  background-color: var(--bg-);
  .back {
    position: absolute;
    left: 9px;
    top: 11px;
  }
}
.container {
  padding: (40/@r) 17px 0;
  background-image: url('@/assets/img/share_bg.png');
  background-repeat: no-repeat;
  background-size: 100% auto;
  position: relative;
}
.container1 {
  padding: 10px 17px 1px;
  background-image: url('@/assets/img/share_bg2.png');
  background-repeat: repeat;
  background-size: 100% auto;
  position: relative;
}
.page-title {
  text-align: center;
  font-size: (38/@r);
  font-weight: 600;
  color: #FFFFFF;
  margin-bottom: (32/@r);
  &.en {
    font-size: (25/@r);
    padding-top: (10/@r);
  }
}
.page-tip {
  text-align: center;
  font-weight: 500;
  font-size: (14/@r);
  color: #FFFFFF;
  margin-bottom: (334/@r);
}
.box {
  background-color: #FFFFFF;
  box-shadow: 0px 4px 13px 0px rgba(225,225,225,0.5);
  border-radius: 8px;
  padding: 19px 0 18px 20px;
  position: relative;
  .icon {
    width: 66px;
    margin-right: 1px;
  }
  .right {
    flex: 1;
    min-width: 10%;
    max-width: 100%;
    padding-left: 2px;
    font-size: 12px;
    color: #A3A3A3;
    line-height: 16px;
  }
  .title {
    font-size: 16px;
    color: #000000;
    margin-bottom: 8px;
  }
  .radius {
    background-color: var(--color-);
    border-radius: 50%;
    width: 9px;
    height: 9px;
    margin-right: 8px;
    position: relative;
    z-index: 2;
  }
  .line {
    position: absolute;
    top: 51px;
    bottom: 44px;
    width: 1px;
    left: 24px;
    background-image: linear-gradient(to bottom, #D5D5D5 0%, #D5D5D5 50%, transparent 50%);
    background-size: 1px 10px;
    background-repeat: repeat-y;
  }
}
.box1 {
  background: #FFFFFF;
  box-shadow: 0px 4px 13px 0px rgba(225,225,225,0.5);
  border-radius: 8px;
  margin-bottom: 10px;
  padding: 17px;
  position: relative;
  .title {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 9px;
  }
  .number {
    color: var(--color-);
  }
  .tip {
    color: #A3A3A3;
    line-height: 16px;
    font-size: 12px;
    margin-bottom: 15px;
  }
  .btn {
    cursor: pointer;
    background-color: var(--color-);
    color: #fff;
    text-align: center;
    font-size: 16px;
    font-weight: 600;
    border-radius: 6px;
    line-height: 40px;
    padding: 0 20px;
  }
  .icon {
    position: absolute;
    right: 11px;
    bottom: 13px;
    width: 89px;
  }
  .icon1 {
    position: absolute;
    right: 0;
    bottom: 6px;
    width: 125px;
  }
  .tip1 {
    margin-top: 15px;
    font-size: 12px;
    line-height: 16px;
    color: #A3A3A3;
  }
}
.box2 {
  background: #FFFFFF;
  box-shadow: 0px 4px 13px 0px rgba(225,225,225,0.5);
  border-radius: 8px;
  margin-bottom: 10px;
  padding: 16px 15px 7px;
  position: relative;
  .tabs {
    background: #F3F7F8;
    border-radius: 8px;
    margin-bottom: 2px;
  }
  .tab {
    text-align: center;
    flex: 1;
    line-height: 42px;
    font-size: 16px;
    color: #676767;
    cursor: pointer;
    &.active {
      background: #FFFFFF;
      box-shadow: 0px 2px 5px 0px rgba(188,192,193,0.31);
      border-radius: 8px;
    }
  }
}
.fixed {
  position: sticky;
  bottom: 0;
  z-index: 3;
  background-color: #fff;
  padding: 15px 17px 34px;
}
.box3 {
  background: #FFEEE7;
  border-radius: 3px;
  padding: 10px 30px 12px 14px;
  position: relative;
  line-height: 16px;
  word-break: break-all;
  margin-bottom: 20px;
  color: var(--color-);
  font-weight: 600;
  .copy {
    position: absolute;
    right: 7px;
    top: 50%;
    transform: translateY(-50%);
    width: 19px;
    cursor: pointer;
  }
}
.box4 {
  cursor: pointer;
  background-color: var(--color-);
  color: #FEFFFF;
  text-align: center;
  line-height: 44px;
  border-radius: 6px;
  font-size: 16px;
  font-weight: 600;
}
.rule-box {
  position: absolute;
  right: 0;
  top: 119px;
  text-align: center;
  color: #565656;
  line-height: 21px;
  padding: 26px 9px 27px 13px;
  background-image: url('@/assets/img/share_bg1.png');
  background-repeat: no-repeat;
  background-size: 100% 100%;
}
.rule-pop {
  background-color: transparent;
  padding: 70px 0;
}
.share-popup {
  background-color: transparent;
}
</style>