<template>
  <div class="pay-bg">
    <img src="../../assets/images/pay1.png" alt />
    <img src="../../assets/images/pay2.png" alt />
    <div class="img-con">
      <img src="../../assets/images/pay3.png" alt />
      <img src="../../assets/images/pay4.png" class="img4" alt />
    </div>
    <div class="pay-btn">
      <div class="num">
        ¥
        <span>199</span>.00
        <!-- <i>课程有效期剩余30天</i> -->
      </div>
      <div class="btn" @click="goPay" v-if="orderStatus === 100">立即购买</div>
      <div class="btn active" v-if="orderStatus === 1" @click="goHome">已购买</div>
      <div class="btn" v-if="orderStatus === -1" @click="goMyOrder">待支付</div>
      <!-- <div class="btn xf" @click="goMyOrder">续费</div> -->
    </div>
    <pay-we-chart :requestParam="requestParam"></pay-we-chart>
  </div>
</template>
<script>
import * as api from '@/api/pay.js'
import { getUser } from '@/api/account'

export default {
  metaInfo: {
    title: '课程详情'
  },
  data() {
    const productId =
      webConf.isDev === 'production'
        ? '6684359515615264768'
        : '6682927830365372416'
    return {
      userId: '',
      requestParam: {
        product_id: productId, // 提前设定
        open_id: '', // 通过微信授权获取
        prepay_id: '', // 通过调用接口获取
        app_id: '', // 提前设定
        app_secret: '' // 提前设定
      },
      orderStatusTime: null,
      orderStatus: 100
    }
  },
  methods: {
    async init() {
      const isLogin = await this.$store.dispatch('checkLogin')
      if (isLogin) {
        this.getOrderStatus()
        this.orderStatusTime = setInterval(this.getOrderStatus, 1000)
      }
    },
    // 已购买
    goHome() {
      this.$toast('您已购买！')
    },
    // 支付
    goPay() {
      wx.miniProgram.navigateTo({
        url: `/pages/pay/index?data=${this.requestParam.product_id}`
      })
    },
    // 去我的订单页面
    goMyOrder() {
      wx.miniProgram.navigateTo({
        url: `/pages/web/index?src=${window.location.origin}/my/buyCourses`
      })
    },
    // 获取订单状态
    getOrderStatus() {
      api.getOrderStatus().then(res => {
        this.orderStatus = res.status
        if (this.orderStatus === 1) {
          this.orderStatusTime && clearInterval(this.orderStatusTime)
        }
      })
    },
    // 获取openID
    getOpenid() {
      getUser().then(res => {
        api.getOpenid({ id: res.id, identity: 'transport' }).then(res => {
          if (res.code === 0) {
            this.requestParam.open_id = res.data.openid
          }
        })
      })
    }
  },
  beforeMount() {
    this.init()
  },
  destroyed() {
    this.orderStatusTime && clearInterval(this.orderStatusTime)
  }
}
</script>
<style lang="scss" scoped>
.pay-bg {
  width: 100%;
  padding-bottom: 1rem;
  padding-bottom: env(safe-area-inset-bottom);
  img {
    width: 100%;
  }
}
.pay-btn {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: #fff;
  box-shadow: 0px 0px 6px 0px rgba(0, 0, 0, 0.05);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 0.4rem env(safe-area-inset-bottom);
  .num {
    font-size: 0.26rem;
    color: #ff6767;
    font-weight: bold;
    i{
      font-weight: normal;
    }
    span {
      font-size: 0.4rem;
    }
  }
  .btn {
    width: 4.65rem;
    height: 0.7rem;
    background: rgba(255, 103, 103, 1);
    border-radius: 0.12rem;
    text-align: center;
    line-height: 0.7rem;
    color: #fff;
    font-size: 0.3rem;
    margin: 0.15rem 0;
  }
  .xf {
    width:2.51rem;
    height:.7rem;
    background:rgba(198,24,24,1);
  }
  .active {
    background: #ccc;
  }
}
.img-con{
  background: #fff;
  padding: .4rem .3rem 1.2rem .3rem;
  .img4{
    margin-top: .4rem;
  }
}
</style>
