<template>
  <div class="order-success-wrap">
    <van-nav-bar
      title="支付结果"
      left-text=""
      left-arrow
      @click-left="handleAppButton"
      v-if="!isInHooliApp"
    ></van-nav-bar>
    <div class="wrap">
      <div class="done">
        <img src="../../assets/imgs/done@2x.png" alt="">
        <h5>支付成功！</h5>
        <p>该订单记录可在【我的订单】中查询</p>
        <div class="btn">
          <van-button type="default" @click="handleAppButton">返回订单</van-button>
          <van-button type="default" class="backhome" @click="handleBackHome">回到首页</van-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "OrderSuccess",
  data() {
    return {
      env: "",
      isFinish: false,
      isInHooliApp: false
    };
  },
  created: function() {
    if (this.$util.checkIsHooliAndroid() || this.$util.checkIsHooliIos())
      this.isInHooliApp = true;
    if (this.$util.checkIsHooliAndroid()) {
      this.env = "android";
    } else if (this.$util.checkIsHooliIos()) {
      this.env = "ios";
    }
  },
  methods: {
    // 回传 APP / 返回
    handleAppButton() {
      if (this.isInHooliApp) {
        window.AppCallback = {};
        window.nativeToJavaScript = result => {
          try {
            if (result) {
              result = JSON.parse(decodeURIComponent(result));
              let { type, data } = result;
              type = parseInt(type);
              window.AppCallback[type] = data;
              switch (type) {
                case 8:
                  this.isFinish = true;
                  break;
                default:
                  break;
              }
            }
          } catch (e) {
            alert(JSON.stringify(e));
          }
        };
        let params = {
          type: 8,
          data: {
            pay_status: true,
            is_finish: this.isFinish
          }
        };
        params = encodeURIComponent(JSON.stringify(params));
        if (this.env === "android") {
          window.Android.javaScriptToNative(params);
        } else if (this.env === "ios") {
          window.webkit.messageHandlers.javaScriptToNative.postMessage(params);
        }
      } else {
        let lang = this.$store.state.langStr;
        let id = window.localStorage.getItem("orderInfoId");
        if (id) {
          window.location.replace(`/${lang}/orderInfo/${id}`);
        } else {
          window.location.replace(`/${lang}/user/orders`);
        }
      }
    },
    // 返回首页
    handleBackHome() {
      window.location.replace("/");
    }
  }
};
</script>

<style scoped lang='less'>
.order-success-wrap {
  background: #fff;
  min-height: 100vh;
  .wrap {
    padding: 15px 20px 0;
    .done {
      text-align: center;
      padding: 90px 0 0;
      img {
        width: 75px;
        height: 75px;
      }
      h5 {
        font-size: 18px;
        color: #333;
        line-height: 30px;
        margin: 20px 0 0;
        font-weight: 400;
      }
      p {
        font-size: 13px;
        color: #999;
        line-height: 30px;
        margin: 0 0 30px;
        font-weight: 400;
      }
      .btn {
        button {
          width: 200px;
          height: 45px;
          font-size: 18px;
          background: #fff;
          border-radius: 23px;
          color: #ff523d;
          font-weight: 400;
          border-color: #ff523d;
        }
        .backhome {
          margin-top: 20px;
          background: #ff523d;
          color: #fff;
        }
      }
    }
  }
}
</style>
