<template>
  <div class="guidelines">
    <div class="zhiying" v-if="micromessenger == 0">
      <img :src="zhiying" alt="">
    </div>
  </div>
</template>

<script>
  import {getBillRefreshApi, getStatusWechatApi, getCourseInfoApi} from "../../service/api"
  import {Popup, Lazyload, Button, Dialog, Toast} from 'vant';
  import common from '../../util/common'
  import zhiying from "../../assets/punchTheClock/zhiying.png"
  import zhufubao from "../../assets/punchTheClock/zhifubaotup.png"

  let timer = null;
  export default {
    name: "guidelines",
    data() {
      return {
        micromessenger: 0,  // 判断在微信里还是在浏览器里
        zhiying: zhiying,   // 图片
        zhufubao: zhufubao
      }
    },
    methods: {
      outTrade() {
        timer = setInterval(() => {
          let outTradEno = JSON.parse(window.localStorage.out_trade_no);
          getBillRefreshApi({outTradeNo: outTradEno.out_trade_no}).then(res => {
            // 支付成功
            if (res.status === "SUCCESS") {
              getCourseInfoApi().then(response => {
                this.userCourse = JSON.parse(JSON.stringify(response));
                console.log(this.userCourse)
                localStorage.setItem("userCourse", JSON.stringify(this.userCourse));
                localStorage.setItem("buyindex", 1);
                Dialog.confirm({title: '标题', message: '支付成功', confirmButtonText: '去查看'}).then(() => {
                  if (response.teacher_alias && response.teacher_alias.substr(0, 1) == 1 && outTradEno.goods_type != 4 && outTradEno.is_subscribe == 1) {
                    clearInterval(timer);
                    this.$router.push({name: "guide", query: {shopId: outTradEno.shopId}});
                    localStorage.removeItem('out_trade_no')
                  } else {
                    if (res.buy_type == 1) {
                      clearInterval(timer);
                      this.$router.push({name: "buySuccess", query: outTradEno});
                      localStorage.removeItem('out_trade_no')
                    } else {
                      clearInterval(timer);
                      this.$router.push({name: "success", query: outTradEno});
                      localStorage.removeItem('out_trade_no')
                    }
                  }
                }).catch(() => {
                  clearInterval(timer);
                  localStorage.removeItem('out_trade_no')
                });
              });
            } else if (res.status === "NO_SYNC") {
              Dialog.confirm({title: '标题', message: '您有一笔未支付的订单', confirmButtonText: '去支付'}).then(() => {
                clearInterval(timer);
                location.href = `http://${window.location.host}/#/guidelines?pay_url=${encodeURI(window.localStorage.pay_url)}&out_trade_no=${window.localStorage.out_trade_no}`
                localStorage.removeItem('out_trade_no')
              }).catch(() => {
                clearInterval(timer);
                localStorage.removeItem('out_trade_no')
              });
            }
          })
        }, 6000)
      }
    },
    mounted() {
      // 0 -> 微信 1 -> 支付宝
      this.micromessenger = common.isWeixinBrowser()
      // 到浏览器里支付宝支付
      if (this.micromessenger == 1) {
        location.href = this.$route.query.pay_url
      }

      if (this.$route.query.out_trade_no) {
        localStorage.setItem('out_trade_no', this.$route.query.out_trade_no);
      }
      this.outTrade()
    }
  }
</script>

<style scoped lang="less">
  @import "../../util/public";

  .guidelines {
    width: 100%;
    height: 100%;
    padding-top: 3 * @toVw;
    background: #F3F2F7;
    text-align: center;

    .zhiying {
      width: 244 * @toVw;
      height: 361 * @toVw;
      margin-left: 112 * @toVw;
      box-sizing: border-box;

      img {
        width: 100%;
        height: 100%;
      }
    }
  }

</style>
