<template>
  <view></view>
</template>

<script>
import { go2, back } from '@/utils/hook.js'
import { confirmContract } from '@/apis/orderApi.js'
export default {
  data() {
    return {
      // 给一次重试机会
      retry: true,
      backInfo: {
        isBack: false,
        isBackTo: ''
      }
    }
  },
  methods: {
    go2,
    back,
    // 订单合同签约成功，跳转到订单详情页
    certifySign(orderId) {
      if (!orderId) {
        this.handleParamError()
        return
      }
      uni.showToast({
        title: '正在查询结果,请稍候~',
        icon: 'none',
        duration: 3000,
        success: () => {
          setTimeout(() => {
            confirmContract({ orderId: orderId }).then((res) => {
              if (res) {
                // 允许回退
                this.backInfo.isBack = true
                this.backInfo.isBackTo = 'order-list'
                go2('order-detail', { orderId: orderId }, true)
              } else {
                this.handleResultError({ fn: this.certifySign, data: orderId, errType: '订单合同签约' })
              }
            })
          }, 3000)
        }
      })
    },
    // 参数错误，统一处理
    handleParamError() {
      uni.showToast({
        title: '页面参数校验失败',
        icon: 'none',
        success: () => {
          setTimeout(() => {
            g2('store', {}, true)
          }, 2000)
        }
      })
    },
    // 结果错误，统一处理
    handleResultError({ fn, data, errType }) {
      if (this.retry) {
        this.retry = false
        fn(data)
      } else {
        uni.showToast({
          title: `${errType}结果校验失败,正在跳转`,
          icon: 'error',
          success: () => {
            setTimeout(() => {
              g2('store', {}, true)
            }, 1000)
          }
        })
      }
    }
  },
  onLoad(option) {
    if (option.type) {
      // 签合同成功
      if (option.type === 'signSuccess') {
        this.certifySign(option.data)
      }
    } else {
      this.handleParamError()
    }
  },
  onShow() {
    if (this.backInfo.isBack) {
      this.backInfo.isBack = false
      go2(this.backInfo.isBackTo, {}, true)
    }
  }
}
</script>

<style lang="scss" scoped></style>
