<template>
  <div class="reward-cash" @click.stop>
    <div class="head-box">
      <img class="head" :src="channelInfo.rewardPhotoUrl || channelInfo.chatAdminAvaUrl" />
    </div>
    <div class="title">{{ channelInfo.rewardName || '' }}</div>
    <ul class="reward-btn-list">
      <li
        class="item"
        v-for="(item, index) in channelInfo.rewardConfig"
        :key="index"
      >
        <div class="icon">
          <img :src="(imageLists[index > 3 ? 3 : index] || {src: ''}).src" alt="">
        </div>
        <div class="money">
          <span class="unit">￥</span>
          <span>{{ item }}</span>
        </div>
        <div class="btn" @click.stop="_cashReward(item)">
          <span>打赏</span>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
import { reward } from '@/plugins/API/apiReward';
import { mapGetters } from 'vuex';
export default {
  name: 'suspension-reward-cash',
  data() {
    return {
      imageLists: []
    };
  },
  computed: {
    ...mapGetters({
      channelInfo: 'channelInfo'
    })
  },
  mounted() {
    this.imageLists = [
      {
        id:0,
        src:require('./img/icon-money-2.png')
      },{
        id:1,
        src:require('./img/icon-money-5.png')
      },{
        id:2,
        src:require('./img/icon-money-10.png')
      },{
        id:3,
        src:require('./img/icon-money-more.png')
      }
    ]
  },
  methods: {
    async _cashReward(cash) {
      const res = await reward({
        uin: this.channelInfo.uin,
        channelId: this.channelInfo.id,
        money: cash,
        backUrl: window.location.href,
        payType: 1
      });
      if (res.code === 200 && res.errorCode === 0) {
        const { tradeType, prepayInfo } = res.data;
        const { uin } = this.channelInfo;
         const DEF_SITE = process.client
          ? process.env.DEF_SITE + (window.location.protocol.split(':')[0] === 'https' ? process.env.HTTPS_PORT : process.env.HTTP_PORT)
          : `http:${process.env.DEF_SITE + process.env.HTTP_PORT}`;
        if (res.data.url) {
          window.location.href = res.data.url;
        } else if (tradeType === 'MWEB') {
          const wxh5pay = `https:${process.env.DEF_SITE + process.env.HTTPS_PORT}/pay/wxh5pay?uin=${uin}`;
          const backUrl = encodeURIComponent(window.location.href);
          const transferUrl = encodeURIComponent(`${wxh5pay}&url=${backUrl}`);
          const payUrl = encodeURIComponent(`${prepayInfo.mweb_url}&redirect_url=${transferUrl}`);
          const url = `${wxh5pay}&url=${payUrl}`;
          window.location.href = url;
        } else if (tradeType === 'JSAPI') {
          const data = encodeURIComponent(JSON.stringify(res.data));
          const url = `${DEF_SITE}/pay/wxpay?uin=${uin}&payconfig=${data}`;
          window.location.href = url;
        } else {
          this.$layer.msg('网络错误，请稍后重试！');
        }
      }
    }
  }
};
</script>

<style lang="less" scoped>
.reward-box {
  .head-box {
    position: relative;
    // background: url(./img/reward-bg.png) no-repeat top center;
    background-size: 100% auto;
    padding-top: 0.1rem;
    .head {
      display: block;
      margin: auto;
      width: 0.7rem;
      height: 0.7rem;
      border-radius: 100%;
    }
  }
  .title {
    padding-top: 0.05rem;
    line-height: 0.2rem;
    font-size: 0.16rem;
    color: #404040;
    text-align: center;
  }
  .reward-btn-list {
    padding: 0.25rem 0.1rem;
    text-align: center;
    display: flex;
    justify-content: center;
    flex-wrap: wrap;

    .item {
      margin: 0 0.02rem 0.15rem 0.02rem;
      padding: 0.1rem;
      width: 0.8rem;
      line-height: 0.3rem;
      vertical-align: top;
      background-color: #f1f6ff;
      color: #83b7d8;
      border-radius: 0.05rem;
      // font-style: italic;
      font-size: 0.16rem;
      font-weight: 700;
      .unit {
        font-size: 0.1rem;
        font-style: normal;
        margin-right: -.05rem;
      }
      .icon {
        width: 20px;
        height: 20px;
        margin-left: calc(50% - 10px);
        margin-bottom: .06rem;
        margin-top: .03rem;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .btn {
        width: 100%;
        height: .23rem;
        background-color: #459cfa;
        color: #fff;
        font-size: 0.08rem;
        font-weight: 300;
        line-height: .15rem;
        border-radius: 3px;
        padding: .04rem 0;
      }
    }
  }
}
</style>
