<template>
  <div v-loading.body.fullscreen="fullscreenLoading" element-loading-background="rgba(255, 255, 255, 0)">
    <div class="container" :class="{app: appFlag}">
      <div class="header flex" v-if="!appFlag">
        <img @click="cancel" class="icon" src="/img/icon/back2.png" alt="极度证券">
        <div>
          <!-- 邀请好友 -->
          {{ $t('share.l38') }}
        </div>
      </div>
      <div class="page-title">
        <!-- 每邀请一位好友 -->
        {{ $t('share.l41') }}
      </div>
      <!-- 即可赚取高达  -->
      <div class="page-title" v-html="getHtml()">
      </div>
      <div class="box">
        <van-row gutter="10" type="flex">
          <van-col span="8">
            <div class="cell cell1">
              <div class="sub">
                <!-- 邀请好友注册 -->
                {{ $t('share.l42') }}
              </div>
              <div class="text">
                <!-- 好友可得 -->
                {{ $t('share.l43') }}
              </div>
              <div class="text">
                <!-- 新人大礼包 -->
                {{ $t('share.l45') }}
              </div>
              <div class="num">
                1200 USD
              </div>
            </div>
          </van-col>
          <van-col span="8">
            <div class="cell cell2">
              <div class="sub">
                <!-- 好友存款 -->
                {{ $t('share.l58') }}
              </div>
              <div class="text">
                <!-- 您可得抵扣券 -->
                {{ $t('share.l46') }}
              </div>
              <div class="num">
                500 USD
              </div>
            </div>
          </van-col>
          <van-col span="8">
            <div class="cell cell3">
              <div class="sub">
                <!-- 好友交易 -->
                {{ $t('share.l44') }}
              </div>
              <div class="text">
                <!-- 您可得现金券 -->
                {{ $t('share.l47') }}
              </div>
              <div class="num">
                50 USD
              </div>
            </div>
          </van-col>
        </van-row>
      </div>
      <div class="rule-box" :class="{cn: language == 'zh-CN' || language == 'zh-TW'}" @click="rulePop = true">
        <!-- 活动规则 -->
        {{ $t('share.l39') }}
      </div>
    </div>
    <div class="container1">
      <div class="box1">
        <div class="radius radius1"></div>
        <div class="top">
          <div class="left flex" v-if="discountAmount == 0">
            <div>
              <div>
                <!-- 未获得 -->
                {{ $t('share.l49') }}
              </div>
              <div>
                <!-- 抵扣券 -->
                {{ $t('vip.l15') }}
              </div>
            </div>
          </div>
          <div class="left1 flex" v-else>
            <div>
              <div class="num-box">
                <div class="num">{{ discountAmount }}</div><div>USD</div>
              </div>
              <div>
                <!-- 已获得抵扣券 -->
                {{ $t('share.l51') }}
              </div>
            </div>
          </div>
          <div class="right flex">
            <div>
              <div class="text">
                <!-- 每位好友累计存款≥100USD或等值货币后您将获得500USD抵扣券 -->
                {{ $t('share.l48') }}
              </div>
              <div class="flex">
                <div class="href" @click="sharePopShow">
                  {{ $t('my.l10') }}<van-icon name="arrow" />
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="bottom flex">
          <div class="left flex">
            <img class="icon" src="/img/share/icon5.png?1" alt="极度证券">
            <!-- 当前可领抵扣券 <span class="span">{{ discountList.length }}</span>张 -->
            <div v-html="getDiscountHtml()">
            </div>
          </div>
          <div class="btn" @click="enableCardOrder">
            <!-- 立即领取 -->
            {{ $t('share.l50') }}
          </div>
        </div>
        <div class="radius radius2"></div>
      </div>
      <div class="box1">
        <div class="radius radius1"></div>
        <div class="top">
          <div class="left flex" v-if="cashAmount == 0">
            <div>
              <div>
                <!-- 未获得 -->
                {{ $t('share.l49') }}
              </div>
              <div>
                <!-- 现金券 -->
                {{ $t('share.l9') }}
              </div>
            </div>
          </div>
          <div class="left1 flex" v-else>
            <div>
              <div class="num-box">
                <div class="num">{{ cashAmount }}</div><div>USD</div>
              </div>
              <div>
                <!-- 已获得现金券 -->
                {{ $t('share.l53') }}
              </div>
            </div>
          </div>
          <div class="right flex">
            <div>
              <div class="text">
                <!-- 每位好友股票市场累计交易量≥50000USD或综合市场累计交易量≥5手您将获得25USD现金券 -->
                {{ $t('share.l54') }}
              </div>
              <div class="flex">
                <div class="href" @click="sharePopShow">
                  {{ $t('my.l10') }}<van-icon name="arrow" />
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="bottom flex">
          <div class="left flex">
            <img class="icon" src="/img/share/icon5.png?1" alt="极度证券">
            <!-- 当前可领现金券 <span class="span">{{ cashList.length }}</span>张 -->
            <div v-html="getCashHtml()">
            </div>
          </div>
          <div class="btn" @click="openCardOrder">
            <!-- 立即领取 -->
            {{ $t('share.l50') }}
          </div>
        </div>
        <div class="radius radius2"></div>
      </div>
      <div class="box2">
        <div class="tabs flex">
          <div class="tab" :class="{active:type == 1}" @click="type = 1">
            <!-- 邀请记录 -->
            {{ $t('share.l59') }}
            <div class="line"></div>
          </div>
          <div class="tab" :class="{active:type == 2}" @click="type = 2">
            <!-- 奖励记录 -->
            {{ $t('share.l60') }}
            <div class="line"></div>
          </div>
        </div>
        <userList :list="userList" v-if="type == 1" />
        <rewardList :couponList="couponList" v-if="type == 2"/>
      </div>
    </div>
    <div class="fixed" v-if="!appFlag && userInfo.code">
      <div class="box3 flex">
        <div class="left">
          <!-- 邀请链接： -->
          {{ $t('share.l56') }}{{ link }}
        </div>
        <div class="right flex" @click="copyText(link)">
          <img class="copy" src="/img/icon/copy1.png" alt="极度证券">
          <div>
            <!-- 复制 -->
            {{ $t('option.l22') }}
          </div>
        </div>
      </div>
      <div class="btns">
        <div class="btn flex" @click="sharePop = true">
          <div>
            <!-- 立即邀请 -->
            {{ $t('my.l10') }}
          </div>
        </div>
      </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 get-container="body" v-model="rulePop" round position="bottom">
      <rulePop @cancel="rulePop = false" />
    </van-popup>
    <van-popup class="share-popup" v-model="sharePop">
      <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, getCashCardList } 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,
      cardList: [],
    }
  },
  computed: {
    ...mapGetters(['userInfo', 'currencyList']),
    link() {
      return location.origin + '/rf/' + this.userInfo.code
    },
    discountAmount() {
      var list = this.couponList.filter(item => ((item.type == 1 || item.type == 2) && item.status != 0))
      var amount = 0
      list.forEach(item => {
        amount += ((item.balance || 0) / (this.currencyList[this.serverUtil[item.market]] || 1))
      })
      return Math.floor(amount)
    },
    cashAmount() {
      var list = this.couponList.filter(item => ((item.type == 12) && item.status != 0))
      var amount = 0
      list.forEach(item => {
        amount += ((item.balance || 0) / (this.currencyList[this.serverUtil[item.market]] || 1))
      })
      return Math.floor(amount)
    },
    discountList() {
      return this.couponList.filter(item => ((item.type == 1 || item.type == 2) && item.status == 0))
    },
    cashList() {
      return this.couponList.filter(item => (item.type == 10 && item.status == 0))
    }
  },
  created () {
    this.getChildrenList()
    this.getCardOrderList()
    // this.getCashCardList()
    // this.getParams()
  },
  methods: {
    getCashCardList() {
      getCashCardList({
        pageNum: 1,
        pageSize: 10,
      }).then((res) => {
        this.cardList = res.list || []
      }).catch(() => {})
    },
    openCardOrder() {
      if (this.cashList.length) {
        this.fullscreenLoading = true
        openCardOrder({
          ordernum: this.cashList[0].ordernum
        }).then(res => {
          this.amount = res
          this.cashPop = true
          this.getCardOrderList()
        }).catch(() => {}).finally(() => {
          this.fullscreenLoading = false
        })
      }
    },
    enableCardOrder() {
      if (this.discountList.length) {
        this.discountListLength = this.discountList.length
        this.fullscreenLoading = true
        var 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 => {
        var 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(() => {})
    },
    sharePopShow() {
      if (window.webkit && window.webkit.jsHandler) {
        window.webkit.messageHandlers.jsHandler.postMessage("sharePopShow");
        return
      } else if (window.jsHandler) {
        window.jsHandler.sharePopShow()
        return
      }
      this.sharePop = true
    },
    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)
    },
    cancel() {
      if (window.history.length <= 2) {
        this.$router.replace('/')
      } else {
        this.$router.back()
      }
    },
    getDiscountHtml() {
      return this.$t('share.l52').replace('{p}', `<span class="span">${this.discountList.length}</span>`)
    },
    getHtml() {
      return this.$t('share.l40').replace('{p}', `<span class="span">550 USD</span>`)
    },
    getCashHtml() {
      return this.$t('share.l55').replace('{p}', `<span class="span">${this.cashList.length}</span>`)
    }
  }
}
</script>
<style lang="less" scoped>
@r: 3.75vw;
.header {
  height: 50px;
  font-size: 16px;
  color: #FFFFFF;
  column-gap: 14px;
  margin-bottom: 2px;
  .icon {
    width: 8px;
    display: block;
    cursor: pointer;
  }
}
.container {
  padding: 0 16px;
  background-image: url('@/assets/img/share_bg.png');
  background-repeat: no-repeat;
  background-size: 100% auto;
  position: relative;
  z-index: 2;
  &.app {
    padding-top: 52px;
  }
}
.container1 {
  margin-top: (-29/@r);
  padding: (49/@r) 16px 20px;
  background: linear-gradient( 180deg, rgba(246,246,246,0) 0%, #F6F6F6 100%);
  position: relative;
}
.page-title {
  font-weight: 600;
  font-size: (22/@r);
  color: #FFFFFF;
  margin-bottom: (14/@r);
}
.box {
  padding-top: (107/@r);
  font-size: 12px;
  .cell {
    height: 100%;
    border-radius: 10px;
    padding: 16px 10px;
    word-break: break-all;
  }
  .sub {
    font-weight: 600;
    font-size: 14px;
    margin-bottom: 10px;
  }
  .text {
    margin-bottom: 4px;
  }
  .num {
    font-weight: 600;
    font-size: 14px;
    color: #4670F6;
  }
  .cell1 {
    background-color: rgba(109, 250, 251, 0.9);
  }
  .cell2 {
    background-color: rgba(185, 245, 71, 0.9);
  }
  .cell3 {
    background-color: rgba(239, 126, 178, 0.9);
  }
}
.page-tip {
  text-align: center;
  font-weight: 500;
  font-size: (14/@r);
  color: #FFFFFF;
  &.ru {
    font-size: (12/@r);
  }
}
.box1 {
  background: #FFFFFF;
  box-shadow: 0px 0px 6px 0px rgba(0,0,0,0.2);
  border-radius: 10px;
  margin-bottom: 20px;
  position: relative;
  overflow: hidden;
  .radius {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    position: absolute;
    left: 110px;
    background-color: transparent;
    box-shadow: inset 0px 0px 6px 0px rgba(0,0,0,0.2);
  }
  .radius1 {
    top: 0;
    transform: translateY(-50%);
  }
  .radius2 {
    bottom: 0;
    transform: translateY(50%);
  }
  .top {
    padding: 8px 10px;
    border-bottom: 1px solid #F6F6F6;
    display: flex;
    .left {
      width: 105px;
      justify-content: center;
      font-weight: 600;
      font-size: 14px;
      color: #888888;
      text-align: center;
      line-height: 16px;
    }
    .left1 {
      width: 105px;
      font-weight: 600;
      font-size: 12px;
      color: #888888;
    }
    .num-box {
      display: flex;
      align-items: baseline;
      column-gap: 2px;
      margin-bottom: 6px;
      .num {
        font-size: 22px;
        color: #000;
      }
    }
    .right {
      border-left: 1px dashed #F6F6F6;
      flex: 1;
      min-width: 0px;
      padding-left: 5px;
      min-height: 74px;
      .text {
        font-size: 12px;
        line-height: 16px;
        color: #888888;
        margin-bottom: 8px;
      }
      .href {
        font-size: 12px;
        color: #4670F6;
        cursor: pointer;
      }
    }
  }
  .bottom {
    justify-content: space-between;
    padding: 7px 10px 8px;
    .left {
      flex: 1;
      min-width: 0px;
      padding-right: 4px;
      font-size: 12px;
      color: #888888;
    }
    .icon {
      width: 14px;
      margin-right: 6px;
    }
  }
  .btn {
    background: #4670F6;
    border-radius: 4px;
    padding: 7px 10px;
    cursor: pointer;
    font-size: 12px;
    color: #FFFFFF;
  }
}
.box2 {
  background: #FFFFFF;
  border-radius: 10px;
  position: relative;
  .tabs {
    border-bottom: 1px solid #F6F6F6;
    column-gap: 20px;
    padding-left: 20px;
    padding-right: 10px;
  }
  .tab {
    position: relative;
    font-size: 16px;
    color: #888888;
    padding: 17px 0;
    .line {
      display: none;
      position: absolute;
      left: 50%;
      bottom: 9px;
      transform: translateX(-50%);
      width: 12px;
      height: 2px;
      border-radius: 1px;
      background-color: #000;
    }
    &.active {
      font-weight: 600;
      color: #000000;
      .line {
        display: block;
      }
    }
  }
}
.fixed {
  position: sticky;
  bottom: 0;
  z-index: 3;
  background-color: #fff;
  .btns {
    padding: 10px 14px;
  }
  .btn {
    cursor: pointer;
    height: 50px;
    background: #000000;
    justify-content: center;
    padding: 0 10px;
    font-weight: 600;
    font-size: 16px;
    color: #FFFFFF;
  }
}
.box3 {
  position: relative;
  word-break: break-all;
  height: 54px;
  padding: 4px 16px;
  .left {
    flex: 1;
    min-width: 0px;
    padding-right: 4px;
  }
  .right {
    cursor: pointer;
    flex-direction: column;
    justify-content: center;
    font-size: 12px;
  }
  .copy {
    width: 14px;
    margin-bottom: 4px;
  }
}
.rule-box {
  position: absolute;
  right: 0;
  top: 102px;
  text-align: center;
  background: #75B19D;
  border-radius: 4px 0px 0px 4px;
  padding: 6px 4px;
  font-size: 12px;
  color: #FFFFFF;
  line-height: 14px;
  &.cn {
    width: 24px;
  }
}
.share-popup {
  background-color: transparent;
}
::v-deep {
  .page-title {
    .span {
      color: #4670F6;
    }
  }
  .box1 {
    .span {
      color: #000;
      font-weight: 600;
    }
  }
}
</style>