<template>
  <div class="reward-cash">
    <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"
        @click="_cashReward(item)"
      >
        {{ item }}
        <span class="unit">元</span>
      </li>
    </ul>
  </div>
</template>

<script>
import { reward } from '@/plugins/API/apiReward';
import { mapGetters } from 'vuex';
export default {
  name: 'suspension-reward-cash',
  data() {
    return {};
  },
  computed: {
    ...mapGetters({
      channelInfo: 'channelInfo'
    })
  },
  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.3rem;
    text-align: center;

    .item {
      display: inline-block;
      margin: 0 0.1rem 0.15rem 0.1rem;
      width: 0.65rem;
      height: 0.3rem;
      line-height: 0.28rem;
      vertical-align: top;
      border: 0.01rem solid #ff5454;
      color: #ff5454;
      border-radius: 0.03rem;
      font-style: italic;
      font-size: 0.16rem;
      font-weight: 700;

      .unit {
        font-size: 0.13rem;
        font-style: normal;
      }
    }
  }
}
</style>
