<template>
  <div class="main-container">
    <ul>
      <li class="order-item" v-for="(item, index) in order" :key="index">
        <div class="order-info">
          <div class="no-c"></div>
          <img :src="item.product_image" />
          <div class="txt">{{item.product_name}}</div>
        </div>
        <div class="deti">
          <div class="tit">课程状态：</div>
          <div class="val">{{item.status | courseStatusText }}</div>
        </div>
        <div class="deti">
          <div class="tit">共计：</div>
          <div class="val">{{item.total_fee}}</div>
        </div>
        <div class="deti">
          <div class="invo" v-if="item.status == 1" @click="goInvoice">查看发票</div>
          <div class="val col" v-if="item.status === '-1'" @click="goBuy(item)">继续支付</div>
          <div class="val col mar" v-else>{{item.status | orderStatusText}}</div>
        </div>
      </li>
    </ul>
  </div>
</template>
<script>
import * as api from '@/api/pay.js'
export default {
  metaInfo: {
    title: '已购课程'
  },
  data() {
    return {
      order: [],
      orderTime: null
    }
  },
  mounted() {
    this.getMyOrder()
  },
  computed: {
    isWeapp() {
      return this.$store.state.isWeapp
    }
  },
  filters: {
    courseStatusText(status) {
      return status === '1' ? '已付款' : '未付款'
    },
    orderStatusText(status) {
      const map = {
        '-1': '待支付',
        0: '已取消',
        1: '购买成功',
        2: '已超时'
      }
      return map[status]
    }
  },
  methods: {
    goInvoice() {
      if (this.isWeapp) {
        wx.miniProgram.navigateTo({
          url: `/pages/web/index?src=${window.location.origin}/my/invoice`
        })
      } else {
        this.$router.push({
          path: '/my/invoice'
        })
      }
    },
    getMyOrder() {
      api.getMyOrder().then(res => {
        if (res.code === 0) {
          this.order = res.data.orders
        }
      })
    },
    goBuy(data) {
      clearInterval(this.orderTime)
      this.orderTime = setInterval(() => {
        this.getMyOrder()
      }, 1000)
      wx.miniProgram.navigateTo({
        url: `/pages/pay/index?data=${data.product_id}`
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.main-container {
  .order-item {
    margin: 0.4rem 0;
  }
  .order-info {
    position: relative;
    .no-c{
      position: absolute;
      top: 0;
      left: 0;
      z-index: 9;
      width: 100%;
      height: 100%;
    }
    display: flex;
    img {
      width: 2.4rem;
      height: 2.4rem;
      display: block;
      background-color: #eee;
      object-fit: contain;
    }
    .txt {
      margin-left: 0.2rem;
      font-size: 0.3rem;
      color: #222;
      word-wrap: break-word;
    }
  }
  .deti {
    display: flex;
    margin-top: 0.4rem;
    .tit {
      font-size: 0.26rem;
      color: #222;
    }
    .val {
      font-size: 0.26rem;
      color: #222;
      margin-left: auto;
    }
    .invo{
      font-size: .3rem;
      margin-left: auto;
    }
    .col {
      color: #ff6767;
      font-size: 0.3rem;
    }
    .mar{
      margin-left: .3rem;
    }
  }
}
</style>
