<template>
<view id="recharge-success">

    <view class="contents" v-if="init">
        <view class="is-ok" v-if="isOK">
            <view class="text">
                <i class="iconfont icon-chenggong"></i>  充值成功
            </view>
            <view class="info">
                本次充值 <span>{{amount / 100}}</span> 元 当前可用 <i>{{sum / 100}}</i> 元
            </view>
        </view>
        <view class="is-ok" v-if="!isOK">
            <view class="no-text">
                <i class="iconfont icon-shibai"></i>  充值失败
            </view>
        </view>
        <view class="point-box" v-if="isOK && text">
            {{text}}
        </view>
        <div class="button-box" @tap="jump">
            查看余额
        </div>
    </view>
</view>
</template>
<script>
import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';

export default {
  data() {
    return {
      isOK: true,
      text: '',
      amount: 0,
      sum: 0,
	  init:''
    };
  },

  onLoad(e) {
    wx.showLoading();
    pageLogin(getUrl(), () => {
      setTimeout(() => {
        this.queryBalanceStatus(e.order_no);
      }, 500);
    });
  },

  components: {},
  props: {},
  methods: {
    jump() {
      wx.redirectTo({
        url: '/pages/recharge/balance/balance'
      });
    },

    queryBalanceStatus(order_no) {
      var token = this.$cookieStorage.get('user_token');
      this.$http.get({
        api: 'api/users/balance/paid',
        header: {
          Authorization: token
        },
        data: {
          order_no: order_no
        }
      }).then(res => {
        if (res.statusCode == 200) {
          res = res.data;
          this.setData({
            isOK: Boolean(res && res.data && res.data.order.pay_status),
			init:true
          });

          if (this.isOK) {
            this.setData({
              amount: res.data.order.amount,
              sum: res.data.sum
            });

            if (res.data.order.recharge) {
              this.setData({
                text: res.data.order.recharge.title
              });
            }
          } else {
            this.setData({
              isOK: false
            });
            wx.showModal({
              content: res.message || "充值失败",
              showCancel: false,
              success: res => {
                if (res.confirm) {
                  wx.redirectTo({
                    url: '/pages/recharge/balance/balance'
                  });
                }
              }
            });
          }

          wx.hideLoading();
        } else {
          wx.hideLoading();
          this.setData({
            isOK: false
          });
          wx.showModal({
            title: '',
            content: "充值失败",
            showCancel: false,
            success: res => {
              if (res.confirm) {
                wx.redirectTo({
                  url: '/pages/recharge/balance/balance'
                });
              }
            }
          });
        }
      });
    },

    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  },
  computed: {},
  watch: {}
};
</script>
<style rel="stylesheet/less" lang="less">
    @import "success";
</style>