<template>
  <view class="body">
    <view style="margin: 0px 20px;">
      <view class="ft18">请选择充值金额 <label class="ft14" style="float: right;">账户余额<label class="ft14 kb-txt">{{
        user_info.total_coin }}</label>K币</label></view>
      <text class="ft14" style="font-weight: bold;padding-top: 10px;display: block;">1元=100K币</text>
    </view>
    <view class="pay-box">
      <block v-for="(item, key) in list" :key="key">
        <view v-if="item.type == 'COIN'" @click="selectPayItem(item)" :class="item.id == select.id ? 'pay-select' : ''"
          class="box pay-box-items">
          <view style="height: 20px;align-self: flex-end;"><text class="ft12 top-lab" v-if="item.tip_text">{{
            item.tip_text }}</text></view>
          <text class="ft18 pay-box-items-txt" style="color: #FF9800;">{{ item.price }}元</text>
          <view class="ft14 pay-box-items-txt" style="flex-direction: row;">
            {{ item.price_text }} <text class="ft14" style="color: #FF9800;">+{{ item.given }}K币</text> </view>
          <view class="ft14 pay-box-items-txt" style="flex-direction: row;display: flex;justify-content:  space-between;"
            :class="item.id == select.id ? 'select-text' : 'given-txt'">
            {{ item.given_amount }} <text v-if="item.id == select.id" class="ft14"
              style="margin-right: 20px;">&#10003;️</text></view>
        </view>
        <view v-else @click="selectPayItem(item)" :class="item.id == select.id ? 'pay-select' : ''"
          class="box pay-box-items">
          <view style="height: 20px;align-self: flex-end;"><text class="ft12 top-lab" v-if="item.tip_text">{{
            item.tip_text }}</text></view>
          <view class="vip-center">
            <view class="">
              <text class="ft18 pay-box-items-txt" style="color: #FF9800;">{{ item.price }}0元</text>
              <view class="ft14 pay-box-items-txt">{{ item.price_text }}</text></view>
            </view>
            <image class="vip-level-icon" src="/static/icon/vip.png"></image>
          </view>
          <view class="ft14 pay-box-items-txt" style="flex-direction: row;display: flex;justify-content: space-between;"
            :class="item.id == select.id ? 'select-text' : 'given-txt'">
            <text class="ft14">{{ item.given_amount }}</text> <text v-if="item.id == select.id" class="ft14"
              style="margin-right: 20px;">&#10003;️</text>
          </view>
        </view>
      </block>
    </view>
    <view @click="toPay" class="ft16 btn-cz  mt16 ">
      确认充值{{ select.price || 0 }}
    </view>
    <view class="ft14" style="margin: 10px 0; padding: 0 20px;">付费须知</view>
    <view class="ft14" style="padding: 0 20px;margin-bottom: 50px;display: flex;flex-direction: column;">
      <text class="ft12">1、虚拟商品，一经购买不得退换</text>
      <text class="ft12">2、充值后K币可能有延迟，1小时未到账请在“我的”页面联系客服</text>
      <text class="ft12">3、未满18岁的未成年需要在监护人陪同下购买并观看短剧剧</text>
      <text class="ft12">4、购买成功后，“K币”仅在本小程序中使用</text>
      <text class="ft12">5、购买成功后可在”我的”页面订单中心进行查看</text>
    </view>
  </view>
</template>

<script>

import { getUserInfo } from "@/common/apis/my.js";
import {
  getOptions,
  getPayInfo,

} from "@/common/apis/recharge.js"
export default {
  data() {
    return {
      user_info: {},
      list: [],
      select: {},
      pay_success: false
    }
  },
  async onShow() {
    let res = await getUserInfo();
    this.user_info = res.data
    this.get_options();
  },
  methods: {
    async get_options() {
      let res = await getOptions();
      this.list = res.data;
      this.list.forEach((item, index) => {
        if (item.is_default == 1) {
          this.select = item;
        }
      })
    }, selectPayItem(item) {
      this.select = item;
    }, async toPay() {
      if (this.select.lenght < 1) {
        uni.showToast({ title: '请选择要充值的项', icon: "none" });
        return false;
      }
      let params = {
        pay_proudct_id: this.select.id,
        video_id: this.video_info && this.video_info.video_id ? this.video_info.video_id : 0,
        video_series_sequence: this.video_info && this.video_info.video_series_sequence ? this.video_info.video_series_sequence : 0,
        is_first_pay: this.select.is_first_pay,
      };
      let result = false;

      let res = await getPayInfo(params);
      if (res.data) {
        // #ifdef MP-WEIXIN
        this.wxMinPay(res.data);
        // #endif
      }
    }, wxMinPay(param) {
      wx.requestPayment({
        timeStamp: param.timeStamp,
        nonceStr: param.nonceStr,
        package: param.package,
        signType: param.signType,
        paySign: param.paySign,
        success(res) {
          this.pay_success = true;
          uni.showToast({ title: '支付成功', icon: "none" });
          // console.log('success:' + JSON.stringify(res));
        },
        fail(e) {
          if (e.errMsg == "requestPayment:fail cancel") {
            uni.showToast({ title: '取消支付', icon: "none" });
          } else {
            uni.showToast({ title: '支付失败', icon: "none" });
          }
        }
      })
    }
  }
}
</script>

<style>
@import url("style/pay.css");
</style>
