<template>
  <div class="main-container" v-show="loaded">
    <ul v-if="list.length">
      <li class="order-item" v-for="(item, index) in list" :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">￥{{parseInt(item.total_fee)/100}}</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>
    <van-empty description="暂无内容" v-else />
    <div class="succ-pop" v-if="isPopShow">
      <div class="pop">
        <div class="close" @click="isPopShow = false">×</div>
        <div class="tit">恭喜您，课程已购买成功！</div>
        <div class="txt">请添加助教微信，有任何问题可随时沟通老师。</div>
        <img @click="imagePreview" src="https://zws-imgs-pub.ezijing.com/static/public/dcef7bc62237a077b10d7e49c44a1e51.jpg" alt="">
      </div>
    </div>
  </div>
</template>
<script>
import * as api from '@/api/pay.js'
export default {
  metaInfo: {
    title: '已购课程'
  },
  data() {
    return {
      isPopShow: false,
      loaded: false,
      list: [],
      orderTime: null,
      goFlag: true
    }
  },
  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.list = res.data.orders
          }
        })
        .finally(() => {
          this.loaded = true
        })
    },
    goBuy(data) {
      if (this.goFlag) {
        this.goFlag = false
        clearInterval(this.orderTime)
        this.orderTime = setInterval(() => {
          this.getMyOrder()
          api.getOrderStatus().then(res => {
            if (this.orderStatus === 1) {
              this.isPopShow = true
              clearInterval(this.orderTime)
            }
          })
        }, 1000)
        setTimeout(() => {
          this.goFlag = true
        }, 1000)
        wx.miniProgram.navigateTo({
          url: `/pages/pay/index?data=${data.product_id}`
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.main-container {
  ul {
    padding-top: 0.4rem;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .order-item {
    margin-bottom: 0.4rem;
  }
  .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;
      font-weight: bold;
    }
  }
  .deti {
    display: flex;
    margin-top: 0.4rem;
    .tit {
      font-size: 0.26rem;
      color: #222;
    }
    .val {
      font-size: 0.26rem;
      color: #222;
      margin-left: auto;
      font-weight: bold;
    }
    .invo {
      font-size: 0.3rem;
      margin-left: auto;
    }
    .col {
      color: #ff6767;
      font-size: 0.3rem;
    }
    .mar {
      margin-left: 0.3rem;
    }
  }
}
.succ-pop{
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.5);
  .pop{
    position: absolute;
    top: 50%;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
    width:5.9rem;
    height:6.98rem;
    background:rgba(255,255,255,1);
    border-radius:.12rem;
    .tit{
      line-height: 1.1rem;
      text-align: center;
      font-size: .3rem;
      color: #222;
      font-weight: bold;
    }
    .txt{
      width:5.1rem;
      font-size: .3rem;
      color:rgba(34,34,34,1);
      margin: 0 auto;
    }
    img{
      width: 4.3rem;
      height: 4.3rem;
      margin: 0.4rem auto;
      display: block;
    }
    .close{
      position: absolute;
      top: -.8rem;
      right: 0;
      font-size: .6rem;
      color: #fff;
    }
  }
}
</style>
