import {
  apiQueryOrderId
} from "../../utils/db_api"

const app = getApp()
Component({

  data: {
    firstPrize: {},
    lastPrize: []
  },

  properties: {
    prizeInfoVO: Array
  },

  attached() {
    this.time = 0
    console.log(this)
    console.error("看看文案3123123", this.data)
    const firstPrize = this.data.prizeInfoVO.filter((item) => {
      return item.type == 1 || item.type == 2
    })[0]
    const lastPrize = this.data.prizeInfoVO.filter((item) => {
      return item.type != 1 && item.type != 2
    })
    this.setData({
      firstPrize,
      lastPrize
    })
  },

  methods: {
    onModalClose() {
      this.triggerEvent("onModalClose", 'openAward');
    },
    checkAuth() {
      this.triggerEvent("checkAuth")
    },
    async happyAccept() {
      console.warn("happyAccept")
      if (this.data.firstPrize.type == 1) {
        wx.showLoading({
          mask: true
        })
        this.getorderIddd((no) => {
          wx.hideLoading()
          //微信红包
          this.triggerEvent("onModalClose", {
            type: 'openWxMoneyAward',
            projectOrderNo: no,
            recordId: this.data.firstPrize.recordId
          });
        })

      } else {
        this.triggerEvent("onModalClose", 'openAward');
      }
    },
    //轮询projectOrderNo
    // async getProjectOrderNo() {
    //   return new Promise(async (r) => {
    //     const projectOrderNo = await this.getorderIddd()
    //     if (projectOrderNo) {
    //       r()
    //     } else {

    //     }
    //   })
    // },


    async getorderIddd(cb) {
      if (this.time >= 5) {
        cb && cb(null)
        return
      }
      this.time++
      const apiQueryOrderIdResp = await apiQueryOrderId({
        orderId: this.data.firstPrize.recordId
      })
      if (apiQueryOrderIdResp.success && apiQueryOrderIdResp.data) {
        cb && cb(apiQueryOrderIdResp.data)
      } else {
        setTimeout(() => {
          this.getorderIddd()
        }, 500)
      }
    },
  }
})