Component({
  data: {
    loaded: false,
    success: false,
    title: '操作结果',
    message: '',
    voucherCode: '',
    pointsAwarded: 0,
    relatedPage: ''
  },
  lifetimes: {
    created() {
      this._viewCtx = wx.modelContext.getViewContext(this)
      const modelCtx = wx.modelContext.getContext(this)
      const { NotificationType } = wx.modelContext
      modelCtx.on(NotificationType.Result, (data) => {
        const result = data && data.result ? data.result : {}
        const sc = result.structuredContent || {}
        const meta = result._meta || {}
        const success = !!sc.success
        this.setData({
          loaded: true,
          success,
          title: success ? '报名成功' : '报名未完成',
          message: success ? '你已成功报名该活动。' : (sc.message || '本次报名没有完成。'),
          voucherCode: sc.voucherCode || '',
          pointsAwarded: sc.pointsAwarded || 0,
          relatedPage: sc.relatedPage || meta.relatedPage || ''
        })
      })
    }
  },
  methods: {
    onOpenPage() {
      if (this.data.relatedPage && this._viewCtx && this._viewCtx.openDetailPage) {
        this._viewCtx.openDetailPage({ url: this.data.relatedPage })
      }
    }
  }
})
