<template>
<view id="pointstore-success">
    <view class="contents">
        <view class="is-ok" v-if="isOK === true">
            <view class="text">
                兑换成功
            </view>
            <view>
                感谢惠顾~
            </view>
        </view>
        <view class="is-ok" v-if="isOK === false">
            <view class="no-text">
                兑换失败
            </view>
            <view>
                我还会在回来的~
            </view>
        </view>

        <view class="point-box mx-1px-top" v-if="isOK === true">
            <view>本次消耗 <i class="red">{{order_info.pointUsed}}</i> 积分</view>
            <view>当前可用 <i>{{order_info.pointTotal}}</i> 积分</view>
        </view>

        <view class="button-box">
            <view class="lock" :style="'color: ' + config.mainColor" v-if="isOK === true" @tap="jumpOrder">
                查看订单详情
            </view>
            <view @tap="jumpStore" :style="'background: ' + config.mainColor">
                继续兑换
            </view>
        </view>
    </view>
</view>
</template>
<script>
import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';

export default {
  data() {
    return {
      isOK: null,
      order_info: {},
      order_no: "",
      config: ''
    };
  },

  onLoad(e) {
    // 第三方平台配置颜色
    var gbConfig = this.$cookieStorage.get('globalConfig') || '';
    this.setData({
      config: gbConfig
    });
    console.log(e);
    var data = {
      order_no: e.order_no
    };
    this.setData({
      order_no: e.order_no
    });
    this.queryPaymentStatus(data);
  },

  components: {},
  props: {},
  methods: {
    jumpStore() {
      wx.redirectTo({
        url: '/pages/pointStore/index/index'
      });
    },

    jumpOrder() {
      wx.redirectTo({
        url: '/pages/pointStore/orderdetail/orderdetail?no=' + this.order_no
      });
    },

    queryPaymentStatus(data) {
      var oauth = this.$cookieStorage.get('user_token');
      this.$http.post({
        api: `api/shopping/order/paid`,
        header: {
          Authorization: oauth
        },
        data: data
      }).then(res => {
        res = res.data;

        if (res.status) {
          this.setData({
            isOK: !!(res && res.status && res.data.order.pay_status == 1)
          });

          if (this.isOK) {
            this.setData({
              order_info: {
                pointTotal: res.data.pointInfo.pointTotal,
                // 当前积分
                pointUsed: res.data.pointInfo.pointUsed // 消耗积分

              }
            });
          }
        } else {
          wx.showModal({
            content: res.message || '兑换失败',
            showCancel: false
          });
          this.setData({
            isOK: false
          });
        }
      });
    },

    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>